2011年11月24日木曜日

顔認識テスト


package com.test;

import android.app.Activity;
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.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class Test01Activity 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.a0002_006064, BitmapFactoryOptionsbfo);
   
   myBitmap = BitmapFactory.decodeFile("/mnt/sdcard/Pictures/09kaiga-bu.jpg/" , BitmapFactoryOptionsbfo);
   //myBitmap     = myBitmap.copy(Bitmap.Config.RGB_565, true);
   
   
   imageWidth = myBitmap.getWidth();
   imageHeight = myBitmap.getHeight();
   myFace = new FaceDetector.Face[numberOfFace];
   myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
   numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace);
   
   Log.d("顔の個数", String.valueOf( numberOfFaceDetected ) );
   
   Face face = myFace[0]; 
   PointF point = new PointF();
   face.getMidPoint(point);//両目の中心座標
   myEyesDistance = face.eyesDistance();//両目間の距離
    
   Log.d("信頼度", String.valueOf( myFace[0].confidence() ) );
   Log.d("両目の距離", String.valueOf( myEyesDistance) );//49.265625
   
   Log.d("x", String.valueOf( point.x) );//211.83594
   Log.d("y", String.valueOf( point.y) );//108.375

  }

  @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),//162.570315
               (int)(myMidPoint.y - myEyesDistance),//59.109375
               (int)(myMidPoint.x + myEyesDistance),//261.101565
               (int)(myMidPoint.y + myEyesDistance),//157.640625
               myPaint);
            }
  }
    }
}

関連記事

0 件のコメント:

コメントを投稿