在刷朋友圈時,總會被一些有趣的小游戲刷屏。這些游戲操作簡單,老少皆宜並且傳播速度非常快,分分鍾就霸屏朋友圈。你是否也想做出一款爆款有趣的小游戲呢?華為機器學習服務提供的人臉識檢測、手部關鍵點識別功能可以幫助你實現。
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
作者:胡椒