Merry Christmas-集成华为HMS ML Kit手部关键点识别来接住圣诞老人的礼物吧!

前言

We wish you a Merry Christmas, We wish you a Merry Christmas, We wish you a Merry Christmas and Happy New Year。圣诞节来临是不是大街小巷已经想起了这首熟悉的圣诞歌曲了呢?铺天盖地的圣诞树,圣诞玩偶,和雪花都在提醒着你圣诞节的到来,一款集成了华为HMS ML Kit手部关键点检测的圣诞小游戏就非常应景啦,一起来体验看看吧!

应用场景

圣诞小游戏是通过手势识别来控制雪橇车左右移动,从而接住从天掉落下来的各类圣诞礼物,每隔15秒将提一次速,给玩家带来不一样的购物游戏体验,快来接住圣诞老人的礼物吧!

在这里插入图片描述

开发实战

1.配置Maven仓地址
打开Android Studio项目级“build.gradle”文件

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) {
    // 异常处理逻辑。
}
  1. 检测完成,停止分析器,释放检测资源
if (analyzer != null) {
    analyzer.stop();
}
if (lensEngine != null) {
    lensEngine.release();
}

结束语

手部关键点识别支持识别包括手指指尖,手腕等21个手部关键点,并返回关键点的位置数据。这款圣诞小游戏就是通过识别手部动态轨迹而控制雪橇车的移动,来接住各类从天而降的礼物,增加游戏的可玩性和趣味性。手部关键点识别还可以广泛应用在其它类别的APP上,大家一起来打开脑洞开发试试吧!

了解更多

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

华为开发者联盟官网:https://developer.huawei.com/consumer/cn/hms?ha_source=hms1

获取开发指导文档:https://developer.huawei.com/consumer/cn/doc/development?ha_source=hms1

参与开发者讨论请到Reddit社区:https://www.reddit.com/r/HMSCore/

下载demo和示例代码请到Github:https://github.com/HMS-Core

解决集成问题请到Stack Overflow:https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest


原文链接:https://developer.huawei.com/consumer/cn/forum/topic/0202442756764380482?fid=18

原作者:timer

猜你喜欢

转载自blog.51cto.com/14772288/2575688