1

I am exploring android vision api and following the github samples provided in android developers documentation.The sample application is detecting the faces.

I want to crop each of the detected faces. I have a rect obj having left,top,right,bottom coordinates.

But I lack the source Bitmap to crop the face.

Things I have done:

1.Tried using the custom detector given in this SOF post

Here myFaceDetector's SparseArray detect(Frame frame) method is getting called repeatedly and it is not detecting the face.The Processor set is not getting called. I used the below code

    FaceDetector detector = new FaceDetector.Builder(context)
            .setClassificationType(FaceDetector.ALL_CLASSIFICATIONS)
            .build();

    MyFaceDetector myFaceDetector = new MyFaceDetector(detector);

    myFaceDetector.setProcessor(new MultiProcessor.Builder<>(new GraphicFaceTrackerFactory())
    .build());

    mCameraSource = new CameraSource.Builder(context, myFaceDetector)
            .setRequestedPreviewSize(640, 480)
            .setFacing(CameraSource.CAMERA_FACING_FRONT)
            .setRequestedFps(2.0f)
            .build();

The GraphicFaceTrackerFactory() is not getting called after the camera preview started.

2.Tried to take the camera picture as source bitmap but struck in taking pictures continuously.

Any help will be very useful.thanks in advance.

Community
  • 1
  • 1
adi
  • 984
  • 15
  • 33

1 Answers1

0

I want to crop each of the detected faces. I have a rect obj having left,top,right,bottom coordinates.

Once the faces have been detected, you can obtain the location and dimension of the faces with Face.getPosition() , Face.getHeight(), and Face.getWidth()

Here myFaceDetector's SparseArray detect(Frame frame) method is getting called repeatedly and it is not detecting the face

Have you tried setting the correct orientation? in the Frame.Builder, add setRotation(ROT), where ROT is an int with any of these values: 0,1,2 or 3 (representing 0,90,180, and 270 degs., respectively). This was mentioned as a note in your quoted answer :)

But I lack the source Bitmap to crop the face.

a Bitmap can be generated from the current frame like this

Bitmap frameToBitmap(Frame currentFrame){
    ByteBuffer buffer = currentFrame.getGrayscaleImageData(); // getBitmap only works when the frame was created using Bitmap
    byte[] bytes = new byte[buffer.capacity()];
    buffer.get(bytes);
    int[] pixels = applyGrayScale(new int[bytes.length], bytes, img.getWidth(),img.getHeight());
    return Bitmap.createBitmap(pixels, img.getWidth(), img.getHeight(), Bitmap.Config.ARGB_8888);
}

static int[] applyGrayScale(int[] pixels, byte[] data, int width, int height) { //from another SOF answer...
    int p;
    int size = width*height;
    for(int i = 0; i < size; i++) {
        p = data[i] & 0xFF;
        pixels[i] = 0xff000000 | p<<16 | p<<8 | p;
    }
    return pixels;
}

as a side note, there is code available for the CamerSource class, although in the neighbor example: BarCode Reader

pwoolvett
  • 524
  • 4
  • 13