2017-08-05 21 views
-1

Google Mobile Vision APIを使用して目を検出して眼の上に眼鏡をかけようとしています。ここランドマークの位置に基づいて顔にオブジェクトを配置するgoogle vision api

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

である私は

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

を試してみましたが、どのようなこの私のコードはGoogleのソースからコピーされています正常に動作しますが、画像を移動すると表示されます。

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

画像ビューは間違って配置されています。

誰かが説明したり、私が逃しているものを助けることができますか?

答えて

0

double diff = leftEye.getPosition().x * mImageView.scale - rightEye.getPosition().x * mImageView.scale - 15; -15を削除してもう一度お試しください。

関連する問題