当前位置:网站首页>超简单集成HMS ML Kit 实现parental control
超简单集成HMS ML Kit 实现parental control
2022-07-29 05:19:00 【量化NPC】
前言
各位应用程序开发者有没有在后台收到过家长们的反馈? 希望能够提供一个开关,采取一些措施保护小孩的眼睛,因为现在小孩子的近视率越来越高,和他们长时间近距离盯着屏幕有很大的关系。最近有一个海外的客户通过集成了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

边栏推荐
猜你喜欢

完全去中心化的编程模式,不需要服务器,也不需要ip,就像一张漫无目的的网络、四处延伸

熊市下PLATO如何通过Elephant Swap,获得溢价收益?

MySQL解压版windows安装

HCIA-R&S自用笔记(25)NAT技术背景、NAT类型及配置

The Platonic metauniverse advocated by musk has long been verified by platofarm
![[C language series] - three methods to simulate the implementation of strlen library functions](/img/b2/00cd2b79adc23813088656ec3bc17e.png)
[C language series] - three methods to simulate the implementation of strlen library functions

DAY4:MySQL 数据库的建立及简单实用

·Let's introduce ourselves to the way of programming·

MOVE PROTOCOL全球健康宣言,将健康运动进行到底

Hcia-r & s self use notes (24) ACL
随机推荐
Fvuln-自动化web漏洞检测工具
微信小程序-组件传参,状态管理
Laravel Swagger添加访问密码
365 day challenge leetcode1000 question - day 036 binary tree pruning + subarray and sorted interval sum + delete the shortest subarray to order the remaining arrays
Solve the problem that the prompt information of form verification does not disappear and the assignment does not take effect
重绘与回流的关系
【无标题】
与多家机构战略合作,背后彰显PlatoFarm元宇宙龙头的实力
js简单代码判断打开页面的设备是电脑PC端或手机H5端或微信端
Read and understand move2earn project - move
uniapp之常用提示弹框
7 月 28 日 ENS/USD 价值预测:ENS 吸引巨额利润
焕然一新,swagger UI 主题更改
Similarities and differences between REM and PX and EM
Build msys2 environment with win10
Laravel服务容器(上下文绑定的运用)
常见特征工程操作
Starfish OS:以现实为纽带,打造元宇宙新范式
加密资产熊市之下,PlatoFarm的策略玩法依旧能获得稳定收益
Go|Gin 快速使用Swagger