欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

Android API教程:人脸检测(上)

来源: 开发者 投稿于  被查看 29060 次 评论:216

Android API教程:人脸检测(上)


图片来源: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。代码如下:

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

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

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

图1:单一人脸检测

多人脸检测

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

  1. private static final int MAX_FACES = 10;  

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

图2:多人人脸检测

定位眼睛中心位置

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

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

  1. public class TutorialOnFaceDetect extends Activity {   
  2.  private MyImageView mIV;   
  3.  private Bitmap mFaceBitmap;   
  4.  private int mFaceWidth = 200;   
  5.  private int mFaceHeight = 200;   
  6.  private static final int MAX_FACES = 10;   
  7.  private static String TAG = "TutorialOnFaceDetect";   
  8. private static boolean DEBUG = false;   
  9.    
  10. protected static final int GUIUPDATE_SETFACE = 999;   
  11. protected Handler mHandler = new Handler(){   
  12. // @Override   
  13. public void handleMessage(Message msg) {   
  14. mIV.invalidate();   
  15.    
  16. super.handleMessage(msg);   
  17. }   
  18. };   
  19.    
  20. @Override   
  21. public void onCreate(Bundle savedInstanceState) {   
  22. super.onCreate(savedInstanceState);   
  23.    
  24. mIV = new MyImageView(this);   
  25. setContentView(mIV, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));   
  26.    
  27. // load the photo   
  28. Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.face3);   
  29. mFaceBitmap = b.copy(Bitmap.Config.RGB_565, true);   
  30. b.recycle();   
  31.    
  32. mFaceWidth = mFaceBitmap.getWidth();   
  33. mFaceHeight = mFaceBitmap.getHeight();   
  34. mIV.setImageBitmap(mFaceBitmap);   
  35. mIV.invalidate();   
  36.    
  37. // perform face detection in setFace() in a background thread   
  38. doLengthyCalc();   
  39. }   
  40.    
  41. public void setFace() {   
  42. FaceDetector fd;   
  43. FaceDetector.Face [] faces = new FaceDetector.Face[MAX_FACES];   
  44. PointF eyescenter = new PointF();   
  45. float eyesdist = 0.0f;   
  46. int [] fpx = null;   
  47. int [] fpy = null;   
  48. int count = 0;   
  49.    
  50. try {   
  51. fd = new FaceDetector(mFaceWidth, mFaceHeight, MAX_FACES);   
  52. count = fd.findFaces(mFaceBitmap, faces);   
  53. } catch (Exception e) {   
  54. Log.e(TAG, "setFace(): " + e.toString());   
  55. return;   
  56. }   
  57.    
  58. // check if we detect any faces   
  59. if (count > 0) {   
  60. fpx = new int[count * 2];   
  61. fpy = new int[count * 2];   
  62.    
  63. for (int i = 0; i < count; i++) {   
  64. try {   
  65. faces[i].getMidPoint(eyescenter);   
  66. eyesdist = faces[i].eyesDistance();   
  67.    
  68. // set up left eye location   
  69. fpx[2 * i] = (int)(eyescenter.x - eyesdist / 2);   
  70. fpy[2 * i] = (int)eyescenter.y;   
  71.    
  72. // set up right eye location   
  73. fpx[2 * i + 1] = (int)(eyescenter.x + eyesdist / 2);   
  74. fpy[2 * i + 1] = (int)eyescenter.y;   
  75.    
  76. if (DEBUG) {   
  77. Log.e(TAG, "setFace(): face " + i + ": confidence = " + faces[i].confidence()   
  78. + ", eyes distance = " + faces[i].eyesDistance()   
  79. + ", pose = ("+ faces[i].pose(FaceDetector.Face.EULER_X) + ","   
  80. + faces[i].pose(FaceDetector.Face.EULER_Y) + ","   
  81. + faces[i].pose(FaceDetector.Face.EULER_Z) + ")"   
  82. + ", eyes midpoint = (" + eyescenter.x + "," + eyescenter.y +")");   
  83. }   
  84. } catch (Exception e) {   
  85. Log.e(TAG, "setFace(): face " + i + ": " + e.toString());   
  86. }   
  87. }   
  88. }   
  89.    
  90. mIV.setDisplayPoints(fpx, fpy, count * 2, 1);   
  91. }   
  92.    
  93. private void doLengthyCalc() {   
  94. Thread t = new Thread() {   
  95. Message m = new Message();   
  96.    
  97. public void run() {   
  98. try {   
  99. setFace();   
  100. m.what = TutorialOnFaceDetect.GUIUPDATE_SETFACE;   
  101. TutorialOnFaceDetect.this.mHandler.sendMessage(m);   
  102. } catch (Exception e) {   
  103. Log.e(TAG, "doLengthyCalc(): " + e.toString());   
  104. }   
  105. }   
  106. };   
  107.    
  108. t.start();   
  109. }   
  110. }   

图3:定位眼睛中心位置

编辑推荐】

用户评论