網(wǎng)站菜單代碼廊坊網(wǎng)站
為了提升用戶體驗,各類應用正通過融合人工智能技術,致力于提供更智能、更高效的服務。應用不僅能通過文字和語音的方式與用戶互動,還能深入分析圖片內容,為用戶提供精準的解決方案。
在解析圖片之前,應用首先需要準確識別圖片中包含的元素,基于此,HarmonyOS SDK 基礎視覺服務(Core Vision Kit)提供了多目標識別功能,可以同時檢測出給定圖片中的各種物體,包括風景、動物、植物、建筑、樹、人臉、表格、文本等位置,并框選出物體,在有需要的情況下,也可將識別結果展示在界面上。
多目標識別功能是一項應用范圍較為廣泛的基礎能力,在搜索引擎應用場景中,應用可借助該能力實現(xiàn)圖像的搜索和分類;在AR游戲中,多目標識別功能可以識別出攝像頭前的玩家,從而與虛擬游戲內容產(chǎn)生互動;此外,多目標識別功能還可應用于安防監(jiān)控系統(tǒng)中,通過識別行人、車輛等等目標進行安全分析和緊急響應。
開發(fā)步驟
1.在使用多目標識別時,將實現(xiàn)多目標識別相關的類添加至工程。
import { BusinessError } from '@kit.BasicServicesKit';
import { objectDetection, visionBase } from '@kit.CoreVisionKit';
2.簡單配置頁面的布局,并在Button組件添加點擊事件,拉起圖庫,選擇圖片。
Button('選擇圖片')
.type(ButtonType.Capsule)
.fontColor(Color.White)
.alignSelf(ItemAlign.Center)
.width('80%')
.margin(10)
.onClick(() => {
// 拉起圖庫,獲取圖片資源
this.selectImage();
})
3.通過圖庫獲取圖片資源,將圖片轉換為PixelMap。
private async selectImage() {let uri = await this.openPhoto()if (uri === undefined) {hilog.error(0x0000, 'objectDetectSample', "Failed to defined uri.");}this.loadImage(uri)
}private openPhoto(): Promise<string> {return new Promise<string>((resolve, reject) => {let photoPicker: photoAccessHelper.PhotoViewPicker = new photoAccessHelper.PhotoViewPicker();photoPicker.select({MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE, maxSelectNumber: 1}).then(res => {resolve(res.photoUris[0])}).catch((err: BusinessError) => {hilog.error(0x0000, 'objectDetectSample', `Failed to get photo image uri. code:${err.code},message:${err.message}`);reject('')})})
}private loadImage(name: string) {setTimeout(async () => {let fileSource = await fileIo.open(name, fileIo.OpenMode.READ_ONLY);this.imageSource = image.createImageSource(fileSource.fd);this.chooseImage = await this.imageSource.createPixelMap();}, 100)
4.實例化Request對象,并傳入待檢測圖片的PixelMap,調用多目標識別的實現(xiàn)多目標識別功能。
// 調用多目標檢測接口
let request: visionBase.Request = {inputData: { pixelMap: this.chooseImage }
};
let data: objectDetection.ObjectDetectionResponse = await (await objectDetection.ObjectDetector.create()).process(request);
5.(可選)如果需要將結果展示在界面上,可以使用下列代碼。
let objectJson = JSON.stringify(data);
hilog.info(0x0000, 'objectDetectSample', `Succeeded in face detect:${objectJson}`);
this.dataValues = objectJson;
了解更多詳情>>
訪問基礎視覺服務聯(lián)盟官網(wǎng)
獲取多目標識別開發(fā)指導文檔