9

On the official sources did not find the information ... There are many examples of how to work with the Camera API, but how to work with Camera2API anything ... a couple of discussions at Stake and all ... there is a similar question to me, but I have a problem not solved. ..

link to the same quetion : Android camera2 face recognition

I took the example of Google's API for Camera 2.

That's what I did, but I did not detect a face :

I added events

    private void createCameraPreviewSession() {
    try {
        SurfaceTexture texture = mTextureView.getSurfaceTexture();
        assert texture != null;

        // We configure the size of default buffer to be the size of camera preview we want.
        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());

        // This is the output Surface we need to start preview.
        Surface surface = new Surface(texture);

        // We set up a CaptureRequest.Builder with the output Surface.
        mPreviewRequestBuilder
                = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);

        // Here, we create a CameraCaptureSession for camera preview.
        mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
                new CameraCaptureSession.StateCallback() {

                    @Override
                    public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                        // The camera is already closed
                        if (null == mCameraDevice) {
                            return;
                        }

                        // When the session is ready, we start displaying the preview.
                        mCaptureSession = cameraCaptureSession;
                        try {



//           ---->>         Установка модуля распознания лица
                            mPreviewRequestBuilder.set(CaptureRequest.STATISTICS_FACE_DETECT_MODE,
                                    CameraMetadata.STATISTICS_FACE_DETECT_MODE_FULL);



                            // Auto focus should be continuous for camera preview.
                            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

                            // Flash is automatically enabled when necessary.
                            setAutoFlash(mPreviewRequestBuilder);

                            // Finally, we start displaying the camera preview.
                            mPreviewRequest = mPreviewRequestBuilder.build();

                            mCaptureSession.setRepeatingRequest(mPreviewRequest,
                                    mCaptureCallback, mBackgroundHandler);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onConfigureFailed(
                            @NonNull CameraCaptureSession cameraCaptureSession) {
                        System.out.println("Failed строка 757");
                    }
                }, null
        );
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

private CameraCaptureSession.CaptureCallback mCaptureCallback
        = new CameraCaptureSession.CaptureCallback() {

    @Override
    public void onCaptureProgressed(@NonNull CameraCaptureSession session,
                                    @NonNull CaptureRequest request,
                                    @NonNull CaptureResult partialResult) {
        process(partialResult);
    }

    @Override
    public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                   @NonNull CaptureRequest request,
                                   @NonNull TotalCaptureResult result) {
        process(result);
    }

    private void process(CaptureResult result) {


--->   //здесь проверяю получает ли он массив лиц или нет + непонятный мод
        Integer mode = result.get(CaptureResult.STATISTICS_FACE_DETECT_MODE);
        Face[] faces = result.get(CaptureResult.STATISTICS_FACES);
        if(faces != null && mode != null)
            System.out.println("tagDDDDDDDDDDDDDDDDDDDDDDDD" + "faces : " +
                    faces.length + " , mode : " + mode);

        switch (mState) {
            case STATE_PREVIEW: {
                // We have nothing to do when the camera preview is working normally.

//                  Here i set Face Detection
                mPreviewRequestBuilder.set(CaptureRequest.STATISTICS_FACE_DETECT_MODE,
                        CameraMetadata.STATISTICS_FACE_DETECT_MODE_FULL);
                break;
            }

here i am checking max count of faces that camera can get

    private void setUpCameraOutputs(int width, int height) {

    CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics
                    = manager.getCameraCharacteristics(cameraId);

            // We don't use a front facing camera in this sample.
            Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
            }

            max_count = characteristics.get(
                    CameraCharacteristics.STATISTICS_INFO_MAX_FACE_COUNT);
            modes = characteristics.get(
                    CameraCharacteristics.STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES);

            System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! max_count " + max_count + " modes " + modes);

** Output is: **

I/System.out: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! max_count 16 modes [I@3e2907e8

** And this is what a log prints **

    03-08 18:34:07.018 7405-7438/com.example.android.camera2basic I/System.out: tagDDDDDDDDDDDDDDDDDDDDDDDDfaces : 0 , mode : 1
03-08 18:34:07.048 7405-7438/com.example.android.camera2basic I/System.out: tagDDDDDDDDDDDDDDDDDDDDDDDDfaces : 0 , mode : 1
03-08 18:34:07.078 7405-7438/com.example.android.camera2basic I/System.out: tagDDDDDDDDDDDDDDDDDDDDDDDDfaces : 0 , mode : 1
03-08 18:34:07.118 7405-7438/com.example.android.camera2basic I/System.out: tagDDDDDDDDDDDDDDDDDDDDDDDDfaces : 0 , mode : 1
03-08 18:34:07.148 7405-7438/com.example.android.camera2basic I/System.out: tagDDDDDDDDDDDDDDDDDDDDDDDDfaces : 0 , mode : 1
03-08 18:34:07.178 7405-7438/com.example.android.camera2basic I/System.out: tagDDDDDDDDDDDDDDDDDDDDDDDDfaces : 0 , mode : 1
03-08 18:34:07.218 7405-7438/com.example.android.camera2basic I/System.out: tagDDDDDDDDDDDDDDDDDDDDDDDDfaces : 0 , mode : 1
03-08 18:34:07.258 7405-7438/com.example.android.camera2basic I/System.out: tagDDDDDDDDDDDDDDDDDDDDDDDDfaces : 0 , mode : 1
03-08 18:34:07.288 7405-7438/com.example.android.camera2basic I/System.out: tagDDDDDDDDDDDDDDDDDDDDDDDDfaces : 0 , mode : 1
03-08 18:34:07.308 7405-7438/com.example.android.camera2basic I/System.out: tagDDDDDDDDDDDDDDDDDDDDDDDDfaces : 0 , mode : 1
03-08 18:34:07.348 7405-7438/com.example.android.camera2basic I/System.out: tagDDDDDDDDDDDDDDDDDDDDDDDDfaces : 0 , mode : 1

Why it isn't reterning face? If someone have a correct working exsample, give please a link. How i can make face detection with camera2API. Throughout the week, I can not understand((

Community
  • 1
  • 1
Sirop4ik
  • 4,543
  • 2
  • 54
  • 121
  • Have you read the android developer information here: http://developer.android.com/reference/android/hardware/camera2/package-summary.html ? – Dale Wilson Mar 08 '16 at 21:41
  • yeah, but it just general discription... How it halp me in Face Detection Camera 2 API? – Sirop4ik Mar 09 '16 at 10:07
  • Will these help ? 1) https://developer.qualcomm.com/software/snapdragon-sdk-android/facial-recognition/user-guide (2) http://code.tutsplus.com/tutorials/an-introduction-to-face-detection-on-android--cms-25212 (3) http://androidbite.blogspot.in/2012/11/android-face-detection-example.html – Sreehari Mar 15 '16 at 07:07
  • Stack Overflow is English only. I have no idea what the Russian text in your question means, could you use English ? You're also more likely to get a good answer if you use English. – Jonas Czech Mar 18 '16 at 12:24
  • Hello Aleksey Timoshchenko, I am also getting same issue. Have you found a solution for this? – Shavareppa Feb 24 '20 at 09:11
  • I am also using camera2 API for face detection. So I want to set face detection for the back camera. Can anyone help me with this – Shavareppa Feb 25 '20 at 11:43

2 Answers2

3

With play services 8.3, Google introduced Mobile Vision APIs. It includes an easy to use API called Face API that detects human faces in images and videos. Do not confuse this with face recognition as Face API currently supports detection only.

Face API detecting human faces

Detecting a face

When the API detects a human face, it is returned as a Face object. The Face object provides the spatial data for the face so you can, for example, draw bounding rectangles around a face, or, if you use landmarks on the face, you can add features to the face in the correct place, such as giving a person a new hat.

Landmarks detected by Face API

It also comes with cool methods like whether the user is smiling :) or is he winking ;) etc to name a few.

Check out the documentation and reference to learn more.

  1. Mobile Vision: Find objects in photos and video, using real-time on-device vision technology.
  2. Face API- Google Developers
  3. Mobile Vision- Youtube
  4. Android developers- Face detection API

Hope this helps :)

vishnu narayanan
  • 3,813
  • 2
  • 24
  • 28
  • really no, i have read this all resources, but smth doing wrong and i wasn't getting face... it always returns 0. It is why i am asking example – Sirop4ik Mar 19 '16 at 15:21
  • yes, i tried out it... but as far as i understand it isn't working with Camera 2 API... There are a face detection samples on photo, but i need on preview... And there aren't code samples how to implement it – Sirop4ik Mar 20 '16 at 07:05
  • Check these samples out: https://github.com/googlesamples/android-vision FaceTracker- This one detects faces on live preview https://github.com/googlesamples/android-vision/tree/master/visionSamples/FaceTracker – vishnu narayanan Mar 20 '16 at 10:10
  • There are samples also work with Camera Api, not with Camera 2 API – Sirop4ik Mar 20 '16 at 10:17
1

Aleksey,

Your RequestBuilder looks correct, but I think that the issue lies in how you are selecting the camera device to use. I have the same setup as you, and it's working.

private boolean setUpCameraOutputs() {
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    if (manager == null) {
        ErrorDialog.buildErrorDialog("This device doesn't support the Camera2 API.").
            show(getFragmentManager(), "dialog");
        return false;
    }
    try {
        // Find a CameraDevice that supports Face recognition, and configure state.
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics
                    = manager.getCameraCharacteristics(cameraId);

            // We only use a camera that supports Faces.
            if (!contains(characteristics.get(
                            CameraCharacteristics.STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES),
                    CameraCharacteristics.STATISTICS_FACE_DETECT_MODE_SIMPLE)) {
                continue;
            }

            StreamConfigurationMap map = characteristics.get(
                    CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            synchronized (mCameraStateLock) {
                mCharacteristics = characteristics;
                mCameraId = cameraId;
            }
            return true;
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

    // If we found no suitable cameras for capturing Faces, warn the user.
    ErrorDialog.buildErrorDialog("This device doesn't support Face detection").
            show(getFragmentManager(), "dialog");
    return false;
}

Hopefully, the above code will at least help you identify whether the problem lies with your device or with the way you are accessing the camera.

  • Thanks for your answer! but i know that camera is working properly, because i got this output `max_count 16` it means that my device can count 16 face... and second fact, in my app i use only back camera it means that it should detect faces because it is main camera... but it still doesn't detect... really i am a rookie in Android. If you have a success in your app with face detection, could you show me please this part of your code where you made it? – Sirop4ik Mar 29 '16 at 08:24
  • Since I used the same googlesamples project as you as the basis for my working example, the only difference should be in this module. Could you try swapping in the above code and see which code paths are being activated? This could give a hint as to the culprit. – James Hodson Apr 01 '16 at 21:37
  • Sorry i have tryed solve this issue for myself, but again without success... I am not understend exactly what do you mean "see which code paths are being activated"? – Sirop4ik Apr 04 '16 at 15:17
  • If you drop in the module above, does it give you the same issues? – James Hodson Apr 10 '16 at 22:17
  • Sorry i am rookie... If i unfrstand you correctly, should i drop in (move) above the code this module ??mPreviewRequestBuilder.set(CaptureRequest.STATISTICS_FACE_DETECT_MODE, CameraMetadata.STATISTICS_FACE_DETECT_MODE_FULL); – Sirop4ik Apr 11 '16 at 08:05