FD:
1、
AndroidManifest:
<!--todo step 1: add authorization of camera --> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.CAMERA"/>
2、
private void createFaceAnalyzer() { // Create a face analyzer. You can create an analyzer using the provided customized face detection parameter // MLFaceAnalyzerSetting // todo step 2: add on-device face analyzer MLFaceAnalyzerSetting setting = new MLFaceAnalyzerSetting.Factory() .setFeatureType(MLFaceAnalyzerSetting.TYPE_FEATURES) .setPerformanceType(MLFaceAnalyzerSetting.TYPE_SPEED) .allowTracing() .create(); analyzer = MLAnalyzerFactory.getInstance().getFaceAnalyzer(setting);
3、
private void createLensEngine() { // todo step 3: add on-device LensEngine Context context = this.getApplicationContext(); // Create LensEngine this.mLensEngine = new LensEngine.Creator(context, this.analyzer) .setLensType(this.lensType) .applyDisplayDimension(640, 480) .applyFps(25.0f) .enableAutomaticFocus(true) .create(); }
4、
public void objectUpdateCallback(MLAnalyzer.Result<MLFace> var1, MLFace obj) { LiveFaceAnalyseActivity.this.overlay.clear(); if (obj == null) { return; } // todo step 4: add on-device face graphic 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); } }
5、
private void takePhoto() { // todo step 5: add on-device face takePhoto this.mLensEngine.photograph(null, new LensEngine.PhotographListener() { @Override public void takenPhotograph(byte[] bytes) { mHandler.sendEmptyMessage(STOP_PREVIEW); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); saveBitmapToDisk(bitmap); } }); }
IS:
1、
<!--todo step 1: add authorization of READ_EXTERNAL_STORAGE ,WRITE_EXTERNAL_STORAGE --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2、3、4、5
private void createImageTransactor() { // todo step 2: add on-device ImageSegmentation analyzer MLImageSegmentationSetting setting = new MLImageSegmentationSetting.Factory().setAnalyzerType(MLImageSegmentationSetting.BODY_SEG).create(); this.analyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(setting); if (this.isChosen(this.originBitmap)) { // todo step 3: add on-device ImageSegmentation mlFrame MLFrame mlFrame = new MLFrame.Creator().setBitmap(this.originBitmap).create(); // todo step 4: add on-device ImageSegmentation task Task<MLImageSegmentation> task = this.analyzer.asyncAnalyseFrame(mlFrame); task.addOnSuccessListener(new OnSuccessListener<MLImageSegmentation>() { @Override public void onSuccess(MLImageSegmentation mlImageSegmentationResults) { // Transacting logic for segment success. if (mlImageSegmentationResults != null) { // todo step 5: add on-device ImageSegmentation result. StillCutPhotoActivity.this.foreground = mlImageSegmentationResults.getForeground(); StillCutPhotoActivity.this.preview.setImageBitmap(StillCutPhotoActivity.this.foreground); StillCutPhotoActivity.this.processedImage = ((BitmapDrawable) ((ImageView) StillCutPhotoActivity.this.preview).getDrawable()).getBitmap(); } else { StillCutPhotoActivity.this.displayFailure(); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { // Transacting logic for segment failure. StillCutPhotoActivity.this.displayFailure(); return; } }); } else { Toast.makeText(this.getApplicationContext(), R.string.please_select_picture, Toast.LENGTH_SHORT).show(); return; } }
INIT:
1、
<!--todo step 1: add authorization of camera --> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.CAMERA"/>
2、3
private MLFaceAnalyzer createFaceAnalyzer() { // todo step 2: add on-device face analyzer MLFaceAnalyzerSetting setting = new MLFaceAnalyzerSetting.Factory() .setFeatureType(MLFaceAnalyzerSetting.TYPE_FEATURES) .setPerformanceType(MLFaceAnalyzerSetting.TYPE_SPEED) .allowTracing() .create(); this.analyzer = MLAnalyzerFactory.getInstance().getFaceAnalyzer(setting); // finish this.analyzer.setTransactor(new FaceAnalyzerTransactor(this.mOverlay)); return this.analyzer; } private void createLensEngine() { Context context = this.getApplicationContext(); // todo step 3: add on-device lens engine this.mLensEngine = new LensEngine.Creator(context, this.analyzer) .setLensType(this.lensType) .applyDisplayDimension(1600, 1024) .applyFps(25.0f) .enableAutomaticFocus(true) .create(); // finish }
4、
public void transactResult(MLAnalyzer.Result<MLFace> result) { this.mGraphicOverlay.clear(); SparseArray<MLFace> faceSparseArray = result.getAnalyseList(); for (int i = 0; i < faceSparseArray.size(); i++) { // todo step 4: add on-device face graphic MLFaceGraphic graphic = new MLFaceGraphic(this.mGraphicOverlay, faceSparseArray.valueAt(i)); this.mGraphicOverlay.add(graphic); // finish } }
SD:
1、
<!--todo step 1: add authorization of camera --> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.CAMERA"/>
2、3、4
private void createSkeletonAnalyzer() { // todo step 2: add on-device skeleton analyzer this.analyzer = MLSkeletonAnalyzerFactory.getInstance().getSkeletonAnalyzer(); // todo step 3: set transactor of result this.analyzer.setTransactor(new SkeletonAnalyzerTransactor(this, this.graphicOverlay)); } private void createLensEngine() { // todo step 4: add on-device lens engine Context context = this.getApplicationContext(); // Create LensEngine. this.mLensEngine = new LensEngine.Creator(context, this.analyzer) .setLensType(this.lensType) .applyDisplayDimension(1280, 720) .applyFps(25.0f) .enableAutomaticFocus(true) .create(); }
5、
public void transactResult(MLAnalyzer.Result<MLSkeleton> result) { this.mGraphicOverlay.clear(); SparseArray<MLSkeleton> skeletonSparseArray = result.getAnalyseList(); List<MLSkeleton> list = new ArrayList<>(); for (int i = 0; i < skeletonSparseArray.size(); i++) { list.add(skeletonSparseArray.valueAt(i)); } // todo step 5: add on-device skeleton graphic SkeletonGraphic graphic = new SkeletonGraphic(mGraphicOverlay, list); this.mGraphicOverlay.add(graphic); LiveSkeletonAnalyseActivity mainActivity = mMainActivityWeakReference.get(); if(mainActivity != null && !mainActivity.isFinishing()) { mainActivity.compareSimilarity(list); } }