关于android:集成华为机器学习服务ML-Kit轻松打造爆款小游戏

在刷朋友圈时,总会被一些乏味的小游戏刷屏。这些游戏操作简略,老少皆宜并且传播速度十分快,分分钟就霸屏朋友圈。你是否也想做出一款爆款乏味的小游戏呢?华为机器学习服务提供的人脸识检测、手部关键点辨认性能能够帮忙你实现。

Crazy Rockets——这款游戏集成人脸识检测、手部关键点辨认性能。开发出两种玩法,一种是通过人脸的高低挪动来管制火箭穿梭通过巨石阵。另一种是通过手势的高低挪动来管制。两种形式都是通过检测人脸和手部关键点来反馈信息,进而管制火箭挪动,趣味十足!

疯狂购物车小游戏是通过集成手部关键点检测性能来实现的,通过手势检测能够管制购物车左右挪动,从而接住掉落下来的各类商品,每隔15秒将提一次速,给玩家带来不一样的购物游戏体验。

Crazy Rockets开发实战

(一)人脸

1.配置maven仓库

  • 在“allprojects > repositories”中配置HMS Core SDK的Maven仓地址。
allprojects {
    repositories {
        google()
        jcenter()
        maven {url 'https://developer.huawei.com/repo/'}
    }
}
  • 在“buildscript > repositories”中配置HMS Core SDK的Maven仓地址。
buildscript {   
    repositories {       
       google()       
       jcenter()       
       maven {url 'https://developer.huawei.com/repo/'}   
    }
}
  • 在“buildscript > dependencies”中减少agcp配置。
dependencies {
        ...       
        classpath 'com.huawei.agconnect:agcp:1.3.1.300'   
     }   
}

2.集成sdk

Implementation  'com.huawei.hms:ml-computer-vision-face:2.0.1.300'

3.创建人脸分析器

MLFaceAnalyzer analyzer = MLAnalyzerFactory.getInstance().getFaceAnalyzer();

4.创立解决类

public class FaceAnalyzerTransactor implements MLAnalyzer.MLTransactor<MLFace> {
    @Override
    public void transactResult(MLAnalyzer.Result<MLFace> results) {
        SparseArray<MLFace> items = results.getAnalyseList();
        // 开发者依据须要解决辨认后果,须要留神,这里只对检测后果进行解决。
        // 不可调用ML Kit提供的其余检测相干接口。
    }
    @Override
    public void destroy() {
        // 检测完结回调办法,用于开释资源等。
    }
}

5.创立LensEngine,用于捕获相机动静视频流并传入分析器

LensEngine lensEngine = new LensEngine.Creator(getApplicationContext(), analyzer)
    .setLensType(LensEngine.BACK_LENS)
    .applyDisplayDimension(1440, 1080)
    .applyFps(30.0f)
    .enableAutomaticFocus(true)
    .create();

6. 调用run办法,启动相机,读取视频流,进行辨认

// 请自行实现SurfaceView控件的其余逻辑。
SurfaceView mSurfaceView = findViewById(R.id.surface_view);
try {
    lensEngine.run(mSurfaceView.getHolder());
} catch (IOException e) {
    // 异样解决逻辑。
}

7. 开释检测资源

if (analyzer != null) {
    try {
        analyzer.stop();
    } catch (IOException e) {
         // 异样解决。
    }
}
if (lensEngine != null) {
    lensEngine.release();
}

(二)手势辨认

1. 配置maven仓库

在“allprojects > repositories”中配置HMS Core SDK的Maven仓地址。

allprojects {
    repositories {
        google()
        jcenter()
        maven {url 'https://developer.huawei.com/repo/'}
    }
}

在“buildscript > repositories”中配置HMS Core SDK的Maven仓地址。

buildscript {   
    repositories {       
       google()       
       jcenter()       
       maven {url 'https://developer.huawei.com/repo/'}   
    }
}

在“buildscript > dependencies”中减少agcp配置。

dependencies {
        ...       
        classpath 'com.huawei.agconnect:agcp:1.3.1.300'   
     }
 }

2. 集成sdk

// 引入根底SDK
implementation 'com.huawei.hms:ml-computer-vision-handkeypoint:2.0.4.300'
// 引入手部关键点检测模型包
implementation 'com.huawei.hms:ml-computer-vision-handkeypoint-model:2.0.4.300'

3. 创立默认手势分析器

MLHandKeypointAnalyzer analyzer =MLHandKeypointAnalyzerFactory.getInstance().getHandKeypointAnalyzer();

4. 创立解决类

public class HandKeypointTransactor implements MLAnalyzer.MLTransactor<List<MLHandKeypoints>> {
@Override
public void transactResult(MLAnalyzer.Result<List<MLHandKeypoints>> results) {
SparseArray<List<MLHandKeypoints>> analyseList = results.getAnalyseList();
// 开发者依据须要解决辨认后果,须要留神,这里只对检测后果进行解决。
// 不可调用ML Kit提供的其余检测相干接口。
}
@Override
public void destroy() {
// 检测完结回调办法,用于开释资源等。
}
}

5. 设置解决类

analyzer.setTransactor(new HandKeypointTransactor());

6. 创立Lengengine

LensEngine lensEngine = new LensEngine.Creator(getApplicationContext(), analyzer)
.setLensType(LensEngine.BACK_LENS)
.applyDisplayDimension(1280, 720)
.applyFps(20.0f)
.enableAutomaticFocus(true)
.create();

7. 调用run办法,启动相机,读取视频流,进行辨认

// 请自行实现SurfaceView控件的其余逻辑。
SurfaceView mSurfaceView = findViewById(R.id.surface_view);
try {
lensEngine.run(mSurfaceView.getHolder());
} catch (IOException e) {
// 异样解决逻辑。
}

8. 开释检测资源

if (analyzer != null) {
analyzer.stop();
}
 
if (lensEngine != null) {
lensEngine.release();
}

(三)疯狂购物车开发实战

1. 配置Maven仓地址

buildscript {
    repositories {
        google()
        jcenter()
        maven {url 'https://developer.huawei.com/repo/'}
    }
    dependencies {
        ...
        classpath 'com.huawei.agconnect:agcp:1.4.1.300'
    }
}
 
allprojects {
    repositories {
        google()
        jcenter()
        maven {url 'https://developer.huawei.com/repo/'}
    }
}

2. Full SDK集成

dependencies{
    // 引入根底SDK
    implementation 'com.huawei.hms:ml-computer-vision-handkeypoint:2.0.4.300'
    // 引入手部关键点检测模型包
    implementation 'com.huawei.hms:ml-computer-vision-handkeypoint-model:2.0.4.300'
}

用上述形式两种办法之一集成SDK后,在文件头增加配置。

在apply plugin: ‘com.android.application’后增加apply plugin: ‘com.huawei.agconnect’

3. 创立手部关键点分析器

MLHandKeypointAnalyzer analyzer =MLHandKeypointAnalyzerFactory.getInstance().getHandKeypointAnalyzer();

4. 创立辨认后果解决类“HandKeypointTransactor”

public class HandKeypointTransactor implements MLAnalyzer.MLTransactor<List<MLHandKeypoints>> {
    @Override
    public void transactResult(MLAnalyzer.Result<List<MLHandKeypoints>> results) {
        SparseArray<List<MLHandKeypoints>> analyseList = results.getAnalyseList();
        // 开发者依据须要解决辨认后果,须要留神,这里只对检测后果进行解决。
        // 不可调用ML Kit提供的其余检测相干接口。
    }
    @Override
    public void destroy() {
        // 检测完结回调办法,用于开释资源等。
    }
}

5.设置辨认后果处理器,实现分析器与后果处理器的绑定

analyzer.setTransactor(new HandKeypointTransactor());

6. 创立LensEngine

LensEngine lensEngine = new LensEngine.Creator(getApplicationContext(), analyzer)
    .setLensType(LensEngine.BACK_LENS)
    .applyDisplayDimension(1280, 720)
    .applyFps(20.0f)
    .enableAutomaticFocus(true)
    .create();

7. 调用run办法,启动相机,读取视频流,进行辨认

// 请自行实现SurfaceView控件的其余逻辑。
SurfaceView mSurfaceView = findViewById(R.id.surface_view);
try {
    lensEngine.run(mSurfaceView.getHolder());
} catch (IOException e) {
    // 异样解决逻辑。
}

8. 检测实现,进行分析器,开释检测资源

if (analyzer != null) {
    analyzer.stop();
}
if (lensEngine != null) {
    lensEngine.release();
}

看完次要开发步骤是不是感觉集成简略又疾速,除了上述两个小游戏,人脸识检测、手部关键点辨认技术在生活中有很多的利用场景。比方拍摄短视频的软件在集成了这种技术后,能够依据手部关键点生成一些可恶或者搞笑的特效,减少短视频的趣味性。或者是在面向智能家居的场景中,能够自定义一些手势作为智能家电的远距离操控指令,进行一些更加智能的人机交互形式。快来试试,一起开发好玩又乏味的利用吧!

欲了解更多详情,请参阅:

华为开发者联盟官网、获取开发领导文档

参加开发者探讨请到Reddit

下载demo和示例代码请到Github

解决集成问题请到Stack Overflow


原文链接:
https://developer.huawei.com/consumer/cn/forum/topic/0204406585449080270?fid=18&pid=0304406585449080230

作者:胡椒

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理