虹软人证核验增值版-node.js调用C++SDK

147 阅读3分钟

一、前言      随着5G时代的到来,人脸识别技术越来越贴近我们的生活,对于开发人员来说要面临的挑战也越来越艰巨。虹软作为国内领先的人脸识别算法厂商之一,提供了多平台多语言的人脸识别SDK,使用场景广泛。产品主要功能有:人脸检测、追踪、特征提取、特征比对、属性检测,活体检测,图像质量检测等。此外,虹软提供的是基于本地算法特征的离线识别SDK,提供全平台的离线支持。      随着node.js的广泛应用,基于node.js诞生的前端及后端框架也越来越多,例如:桌面版跨平台应用框架:Electron;服务端框架:express、koa、thinkjs、eggjs等等。细心的小伙伴可能会发现,官方并未提供node.js的SDK,本文将讲解在node.js环境下如何调用虹软人证核验增值版Windows X64位 3.0版 C++ SDK包。 二、环境配置 - 安装 node.js > v10.0.0,本人使用v12.19.1 - 配置 npm 镜像 ` npm config set registry registry.npm.taobao.org ` - 安装 node-ffi 编译工具:windows-build-tools ``` 安装过程需要花费较长时间,请耐心等待,期间千万不要中断,控制台显示 all success 代表完成。 此操作会自动配置node-gyp、python等 ``` ``` npm install -g windows-build-tools ``` 三、框架引入封装的npm包 ``` npm包已将所有sdk方法集成,只需调用对应方法即可。 npm包可在electron、koa等框架下直接引入调用。 如大家有兴趣研究调用过程的,可直接参考代码。 项目源代码地址:github ``` ` npm i arcsoft-idcardpro --save ` ``` node.js调用C++动态库依赖node-ffi库,因node-ffi支持的node版本版本过低,在electron高版本中无法使用; 有一位国外作者提供了 node-ffi-napi 的库来支持高版本的node.js,推荐大家使用。 npm包会自动下载所需要的依赖:包括 ffi-napi、ref-array-di、ref-napi、ref-struct-di以及图像处理库jimp ``` 四、调用方法 `` 以下方法的配置和回调参数请参考 虹软官方文档 `` ```c 'use struct'; const path = require('path'); const IdCardPro = require('arcsoft-idcardpro'); const idcard = new IdCardPro(); (async function () { // 设置引入文件路径,将sdk下的libarcsoft_face.dll、libarcsoft_face_engine.dll、libarcsoft_idcardveri.dll、libarcsoft_idcardveri.lib放置您预设的调用目录即可。 process.env.PATH = `process.env.PATH{process.env.PATH}{path.delimiter}${path.join(__dirname, './dll')}`; try { // 在线激活 const onlineActivationRes = idcard.onlineActivation('libarcsoft_idcardveri', { appId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // 请使用自己的 sdkKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // 请使用自己的 activeKey: 'xxxx-xxxx-xxxx-xxxx' // 请使用自己的 }); console.log(onlineActivationRes); // 离线激活 const offlineActivationRes = idcard.offlineActivation('libarcsoft_idcardveri', path.join(__dirname, '../A621114C3133JNGR.dat')); console.log(offlineActivationRes); // 获取激活文件信息 const getActiveFileInfoRes = idcard.getActiveFileInfo('libarcsoft_idcardveri'); console.log(getActiveFileInfoRes); // 配置及初始化引擎 const initOpts = { libFile: 'libarcsoft_idcardveri', // 检测属性配置:1:开启RGB活体检测,2:开启IR活体检测,3:开启图像质量检测,4:开启RGB+IR活体检测,5:开启RGB活体检测和图像质量检测,6:开启IR活体检测和图像质量检测,7:开启RGB+IR活体检测和图像质量检测 combinedMask: 1, imgQualityThreshold: 0.4, // 照片图像质量阈值 modelThreshold_RGB: 0.5, // RGB活体检测阈值 modelThreshold_IR: 0.7 // IR活体检测 }; const initRes = idcard.initialEngine(initOpts); if (initRes !== 0) { throw new Error('Initial Engine Failed!'); } // 人证照片比对 const compareOpts = { type: 0, // 人脸数据类型 1-视频 0-静态图片 compareThreshold: 0.5, // 人证照比对阈值 idcardFile: await idcard.parseImage(path.join(__dirname, './img/f1.jpg')), faceFile: await idcard.parseImage(path.join(__dirname, './img/faceA.jpg')) }; const compareRes = idcard.faceIdcardCompare(compareOpts); console.log(compareRes); } catch (err) { console.error(err); } })(); ```