1. 程式人生 > 其它 >整合華為機器學習服務(ML Kit)輕鬆打造爆款小遊戲

整合華為機器學習服務(ML Kit)輕鬆打造爆款小遊戲

技術標籤:android

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

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

作者:胡椒