一、前言
在手機相機功能日益強大的今天,相對於相機硬件的差異,圖像處理算法逐漸顯示出更加重要的地位。現在的消費者也開始由看重硬件能力慢慢轉向對算法能力的重視。用戶在拍照發朋友圈之前往往需要根據場景逐個調整畫面參數,這一過程費時費力,對於小白用戶又很難調節到最佳狀態。有沒有一種方式可以精細化區分場景,實現一鍵式智能拍照修圖呢?華為機器學習服務近期推出的場景識別支持102種細分場景的識別,對於生活旅行常見的場景諸如沙灘、藍天、美食、夜景、綠植、典型建築等場景都可以精准地識別出來,配合圖像矩陣進行精細化調參,幫助打造更加主動智能、省時省力的移動應用。讓我們看一看增強效果。
二、增強效果
對於城市夜景圖,場景識別可以准確識別出夜景,然后將圖片中的亮部增量,暗部變暗,整個照片比之前看起來層次感更強,夜景效果更加純粹。
然后測試一張天空的圖片,在准確識別天空場景之后通過增強矩陣將略顯昏暗的天空變得明亮起來。
以及對於綠植、花朵的拍照增強。
當然,以上demo對於各種圖片的增強效果可能有細微的不同,當然可以根據自己的風格來選擇或搭配濾鏡。
所以讓我們了解開發原理之后再開發自己的相機模式吧。
三、開發步驟
- 創建場景識別檢測器實例。
MLSceneDetectionAnalyzer analyzer = MLSceneDetectionAnalyzerFactory.getInstance().getSceneDetectionAnalyzer();
- 通過android.graphics.Bitmap構造MLFrame,支持的圖片格式包括:jpg/jpeg/png/bmp。
MLFrame frame = new MLFrame.Creator().setBitmap(bitmap).create();
- 場景識別。
Task<List<MLSceneDetection>> task = this.analyzer.asyncAnalyseFrame(frame);
task.addOnSuccessListener(new OnSuccessListener<List<MLSceneDetection>>() {
@Override
public void onSuccess(List<MLSceneDetection> sceneInfos) {
// Processing logic for scene detection success.
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// Processing logic for scene detection failure.
if (e instanceof MLException) {
MLException exception = (MLException) e;
// Obtain the result code.
int errCode = exception.getErrCode();
// Obtain the error information.
String message = exception.getMessage();
} else {
// Other errors.
}
}
});
- 檢測完成,停止分析器,釋放檢測資源。
if (analyzer != null) {
analyzer.stop();
}
maven地址
buildscript {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
allprojects {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}
引入SDK
dependencies {
// Scene detection SDK.
implementation 'com.huawei.hms:ml-computer-vision-scenedetection:2.0.3.300'
// Scene detection model.
implementation 'com.huawei.hms:ml-computer-vision-scenedetection-model:2.0.3.300'
}
清單文件
<manifest
...
<meta-data
android:name="com.huawei.hms.ml.DEPENDENCY"
android:value="1" />
...
</manifest>
權限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.autofocus" />
動態權限申請
if (!(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
requestCameraPermission();
}
四、總結
華為機器學習場景識別提供的更加主動的智能是未來應用趨勢,除了用於拍照效果增強,場景識別還可以用來進行相冊管理及場景圖片檢索,幫助您構建精細化的分類獲取和管理。
欲了解更多詳情,請參閱:
華為開發者聯盟官網:https://developer.huawei.com/consumer/cn/hms
獲取開發指導文檔:https://developer.huawei.com/consumer/cn/doc/development
參與開發者討論請到Reddit社區:https://www.reddit.com/r/HMSCore/
添加鏈接描述
下載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/0204423925515690659?fid=18
原作者:timer