5

I am trying to do face detection on android, and I am following the guide http://www.richardnichols.net/2011/01/java-facial-recognition-haar-cascade-with-jjil-guide/

but on android instead. When i do

Gray8DetectHaarMultiScale detectHaar = new Gray8DetectHaarMultiScale(is, minScale, maxScale);
RgbAvgGray toGray = new RgbAvgGray();
toGray.push(RgbImage);
detectHaar.pushAndReturn(toGray.getFront());

It seems that pushAndReturn is only returning one face from the image on Android although the exact code returns 2 faces using the netbeans code. The difference is only in the type of the image (RgbImage on android and RgbImageJ2se on netbeans)

I don't know what i m missing and why i can't detect more than one face on Android ?

I am using JJIL so i mean by RgbImage: jjil.core.RgbImage type, vs. RgbImageJ2SE type. The rest is just the same!! it seems that pushAndReturn is only returning one entry in the stack. This does not work on any image with more than one face.

Tofeeq Ahmad
  • 11,656
  • 4
  • 58
  • 85
Adroidist
  • 544
  • 3
  • 10
  • 21
  • It's not clear what RgbImage is, how you are determining that one face is returned and not 2, or how this Java code on Android is different from what you ran in Java on a PC. Can you give an example of full code that has a different result in Java on a PC from Java on Android, and give some more details? – Rob Lourens Feb 14 '12 at 00:04
  • for a full example of Android Face Detection see my answer here: http://stackoverflow.com/questions/4125821/face-detection-in-android – Jorgesys Nov 12 '14 at 22:47

1 Answers1

13

Go for this its working and detecting all faces from a given picture

    public class AndroidFaceDetector extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        setContentView(new myView(this));
    }

    private class myView extends View{

     private int imageWidth, imageHeight;
     private int numberOfFace = 5;
     private FaceDetector myFaceDetect; 
     private FaceDetector.Face[] myFace;
     float myEyesDistance;
     int numberOfFaceDetected;

     Bitmap myBitmap;


    public myView(Context context) {
   super(context);
   // TODO Auto-generated constructor stub

   BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
   BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; 
   myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.face5,   
      BitmapFactoryOptionsbfo);
   imageWidth = myBitmap.getWidth();
   imageHeight = myBitmap.getHeight();
   myFace = new FaceDetector.Face[numberOfFace];
   myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
   numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace); 

  }

  @Override
  protected void onDraw(Canvas canvas) {
   // TODO Auto-generated method stub

            canvas.drawBitmap(myBitmap, 0, 0, null);

            Paint myPaint = new Paint();
            myPaint.setColor(Color.GREEN);
            myPaint.setStyle(Paint.Style.STROKE); 
            myPaint.setStrokeWidth(3);

            for(int i=0; i < numberOfFaceDetected; i++)
            {
             Face face = myFace[i];
             PointF myMidPoint = new PointF();
             face.getMidPoint(myMidPoint);
    myEyesDistance = face.eyesDistance();
             canvas.drawRect(
               (int)(myMidPoint.x - myEyesDistance),
               (int)(myMidPoint.y - myEyesDistance),
               (int)(myMidPoint.x + myEyesDistance),
               (int)(myMidPoint.y + myEyesDistance),
               myPaint);
            }
  }
    }
}
Peter
  • 4,740
  • 22
  • 74
  • 114
Tofeeq Ahmad
  • 11,656
  • 4
  • 58
  • 85
  • hey Sameer, thank u it works but it seems to work only on small sized images, because when i use the camera of my samsung galaxy s it cannot detect any face, does it have to do with the code? – Adroidist Feb 14 '12 at 12:25
  • It will work for every size image.Try to play with it as i have not meet this scenario.And if you are satisfied with solution then please mark it as answer. – Tofeeq Ahmad Feb 14 '12 at 12:25
  • For FaceDetection API, it seems to detect only the middle of the eyes and distance between them, but is it possible to get to individual eyes? Or is it possible to know that the face is slanted? – Lim Thye Chean Jul 26 '14 at 11:29