Simple Example of FaceDetector in Android, android facial recognition
The Android SDK (API level 1) has been integrated with simple facial recognition functions in Version 1.0, by calling FaceDetector, we can implement Bitmap multi-face recognition on the Android platform (a picture can contain multiple faces ). The principle of searching for a face is:Eye searching. It returns the face data. By calling public float eyesDistance () and public void getMidPoint (PointF point), we can get the detected distance between two eyes and the center point of the two eyes (MidPoint ). Public float confidence () can return the credibility of the face data (0 ~ 1) The larger the value, the higher the accuracy of the face data.
Process
1. Read an image to Bitmap (from Resource or from mobile album)
2. Use the FaceDetector API to analyze Bitmap and store the detected Face data in a Face list;
3. display the face box on the image.
Source code
Activity_main.xml
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" android: padding = "10dp"> <Button android: id = "@ + id/btn_select" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "select image"/> <Button android: id = "@ + id/btn_detect" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_marginTop = "5dp" android: text = ""/> <ImageView android: id = "@ + id/image" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_marginTop = "5dp"/> </LinearLayout>
MainActivity. java
Package com. example. faceandroidtest; import android. app. activity; import android. app. progressDialog; import android. content. intent; import android. database. cursor; 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. me Dia. faceDetector. face; import android.net. uri; import android. OS. asyncTask; import android. OS. bundle; import android. provider. mediaStore; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {private static final int REQUEST_CODE_SE LECT_PIC = 120; private static final int MAX_FACE_NUM = 10; // The maximum number of faces that can be detected is private int realFaceNum = 0; // the actual number of faces detected is private Button selectBtn; private Button detectBtn; private ImageView image; private ProgressDialog pd; private Bitmap bm; // The Bitmap object private Paint paint of the selected image; // Paint private boolean hasDetected = false used in the face area; // indicates whether the face is detected @ Override protected void onCreate (Bundle savedInstanceState ){ Super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView (); paint = new Paint (); paint. setColor (Color. BLUE); paint. setStrokeWidth (2); paint. setStyle (Paint. style. STROKE); // you can specify a hollow box instead of a solid box. pd = new ProgressDialog (this); pd. setTitle ("prompt"); pd. setMessage ("detecting, please wait");}/*** control initialization */private void initView () {selectBtn = (Button) findViewById (R. id. btn_select); selectBtn. s EtOnClickListener (this); detectBtn = (Button) findViewById (R. id. btn_detect); detectBtn. setOnClickListener (this); image = (ImageView) findViewById (R. id. image);}/*** select image from image Library */private void selectPicture () {Intent intent = new Intent (); intent. setAction (Intent. ACTION_GET_CONTENT); intent. setType ("image/*"); startActivityForResult (intent, REQUEST_CODE_SELECT_PIC);} @ Override protected void onActi VityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); if (requestCode = REQUEST_CODE_SELECT_PIC & resultCode = Activity. RESULT_ OK) {// obtain the selected image Uri selectedImage = data. getData (); String [] filePathColumn = {MediaStore. images. media. DATA}; Cursor cursor = getContentResolver (). query (selectedImage, filePathColumn, null, null); cursor. MoveToFirst (); int columnIndex = cursor. getColumnIndex (filePathColumn [0]); String selectedImagePath = cursor. getString (columnIndex); bm = BitmapFactory. decodeFile (selectedImagePath); // to use the Android built-in facial recognition, you must convert the Bitmap object to the RGB_565 format; otherwise, the bm = bm cannot be recognized. copy (Bitmap. config. RGB_565, true); cursor. close (); image. setImageBitmap (bm); hasDetected = false; }}// private void selectPicture () {// bm = BitmapFactory. deco DeResource (getResources (), R. drawable. test1); // bm = bm. copy (Bitmap. config. RGB_565, true); // image. setImageBitmap (bm); // hasDetected = false; //}/*** detect a face */private void detectFace () {if (bm = null) {Toast. makeText (this, "select an image first", Toast. LENGTH_SHORT ). show (); return;} if (hasDetected) {Toast. makeText (this, "detected faces", Toast. LENGTH_SHORT ). show () ;}else {new findfacetask(cmd.exe cute () ;}} private vo Id drawFacesArea (FaceDetector. face [] faces) {Toast. makeText (this, "detected in the image" + realFaceNum + "face", Toast. LENGTH_SHORT ). show (); float eyesDistance = 0f; // Canvas canvas Canvas = new Canvas (bm); for (int I = 0; I <faces. length; I ++) {FaceDetector. face face = faces [I]; if (face! = Null) {PointF pointF = new PointF (); face. getMidPoint (pointF); // obtain the facial center point eyesDistance = face. eyesDistance (); // obtain the distance between two eyes of a face. // draw the canvas of the area of the face. drawRect (pointF. x-eyesDistance, pointF. y-eyesDistance, pointF. x + eyesDistance, pointF. y + eyesDistance, paint); hasDetected = true ;}}// refresh the ImageView image after drawing the face area. invalidate ();}/*** it takes some time to detect the face in the image. Therefore, it takes some time to execute the * @ author yubo **/private class FindFaceTask extends AsyncTask <Void, Void, faceDetector. face []> {@ Override protected void onPreExecute () {super. onPreExecute (); pd. show () ;}@ Override protected Face [] doInBackground (Void... arg0) {// The most critical code is the following three statements: FaceDetector faceDetector = new FaceDetector (bm. getWidth (), bm. getHeight (), MAX_FACE_NUM); FaceDetector. face [] faces = new FaceDetector. face [MAX_FACE_NUM]; realFaceNum = faceDetector. findFaces (bm, faces); if (realFaceNum> 0) {return faces;} return null;} @ Override protected void onPostExecute (Face [] result) {super. onPostExecute (result); pd. dismiss (); if (result = null) {Toast. makeText (MainActivity. this, "Sorry, no face is detected in the image", Toast. LENGTH_SHORT ). show () ;}else {drawFacesArea (result) ;}} public void onClick (View arg0) {switch (arg0.getId () {case R. id. btn_select: // select the image selectPicture (); break; case R. id. btn_detect: // detect the face detectFace (); break ;}}}
In the source code, set the maximum number of faces that can be detected by MAX_FACE_NUM to 5.
Private static final int MAX_FACE_NUM = 5; // The maximum number of faces that can be detected
Detection Result
The guess should be to select the five portraits with the highest recognition rate in the group. Because the maximum number of faces that can be detected is 5, only the first five portraits with the highest recognition rate are displayed.
Set the maximum number of faces that can be detected by MAX_FACE_NUM to 10.
Private static final int MAX_FACE_NUM = 10; // The maximum number of faces that can be detected
Same image detection result
Reference
- Android API Face Detect)
- Use Face Detector in AndroidSDK to implement Face Recognition
- Face ++ Official Website