android-人臉檢測FaceDetector – Android移動開發技術文章_手機開發 Android移動開發教學課程

無意中看到這個類,就查瞭一下。
FaceDetector檢測到的並不是人的全臉,而隻是雙眼。

實際測試中,發現圖片太小的話檢測不到人臉,試驗中使用小於100×100的圖片檢測不到人臉,但是由於Android內存有限,圖片太大的話,會出現無法加載圖片的異常。圖片的格式必須為Bitmap RGB565格式。

文檔  findFaces(Bitmap bitmap, Face[] faces)方法中有說明。

方法有

float confidence() ;可信度(0-1)


float eyesDistance(); 雙眼的距離


void getMidPoint(PointF point);設置雙眼的中點


float pose(int euler); 眼睛的pose,傳入的為歐拉軸檢索的角度。


view plaincopy to clipboardprint?package com.shao.face; 
 
import android.app.Activity; 
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.media.FaceDetector; 
import android.media.FaceDetector.Face; 
import android.os.Bundle; 
import android.view.View; 
 
public class FaceDetectorActivity extends Activity { 
      /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(new myView(this)); 
    } 
  
    private class myView extends View{ 
  
        private int imageWidth, imageHeight; 
        private int numberOfFace = 5; 
        private FaceDetector myFaceDetect; 
        private FaceDetector.Face[] myFace; 
        float myEyesDistance,mConfidence ,mPose;  
        int numberOfFaceDetected; 
  
        Bitmap myBitmap; 
  
        public myView(Context context) { 
            super(context); 
            BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options(); 
            BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; 
            myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.images2, BitmapFactoryOptionsbfo); 
            imageWidth = myBitmap.getWidth(); 
            imageHeight = myBitmap.getHeight(); 
            myFace = new FaceDetector.Face[numberOfFace]; 
            myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace); 
            numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace); 
        } 
  
        protected void onDraw(Canvas canvas) { 
            // TODO Auto-generated method stub  
  
            canvas.drawBitmap(myBitmap, 0, 0, null); 
  
            Paint myPaint = new Paint(); 
            myPaint.setColor(Color.GREEN); 
            myPaint.setStyle(Paint.Style.STROKE); 
            myPaint.setStrokeWidth(3); 
  
            for(int i=0; i < numberOfFaceDetected; i++) 
            { 
                Face face = myFace[i]; 
                PointF myMidPoint = new PointF(); 
                face.getMidPoint(myMidPoint); 
                myEyesDistance = face.eyesDistance(); 
                mConfidence = face.confidence(); 
                System.out.println("mConfidence"+mConfidence); 
                mPose =  face.pose(face.EULER_X); 
                System.out.println("mPose"+mPose); 
                canvas.drawRect( 
                        (int)(myMidPoint.x – myEyesDistance), 
                        (int)(myMidPoint.y – myEyesDistance), 
                        (int)(myMidPoint.x + myEyesDistance), 
                        (int)(myMidPoint.y + myEyesDistance), 
                        myPaint); 
            } 
        } 
    } 

發佈留言