当前位置:网站首页>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

边栏推荐
- Use QSS to style the form
- Record the SQL injection vulnerability of XX company
- Laravel swagger add access password
- Related knowledge of elastic box
- sql-server 数据表的简单操作
- Markdown语法
- 如何在加密市场熊市中生存?
- Move protocol global health declaration, carry out the health campaign to the end
- 焕然一新,swagger UI 主题更改
- Extreme deflation and perpetual motion machine model will promote the outbreak of platofarm
猜你喜欢

C# 判断用户是手机访问还是电脑访问

量化开发必掌握的30个知识点【什么是Level-2数据】

Move protocol global health declaration, carry out the health campaign to the end

Gluster cluster management analysis

DAY15(DAY16拓展):文件包含漏洞

“山东大学移动互联网开发技术教学网站建设”项目实训日志三

Fantom (FTM) prices will soar by 20% in the next few days

Plato Farm有望通过Elephant Swap,进一步向外拓展生态

DAY4:SQL Sever 简单使用

"Shandong University mobile Internet development technology teaching website construction" project training log V
随机推荐
Laravel Swagger添加访问密码
Dao race track is booming. What are the advantages of m-dao?
打印出1-100之间的所有质数
第五空间智能安全⼤赛真题----------PNG图⽚转换器
剑指核心-TaoCloud全闪SDS助力构建高性能云服务
机器学习让文字识别更简单:Kotlin+MVVM+华为ML Kit
Fantom (FTM) prices will soar by 20% in the next few days
Strategic cooperation with many institutions shows the strength of the leading company of platofarm yuancosmos
July 28 ens/usd Value Forecast: ENS attracts huge profits
Changed crying, and finally solved cannot read properties of undefined (reading 'parsecomponent')
赓续新征程,共驭智存储
What is nmap and how to use it
Fvuln automated web vulnerability detection tool
Read and understand move2earn project - move
QT setting background image method
PHP如何生成二维码?
H5 semantic label
dcat 批量操作弹窗及参数传递
Fantom (FTM) 在 FOMC会议之前飙升 45%
Build msys2 environment with win10