learner
learner

Reputation: 4818

How to use camera resource with FaceDetector in android?

I want to use FaceDetector in android. I tried FaceDetector with a bitmap image and its working fine.

(with the help of: http://code.tutsplus.com/tutorials/an-introduction-to-face-detection-on-android--cms-25212)

Now, I want to detect face, when camera is on. The documentation writes:

Alternatively, a detector may be used within a pipeline structure, in conjunction with sources (e.g., CameraSource) and processors (e.g., LargestFaceFocusingProcessor), enabling you to construct fairly advanced detection pipelines with minimal coding. For example, the code below creates and starts a pipeline that continuously receives preview frames from a camera source for the front facing camera, runs detection on the frames, manages tracking of the most prominent face, and delivers continuous update notifications over time to a developer-defined "FaceTracker" instance.

https://developers.google.com/android/reference/com/google/android/gms/vision/face/FaceDetector

The code it provides:

detector.setProcessor(
   new LargestFaceFocusingProcessor(
     detector,
     new FaceTracker()));

 CameraSource cameraSource = new CameraSource.Builder(context, detector)
   .setFacing(CameraSource.CAMERA_FACING_FRONT)
   .setRequestedPreviewSize(320, 240)
   .build()
   .start();

I want to implement this in my code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FaceDetector detector = new FaceDetector.Builder(this)
                .setProminentFaceOnly(true)
                .build();
        detector.setProcessor(
                new LargestFaceFocusingProcessor(
                        detector,
                        new FaceTracker()));

        try {
            CameraSource cameraSource = new CameraSource.Builder(this, detector)
                    .setFacing(CameraSource.CAMERA_FACING_FRONT)
                    .setRequestedPreviewSize(320, 240)
                    .build()
                    .start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class FaceTracker extends Tracker
    {
        public void onDone ()
        {

        }


    }

}

but, unable to move forward. Can you please help me to code it further?

Upvotes: 1

Views: 3309

Answers (1)

pm0733464
pm0733464

Reputation: 2862

Check out the face tracker sample code, which is similar. It also handles getting camera permissions and activity pause/resume:

https://github.com/googlesamples/android-vision/blob/master/visionSamples/FaceTracker/app/src/main/java/com/google/android/gms/samples/vision/face/facetracker/FaceTrackerActivity.java

What problems are you encountering?

Note that the "prominentFaceOnly" option will only detect faces that appear relatively large in the camera preview (i.e., face is around 35% or more of the width of the preview image).

Upvotes: 2

Related Questions