// This file is auto-generated, don't edit it
|
import facebody20191230, * as $facebody20191230 from '@alicloud/facebody20191230';
|
// 依赖的模块可通过下载工程中的模块依赖文件或右上角的获取 SDK 依赖信息查看
|
import * as $OpenApi from '@alicloud/openapi-client';
|
import Util, * as $Util from '@alicloud/tea-util';
|
import ViapiUtil from '@alicloud/viapi-utils';
|
import fs from 'fs';
|
import { confingInfoStore } from '@/stores/StoresConfing'
|
import {sendPationCodeApi} from './httpApi'
|
import { sockteStore } from '@/stores/sockteInfo';
|
|
// 要识别的人脸库
|
const faceDatabase= confingInfoStore().confingInfo.face_database
|
const accessKeyId='LTAI5tPBxZiqgd9h6gcL9Qzc'
|
const accessKeySecret='IE6nsjJMTul2ZHkeQ27bg4wmWIngTu'
|
const client=null
|
/**
|
* 使用AK&SK初始化账号Client
|
* @return Client
|
* @throws Exception
|
*/
|
const createClient=()=>{
|
let config = new $OpenApi.Config({
|
// 必填,您的 AccessKey ID
|
accessKeyId: 'LTAI5tPBxZiqgd9h6gcL9Qzc',
|
// 必填,您的 AccessKey Secret
|
accessKeySecret: 'IE6nsjJMTul2ZHkeQ27bg4wmWIngTu',
|
});
|
// 访问的域名
|
config.endpoint = `facebody.cn-shanghai.aliyuncs.com`;
|
console.log('人脸识别客户端已启动')
|
return new facebody20191230(config);
|
}
|
// 将文件上传到oss
|
const getOssUrl=async(baseURL64:any)=>{
|
let file: string = baseURL64;
|
let ossUrl: string = await ViapiUtil.upload(accessKeyId, accessKeySecret, file);
|
return ossUrl
|
}
|
/**
|
*
|
* @param path 图片路径
|
*/
|
const faceShibie= async(path:any)=>{
|
const fileStream = fs.createReadStream(path);
|
let client=createClient()
|
// 通过本地文件
|
const facedata= confingInfoStore().confingInfo.face_database
|
const faceScore=confingInfoStore().confingInfo.face_score
|
console.log(facedata,'人脸数据库')
|
let searchFaceAdvanceRequest = new $facebody20191230.SearchFaceAdvanceRequest();
|
searchFaceAdvanceRequest.imageUrlObject = fileStream;
|
searchFaceAdvanceRequest.dbName = facedata
|
searchFaceAdvanceRequest.limit = 2;
|
let runtime = new $Util.RuntimeOptions({ });
|
console.log('-----监测图片--')
|
client.searchFaceAdvance(searchFaceAdvanceRequest, runtime).then(re=>{
|
// console.log('返回结果')
|
// console.log(re.statusCode)
|
if(re.statusCode===200){
|
const matchList=re.body.data?.matchList
|
// console.log('得到的人脸库')
|
console.log(matchList)
|
if(matchList?.length>0){
|
const faceItems =matchList[0].faceItems
|
if(faceItems[0].score>=faceScore){
|
const entityId=faceItems[0].entityId
|
console.log(entityId,'得到了人脸识别id,存患者code到vuex')
|
sockteStore().setfaceSockte({
|
deviceType: "人脸识别",
|
deviceName: "人脸识别",
|
result: entityId,
|
resultTime: ''
|
})
|
}else{
|
console.log('那些人脸都不是')
|
}
|
}
|
|
}
|
}).finally(()=>{
|
// console.log('---------------都要执行的')
|
// 删除图片
|
delImg(path)
|
})
|
}
|
// base64z转文件后验证
|
const base64toFile = (dataurl:any,filename='file') => {
|
if (!fs.existsSync('./imgs')) {
|
fs.mkdir('./imgs', (err) => {
|
if (err) throw err
|
console.log('文件夹创建成功')
|
})
|
}
|
const path = './imgs/'+ Date.now() +'.png';
|
const base64 = dataurl.replace(/^data:image\/\w+;base64,/,""); //去掉图片base64码前面部分data:image/png;base64
|
// new Buffer 操作权限太大,v6.0后使用Buffer.from()创建构造函数
|
const dataBuffer = new Buffer(base64, 'base64'); //把base64码转成buffer对象,
|
// return dataBuffer
|
fs.writeFile(path, dataBuffer, function(err){//用fs写入文件
|
if(err){
|
console.log(err);
|
}else{
|
console.log('写入成功!',path);
|
// getOssUrl(path)
|
// 验证图片
|
faceShibie(path)
|
}
|
})
|
}
|
//验证后删除文件图片
|
const delImg=(path:string)=>{
|
fs.unlink(path,(err:any)=>{
|
if(err){
|
console.log('删除失败')
|
}else{
|
console.log('删除成功')
|
}
|
})
|
}
|
export {
|
createClient,
|
faceShibie,
|
base64toFile
|
}
|