春暖花開,萬物復蘇,正是踏青好時候,周末,阿珍組織班級里的小朋友去公園踏青,程式員阿強,作為護花使者也一同前往,
阿強本以為,可以肆意在林間草地自由地奔跑,回憶一下逝去的童真時光,沒想到卻成了小朋友們的“植物識別器”,整個踏青之旅變成大型科普現場,面對大自然,小朋友們滿腦子都是“這啥花這啥草”,配以崇拜的小眼神真誠發問,讓阿強即使手忙腳亂地偷偷上網搜索,也要給出正確答案,
但其實,植物科普不必這般費力,平時對大自然的了解甚少,也有辦法輕松hold住小朋友稀奇古怪的發問,執行力滿滿的阿強,開發出一款拍照識花App,只需拿起手機拍攝一張完整花朵的照片,即可快速識別花卉種類,在App的協助下,秒變植物專家,
Demo示例

實作原理
拍照識花功能用到了華為機器學習服務的圖片分類能力,通過對圖片中的物體物件進行分類并添加標注資訊,幫助定義圖片題材和適用場景等,圖片分類支持端側識別和云測識別,端側識別支持超過400個類別,云測識別支持12000個分類,同時,該服務提供了自定義模型能力,支持用戶自定義圖片分類模型,
開發準備
1、在華為開發者聯盟網站創建應用并配置簽名證書
2、配置華為 Maven倉地址,在應用級的“build.gradle”檔案中添加編譯SDK依賴
dependencies{
// 引入基礎SDK
implementation 'com.huawei.hms:ml-computer-vision-classification:2.0.1.300'
// 引入圖片分類模型包
implementation 'com.huawei.hms:ml-computer-vision-image-classification-model:2.0.1.300'
}
3、設定自動更新機器學習模型
添加如下陳述句到AndroidManifest.xml檔案中,用戶從華為應用市場安裝您的應用后,將自動更新機器學習模型到設備:
<manifest
...
<meta-data
android:name="com.huawei.hms.ml.DEPENDENCY"
android:value= "label"/>
...
</manifest>
4、配置混淆腳本
上述步驟具體可參考開發者網站中的開發準備介紹:
https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides-V5/config-agc-0000001050990353-V5?ha_source=hms1
5、在AndroidManifest.xml檔案里面宣告系統權限
為了可以通過相機和相冊進行圖片的獲取,需要在Manifest檔案中申請相關的權限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
開發步驟
1.創建并配置云端圖片分類分析器
創建圖片分類分析器類
public class RemoteImageClassificationTransactor extends BaseTransactor<List<MLImageClassification>>
之后在該類中,通過影像分類自定義類MLRemoteClassificationAnalyzerSetting創建分析器,并設定對應的引數配置,同時配置Handler
private final MLImageClassificationAnalyzer detector;
private Handler handler;
MLRemoteClassificationAnalyzerSetting options = new MLRemoteClassificationAnalyzerSetting.Factory().setMinAcceptablePossibility(0f).create();
this.detector = MLAnalyzerFactory.getInstance().getRemoteImageClassificationAnalyzer(options);
this.handler = handler;
2.呼叫asyncAnalyseFrame方法進行影像處理
使用異步處理方式,對傳入的MLFrame物件進行分類處理
@Override
protected Task<List<MLImageClassification>> detectInImage(MLFrame image) {
return this.detector.asyncAnalyseFrame(image);
}
3.獲取分類成功后的結果
在RemoteImageClassificationTransactor中重寫onSuccess()方法,將識別到的物體名稱顯示到圖片中
@Override
protected void onSuccess(
Bitmap originalCameraImage,
List<MLImageClassification> classifications,
FrameMetadata frameMetadata,
GraphicOverlay graphicOverlay) {
graphicOverlay.clear();
this.handler.sendEmptyMessage(Constant.GET_DATA_SUCCESS);
List<String> classificationList = new ArrayList<>();
for (int i = 0; i < classifications.size(); ++i) {
MLImageClassification classification = classifications.get(i);
if (classification.getName() != null) {
classificationList.add(classification.getName());
}
}
RemoteImageClassificationGraphic remoteImageClassificationGraphic =
new RemoteImageClassificationGraphic(graphicOverlay, this.mContext, classificationList);
graphicOverlay.addGraphic(remoteImageClassificationGraphic);
graphicOverlay.postInvalidate();
}
如果錯誤的話,進行對應的錯誤處理和Log顯示
@Override
protected void onFailure(Exception e) {
this.handler.sendEmptyMessage(Constant.GET_DATA_FAILED);
Log.e(RemoteImageClassificationTransactor.TAG, "Remote image classification detection failed: " + e.getMessage());
}
4.識別完成釋放資源
識別完成后,需要將原有的分析器停止,并釋放檢測資源,在RemoteImageClassificationTransactor中重寫stop()方法
@Override
public void stop() {
super.stop();
try {
this.detector.stop();
} catch (IOException e) {
Log.e(RemoteImageClassificationTransactor.TAG,
"Exception thrown while trying to close remote image classification transactor" + e.getMessage());
}
}
>>訪問華為開發者聯盟官網,了解更多相關內容
>>獲取開發指導檔案
>>華為移動服務開源倉庫地址:GitHub、Gitee
關注我們,第一時間了解華為移動服務最新技術資訊~
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/272015.html
標籤:其他
