当前位置:网站首页>超简单集成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

边栏推荐
- How does the MD editor of CSDN input superscripts and subscripts? The input method of formula and non formula is different
- js简单代码判断打开页面的设备是电脑PC端或手机H5端或微信端
- uniapp组件之tab选项卡滑动切换
- Sqlmap是什么以及使用方法
- 2022 mathematical modeling competition summer training lecture - optimization method: goal planning
- sql-server 数据表的简单操作
- What is wapiti and how to use it
- 如何在加密市场熊市中生存?
- 【TypeScript】深入学习TypeScript函数
- Installation steps and environment configuration of vs Code
猜你喜欢

量化开发必掌握的30个知识点【什么是分笔逐笔数据】?

第五空间智能安全⼤赛真题----------PNG图⽚转换器

Laravel服务容器(继承与事件)

熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊

DAY14:Upload-labs 通关教程
![[C language series] - detailed explanation of file operation (Part 1)](/img/12/2d47fde0385d3f1dcb31f5efa82f7b.png)
[C language series] - detailed explanation of file operation (Part 1)

Qtcreator+cmake compiler settings

Introduction to C language array to proficiency (array elaboration)

php写一个购买全网最低价的纸尿裤

QT setting background image method
随机推荐
一文读懂Move2Earn项目——MOVE
Crypto巨头们ALL IN元宇宙,PlatoFarm或能突围
微信小程序更改属性值-setData-双向绑定-model
Related knowledge of elastic box
Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
Fantom (FTM) 在 FOMC会议之前飙升 45%
Differences between href and SRC
Detailed explanation of typical application code of C language array - master enters by mistake (step-by-step code explanation)
uniapp之常用提示弹框
Common characteristic engineering operations
[JS question solution] questions 1-10 in JS of niuke.com
量化开发必掌握的30个知识点【什么是分笔逐笔数据】?
重绘与回流的关系
【JS题解】牛客网JS篇1-10题
Qt设置背景图片方法
[C language series] - constants and variables that confuse students
弹性盒子相关知识
DAY15:文件包含漏洞靶场手册(自用 file-include 靶场)
DAY4:MySQL 数据库的建立及简单实用
The completely decentralized programming mode does not need servers or IP, just like a aimless network extending everywhere