Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize face Detection by Android API

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces Android API how to achieve face detection, the article is very detailed, has a certain reference value, interested friends must read it!

Photo Source: Wikipedia

The so-called face detection refers to calibrating the position and size of all faces from a picture or a video. Face detection is an important part of face recognition system, and it can also be applied to video surveillance independently. Today, with the increasing popularity of digital media, the use of face detection technology can also help us to quickly screen out pictures containing faces from massive image data. In current digital cameras, face detection can be used to complete autofocus, that is, "face focus". "face focus" is the most important photography innovation in the past two decades after the invention of autoexposure and autofocus. The vast majority of household digital cameras take people as the main body of the photos, which requires the automatic exposure and focus of the camera to be based on the characters.

Via cdstm.cn

Construct an Android Activity for face detection

You can build a general Android Activity, we extended the base class ImageView to become MyImageView, and we need to detect the bitmap file containing the face must be in 565 format for API to work properly. The detected face needs a confidence measure (confidence measure), which is defined in android.media.FaceDetector.Face.CONFIDENCE_THRESHOLD.

The most important method is implemented in setFace (), which instantiates the FaceDetector object and calls findFaces at the same time, the result is stored in faces, and the midpoint of the face is transferred to MyImageView. The code is as follows:

Public class TutorialOnFaceDetect1 extends Activity {private MyImageView mIV; private Bitmap mFaceBitmap; private int mFaceWidth = 200; private int mFaceHeight = 200; private static final int MAX_FACES = 1; private static String TAG = "TutorialOnFaceDetect"; @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); mIV = new MyImageView (this); setContentView (mIV, new LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)) / / load the photo Bitmap b = BitmapFactory.decodeResource (getResources (), R.drawable.face3); mFaceBitmap = b.copy (Bitmap.Config.RGB_565, true); b.recycle (); mFaceWidth = mFaceBitmap.getWidth (); mFaceHeight = mFaceBitmap.getHeight (); mIV.setImageBitmap (mFaceBitmap); / / perform face detection and set the feature points setFace (); mIV.invalidate ();} public void setFace () {FaceDetector fd FaceDetector.Face [] faces = new Facebook [Max _ FACES]; PointF midpoint = new PointF (); int [] fpx = null; int [] fpy = null; int count = 0; try {fd = new FaceDetector (mFaceWidth, mFaceHeight, MAX_FACES); count = fd.findFaces (mFaceBitmap, faces);} catch (Exception e) {Log.e (TAG, "setFace ():" + e.toString ()); return } / / check if we detect any faces if (count > 0) {fpx = new int [count]; fpy = new int [count]; for (int I = 0; I

< count; i++) { try { faces[i].getMidPoint(midpoint); fpx[i] = (int)midpoint.x; fpy[i] = (int)midpoint.y; } catch (Exception e) { Log.e(TAG, "setFace(): face " + i + ": " + e.toString()); } } } mIV.setDisplayPoints(fpx, fpy, count, 0); } } 接下来的代码中,我们在MyImageView中添加setDisplayPoints() ,用来在被检测出的人脸上标记渲染。图1展示了一个标记在被检测处的人脸上处于中心位置。 // set up detected face features for display public void setDisplayPoints(int [] xx, int [] yy, int total, int style) { mDisplayStyle = style; mPX = null; mPY = null; if (xx != null && yy != null && total >

0) {mPX = new int [total]; mPY = new int [total]; for (int I = 0; I

< total; i++) { mPX[i] = xx[i]; mPY[i] = yy[i]; } } }

Figure 1: single face detection

Multi-face detection

The upper limit of the number of faces detected can be set through FaceDetector. For example, set a maximum of 10 faces to be detected:

Private static final int MAX_FACES = 10

Figure 2 shows the detection of multiple faces.

Figure 2: multi-face detection

Locate the center of the eye

Android face detection returns other useful information, such as eyesDistance,pose and confidence. We can use eyesDistance to locate the center of the eye.

In the following code, we put setFace () in doLengthyCalc (). At the same time, figure 3 shows the effect of locating the center of the eye.

Public class TutorialOnFaceDetect extends Activity {private MyImageView mIV; private Bitmap mFaceBitmap; private int mFaceWidth = 200; private int mFaceHeight = 200; private static final int MAX_FACES = 10; private static String TAG = "TutorialOnFaceDetect"; private static boolean DEBUG = false; protected static final int GUIUPDATE_SETFACE = 999; protected Handler mHandler = new Handler () {/ / @ Override public void handleMessage (Message msg) {mIV.invalidate (); super.handleMessage (msg);}} Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); mIV = new MyImageView (this); setContentView (mIV, new LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); / / load the photo Bitmap b = BitmapFactory.decodeResource (getResources (), R.drawable.face3); mFaceBitmap = b.copy (Bitmap.Config.RGB_565, true); b.recycle (); mFaceWidth = mFaceBitmap.getWidth (); mFaceHeight = mFaceBitmap.getHeight () MIV.setImageBitmap (mFaceBitmap); mIV.invalidate (); / / perform face detection in setFace () in a background thread doLengthyCalc ();} public void setFace () {FaceDetector fd; FaceDetector.Face [] faces = new FaceDetector.Facebook [Max _ FACES]; PointF eyescenter = new PointF (); float eyesdist = 0.0f; int [] fpx = null; int [] fpy = null; int count = 0; try {fd = new FaceDetector (mFaceWidth, mFaceHeight, MAX_FACES) Count = fd.findFaces (mFaceBitmap, faces);} catch (Exception e) {Log.e (TAG, "setFace ():" + e.toString ()); return;} / / check if we detect any faces if (count > 0) {fpx = new int [count * 2]; fpy = new int [count * 2]; for (int I = 0; I < count; iPoint +) {try {faces [I] .getMidPoint (eyescenter) Eyesdist = faces.eyesDistance (); / / set up left eye location fpx [2 * I] = (int) (eyescenter.x-eyesdist / 2); fpy [2 * I] = (int) eyescenter.y; / / set up right eye location fpx [2 * I + 1] = (int) (eyescenter.x + eyesdist / 2); fpy [2 * I + 1] = (int) eyescenter.y If (DEBUG) {Log.e (TAG, "setFace (): face" + I + ": confidence =" + faces [I]. Faces () + ", eyes distance =" + faces [I] .eyesDistance () + ", pose = (" + faces [I] .pose (FaceDetector.Face.EULER_X) + "," + faces [I] .pose (FaceDetector.Face.EULER_Y) + "," + faces [I] .pose (FaceDetector.Face.EULER_Z) + ")" + " Eyes midpoint = ("+ eyescenter.x +", "+ eyescenter.y +") } catch (Exception e) {Log.e (TAG, "setFace (): face" + I + ":" + e.toString ());} mIV.setDisplayPoints (fpx, fpy, count * 2,1);} private void doLengthyCalc () {Thread t = new Thread () {Message m = new Message (); public void run () {try {setFace (); m.what = TutorialOnFaceDetect.GUIUPDATE_SETFACE TutorialOnFaceDetect.this.mHandler.sendMessage (m);} catch (Exception e) {Log.e (TAG, "doLengthyCalc ():" + e.toString ());}; t.start ();}} these are all the contents of the article "how Android API implements face detection". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report