前言
不知道有多少人和小編一樣時不時就被一些小游戲刷屏,這些游戲操作簡單,老少皆宜,傳播速度非常的快,分分鍾霸屏朋友圈。小編也有一個夢想,希望自己有一天也能做出能夠霸屏朋友圈的小游戲。但是要做出來一個這樣的爆款小游戲可不是一件簡單的事情,於是小編開始在網上收集信息,終於發現華為HMS ML Kit提供的人臉檢測和手部關鍵點識別可以通過人臉以及手部關鍵點檢測來實現游戲的趣味性。
應用場景
HMS ML Kit人臉檢測服務對人臉多達855個關鍵點進行檢測,返回人臉輪廓、眉毛、眼睛、鼻子、嘴巴、耳朵等部位的坐標以及人臉偏轉角度等信息。集成人臉檢測服務后開發者可以根據這些信息快速構建人臉美化的應用,或者在臉上加一些有趣的元素,增加圖片的趣味性。
手部關鍵點識別技術在生活中有很多的應用場景。比如拍攝短視頻的軟件在集成了這種技術后,可以根據手部關鍵點生成一些可愛或者搞笑的特效,增加短視頻的趣味性。
Crazy Rockets這款游戲集成了上述兩個服務共同開發,這款游戲一共有兩種玩法,一種是通過人臉的上下移動來控制火箭穿梭,通過巨石陣。另一種是通過手勢的上下移動來控制火箭穿梭,通過巨石陣。兩種方式都是通過檢測人臉和手部關鍵點來反饋信息,進而控制火箭移動,趣味十足,下面就先來看看游戲展示吧!
怎么樣?是不是很心動,那就跟着小編一起來看看怎么集成HMS ML Kit人臉檢測能力來實現小游戲(Crazy Rockets)制作吧。
開發實戰
詳細的准備步驟可以參考華為開發者聯盟:
https://developer.huawei.com/consumer/cn/doc/development/HMS-Guides/ml-process-4
這里列舉關鍵的開發步驟。
一.人臉
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();
}
欲了解更多詳情,請參閱:
華為開發者聯盟官網:
https://developer.huawei.com/consumer/cn/hms/huawei-mlkit
獲取開發指導文檔:
參與開發者討論請到Reddit社區:https://www.reddit.com/r/HuaweiDevelopers/
下載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/0201388581574050067?fid=18&pid=0301388581574050321
作者:timer