0

我正在尝试使用 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);
        }
    }

结果是

在此处输入图像描述

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);

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

任何人都可以解释或帮助我错过了什么?

4

1 回答 1

0

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

于 2017-09-07T08:23:14.210 回答