当前位置:网站首页>Super simple integration of HMS ml kit to realize parent control
Super simple integration of HMS ml kit to realize parent control
2022-07-29 05:49:00 【Quantify NPC】
Preface
Have app developers received feedback from parents in the background ? Hope to provide a switch , Take some measures to protect children's eyes , Because now the myopia rate of children is getting higher and higher , It has a lot to do with them staring at the screen closely for a long time . Recently, an overseas customer has integrated ML kit To prevent children's eyes from getting too close to the screen , Or parental control functions that play games for too long .
scene
Parents need this function to prevent children's eyes from getting too close to the screen , Or children spend too much time watching the screen .
Preparation before development
At the project level gradle Add Huawei maven warehouse
open AndroidStudio Project level build.gradle file
Incrementally add the following maven Address :
buildscript {
{
maven {url 'http://developer.huawei.com/repo/'}
}
}
allprojects {
repositories {
maven { url 'http://developer.huawei.com/repo/'}
}
}
At the application level build.gradle Inside plus SDK rely on
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'
}
stay AndroidManifest.xml In the file, apply for a camera 、 Access to network and storage rights
<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" />
Dynamic permission application
Dynamic permission application
if (!(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
requestCameraPermission();
}
Key steps in code development
Create a human face analyzer .
MLFaceAnalyzer analyzer = MLAnalyzerFactory.getInstance().getFaceAnalyzer();
establish LensEngine Examples are used for face detection in video streams , This class is composed of ML Kit SDK Provide , Used to capture the camera's dynamic video stream and pass it to the analyzer .
LensEngine mLensEngine = new LensEngine.Creator(getApplicationContext(), analyzer)
.setLensType(LensEngine.BACK_LENS)
.applyDisplayDimension(640, 480)
.applyFps(30.0f)
.enableAutomaticFocus(true)
.create();
Developers create recognition result processing classes “FaceAnalyzerTransactor”, Implementation of this class MLAnalyzer.Result Interface , Use... In this class transactResult Methods to obtain the detection results of the face presented on the screen , And compare the width height ratio of the mobile phone screen with the width height ratio of the face presented on the screen , If the proportion of faces presented in front of the screen is too large , Then lock the screen
public class FaceAnalyzerTransactor implements MLAnalyzer.MLTransactor<MLFace> {
@Override
public void transactResult(MLAnalyzer.Result<MLFace> results) {
SparseArray<MLFace> items = results.getAnalyseList();
// Developers process the identification results as needed , We need to pay attention to , Only the test results are processed here .
// Not callable ML kit Other detection related interfaces provided .
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) {
// Lock screen prompt , Too close to the screen , Screen lock screen
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
// Prompt when approaching slowly , The distance from the screen
}
});
}
}
}
@Override
public void destroy() {
// Detection end callback method , For releasing resources, etc .
release();
}
}
Set the recognition result processor , Implement the binding between analyzer and result processor
analyzer.setTransactor(new FaceAnalyzerTransactor());
call run Method , Start the camera , Read the video stream , For identification .
SurfaceView mSurfaceView = findViewById(R.id.surface_view);
try {
lensEngine.run(mSurfaceView.getHolder());
} catch (IOException e) {
// exception handling
lensEngine.release();
lensEngine = null;
}
Test complete , Stop the analyzer , Release detection resources
if (mLensEngine != null) {
mLensEngine.release();
}
if (analyzer != null) {
try {
analyzer.stop();
} catch (IOException e) {
// exception handling
}
}
maven Address
buildscript {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
allprojects {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
Demo
边栏推荐
- Fantom (FTM) prices will soar by 20% in the next few days
- Idea using JDBC to connect mysql database personal detailed tutorial
- Plato Farm有望通过Elephant Swap,进一步向外拓展生态
- Some opportunities for young people in rural brand building
- Gluster集群管理小分析
- Laravel swagger add access password
- Strategic cooperation with many institutions shows the strength of the leading company of platofarm yuancosmos
- 与开源项目同步开发& CodeReview & Pull Request & fork怎么拉取原始仓库
- 第五空间智能安全⼤赛真题----------PNG图⽚转换器
- 『全闪实测』数据库加速解决方案
猜你喜欢
极致通缩和永动机模型,将推动 PlatoFarm 爆发
Starfish OS: create a new paradigm of the meta universe with reality as the link
识变!应变!求变!
Record the SQL injection vulnerability of XX company
“山东大学移动互联网开发技术教学网站建设”项目实训日志一
Markdown语法
麦当娜“Hellbent”购买130万美元的nft无聊猿,现在被认为太贵了
IDEA使用JDBC连接MySQL数据库个人详细教程
DAY4:SQL Sever 简单使用
Crypto giants all in metauniverse, and platofarm may break through
随机推荐
Starfish OS: create a new paradigm of the meta universe with reality as the link
中海油集团,桌面云&网盘存储系统应用案例
Display effect of uniapp page title
ReportingService WebService Form身份验证
Breaking through the hardware bottleneck (I): the development of Intel Architecture and bottleneck mining
Shanzhai coin Shib has a US $548.6 million stake in eth whale's portfolio - traders should be on guard
“山东大学移动互联网开发技术教学网站建设”项目实训日志六
华为2020校招笔试编程题 看这篇就够了(上)
我的理想工作,码农的绝对自由支配才是最重要的——未来创业的追求
Use QSS to style the form
性能对比|FASS iSCSI vs NVMe/TCP
量化开发必掌握的30个知识点【什么是Level-2数据】
Fantom (FTM) surged 45% before the FOMC meeting
Windows下cmd窗口连接mysql并操作表
裸金属云FASS高性能弹性块存储解决方案
机器学习让文字识别更简单:Kotlin+MVVM+华为ML Kit
超简单集成HMS ML Kit 实现parental control
大部分PHP程序员,都搞不懂如何安全代码部署
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
Print out all prime numbers between 1-100