赞
踩
本示例展示了使用hiai引擎框架提供的多目标识别功能。
本示例模拟了在应用里,选择一张多目标图片,识别多个目标。
需要使用hiai引擎框架多目标识别接口@hms.ai.vision.objectDetection。
使用说明:
本示例展示的控件在@hms.ai.vision.objectDetection.d.ets定义了多目标识别API:
process(request: visionBase.Request): Promise<ObjectDetectionResponse>;
业务使用时,需要先进行import导入objectDetection 调用通用多目标识别接口,并传入想要识别的图片,接收处理返回的结果(文字信息)。参考:
import { image } from '@kit.ImageKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { picker, fileIo } from '@kit.CoreFileKit'; import objectDetection from '@hms.ai.vision.objectDetection'; import visionBase from '@hms.ai.vision.visionBase'; @Entry @Component struct Index { private imageSource: image.ImageSource | undefined = undefined; @State chooseImage: PixelMap | undefined = undefined @State dataValues: string = '' build() { Column() { Image(this.chooseImage) .objectFit(ImageFit.Fill) .height('60%') Scroll() { Text(this.dataValues) .copyOption(CopyOptions.LocalDevice) .margin(10) .width('60%') } .height('15%') .scrollable(ScrollDirection.Vertical) Button('选择图片') .type(ButtonType.Capsule) .fontColor(Color.White) .alignSelf(ItemAlign.Center) .width('80%') .margin(10) .onClick(() => { // 拉起图库 this.selectImage() }) Button('开始多目标识别') .type(ButtonType.Capsule) .fontColor(Color.White) .alignSelf(ItemAlign.Center) .width('80%') .margin(10) .onClick(async () => { if(!this.chooseImage) { hilog.error(0x0000, 'objectDetectSample', `Failed to choose image. chooseImage: ${this.chooseImage}`); return; } let request: visionBase.Request = { inputData: { pixelMap: this.chooseImage, } }; let data: objectDetection.ObjectDetectionResponse = await (await objectDetection.ObjectDetector.create()).process(request); let objectJson = JSON.stringify(data); hilog.info(0x0000, 'objectDetectSample', `Succeeded in face detect:${objectJson}`); this.dataValues = objectJson; }) } .width('100%') .height('100%') .justifyContent(FlexAlign.Center) } 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 = new picker.PhotoViewPicker() photoPicker.select({ MIMEType: picker.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) } }
以上就是本篇文章所带来的鸿蒙开发中一小部分技术讲解;想要学习完整的鸿蒙全栈技术。可以在结尾找我可全部拿到!
下面是鸿蒙的完整学习路线,展示如下:
除此之外,根据这个学习鸿蒙全栈学习路线,也附带一整套完整的学习【文档+视频】,内容包含如下:
内容包含了:(ArkTS、ArkUI、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、鸿蒙南向开发、鸿蒙项目实战)等技术知识点。帮助大家在学习鸿蒙路上快速成长!
为了避免大家在学习过程中产生更多的时间成本,对比我把以上内容全部放在了↓↓↓想要的可以自拿喔!谢谢大家观看!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。