在刷朋友圈时,总会被一些有趣的小游戏刷屏。这些游戏操作简单,老少皆宜并且传播速度非常快,分分钟就霸屏朋友圈。你是否也想做出一款爆款有趣的小游戏呢?华为机器学习服务提供的人脸识检测、手部关键点识别功能可以帮助你实现。
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
原文链接:
作者:胡椒
Be First to Comment