1. 程式人生 > 實用技巧 >超簡單整合HMS ML Kit 實現parental control

超簡單整合HMS ML Kit 實現parental control

前言


  各位應用程式開發者有沒有在後臺收到過家長們的反饋? 希望能夠提供一個開關,採取一些措施保護小孩的眼睛,因為現在小孩子的近視率越來越高,和他們長時間近距離盯著螢幕有很大的關係。最近有一個海外的客戶通過集成了ML kit 實現了防範小朋友眼睛離螢幕過近,或者玩遊戲時間過長的父母類控制類功能。

場景


  父母需要這個功能防止小朋友眼睛距離螢幕過近,或者小朋友看螢幕時間過長。

開發前準備


在專案級gradle裡新增華為maven倉

  開啟AndroidStudio專案級build.gradle檔案

  增量新增如下maven地址:

buildscript {
    {        
       maven {url 'http://developer.huawei.com/repo/'}
   }    
}
allprojects {
   repositories {      
       maven { url 'http://developer.huawei.com/repo/'}
   }
}

在應用級的build.gradle裡面加上SDK依賴

dependencies {
   implementation 'com.huawei.hms:ml-computer-vision-face:1.0.4.300'
   implementation 'com.huawei.hms:ml-computer-vision-face-shape-point-model:1.0.4.300'
   implementation 'com.huawei.hms:ml-computer-vision-face-emotion-model:1.0.4.300'
   implementation 'com.huawei.hms:ml-computer-vision-face-feature-model:1.0.4.300'
}

在AndroidManifest.xml檔案裡面申請相機、訪問網路和儲存許可權

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

動態許可權申請

動態許可權申請
if (!(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
   requestCameraPermission();
}

程式碼開發關鍵步驟


建立人體臉部分析器。

MLFaceAnalyzer analyzer = MLAnalyzerFactory.getInstance().getFaceAnalyzer();

建立LensEngine例項用於視訊流的人臉檢測,該類由ML Kit SDK提供,用於捕捉相機動態視訊流並傳入分析器。

LensEngine mLensEngine = new LensEngine.Creator(getApplicationContext(), analyzer)
       .setLensType(LensEngine.BACK_LENS)
       .applyDisplayDimension(640, 480)
       .applyFps(30.0f)
       .enableAutomaticFocus(true)
       .create();

開發者建立識別結果處理類“FaceAnalyzerTransactor”,該類實現MLAnalyzer.Result介面,使用此類中的transactResult方法獲取人臉呈現在螢幕上的檢測結果,並根據手機螢幕的寬高比例與呈現在螢幕上臉部的寬高比例進行對比,如果呈現在螢幕前的人臉所佔比率過大,則鎖屏

public class FaceAnalyzerTransactor implements MLAnalyzer.MLTransactor<MLFace> {
   @Override
   public void transactResult(MLAnalyzer.Result<MLFace> results) {
       SparseArray<MLFace> items = results.getAnalyseList();
       // 開發者根據需要處理識別結果,需要注意,這裡只對檢測結果進行處理。
       // 不可呼叫ML kit提供的其他檢測相關介面。

       if (items != null) {
           MLFace features = items.get(0);
           if (features == null) return;

           BigDecimal bigPhoneWidth = new BigDecimal(Float.toString(640));
           BigDecimal bigPhoneHeight = new BigDecimal(Float.toString(480));
           float phoneRatio = bigPhoneWidth.multiply(bigPhoneHeight).floatValue();

           BigDecimal bigFaceWidth = new BigDecimal(Float.toString(features.getWidth()));
           BigDecimal bigFaceHeight = new BigDecimal(Float.toString(features.getHeight()));
           float faceRatio = bigFaceWidth.multiply(bigFaceHeight).floatValue();

           BigDecimal bigPhoneRatio = new BigDecimal(Float.toString(phoneRatio));
           BigDecimal bigFaceRatio = new BigDecimal(Float.toString(faceRatio));
           final float ratio = bigPhoneRatio.divide(bigFaceRatio, 2, BigDecimal.ROUND_HALF_EVEN).floatValue();

           BigDecimal bigRatio = new BigDecimal(Float.toString(ratio));
           BigDecimal schedule = new BigDecimal(Float.toString(10));
           float scheduleRatio = bigRatio.multiply(schedule).floatValue();

           final int realRatio = Math.round(scheduleRatio);
           int distance = Integer.parseInt(mDistance);
           if (distance <= 6)
               distance = 6;

           if (distance >= realRatio) {
               // 鎖屏提示,距離螢幕過近,螢幕鎖屏
           } else {
               runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       // 緩慢靠近時提示,當下距離螢幕前的距離
                   }
               });
           }

       }
   }

   @Override
   public void destroy() {
       // 檢測結束回撥方法,用於釋放資源等。
       release();
   }
}

設定識別結果處理器,實現分析器與結果處理器的繫結

analyzer.setTransactor(new FaceAnalyzerTransactor());

呼叫run方法,啟動相機,讀取視訊流,進行識別。

SurfaceView mSurfaceView = findViewById(R.id.surface_view);

try {
   lensEngine.run(mSurfaceView.getHolder());
} catch (IOException e) {
   // 異常處理
   lensEngine.release();
   lensEngine = null;
}

檢測完成,停止分析器,釋放檢測資源

if (mLensEngine != null) {
   mLensEngine.release();
}
if (analyzer != null) {
   try {
       analyzer.stop();
   } catch (IOException e) {
       // 異常處理
   }
}



maven地址
buildscript {
   repositories {
       maven { url 'https://developer.huawei.com/repo/' }
   }
}
allprojects {
   repositories {
       maven { url 'https://developer.huawei.com/repo/' }
   }
}

Demo



原文連結:https://developer.huawei.com/consumer/cn/forum/topicview?tid=0203325260088000089&fid=18

原作者:旭小夜