Search code examples
androidface-detectionface-recognitiongoogle-vision

draw a text on nose base with face detection API


I'm trying to draw a text on a face using android face detection API.
Right now, I did this

for(Landmark landmark : face.getLandmarks()){
        if(landmark.getType() == Landmark.NOSE_BASE){
            Bitmap moustache = BitmapFactory.decodeResource(resources, R.drawable.moustache);
            canvas.drawText("=====", landmark.getPosition().x, landmark.getPosition().y, mIdPaint);
        }
    }

but turns out the text ===== is draw on top of the head, and I don't know why.
If someone need more code, just let me know


Solution

  • If you are drawing graphics over a live camera preview, you need to take a few things into account:

    1. the device's rotation
    2. the scale of the view relative to the size of the preview image
    3. whether you are using the front facing camera (which will mirror the image)

    The sample code for the face tracker demo has utility methods (translateX, translateY, scaleX, scaleY) to help with this:

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

    https://github.com/googlesamples/android-vision/blob/master/visionSamples/FaceTracker/app/src/main/java/com/google/android/gms/samples/vision/face/facetracker/ui/camera/GraphicOverlay.java#L100