当前位置:操作系统 > 安卓/Android >>

android-人脸检测FaceDetector

无意中看到这个类,就查了一下。
FaceDetector检测到的并不是人的全脸,而只是双眼。

实际测试中,发现图片太小的话检测不到人脸,试验中使用小于100x100的图片检测不到人脸,但是由于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); 
            } 
        } 
    } 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,