android人脸识别技术浅析
生活随笔
收集整理的這篇文章主要介紹了
android人脸识别技术浅析
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
今天小小的研究了一下android自帶的人臉識別技術(shù)的API。這里做一下總結(jié)。
? ? ? ? 首先明白android提供的這套接口功能是比較弱的,它是通過檢測兩個眼睛的存在來判斷是否存在人臉,所以對于側(cè)身的,戴眼鏡的人臉識別效果不佳。在使用上面主要是兩個類,一個是FaceDetector,這個類是最主要的,它里面有完成檢測識別的具體方法。另外一個類就是FaceDetector.Face。這是FaceDetect的內(nèi)部類,主要用來存放最后檢測到的人臉的坐標信息。
? ? ? ? 使用步驟:
? ? ? ? 第一步、把要檢測的bitmap轉(zhuǎn)換成RGB565格式的。
? ? ? ? ? ? ? 這個是必須的,這套API只能對RGB565格式的做識別。轉(zhuǎn)換代碼如下
? ? ? ? ? ? ? BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
? ? ? ? ? ? ? BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
? ? ? ? ? ? ? myBitmap = BitmapFactory.decodeResource(你要轉(zhuǎn)換的bitmap, BitmapFactoryOptionsbfo);
? ? ? ? 第二步、生成一個新的FaceDetector對象,需要傳入待檢測位圖的寬和高,以及最多檢測幾張人臉。
? ? ? ? ? ? ? ? myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace); //imageWidth是待檢測位圖的寬,imageHeight是待檢測位圖的高,n ? ? ? ? ? ? ? ?umberOfFace是最多檢測幾張人臉。
? ? ? ? 第三步、開始檢測
? ? ? ? ? ? ? ? 當然在檢測前必須創(chuàng)建FaceDetect.Face對象。通常會創(chuàng)建一個數(shù)組。
? ? ? ? ? ? ? ? myfaces = new FaceDetect.Face【num】;
? ? ? ? ? ? ? ? int num = myFaceDetect .findFace(bitmap , myfaces);返回值是實際檢測到的人臉數(shù)目。
? ? ? ?第四步、繪制臉部方框。
? ? ? ? ? ? ? ? ?這一步,主要是看你識別出來之后做什么用了。識別出來的人臉信息存放在myfaces數(shù)組中,包含著每張人臉的眼睛中點坐標以及眼睛距離。如果要把識別出來的信息 ? ? ? ? ? ? ? ? 在視圖上繪制出來,那么就可以繪制方框圖。使用myfaces元素的face.getMidPoint()方法可以獲得中點坐標;eyesDistance()方法可以獲得人眼之間的距離。
? ? ? ?下面是一個簡單的源碼。
? ? ? package star.app.facedetect;
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.view.View;
public class ViewMain extends View{
private int imageWidth, imageHeight;
? ? ? ? private int numberOfFace = 5;
? ? ? ? private FaceDetector myFaceDetect;
? ? ? ? private FaceDetector.Face[] myFace;
? ? ? ? float myEyesDistance;
? ? ? ? int numberOfFaceDetected;
? ? ? ? Bitmap myBitmap;
??
? ? ? ? public ViewMain(Context context) { ?
? ? ? ? ? ? super(context);
? ? ? ? ? ? BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
? ? ? ? ? ? BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
? ? ? ? ? ? myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test, BitmapFactoryOptionsbfo);
? ? ? ? ? ? imageWidth = myBitmap.getWidth();
? ? ? ? ? ? imageHeight = myBitmap.getHeight();
? ? ? ? ? ? myFace = new FaceDetector.Face[numberOfFace];
? ? ? ? ? ? myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
? ? ? ? ? ? numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace);
? ? ? ? }
?
? ? ? ? protected void onDraw(Canvas canvas) { ? ?
? ? ? ? ? ? // TODO Auto-generated method stub ?
?
? ? ? ? ? ? canvas.drawBitmap(myBitmap, 0, 0, null);
?
? ? ? ? ? ? Paint myPaint = new Paint();
? ? ? ? ? ? myPaint.setColor(Color.RED);
? ? ? ? ? ? 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+30)),
? ? ? ? ? ? ? ? ? ? ? ? myPaint);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? ? ? 首先明白android提供的這套接口功能是比較弱的,它是通過檢測兩個眼睛的存在來判斷是否存在人臉,所以對于側(cè)身的,戴眼鏡的人臉識別效果不佳。在使用上面主要是兩個類,一個是FaceDetector,這個類是最主要的,它里面有完成檢測識別的具體方法。另外一個類就是FaceDetector.Face。這是FaceDetect的內(nèi)部類,主要用來存放最后檢測到的人臉的坐標信息。
? ? ? ? 使用步驟:
? ? ? ? 第一步、把要檢測的bitmap轉(zhuǎn)換成RGB565格式的。
? ? ? ? ? ? ? 這個是必須的,這套API只能對RGB565格式的做識別。轉(zhuǎn)換代碼如下
? ? ? ? ? ? ? BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
? ? ? ? ? ? ? BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
? ? ? ? ? ? ? myBitmap = BitmapFactory.decodeResource(你要轉(zhuǎn)換的bitmap, BitmapFactoryOptionsbfo);
? ? ? ? 第二步、生成一個新的FaceDetector對象,需要傳入待檢測位圖的寬和高,以及最多檢測幾張人臉。
? ? ? ? ? ? ? ? myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace); //imageWidth是待檢測位圖的寬,imageHeight是待檢測位圖的高,n ? ? ? ? ? ? ? ?umberOfFace是最多檢測幾張人臉。
? ? ? ? 第三步、開始檢測
? ? ? ? ? ? ? ? 當然在檢測前必須創(chuàng)建FaceDetect.Face對象。通常會創(chuàng)建一個數(shù)組。
? ? ? ? ? ? ? ? myfaces = new FaceDetect.Face【num】;
? ? ? ? ? ? ? ? int num = myFaceDetect .findFace(bitmap , myfaces);返回值是實際檢測到的人臉數(shù)目。
? ? ? ?第四步、繪制臉部方框。
? ? ? ? ? ? ? ? ?這一步,主要是看你識別出來之后做什么用了。識別出來的人臉信息存放在myfaces數(shù)組中,包含著每張人臉的眼睛中點坐標以及眼睛距離。如果要把識別出來的信息 ? ? ? ? ? ? ? ? 在視圖上繪制出來,那么就可以繪制方框圖。使用myfaces元素的face.getMidPoint()方法可以獲得中點坐標;eyesDistance()方法可以獲得人眼之間的距離。
? ? ? ?下面是一個簡單的源碼。
? ? ? package star.app.facedetect;
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.view.View;
public class ViewMain extends View{
private int imageWidth, imageHeight;
? ? ? ? private int numberOfFace = 5;
? ? ? ? private FaceDetector myFaceDetect;
? ? ? ? private FaceDetector.Face[] myFace;
? ? ? ? float myEyesDistance;
? ? ? ? int numberOfFaceDetected;
? ? ? ? Bitmap myBitmap;
??
? ? ? ? public ViewMain(Context context) { ?
? ? ? ? ? ? super(context);
? ? ? ? ? ? BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
? ? ? ? ? ? BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
? ? ? ? ? ? myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test, BitmapFactoryOptionsbfo);
? ? ? ? ? ? imageWidth = myBitmap.getWidth();
? ? ? ? ? ? imageHeight = myBitmap.getHeight();
? ? ? ? ? ? myFace = new FaceDetector.Face[numberOfFace];
? ? ? ? ? ? myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
? ? ? ? ? ? numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace);
? ? ? ? }
?
? ? ? ? protected void onDraw(Canvas canvas) { ? ?
? ? ? ? ? ? // TODO Auto-generated method stub ?
?
? ? ? ? ? ? canvas.drawBitmap(myBitmap, 0, 0, null);
?
? ? ? ? ? ? Paint myPaint = new Paint();
? ? ? ? ? ? myPaint.setColor(Color.RED);
? ? ? ? ? ? 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+30)),
? ? ? ? ? ? ? ? ? ? ? ? myPaint);
? ? ? ? ? ? }
? ? ? ? }
? ? }
總結(jié)
以上是生活随笔為你收集整理的android人脸识别技术浅析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pipeline是什么,如何获取??
- 下一篇: DIY人脸跟踪电风扇送女朋友(1)