I want to detect the numbers of faces in the front camera frame. I can detect the face once I get the image using this :http://www.developer.com/ws/android/programming/face-detection-with-android-apis.html. But I don't know how to capture an image using the front camera every 30 seconds without an user interaction.Can someone please help me?
To detect faces in an image, create an InputImage object from either a Bitmap , media. Image , ByteBuffer , byte array, or a file on the device. Then, pass the InputImage object to the FaceDetector 's process method. For face detection, you should use an image with dimensions of at least 480x360 pixels.
It simply means that the face detection system can identify that there is a human face present in an image of video – it cannot identify that person. Face detection is a component of Facial Recognition systems – the first stage of facial recognition is detecting the presence of a human face in the first place.
When it comes to a good, all-purpose face detector, I suggest using OpenCV's DNN face detector: It achieves a nice balance of speed and accuracy. As a deep learning-based detector, it's more accurate than its Haar cascade and HOG + Linear SVM counterparts. It's fast enough to run real-time on CPUs.
Face detection is implemented using the camshift algorithm. The camshift algorithm works on a search window that can find facial movements in each frame. The camshift algorithm that has been applied can calculate the size and location of the search window that will be used for the next frame.
Following code will capture photo from your camera after every 5 secs.
if (TIMER_STARTED) {
multishotTimer.cancel();
multishotTimer.purge();
TIMER_STARTED = false;
} else {
multishotTimer = new Timer();
multishotTimer.schedule(new TimerTask() {
@Override
public void run() {
TIMER_STARTED = true;
Camera camera = surfaceView.getCamera();
camera.takePicture(null, null,
new HandlePictureStorage());
}
}, 1000, 5000L);
}
Here, TIMER_STARTED is boolean which indicate whether timer is running or not.
Following is code for HandlePictureStorage
private class HandlePictureStorage implements PictureCallback {
@Override
public void onPictureTaken(byte[] picture, final Camera camera) {
//do something when picture is captured...
}
}
You can create manually a SurfaceView and preview camera on it as follows:
//First get a reference to the SurfaceView displaying the camera preview
cameraSurface = (SurfaceView) findViewById(R.id.cameraSurface);
cameraSurfaceHolder = cameraSurface.getHolder();
cameraSurfaceHolder.addCallback(cameraSurfaceHolderCallbacks);
.
.
.
private SurfaceHolder.Callback cameraSurfaceHolderCallbacks = new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(mCamera == null)return;
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera = Camera.open();
mCamera.setPreviewDisplay(holder);
} catch (Exception exception) {
if(mCamera == null)return;
mCamera.release();
mCamera = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Parameters cameraParameters = mCamera.getParameters();
cameraParameters.setPreviewSize(320, 240);
cameraParameters.setPictureSize(320, 240);
int avrgExposure = (cameraParameters.getMinExposureCompensation() + cameraParameters.getMaxExposureCompensation())/2;
cameraParameters.setExposureCompensation(avrgExposure);
mCamera.setParameters(cameraParameters);
mCamera.startPreview();
mCamera.takePicture(null, null, mPictureCallback);
}
};
Do not forget to add the proper camera permission in your manifest:
<uses-permission android:name="android.permission.CAMERA"/>
And finally if you are using an Android 4.0 device or above you can use the method:
mCamera.startFaceDetection();
.
.
.
private FaceDetectionListener faceDetectionListener = new FaceDetectionListener() {
@Override
public void onFaceDetection(Face[] faces, Camera camera) {
//Faces have been detected...
}
};
.
.
.
mCamera.setFaceDetectionListener(faceDetectionListener);
You can go to this post which explains everything related to that specific functionality and even provides a functional Android Source Code that you can download to do it yourself.
Regards!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With