Face detection in Phimpme Android’s Camera
The Phimpme Android application comes with a well-featured camera application which offers almost all the functionality an advanced camera user searches for. It comes with a wide range of options to apply different scene modes in the camera and also to detect the faces using the front or the back camera of the device. In this tutorial, I will be discussing how we achieved the face detection functionality in Phimpme.
In the Phimpme application, we have the option in the settings to enable the face detection just as depicted in the screenshot below. After enabling it the Camera starts detecting the faces and draws rectangular boxes on the number of faces detected by the camera.
I will be explaining step by step to achieve this using some code snippets.
Step 1
First, we have to check whether our device supports the face detection functionality to avoid unnecessary application crashes using the Android’s Camera.Parameters class.
After the check we have to create a new class named My FaceDetectionListener which will be implementing the Android’s Camera.FaceDetectionListener. The face detection class overrides the function onFaceDetection and passes the array of Faces detected and the camera as the parameter to this function.
class MyFaceDetectionListener implements CameraController.FaceDetectionListener { @Override public void onFaceDetection(CameraController.Face[] faces) { faces_detected = new CameraController.Face[faces.length]; System.arraycopy(faces, 0, faces_detected, 0, faces.length); } }
Step 2
After creating this class, we need to start the camera of the application to set the face detection listener to it. This can be done by the code snippet provided below
camera = Camera.open(cameraId);
We can open the front camera and the back camera by simply changing the cameraId. If we want to open the front camera, then we need to set the camera Id value as 1 and if we want the back camera to open up we can set the camera Id to be 0.
After this, we can set the face detection listener in the camera. This can be done using the below code snippet.
mCamera.setFaceDetectionListener(fDetectionListener); mCamera.startFaceDetection();
The set face detection listener function takes in the object of the class we created in step 1 as the parameter and calls the Android’s pre defined function to start the face detection. The object of the class we created in step 1 can be created and initialised with the help of code snippet below.
MyFaceDetectionListener fDListener = new MyFaceDetectionListener();
After we have set the detection listener in the camera, as soon as it detects the face, it will call the overridden function onFaceDetection but how do the user know if the face has been detected or not. For this we have to create a rectangular box of size approximately that of the face detected. This can be done with the following code snippet.
int l = faces[i].rect.left; int r = faces[i].rect.right; int t = faces[i].rect.top; int b = faces[i].rect.bottom; Rect uRect = new Rect(l0, t0, r0, b0);
To get the full source code, please check out the Phimpme Android github repository.
You must be logged in to post a comment.