一、@hms.ai.insightIntent(意图开放)
本模块提供意图开放能力,包括共享和删除InsightIntent。应用、服务可以通过调用本模块接口,向系统共享数据。意图框架根据共享的数据学习规律,在合适的时机在系统入口将对应的InsightIntent推荐给用户,用户点击后,根据应用、服务声明的InsightIntent API,跳转到对应的页面。
julishuoming :视频类应用通过InsightIntent提供共享数据(如用户每天看视频的时间点、视频信息)和对应的开放API。意图框架将在用户每天看视频的时间点通过卡片推荐视频,用户点击卡片后,意图框架将调用该开放API打开视频。
代码如下:
let playMusicIntent: insightIntent.InsightIntent = {
intentName: 'PlayMusic',
intentVersion: '1.0',
identifier: '52dac3b0-6520-4974-81e5-25f0879449b5',
intentActionInfo: {
actionMode: 'EXECUTED',
executedTimeSlots: { executedEndTime: 1637393112000, executedStartTime: 1637393212000 }
},
intentEntityInfo: {
entityId: 'a5c9e9e3-3d0a-4e6a-9f3d-6b7c6b8d6b9f',
songName: 'Dream it possible',
artist: ['张靓颖'],
entityName: 'Music',
songDuration: 244000,
songPlayCount: 99,
songImage: 'https://www-file.huawei.com/-/media/corporate/images/home/logo/huawei_logo.png'
}
}
try {
// 共享数据
insightIntent.shareIntent(this.context, [playMusicIntent], (error) => {
if (error?.code) {
// 处理业务逻辑错误
console.error('shareIntent failed, error.code: ${error?.code}, error.message: ${error?.message}');
return;
}
// 执行正常业务
console.log('shareIntent succeed');
});
} catch (error) {
// 处理异常
console.error(`error.code: ${error?.code}, error.message: ${error?.message}`);
}
@hms.ai.ocr.textRecognition(文字识别)
通用文字识别服务提供图像信息转换为字符信息的能力。通过拍照、扫描等光学输入方式,把各种票据、卡证、表格、报刊、书籍等印刷品文字转化为图像信息,再利用文字识别技术将图像信息转化为计算机等设备可以使用的字符信息,便于用户图区字符内容、屏幕坐标及外框。
// 将图片转换为PixelMap, 可以通过context内的资源管理获取
let res = $r('app.media.image');
let resourceValue: resourceManager.Resource = {
bundleName: res.bundleName,
moduleName: res.moduleName,
id: res.id
}
let resource = await getContext(this).resourceManager.getMediaContent(resourceValue);
let imageResource = image.createImageSource(resource.buffer.slice(resource.byteOffset, resource.byteLength + resource.byteOffset));
let pixelMapInstance = await imageResource.createPixelMap();
// 调用文本识别接口
let visionInfo: textRecognition.VisionInfo = {
pixelMap: pixelMapInstance,
};
textRecognition.recognizeText(visionInfo, (error: BusinessError, data: textRecognition.TextRecognitionResult) => {
// 识别成功,获取对应的结果
let recognitionString = JSON.stringify(data);
hilog.info(0x0000, 'testTag', "textRecognition data is " + recognitionString);
});
pixelMapInstance.release();
imageResource.release();
@ohos.ai.mindSporeLite(推理能力)
MindSpore Lite是一款AI引擎,它提供了面向不同硬件设备AI模型推理的功能,目前已经在图像分类、目标识别、人脸识别、文字识别等应用中广泛使用。
let context: mindSporeLite.Context = {};
context.cpu = {};
context.target = ['cpu'];
context.cpu.threadNum = 2;
context.cpu.threadAffinityMode = 0;
context.cpu.precisionMode = 'preferred_fp16';
context.cpu.threadAffinityCoreList = [0, 1, 2];
@ohos.ai.intelligentVoice(智能语音)
智能语音主要提供了语音注册及语音唤醒相关功能。
- IntelligentVoiceManager:智能语音管理类,明确当前智能语音提供的相关功能,当前支持语音注册、语音唤醒。在进行智能语音相关开发前,需先调用getIntellgentVoiceMangaer()确认当前支持智能语音的相关功能,再进行语音注册和语音唤醒的相关开发。
- EnrollIntelligentVoiceEngine:实现语音注册。开发者需要先进行智能语音的注册,然后才进行唤醒。
- WakeupIntelligentVoiceEngine:实现语音唤醒,开发者需要先进行智能语音的注册,然后才能进行唤醒。
import { BusinessError } from '@ohos.base';
let intelligentVoiceManager: intelligentVoice.IntelligentVoiceManager | null = null;
try {
intelligentVoiceManager = intelligentVoice.getIntelligentVoiceManager();
} catch (err) {
let error = err as BusinessError;
console.error(`Get IntelligentVoiceManager failed. Code:${error.code}, message:${error.message}`);
}