你好,游客 登录 注册 搜索
背景:
阅读新闻

Android API教程:人脸检测(Face Detect)

[日期:2012-06-26] 来源:Developer  作者:Chunyen Liu [字体:大 中 小]

接下来的代码中,我们在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:定位眼睛中心位置

linux
相关资讯       Android API  Android API教程  Android高级教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款