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

移动开发 Android
通过两个主要的API,Android提供了一个直接在位图上进行脸部检测的方法,这两个API分别是 android.media.FaceDetector和android.media.FaceDetector.Face,已经包含在Android官方API中。本教程来自Developer网站,向大家介绍了这些API,同时提供教程中实例代码下载。

 

[[68937]]

 

图片来源: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:定位眼睛中心位置

【编辑推荐】

  1. Android API中文文档Button
  2. Android API中文文档GridView
  3. Android API中文文档ImageView
责任编辑:冰凝儿 来源: developer
相关推荐

2017-01-23 21:35:58

Android人脸检测介绍

2013-08-26 10:53:26

人脸检测API

2020-12-30 08:20:04

人脸检测Retina FacemobileNet

2019-07-02 08:00:00

JavaScriptWeb对象

2023-11-23 12:43:53

人脸识别程序

2019-03-27 15:10:02

开源人脸检测库

2018-01-23 09:17:22

Python人脸识别

2018-05-08 14:25:22

Pythondlib人脸检测

2015-02-10 10:08:59

JavaScript

2009-06-16 09:41:00

2018-05-02 15:41:27

JavaScript人脸检测图像识别

2018-07-10 15:50:29

2022-04-05 20:54:21

OpenCVPython人脸检测

2020-11-02 11:24:52

算法人脸识别技术

2018-03-12 16:42:11

华为云

2016-09-26 08:09:53

人脸识别seetaface开源

2011-07-05 17:29:53

PhoneGapevents

2020-11-04 13:18:34

WebAPIWeb Share A

2021-09-10 10:15:24

Python人脸识别AI

2023-10-07 09:00:00

人脸检测Web应用程序
点赞
收藏

51CTO技术栈公众号