由于有这样一件事情,须要获取身份证正面照中的姓名和身份证号,若是所有经过眼看手敲的话,很费事,并且浪费时间,当时能想若是能自动化执行就行了。node
图片识别暂时尚未接触过,因此就去找了可以提供图像识别的第三方应用,这边我是用的是Face++shell
AppKey
和AppSecret
来调用提供的ApiPostman
简单测试后,发现可行,post请求后,提供的接口会返回一组身份的信息JSON数据Postman 是经过能强大的网页调试与发送网页HTTP请求,并能运行测试用例的的Chrome插件,如今提供了客户端。只要在GUI界面里,填写相应的URl和数据,选择相应的方法,
POST
、GET
、PUT
、DELETE
,就可以将结果返回回来,不须要编写代码。官网连接npm
我是在mac的终端下开发的,若是是window环境系,部分命令不适用,须要替换合适的命令。json
/**
* images 资源文件
* package.json 执行npm init 后会自动生产
* index.js 实现的逻辑文件
*/
- idCard
- index.js
- images
- ***.jpg
- ***.png
- ....
- package.json
复制代码
安装 shelljs
包npm install shelljs
由于须要简单的shell命令执行api
index.js实现(比较简单)bash
var shell = require('shelljs')
var images = shell.exec('ls ./images/*', {silent: true}).toString()
var arr = images.split('\n')
var api_key = 'xxx' // 本身申请的apikey
var api_secret = 'xxx' // 本身申请的api_secret
var result = []
for (var item in arr) {
(function(item) {
setTimeout(function() {
var tmp = '@' + arr[item]
if (tmp == '@') {
return
}
var cmd = 'curl -X POST "https://api-cn.faceplusplus.com/cardpp/v1/ocridcard" -F "api_key="' + api_key + '" -F "api_secret="' + api_secret + '" -F "image_file=' + tmp + '"'
var res = shell.exec(cmd, {silent: true}).toString()
result.push(res)
if (item == arr.length - 2) {
for (var index in result) {
var res = JSON.parse(result[index])
if (typeof res.cards == 'object') {
console.log(res.cards[0].name + ' ' + res.cards[0].id_card_number)
} else {
console.log(result[item])
}
}
}
}, item * 1000)
}(item))
}
复制代码
为何要写成setTimeout,而不是直接执行?是由于若是一次请求过多,api接口会返回一个error_message
,说当前接口请求过多,因此每次延时执行,保证不会丢失数据闭包
node index
,即会打印出姓名 + 身份证号主要就是用到了闭包,很简单=_=||curl