Android API如何实现人脸检测(android,api,移动开发)

时间:2024-04-29 02:56:35 作者 : 石家庄SEO 分类 : 移动开发
  • TAG :

图片来源:Wikipedia

所谓人脸检测就是指从一副图片或者一帧视频中标定出所有人脸的位置和尺寸。人脸检测是人脸识别系统中的一个重要环节,也可以独立应用于视频监控。在数字媒体日益普及的今天,利用人脸检测技术还可以帮助我们从海量图片数据中快速筛选出包含人脸的图片。 在目前的数码相机中,人脸检测可以用来完成自动对焦,即“脸部对焦”。“脸部对焦”是在自动曝光和自动对焦发明后,二十年来最重要的一次摄影技术革新。家用数码相机,占绝大多数的照片是以人为拍摄主体的,这就要求相机的自动曝光和对焦以人物为基准。

via cdstm.cn

构建一个人脸检测的Android Activity

你可以构建一个通用的Android Activity,我们扩展了基类ImageView,成为MyImageView,而我们需要进行检测的包含人脸的位图文件必须是565格式,API才能正常工作。被检测出来的人脸需要一个置信测度(confidence measure),这个措施定义在android.media.FaceDetector.Face.CONFIDENCE_THRESHOLD。

最重要的方法实现在setFace(),它将FaceDetector对象实例化,同时调用findFaces,结果存放在faces里,人脸的中点转移到MyImageView。代码如下:

publicclassTutorialOnFaceDetect1extendsActivity{privateMyImageViewmIV;privateBitmapmFaceBitmap;privateintmFaceWidth=200;privateintmFaceHeight=200;privatestaticfinalintMAX_FACES=1;privatestaticStringTAG="TutorialOnFaceDetect";@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);mIV=newMyImageView(this);setContentView(mIV,newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));//loadthephotoBitmapb=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);//performfacedetectionandsetthefeaturepointssetFace();mIV.invalidate();}publicvoidsetFace(){FaceDetectorfd;FaceDetector.Face[]faces=newFaceDetector.Face[MAX_FACES];PointFmidpoint=newPointF();int[]fpx=null;int[]fpy=null;intcount=0;try{fd=newFaceDetector(mFaceWidth,mFaceHeight,MAX_FACES);count=fd.findFaces(mFaceBitmap,faces);}catch(Exceptione){Log.e(TAG,"setFace():"+e.toString());return;}//checkifwedetectanyfacesif(count>0){fpx=newint[count];fpy=newint[count];for(inti=0;i<count;i++){try{faces[i].getMidPoint(midpoint);fpx[i]=(int)midpoint.x;fpy[i]=(int)midpoint.y;}catch(Exceptione){Log.e(TAG,"setFace():face"+i+":"+e.toString());}}}mIV.setDisplayPoints(fpx,fpy,count,0);}}

接下来的代码中,我们在MyImageView中添加setDisplayPoints() ,用来在被检测出的人脸上标记渲染。图1展示了一个标记在被检测处的人脸上处于中心位置。

//setupdetectedfacefeaturesfordisplaypublicvoidsetDisplayPoints(int[]xx,int[]yy,inttotal,intstyle){mDisplayStyle=style;mPX=null;mPY=null;if(xx!=null&&yy!=null&&total>0){mPX=newint[total];mPY=newint[total];for(inti=0;i<total;i++){mPX[i]=xx[i];mPY[i]=yy[i];}}}
Android API如何实现人脸检测

图1:单一人脸检测

多人脸检测

通过FaceDetector可以设定检测到人脸数目的上限。比如设置最多只检测10张脸:

privatestaticfinalintMAX_FACES=10;

图2展示检测到多张人脸的情况。

Android API如何实现人脸检测

图2:多人人脸检测

定位眼睛中心位置

Android人脸检测返回其他有用的信息,例同时会返回如eyesDistance,pose,以及confidence。我们可以通过eyesDistance来定位眼睛的中心位置。

下面的代码中,我们将setFace()放在doLengthyCalc()中。同时图3展示了定位眼睛中心位置的效果。

publicclassTutorialOnFaceDetectextendsActivity{privateMyImageViewmIV;privateBitmapmFaceBitmap;privateintmFaceWidth=200;privateintmFaceHeight=200;privatestaticfinalintMAX_FACES=10;privatestaticStringTAG="TutorialOnFaceDetect";privatestaticbooleanDEBUG=false;protectedstaticfinalintGUIUPDATE_SETFACE=999;protectedHandlermHandler=newHandler(){//@OverridepublicvoidhandleMessage(Messagemsg){mIV.invalidate();super.handleMessage(msg);}};@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);mIV=newMyImageView(this);setContentView(mIV,newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));//loadthephotoBitmapb=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();//performfacedetectioninsetFace()inabackgroundthreaddoLengthyCalc();}publicvoidsetFace(){FaceDetectorfd;FaceDetector.Face[]faces=newFaceDetector.Face[MAX_FACES];PointFeyescenter=newPointF();floateyesdist=0.0f;int[]fpx=null;int[]fpy=null;intcount=0;try{fd=newFaceDetector(mFaceWidth,mFaceHeight,MAX_FACES);count=fd.findFaces(mFaceBitmap,faces);}catch(Exceptione){Log.e(TAG,"setFace():"+e.toString());return;}//checkifwedetectanyfacesif(count>0){fpx=newint[count*2];fpy=newint[count*2];for(inti=0;i<count;i++){try{faces[i].getMidPoint(eyescenter);eyesdist=faces[i].eyesDistance();//setuplefteyelocationfpx[2*i]=(int)(eyescenter.x-eyesdist/2);fpy[2*i]=(int)eyescenter.y;//setuprighteyelocationfpx[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].confidence()+",eyesdistance="+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)+")"+",eyesmidpoint=("+eyescenter.x+","+eyescenter.y+")");}}catch(Exceptione){Log.e(TAG,"setFace():face"+i+":"+e.toString());}}}mIV.setDisplayPoints(fpx,fpy,count*2,1);}privatevoiddoLengthyCalc(){Threadt=newThread(){Messagem=newMessage();publicvoidrun(){try{setFace();m.what=TutorialOnFaceDetect.GUIUPDATE_SETFACE;TutorialOnFaceDetect.this.mHandler.sendMessage(m);}catch(Exceptione){Log.e(TAG,"doLengthyCalc():"+e.toString());}}};t.start();}}
Android API如何实现人脸检测
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Android API如何实现人脸检测的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:如何进行微信JS-SDK中已开放接口的分析下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18