Android人脸检测实例

498)this.width=498;' onmousewheel = 'javascript:return big(this)' width="500" height="334" alt="" src="http://images.51cto.com/files/uploadimg/20120430/2028320.jpg" />


图片来源:Wikipedia

所谓人脸检测就是指从一副图片或者一帧视频中标定出所有人脸的位置和尺寸。人脸检测是人脸识别系统中的一个重要环节,也可以独立应用于视频监控。在数字媒体日益普及的今天,利用人脸检测技术还可以帮助我们从海量图片数据中快速筛选出包含人脸的图片。 在目前的数码相机中,人脸检测可以用来完成自动对焦,即“脸部对焦”。“脸部对焦”是在自动曝光和自动对焦发明后,二十年来最重要的一次摄影技术革新。家用数码相机,占绝大多数的照片是以人为拍摄主体的,这就要求相机的自动曝光和对焦以人物为基准。

via cdstm.cn

构建一个人脸检测的Android Activity

你可以构建一个通用的Android Activity,我们扩展了基类ImageView,成为MyImageView,而我们需要进行检测的包含人脸的位图文件必须是565格式,API才能正常工作。被检测出来的人脸需要一个置信测度(confidence measure),这个措施定义在android.media.FaceDetector.Face.CONFIDENCE_THRESHOLD。

最重要的方法实现在setFace(),它将FaceDetector对象实例化,同时调用findFaces,结果存放在faces里,人脸的中点转移到MyImageView。代码如下:

//百搜技术网:http://www.baisoujs.com 

 
 
 
 

 

 
 
  
  
  
  
  1. public class TutorialOnFaceDetect1 extends Activity {    private MyImageView mIV;   
  2.  private Bitmap mFaceBitmap;    private int mFaceWidth = 200;   
  3.  private int mFaceHeight = 200;    private static final int MAX_FACES = 1;   
  4.  private static String TAG = "TutorialOnFaceDetect";      
  5. @Override   public void onCreate(Bundle savedInstanceState) {   
  6. super.onCreate(savedInstanceState);      
  7. mIV = new MyImageView(this);   setContentView(mIV, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));   
  8.    // load the photo   
  9. Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.face3);   mFaceBitmap = b.copy(Bitmap.Config.RGB_565, true);   
  10. b.recycle();      
  11. mFaceWidth = mFaceBitmap.getWidth();   mFaceHeight = mFaceBitmap.getHeight();   
  12. mIV.setImageBitmap(mFaceBitmap);      
  13. // perform face detection and set the feature points setFace();      
  14. mIV.invalidate();   }   
  15.    public void setFace() {   
  16. FaceDetector fd;   FaceDetector.Face [] faces = new FaceDetector.Face[MAX_FACES];   
  17. PointF midpoint = new PointF();   int [] fpx = null;   
  18. int [] fpy = null;   int count = 0;   
  19.    try {   
  20. fd = new FaceDetector(mFaceWidth, mFaceHeight, MAX_FACES);   count = fd.findFaces(mFaceBitmap, faces);   
  21. } catch (Exception e) {   Log.e(TAG, "setFace(): " + e.toString());   
  22. return;   }   
  23.    // check if we detect any faces   
  24. if (count > 0) {   fpx = new int[count];   
  25. fpy = new int[count];      
  26. for (int i = 0; i < count; i++) {   try {   
  27. faces[i].getMidPoint(midpoint);      
  28. fpx[i] = (int)midpoint.x;   fpy[i] = (int)midpoint.y;   
  29. } catch (Exception e) {   Log.e(TAG, "setFace(): face " + i + ": " + e.toString());   
  30. }   }   
  31. }      
  32. mIV.setDisplayPoints(fpx, fpy, count, 0);   }   
  33. }  

接下来的代码中,我们在MyImageView中添加setDisplayPoints() ,用来在被检测出的人脸上标记渲染。图1展示了一个标记在被检测处的人脸上处于中心位置。

 
 
  
  
  
  
  1. // set up detected face features for display   public void setDisplayPoints(int [] xx, int [] yy, int total, int style) {   
  2.  mDisplayStyle = style;    mPX = null;   
  3.  mPY = null;      
  4. if (xx != null && yy != null && total > 0) {   mPX = new int[total];   
  5. mPY = new int[total];      
  6. for (int i = 0; i < total; i++) {   mPX[i] = xx[i];   
  7. mPY[i] = yy[i];   }   
  8. }   }  


498)this.width=498;' onmousewheel = 'javascript:return big(this)' width="320" height="480" alt="" src="http://images.51cto.com/files/uploadimg/20120430/2028321.jpg" />


图1:单一人脸检测

多人脸检测

通过FaceDetector可以设定检测到人脸数目的上限。比如设置最多只检测10张脸:

 
 
  
  
  
  
  1. private static final int MAX_FACES = 10;  

图2展示检测到多张人脸的情况。


498)this.width=498;' onmousewheel = 'javascript:return big(this)' width="320" height="480" alt="" src="http://images.51cto.com/files/uploadimg/20120430/2028322.jpg" />


图2:多人人脸检测

定位眼睛中心位置

Android人脸检测返回其他有用的信息,例同时会返回如eyesDistance,pose,以及confidence。我们可以通过eyesDistance来定位眼睛的中心位置。

下面的代码中,我们将setFace()放在doLengthyCalc()中。同时图3展示了定位眼睛中心位置的效果。

 
 
  
  
  
  
  1. public class TutorialOnFaceDetect extends Activity {    private MyImageView mIV;   
  2. &nbs

文章出自:http://www.baisoujs.com/detail_137070696118762.html

你可能感兴趣的:(Android人脸检测实例)