基于地标位置google vision api将对象放置在Face上

时间:2017-08-05 09:25:21

标签: java android face

我正试图用Google Mobile Vision api检测眼睛并将眼镜戴在眼睛上。

这是我试过的

Face face = faces.valueAt(0);
                Landmark leftEye = null;
                Landmark rightEye = null;

                for (Landmark landmark : face.getLandmarks())
                {
                    if (landmark.getType() == Landmark.LEFT_EYE)
                        leftEye = landmark;
                    else if (landmark.getType() == Landmark.RIGHT_EYE)
                        rightEye = landmark;
                }
                if(leftEye != null && rightEye != null)
                {
                    double diff = leftEye.getPosition().x * mImageView.scale - rightEye.getPosition().x * mImageView.scale - 15;
                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.raw.glasses1);
                    int width = (int) pxFromDp(this, (float) diff);
                    final double viewWidthToBitmapWidthRatio = (double)width  / (double) bitmap.getWidth();
                    int height = (int) (bitmap.getHeight() * viewWidthToBitmapWidthRatio);
                    filterImg.getLayoutParams().width  = width;
                    filterImg.getLayoutParams().height = height;
                    filterImg.invalidate();
                    float x = (rightEye.getPosition().x + 15) * mImageView.scale;
                    float y = (rightEye.getPosition().y + face.getPosition().y) * mImageView.scale;
                    filterImg.setX(x);
                    filterImg.setY(y);
                    filterImg.setRotation(face.getEulerY());
                    filterImg.setImageResource(rawFile);
                    mImageView.setData(bitmap, faces);
                }
                else
                    Toast.makeText(ImageFiltersActivity.this, "Unable to parse landmarks", Toast.LENGTH_SHORT).show();

这是我从谷歌资源中复制的代码

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mBitmap != null && mFaces != null) {
            double deviceScale = drawBitmapToDeviceSize(canvas);
            drawFaceDetectionBox(canvas, deviceScale);
        }
    }

    private double drawBitmapToDeviceSize(Canvas canvas) {
        double viewWidth = canvas.getWidth();
        double viewHeight = canvas.getHeight();
        double imageWidth = mBitmap.getWidth();
        double imageHeight = mBitmap.getHeight();
        scale = (float) Math.min(viewWidth / imageWidth, viewHeight / imageHeight);

        Rect bitmapBounds = new Rect(0, 0, (int) (imageWidth * scale), (int) (imageHeight * scale));
        return scale;
    }

    private void drawFaceDetectionBox(Canvas canvas, double deviceScale)
    {
        Paint paint = new Paint();
        paint.setColor(Color.YELLOW);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);

        for (int i = 0; i < mFaces.size(); ++i)
        {
            Face face = mFaces.valueAt(i);
            float x1 = (float) (face.getPosition().x * deviceScale);
            float y1 = (float) (face.getPosition().y * deviceScale);
            float x2 = (float) (x1 + face.getWidth() * deviceScale);
            float y2 = (float) (y1 + face.getHeight() * deviceScale);

            for (Landmark landmark : face.getLandmarks())
            {
                int type = landmark.getType();
                float m1 = (float) (landmark.getPosition().x * deviceScale);
                float m2 = (float) (landmark.getPosition().y * deviceScale);

                canvas.drawCircle(m1, m2, 2, paint);
            }

            canvas.drawRect(x1, y1, x2, y2,
                    paint);
        }
    }

结果是

enter image description here

float m1 = (float) (landmark.getPosition().x * deviceScale);
                    float m2 = (float) (landmark.getPosition().y * deviceScale);

                    canvas.drawCircle(m1, m2, 2, paint);

工作正常,但当我移动我的imageView

float x = (rightEye.getPosition().x + 15) * mImageView.scale;
                        float y = (rightEye.getPosition().y + face.getPosition().y) * mImageView.scale;
                        filterImg.setX(x);
                        filterImg.setY(y);

图像视图放错位置,不在眼睛上。

任何人都可以解释或帮助我缺少什么?

1 个答案:

答案 0 :(得分:0)

double diff = leftEye.getPosition().x * mImageView.scale - rightEye.getPosition().x * mImageView.scale - 15; 删除-15然后重试。