2012-04-25 185 views
1

我想在Android中開發一個樣本人臉檢測應用程序。我曾嘗試過使用Android SDK自帶的FaceDetecor類,但它沒有提供正確的結果。我有一個位圖。圖書館應該分析並且應該說出位圖是否可用。請給我一些建議。希望獲得更好的結果。我試過以下Reference Image圖片這張圖片上沒有臉。Android中的人臉檢測

主類:

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); 
      } 
     } 
} 
+0

你應該發佈你使用的代碼。 – Jared 2012-04-25 15:27:22

+0

我已經發布在主類和facedetect類之上。 – RAJESH 2012-04-25 16:17:08

回答

3

getFace.confidence()是你所需要的。

公衆持股信心()

自:API等級1 返回置信因子介於0和1這表示什麼已經發現實際上是如何某些一張臉。 0.3以上的置信度通常足夠好。

請參閱從Android Developers

0

FaceDetector參考實際上只是不能很好地工作。我有一個需要使用它的應用程序,我花了一兩天時間,它只是不能一致或準確地識別臉部。這是不值得使用的,不要浪費你的時間。它根本不符合任何人所期望的標準。我嘗試了幾十張照片,也許有一半它應該承認的面孔能夠被識別。

+0

Android中有一款新的面部檢測器,它的性能更好一些。請注意,它位於不同的包(com.google.android.gms.vision.face.FaceDetector)中。請參閱:https://developers.google.com/vision/ – pm0733464 2015-08-20 16:43:16