通过MLMaxSizeFaceTransactor.Creator创建“MLMaxSizeFaceTransactor”对象用于处理检测到的最大脸,其中objectCreateCallback()方法是在检测到对象的时候调用的,objectUpdateCallback()方法是在对象更新了的时候调用的,在方法里通过Overlay在识别到的最大人脸上标记了一个方块,并通过检测结果获取MLFaceEmotion来识别微笑表情触发拍照。
MLMaxSizeFaceTransactor transactor = new MLMaxSizeFaceTransactor.Creator(analyzer, new MLResultTrailer<MLFace>() { @Override public void objectCreateCallback(int itemId, MLFace obj) { LiveFaceAnalyseActivity.this.overlay.clear(); if (obj == null) { return; } LocalFaceGraphic faceGraphic = new LocalFaceGraphic(LiveFaceAnalyseActivity.this.overlay, obj, LiveFaceAnalyseActivity.this); LiveFaceAnalyseActivity.this.overlay.addGraphic(faceGraphic); MLFaceEmotion emotion = obj.getEmotions(); if (emotion.getSmilingProbability() > smilingPossibility) { safeToTakePicture = false; mHandler.sendEmptyMessage(TAKE_PHOTO); } } @Override public void objectUpdateCallback(MLAnalyzer.Result<MLFace> var1, MLFace obj) { LiveFaceAnalyseActivity.this.overlay.clear(); if (obj == null) { return; } LocalFaceGraphic faceGraphic = new LocalFaceGraphic(LiveFaceAnalyseActivity.this.overlay, obj, LiveFaceAnalyseActivity.this); LiveFaceAnalyseActivity.this.overlay.addGraphic(faceGraphic); MLFaceEmotion emotion = obj.getEmotions(); if (emotion.getSmilingProbability() > smilingPossibility && safeToTakePicture) { safeToTakePicture = false; mHandler.sendEmptyMessage(TAKE_PHOTO); } } @Override public void lostCallback(MLAnalyzer.Result<MLFace> result) { LiveFaceAnalyseActivity.this.overlay.clear(); } @Override public void completeCallback() { LiveFaceAnalyseActivity.this.overlay.clear(); } }).create(); this.analyzer.setTransactor(transactor); 通过LensEngine.Creator创建LensEngine实例进行视频流的人脸检测检测 this.mLensEngine = new LensEngine.Creator(context, this.analyzer).setLensType(this.lensType) .applyDisplayDimension(640, 480) .applyFps(25.0f) .enableAutomaticFocus(true) .create(); 启动相机预览进行人脸检测 this.mPreview.start(this.mLensEngine, this.overlay);往期链接:快服务常见TOP3审核雷区,再不过审就要崩溃啦!
内容来源:https://developer.huawei.com/consumer/cn/forum/topicview?tid=0201256372685820478&fid=18
原作者:littlewhite