1

我正在尝试在 Android 中开发示例人脸检测应用程序。我已经尝试过使用FaceDetecorAndroid SDK 本身提供的类,但它没有提供正确的结果。我有一个位图。库应该分析并说明在位图中是否可以使用人脸。请给我一些建议。期待更好的结果。我已经为此图像尝试了以下参考图像图像,它说没有面孔。

主类:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class DetectFace1Activity extends Activity {
    /** Called when the activity is first created. */

    ImageView image;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        image=(ImageView)findViewById(R.id.image);
        FaceView faceView = new FaceView(this);
        setContentView(faceView);
    }
}    

人脸检测类:

 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
 import android.graphics.PointF;
 import android.graphics.Rect;
 import android.media.FaceDetector;
 import android.util.Log;
 import android.view.View;

 public class FaceView extends View {
 private static final int NUM_FACES = 10; // max is 64
 private static final boolean DEBUG = true;

 private FaceDetector arrayFaces;
 private FaceDetector.Face getAllFaces[] = new FaceDetector.Face[NUM_FACES];
 private FaceDetector.Face getFace = null;

 private PointF eyesMidPts[] = new PointF[NUM_FACES];
 private float  eyesDistance[] = new float[NUM_FACES];

 private Bitmap sourceImage;

 private Paint tmpPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 private Paint pOuterBullsEye = new Paint(Paint.ANTI_ALIAS_FLAG);
 private Paint pInnerBullsEye = new Paint(Paint.ANTI_ALIAS_FLAG);

 private int picWidth, picHeight;
 private float xRatio, yRatio;

 public FaceView(Context context) {
      super(context);

      pInnerBullsEye.setStyle(Paint.Style.FILL);
      pInnerBullsEye.setColor(Color.RED);

      pOuterBullsEye.setStyle(Paint.Style.STROKE);
      pOuterBullsEye.setColor(Color.RED);

      tmpPaint.setStyle(Paint.Style.STROKE);
      tmpPaint.setTextAlign(Paint.Align.CENTER);

      BitmapFactory.Options bfo = new BitmapFactory.Options();
      bfo.inPreferredConfig = Bitmap.Config.RGB_565;

      sourceImage = BitmapFactory.decodeResource( getResources() ,R.drawable.black, bfo);

      picWidth = sourceImage.getWidth();
      picHeight = sourceImage.getHeight();

      arrayFaces = new FaceDetector( picWidth, picHeight, NUM_FACES );
      arrayFaces.findFaces(sourceImage, getAllFaces);

      for (int i = 0; i < getAllFaces.length; i++)
      {
           getFace = getAllFaces[i];
           try {
                PointF eyesMP = new PointF();
                getFace.getMidPoint(eyesMP);
                eyesDistance[i] = getFace.eyesDistance();
                eyesMidPts[i] = eyesMP;

                if (DEBUG)
                {
                     Log.i("Face", i +  " " + getFace.confidence() + " " +     getFace.eyesDistance() + " " + "Pose: ("+ getFace.pose(FaceDetector.Face.EULER_X) + ","
+ getFace.pose(FaceDetector.Face.EULER_Y) + "," +     getFace.pose(FaceDetector.Face.EULER_Z) + ")" + "Eyes Midpoint: ("+eyesMidPts[i].x + "," + eyesMidPts[i].y +")");
                }
           } catch (Exception e) {
                if (DEBUG) Log.e("Face", i + " is null");
           }

      }
 }

 @Override
 protected void onDraw(Canvas canvas)
 {
      xRatio = getWidth()*1.0f / picWidth;
      yRatio = getHeight()*1.0f / picHeight;
      canvas.drawBitmap( sourceImage, null , new Rect(0,0,getWidth(),getHeight()),tmpPaint);
      for (int i = 0; i < eyesMidPts.length; i++)
      {
           if (eyesMidPts[i] != null)
           {
                pOuterBullsEye.setStrokeWidth(eyesDistance[i] /6);
                canvas.drawCircle(eyesMidPts[i].x*xRatio, eyesMidPts[i].y*yRatio, eyesDistance[i] / 2 , pOuterBullsEye);
                canvas.drawCircle(eyesMidPts[i].x*xRatio, eyesMidPts[i].y*yRatio, eyesDistance[i] / 6 , pInnerBullsEye);
           }
      }
 }
4

2 回答 2

3

getFace.confidence()是你需要的。

公众持股量信心 ()

自:API 级别 1 返回介于 0 和 1 之间的置信度因子。这表明所找到的实际上是一张脸的确定程度。高于 0.3 的置信系数通常就足够了。

请参阅Android 开发人员的参考资料

于 2012-10-01T17:38:14.527 回答
0

FaceDetector 实际上只是不能很好地工作。我有一个需要使用它的应用程序,我花了一两天时间在它上面,但它不能一致或准确地识别人脸。不值得使用,不要浪费你的时间。它根本不符合任何人期望的标准。我尝试了几十张照片,也许它应该识别的面孔中有一半能够被识别出来。

于 2014-07-07T17:16:31.310 回答