Using Android's FaceDetector

原文链接:http://www.anddev.org/quick_and_easy_facedetector_demo-t3856.html

Tutorial: Simple implementation of the Android SDK's FaceDetector and FaceDetector.Face classes.


First a few things about the FaceDetector class.
  • According to the documentation, Bitmaps need to use the RGB565 format. However it seems to work with bitmaps using the default constructors for that class but I used mode RGB565 just in case.
  • Seems that the class uses eyes as the "facial detection" this means there is no other detail (face dimensions, mouth location, etc.) and faces that are taken from the side will not be detected. Sunglasses/glasses seem to interfere with detection.
  • The bigger the image the easier it can detect images. However there is a limit to how big of an image you can load into Android's memory. The images I used were all under 1000x1000 but at least 400x300.
  • Regardless of how many faces you specify in the FaceDetector constructor, it seems that it takes the same amount of time, i.e. if there are only 5 faces in the image you won't be penalized for asking for 10.
  • The FaceDetector.Face class cannot be extended, all its constructors are privatized, which means you cannot change the Face class's CONFIDENCE_THRESHOLD.
  • Maximum number of faces you can ask for is 64 after that it won't bother loading the face detection.
  • pose() in class FaceDetector.Face does not seem to do anything.
To make it as simple as possible, I only created 2 classes for this tutorial. You will also need to add an image into your res/drawable folder to use as a test image.

/src/package_name/FaceTest.java

  1. package   com.jrr.facetest ;
  2.  
  3. import   android.app.Activity ;
  4. import   android.os.Bundle ;
  5.  
  6. public   class  FaceTest  extends  Activity  {
  7.      /** Called when the activity is first created. */
  8.     @Override
  9.      public   void  onCreate ( Bundle savedInstanceState )   {
  10.          super . onCreate ( savedInstanceState ) ;
  11.         FaceView faceView  =   new  FaceView ( this ) ;
  12.         setContentView ( faceView ) ;
  13.      }
  14. }
  15.  

/src/package_name/FaceView.java
Note you must change R.drawable.bao102 with the actual resource image you use.

  1. package   com.jrr.facetest ;
  2.  
  3. import   android.content.Context ;
  4. import   android.graphics.Bitmap ;
  5. import   android.graphics.BitmapFactory ;
  6. import   android.graphics.Canvas ;
  7. import   android.graphics.Color ;
  8. import   android.graphics.Paint ;
  9. import   android.graphics.PointF ;
  10. import   android.graphics.Rect ;
  11. import   android.media.FaceDetector ;
  12. import   android.util.Log ;
  13. import   android.view.View ;
  14.  
  15. public   class  FaceView  extends   View   {
  16.          private   static   final   int  NUM_FACES  =   10 ;   // max is 64
  17.          private   static   final   boolean  DEBUG  =   true ;
  18.  
  19.          private  FaceDetector arrayFaces ;
  20.          private  FaceDetector. Face  getAllFaces [ ]   =   new  FaceDetector. Face [ NUM_FACES ] ;
  21.          private  FaceDetector. Face  getFace  =   null ;
  22.        
  23.          private  PointF eyesMidPts [ ]   =   new  PointF [ NUM_FACES ] ;
  24.          private   float   eyesDistance [ ]   =   new   float [ NUM_FACES ] ;
  25.        
  26.          private  Bitmap sourceImage ;
  27.        
  28.          private   Paint  tmpPaint  =   new   Paint ( Paint . ANTI_ALIAS_FLAG ) ;
  29.          private   Paint  pOuterBullsEye  =   new   Paint ( Paint . ANTI_ALIAS_FLAG ) ;
  30.          private   Paint  pInnerBullsEye  =   new   Paint ( Paint . ANTI_ALIAS_FLAG ) ;
  31.        
  32.          private   int  picWidth, picHeight ;
  33.          private   float  xRatio, yRatio ;
  34.        
  35.          public  FaceView ( Context  context )   {
  36.                  super ( context ) ;
  37.                
  38.                 pInnerBullsEye. setStyle ( Paint . Style . FILL ) ;
  39.                 pInnerBullsEye. setColor ( Color . RED ) ;
  40.                
  41.                 pOuterBullsEye. setStyle ( Paint . Style . STROKE ) ;
  42.                 pOuterBullsEye. setColor ( Color . RED ) ;
  43.                
  44.                 tmpPaint. setStyle ( Paint . Style . STROKE ) ;
  45.                 tmpPaint. setTextAlign ( Paint . Align . CENTER ) ;
  46.                
  47.                 BitmapFactory. Options  bfo  =   new  BitmapFactory. Options ( ) ;
  48.                 bfo. inPreferredConfig   =  Bitmap. Config . RGB_565 ;
  49.                
  50.                 sourceImage  =  BitmapFactory. decodeResource (  getResources ( )  ,R. drawable . bao102 , bfo ) ;
  51.  
  52.                 picWidth  =  sourceImage. getWidth ( ) ;
  53.                 picHeight  =  sourceImage. getHeight ( ) ;
  54.                
  55.                 arrayFaces  =   new  FaceDetector (  picWidth, picHeight, NUM_FACES  ) ;
  56.                 arrayFaces. findFaces ( sourceImage, getAllFaces ) ;
  57.                
  58.                  for   ( int  i  =   0 ;  i  <  getAllFaces. length ;  i ++ )
  59.                  {
  60.                         getFace  =  getAllFaces [ i ] ;
  61.                          try   {
  62.                                 PointF eyesMP  =   new  PointF ( ) ;
  63.                                 getFace. getMidPoint ( eyesMP ) ;
  64.                                 eyesDistance [ i ]   =  getFace. eyesDistance ( ) ;
  65.                                 eyesMidPts [ i ]   =  eyesMP ;
  66.                                
  67.                                  if   ( DEBUG )
  68.                                  {
  69.                                         Log. i ( "Face" ,
  70.                                                 i  +    " "   +  getFace. confidence ( )   +   " "   + getFace. eyesDistance ( )   +   " "
  71.                                                  +   "Pose: (" +  getFace. pose ( FaceDetector. Face . EULER_X ) +   ","
  72.                                                  +  getFace. pose ( FaceDetector. Face . EULER_Y )   +   ","
  73.                                                  +  getFace. pose ( FaceDetector. Face . EULER_Z )   +   ")"
  74.                                                  +   "Eyes Midpoint: (" + eyesMidPts [ i ] . x   +   ","   + eyesMidPts [ i ] . y   + ")"
  75.                                          ) ;
  76.                                  }
  77.                          }
  78.                          catch   ( Exception  e )
  79.                          {
  80.                                  if   ( DEBUG )  Log. e ( "Face" , i  +   " is null" ) ;
  81.                          }
  82.                
  83.                  }
  84.          }
  85.        
  86.         @Override
  87.          protected   void  onDraw ( Canvas  canvas )
  88.          {
  89.                 xRatio  =  getWidth ( ) * 1.0f  /  picWidth ;
  90.                 yRatio  =  getHeight ( ) * 1.0f  /  picHeight ;
  91.                 canvas. drawBitmap (  sourceImage,  null  ,  new Rect ( 0,0,getWidth ( ) ,getHeight ( ) ) ,tmpPaint ) ;
  92.                  for   ( int  i  =   0 ;  i  <  eyesMidPts. length ;  i ++ )
  93.                  {
  94.                          if   ( eyesMidPts [ i ]   !=   null )
  95.                          {
  96.                                 pOuterBullsEye. setStrokeWidth ( eyesDistance [ i ]   / 6 ) ;
  97.                                 canvas. drawCircle ( eyesMidPts [ i ] . x * xRatio, eyesMidPts [ i ] . y * yRatio, eyesDistance [ i ]   /  2 , pOuterBullsEye ) ;
  98.                                 canvas. drawCircle ( eyesMidPts [ i ] . x * xRatio, eyesMidPts [ i ] . y * yRatio, eyesDistance [ i ]   /  6 , pInnerBullsEye ) ;
  99.                          }
  100.                  }
  101.          }
  102. }
  103.  


你可能感兴趣的:(Using Android's FaceDetector)