Quick and easy FaceDetector demo

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Re: Quick and easy FaceDetector demo

Postby d43pan » Fri Aug 06, 2010 3:19 pm

Thanks so much! This was really helpful.

Just a note to people having problems getting the faces to be recognized. The FaceDetector documentation says the following:

http://developer.android.com/reference/android/media/FaceDetector.html
Note that the width of the image must be even.


I just did a quick and dirty hack to check if it's odd and then subtract a pixel if it is. It doesn't keep perspective, but this is a really rare case since by default the images from the phone's camera will be even.
Code: Select all
if(picWidth % 2 != 0){
                if(DEBUG){
                        Log.d("picHeight", " "+picHeight);
                       Log.d("picWidth", " "+picWidth);
                 }
                       
                        picWidth = picWidth-1;
                       sourceImage = Bitmap.createBitmap(sourceImage,0,0,picWidth,picHeight);
                 if(DEBUG){
                        Log.d("picHeight", " "+picHeight);
                       Log.d("picWidth", " "+picWidth);
                  }

                }


Any ideas on maintaining perspective?
d43pan
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Aug 06, 2010 3:14 pm

Top

Re: Quick and easy FaceDetector demo

Postby bumdeal2 » Sat Apr 09, 2011 11:10 pm

Great article,

just out of interest could you post the code that instead of hard coding the image, give a user the option of selecting an image from the gallery.

Thank you :-)
bumdeal2
Freshman
Freshman
 
Posts: 2
Joined: Sat Apr 09, 2011 11:05 pm

Re: Quick and easy FaceDetector demo

Postby Prade001 » Sun Apr 10, 2011 4:35 pm

Hiii..... new to android...was just trying to run this app & encountered the force close on my emulator ....do i have to make any changes in my manifest file...did everything else right......this is my logcat details

W/Zygote ( 30): Preloaded drawable resource #0x1080093 (res/drawable-mdpi/sym_def_app_icon.png) that varies with configuration!!

W/Zygote ( 30): Preloaded drawable resource #0x1080002 (res/drawable-mdpi/arrow_down_float.png) that varies with configuration!!

W/Zygote ( 30): Preloaded drawable resource #0x10800b4 (res/drawable/btn_check.xml) that varies with configuration!!

W/Zygote ( 30): Preloaded drawable resource #0x10800b7 (res/drawable-mdpi/btn_check_label_background.9.png) that varies with configuration!!

W/Zygote ( 30): Preloaded drawable resource #0x10800b8 (res/drawable-mdpi/btn_check_off.png) that varies with configuration!!

W/Zygote ( 30): Preloaded drawable resource #0x10800bd (res/drawable-mdpi/btn_check_on.png) that varies with configuration!!

W/Zygote ( 30): Preloaded drawable resource #0x1080004 (res/drawable/btn_default.xml) that varies with configuration!!

W/Zygote ( 30): Preloaded drawable resource #0x1080005 (res/drawable/btn_default_small.xml) that varies with configuration!!

W/Zygote ( 30): Preloaded drawable resource #0x1080006 (res/drawable/btn_dropdown.xml) that varies with configuration!!

W/Zygote ( 30): Preloaded drawable resource #0x1080008 (res/drawable/btn_plus.xml) that varies with configuration!!

W/Zygote ( 30): Preloaded drawable resource #0x1080007 (res/drawable/btn_minus.xml) that varies with configuration!!

W/Zygote ( 30): Preloaded drawable resource #0x1080009 (res/drawable/btn_radio.xml) that varies with configuration!!

W/Zygote ( 30): Preloaded drawable resource #0x108000a (res/drawable/btn_star.xml) that varies with configuration!!
D/MediaPlayer( 52): Couldn't open file on client side, trying server side

E/MediaPlayerService( 31): Couldn't open fd for content://settings/system/notification_sound

E/MediaPlayer( 52): Unable to to create media player
E/AndroidRuntime( 130): ERROR: thread attach failed

D/ActivityManager( 52): Uninstalling process com.pradeep.naik

D/dalvikvm( 130): LinearAlloc 0x0 used 621708 of 5242880 (11%)

W/ResourceType( 52): Resources don't contain package for resource number 0x7f0700e5

W/ResourceType( 52): Resources don't contain package for resource number 0x7f020031

W/ResourceType( 52): Resources don't contain package for resource number 0x7f020030

W/ResourceType( 52): Resources don't contain package for resource number 0x7f050000

W/ResourceType( 52): Resources don't contain package for resource number 0x7f060000

W/ResourceType( 52): Resources don't contain package for resource number 0x7f060001


Any1 tell me wats the error.....i did copy the image in drawable folder
Prade001
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Apr 10, 2011 4:18 pm

Re: Quick and easy FaceDetector demo

Postby b1l » Tue Apr 12, 2011 9:24 pm

hi there
thanks for the quick example
im having some problem dublicating the process. im getting a error message on line 42 of the FaceView.Java source file under the sample picture name i have put in res/drawable-hdpi. it is in Bitmap format. am i doing sonething wrong here?
thanks
b1l
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Apr 12, 2011 8:50 pm

One DOubt

Postby arunrajendran » Sun May 08, 2011 6:48 am

Hi,


When I ran this code, it says
" java.lang.InstantionException:com.example.facedetection" in the cat log. What should I have to change?Pls help
arunrajendran
Once Poster
Once Poster
 
Posts: 1
Joined: Sun May 08, 2011 6:45 am

Re: plz post project

Postby super » Fri Jun 10, 2011 12:51 am

Can anybody post a copy of complete project ? I am a newbie in android dev.
plz share the complete project which I can directly run on my computer.
thanks
super
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat May 21, 2011 12:56 pm

Top

Re: Quick and easy FaceDetector demo

Postby super » Sat Jun 25, 2011 6:16 pm

When I try to open the application I get error message saying "The application FaceTest (process com.jrr.facetest) has stopped unexpectedly. Please try again".

I have saved a bmp file of my picture in this location: res/drawable/bao102.bmp (I named the image as "bao102.bmp")

Here's my code:

FaceView.java

Code: Select all

package com.jrr.facetest;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.media.FaceDetector;
import android.util.Log;
import android.view.View;

public class FaceView extends View {
   private static final int NUM_FACES = 10; // max is 64
   private static final boolean DEBUG = true;

   private FaceDetector arrayFaces;
   private FaceDetector.Face getAllFaces[] = new FaceDetector.Face[NUM_FACES];
   private FaceDetector.Face getFace = null;
   
   private PointF eyesMidPts[] = new PointF[NUM_FACES];
   private float  eyesDistance[] = new float[NUM_FACES];
   
   private Bitmap sourceImage;
   
   private Paint tmpPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
   private Paint pOuterBullsEye = new Paint(Paint.ANTI_ALIAS_FLAG);
   private Paint pInnerBullsEye = new Paint(Paint.ANTI_ALIAS_FLAG);
   
   private int picWidth, picHeight;
   private float xRatio, yRatio;
   
   public FaceView(Context context) {
      super(context);
      
      pInnerBullsEye.setStyle(Paint.Style.FILL);
      pInnerBullsEye.setColor(Color.RED);
      
      pOuterBullsEye.setStyle(Paint.Style.STROKE);
      pOuterBullsEye.setColor(Color.RED);
      
      tmpPaint.setStyle(Paint.Style.STROKE);
      tmpPaint.setTextAlign(Paint.Align.CENTER);
      
      BitmapFactory.Options bfo = new BitmapFactory.Options();
      bfo.inPreferredConfig = Bitmap.Config.RGB_565;
      
      sourceImage = BitmapFactory.decodeResource( getResources() ,R.drawable.bao102, bfo);

      picWidth = sourceImage.getWidth();
      picHeight = sourceImage.getHeight();
      
      arrayFaces = new FaceDetector( picWidth, picHeight, NUM_FACES );
      arrayFaces.findFaces(sourceImage, getAllFaces);
      
      for (int i = 0; i < getAllFaces.length; i++)
      {
         getFace = getAllFaces[i];
         try {
            PointF eyesMP = new PointF();
            getFace.getMidPoint(eyesMP);
            eyesDistance[i] = getFace.eyesDistance();
            eyesMidPts[i] = eyesMP;
            
            if (DEBUG)
            {
               Log.i("Face",
                  i +  " " + getFace.confidence() + " " + getFace.eyesDistance() + " "
                  + "Pose: ("+ getFace.pose(FaceDetector.Face.EULER_X) + ","
                  + getFace.pose(FaceDetector.Face.EULER_Y) + ","
                  + getFace.pose(FaceDetector.Face.EULER_Z) + ")"
                  + "Eyes Midpoint: ("+eyesMidPts[i].x + "," + eyesMidPts[i].y +")"
               );
            }
         }
         catch (Exception e)
         {
            if (DEBUG) Log.e("Face", i + " is null");
         }
      
      }
   }
   
   @Override
   protected void onDraw(Canvas canvas)
   {
      xRatio = getWidth()*1.0f / picWidth;
      yRatio = getHeight()*1.0f / picHeight;
      canvas.drawBitmap( sourceImage, null , new Rect(0,0,getWidth(),getHeight()),tmpPaint);
      for (int i = 0; i < eyesMidPts.length; i++)
      {
         if (eyesMidPts[i] != null)
         {
            pOuterBullsEye.setStrokeWidth(eyesDistance[i] /6);
            canvas.drawCircle(eyesMidPts[i].x*xRatio, eyesMidPts[i].y*yRatio, eyesDistance[i] / 2 , pOuterBullsEye);
            canvas.drawCircle(eyesMidPts[i].x*xRatio, eyesMidPts[i].y*yRatio, eyesDistance[i] / 6 , pInnerBullsEye);
         }
      }
   }
}



FaceTest.java

Code: Select all

package com.jrr.facetest;

import android.app.Activity;
import android.os.Bundle;

public class FaceTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        FaceView faceView = new FaceView(this);
        setContentView(faceView);
    }
}



Need HELP...
super
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat May 21, 2011 12:56 pm

Re: Quick and easy FaceDetector demo

Postby super » Tue Jun 28, 2011 5:58 am

anyone ?
super
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat May 21, 2011 12:56 pm

Re: Quick and easy FaceDetector demo

Postby super » Tue Jul 12, 2011 6:54 pm

Is there anyone who can help with this ?
super
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat May 21, 2011 12:56 pm

Re: Quick and easy FaceDetector demo

Postby wxgeek55 » Tue Oct 18, 2011 3:36 pm

I am also having problems with the EULER_X, EULER_Y and EULER_Z always coming back as 0. Has anyone figured this out?
wxgeek55
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Oct 12, 2011 8:39 pm

Top
Previous

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 8 guests