googlesamples / android-vision

Deprecated: The Mobile Vision API is now a part of ML Kit: Check out this repo:
https://github.com/firebase/quickstart-android/tree/master/mlkit
Apache License 2.0
2.93k stars 1.73k forks source link

Real time landmarks detection #197

Open Siignature opened 7 years ago

Siignature commented 7 years ago

How to implementate the real time detection and position of the landmarks? I'm having problems with the landmarks, which are placed in incorrect positions. That's my code.

public void draw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        Face face = mFace;
        if (face == null) {
            return;
        }

        //Draws a bounding box around the face.
        float xOffset = scaleX(face.getWidth() / 2.0f);
        float yOffset = scaleY(face.getHeight() / 2.0f);
        float left = x - xOffset;
        float top = y - yOffset;
        float right = x + xOffset;
        float bottom = y + yOffset;
        canvas.drawRect(left, top, right, bottom, mBoxPaint);

        //Draws the landmarks
            for (Landmark landmark : face.getLandmarks()) {
                int cx = (int) (landmark.getPosition().x * 2);
                int cy = (int) (landmark.getPosition().y * 2);
                canvas.drawCircle(cx, cy, 10, paint);
            }

    }
pm0733464 commented 7 years ago

The issue appears to be that you need to adjust for scaling between the original image size and the display size. Rather than multiplying by 2, you should multiply by this scale factor. See this example:

https://github.com/googlesamples/android-vision/blob/master/visionSamples/photo-demo/app/src/main/java/com/google/android/gms/samples/vision/face/photo/FaceView.java#L88

hipsterreed commented 7 years ago

I posted a solution to this on issue #217. Hope it helps! :)