当前位置:网站首页>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
边栏推荐
- Under the bear market of encrypted assets, platofarm's strategy can still obtain stable income
- Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
- 学习、研究编程之道
- The difference between link and @import importing external styles
- 我的理想工作,码农的绝对自由支配才是最重要的——未来创业的追求
- D3.JS 纵向关系图(加箭头,连接线文字描述)
- What is nmap and how to use it
- 完全去中心化的编程模式,不需要服务器,也不需要ip,就像一张漫无目的的网络、四处延伸
- DAY4:SQL Sever 简单使用
- IDEA使用JDBC连接MySQL数据库个人详细教程
猜你喜欢
Reporting Services- Web Service
华为2020校招笔试编程题 看这篇就够了(上)
突破硬件瓶颈(一):Intel体系架构的发展与瓶颈挖掘
量化开发必掌握的30个知识点【什么是分笔逐笔数据】?
MySQL decompressed version windows installation
Detailed steps of JDBC connection to database
以‘智’提‘质|金融影像平台解决方案
Laravel service container (inheritance and events)
如何零代码制作深度学习的趣味app(适合新手)
如何 Pr 一个开源composer项目
随机推荐
Bare metal cloud FASS high performance elastic block storage solution
Laravel服务容器(继承与事件)
学习、研究编程之道
Reporting Services- Web Service
xSAN高可用—XDFS与SAN融合焕发新生命力
DAY4:SQL Sever 简单使用
H5 semantic label
What is wapiti and how to use it
麦当娜“Hellbent”购买130万美元的nft无聊猿,现在被认为太贵了
全闪分布式,如何深度性能POC?
第五空间智能安全⼤赛真题----------PNG图⽚转换器
Get the number of daffodils
Seay source code audit system
Xsan is highly available - xdfs and San are integrated with new vitality
华为2020校招笔试编程题 看这篇就够了(下)
完全去中心化的编程模式,不需要服务器,也不需要ip,就像一张漫无目的的网络、四处延伸
量化开发必掌握的30个知识点【什么是分笔逐笔数据】?
与张小姐的春夏秋冬(5)
Sliding switch of tab of uniapp component
钉钉告警脚本