Face detection for your Android app
FaceDetector
Want to detect human faces on a camera preview stream in real time? Well, you came to the right place.
FaceDetector is a library which:
- detects faces
- works on Android
- very simple to use
- works greatly with Fotoapparat
- you can use it with whichever camera library or source you like
- uses C++ core which can easily be ported to iOS (we have plans for that)
Detecting faces with Fotoapparat
is as simple as:
Fotoapparat
.with(context)
.into(cameraView)
.frameProcessor(
FaceDetectorProcessor
.with(context)
.build()
)
.build()
How it works
Step One (optional)
To display detected faces on top of the camera view, set up your layout as following.
<io.fotoapparat.facedetector.view.CameraOverlayLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Adjust parameters as you like. But cameraView has to be inside CameraOverlayLayout -->
<io.fotoapparat.view.CameraView
android:id="@+id/cameraView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- This view will display detected faces -->
<io.fotoapparat.facedetector.view.RectanglesView
android:id="@+id/rectanglesView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectanglesColor="@color/colorAccent"
app:rectanglesStrokeWidth="2dp"/>
</io.fotoapparat.facedetector.view.CameraOverlayLayout>
Step Two
Create FaceDetectorProcessor
:
Java:
FaceDetectorProcessor processor = FaceDetectorProcessor.with(this)
.listener(faces -> {
rectanglesView.setRectangles(faces); // (Optional) Show detected faces on the view.
// ... or do whatever you want with the result
})
.build()
or Kotlin:
private val processor = FaceDetectorProcessor.with(this)
.listener({ faces ->
rectanglesView.setRectangles(faces) // (Optional) Show detected faces on the view.
// ... or do whatever you want with the result
})
.build()
Step Three
Attach the processor to Fotoapparat
Fotoapparat.with(this)
.into(cameraView)
// the rest of configuration
.frameProcessor(processor)
.build()
And you are good to go!
Set up
Add dependency to your build.gradle
repositories {
maven {
url "http://dl.bintray.com/fotoapparat/fotoapparat"
}
}
implementation 'io.fotoapparat:facedetector:1.0.0'
// If you are using Fotoapparat add this one as well
implementation 'io.fotoapparat.fotoapparat:library:1.2.0' // or later version