“HiAI体验”快应用源码在github(https://github.com/Huawei/Consumer/tree/master/HiAI-quickapp-demo)开源了,一步步带你快速在快应用中接入华为HiAI Engine,让你的快应用变得更智能。
1. 引言
华为HiAI Engine提供了免费的人脸比对、人脸解析等多种AI能力,而且还可以在快应用中使用,您可以轻松将多种AI能力和APP或快应用相结合,让您的APP或快应用更加智能强大。最重要的是华为的HiAI Engine是离线的,用户的数据始终都在他们自己的手机上面,不需要担心隐私泄露的问题,可以放心使用。
本次介绍的是HiAI Engine的人脸比对能力。人脸比对是对两张图片的人脸进行高精度分析并返回置信度分数以及判断是否是同一个人 ,如:{'score':1,'samePerson':true},可应用于人证合一验证、与明星相似度等场景中,本次实现效果图如下。
2. 接入准备
人脸比对这个接口在快应用中使用很简单,只需要开发者在华为快应用官网下载华为快应用IDE,然后就可以根据华为HiAI开发指南进行开发了。目前使用华为快应用IDE开发HiAI的能力只需完成以下两步即可开始代码编写,具体如下:
1) manifest配置文件增加如下配置:{'name': 'system.ai'}。
2) 导入AI模块,如:import ai from '@system.ai或 var ai = require('@system.ai ')。
3. 代码实现
代码编写先是完成界面部分,然后实现HiAI接口功能,界面实现可以参考快应用开发指南,这里详细介绍一下HiAI中人脸比对接口的实现,本示例中具体实现步骤如下:
第一步:获取图片资源uri。
第二步:调用HiAI人脸比对接口。
(1) 获取图片资源uri有两种方式,一种是获取手机相册图片uri,另一种是通过手机相机拍照获取图片uri,实现方法如下:
//获取图片资源方法
selectMedia: function (e) {
var that = this;
//判定获取图片资源方式是拍照,调用media库中takePhoto方法
if (e === 'takePhoto') {
//判断点击的是“选择第一张图片”或者是“第二张图片”按钮
if (that.chooseWay === 'selectOneImage') {
that.takePhoto('selectOneImage');
}
else {
that.takePhoto('selectTwoImage');
}
}
//判定获取图片资源方式是从相册中选择,调用media库中pickImage方法
else if (e === 'selectImage') {
if (that.chooseWay === 'selectOneImage') {
that.selectImage('selectOneImage');
} else {
that.selectImage('selectTwoImage');
}
}
else {
that.cancel();
}
},
//拍摄照片
takePhoto: function (e) {
var that = this;
that.isShow = '';
//调用media库中takePhoto方法
media.takePhoto({
success: function (data) {
console.log(`handling success: ${data.uri}`)
//将成功回调的uri存放在oneImgUri或者twoImgUri中
if (e === 'selectOneImage') {
that.oneImgUri = data.uri;
}
else {
that.twoImgUri = data.uri;
}
that.faceCompare();
}
})
},
//从手机相册中选择图片
selectImage: function (e) {
var that = this;
that.isShow = '';
//调用media库中pickImage方法
media.pickImage({
success: function (data) {
console.log(`handling success: ${data.uri}`)
//将成功回调的uri存放在oneImgUri或者twoImgUri中
if (e === 'selectOneImage') {
that.oneImgUri = data.uri;
}
else {
that.twoImgUri = data.uri;
}
//图片资源获取到之后,调用人脸比对方法
that.faceCompare();
}
})
},
(2)调用人脸比对接口
//将两张图片进行对比
faceCompare: function () {
var that = this;
that.result_list = [];
//调用ai库中faceCompare方法
ai.faceCompare({
uri1: that.oneImgUri,
uri2: that.twoImgUri,
success: function (data) {
console.log('handling success: '+JSON.stringify(data));
//根据回调结果判定是一个人,将结果存在变量中
if (data.samePerson) {
that.samePersonResult = '是同一个人';
}
else {
that.samePersonResult = '不是同一个人';
}
var sc = Number(data.score * 100).toFixed(2);
that.score = '相似度 ' + sc + '%';
//将结果放到数组中去,刷新数据源,
that.result_list.push(that.samePersonResult);
that.result_list.push(that.score);
},
fail: function (data, code) {
console.log('handling fail, code: ' + code)
},
complete: function () {
console.log('handling complete');
}
})
}
该接口可以用在需要进行人脸比对的场景,比如比较两个人相似度、与明星相似度等,本示例实现了两个人脸比较并给出判断结果和相似度。
4. 总结
至此,介绍快应用接入华为HiAI的人脸比对这个能力已经描述完了,继上次人脸检测、五官特征检测介绍,本次只介绍人脸比对这个能力,后续将会将其他关于华为HiAI的能力也介绍给大家,如:人脸属性、人脸解析、图像识别、自然语言处理等十几种能力。本次介绍能力源码在github上有上传,大家可以点击下载去看一下。
除此之外,华为还提供了Android端的HiAI,本次分享没有介绍,后期会继续分享一下,有兴趣的朋友可以先去华为开发者网站了解一下。





































苏公安网备 32011402010933号