The cause of

There is such a thing as obtaining the name and ID number in the front photo of the ID card. If all of them are typed by hand, it is very time-consuming and time-consuming. At that time, I thought it would be better if I could automate it.

implementation

Image recognition Api

Image recognition has not been contacted, so I went to find a third-party application that can provide image recognition, here I used Face++

  • Login registration and improve the relevant information providedAppKeyandAppSecretTo call the supplied Api
  • In the use ofPostmanAfter a simple test, it was found to work, and after a POST request, the provided interface returned JSON data with a set of identity information

Postman is a Chrome plugin that enables powerful web debugging and sending HTTP requests to web pages, as well as running test cases, and now provides a client. As long as the GUI interface, fill in the corresponding URl and data, select the corresponding method, POST, GET, PUT, DELETE, you can return the results back, no need to write code. The website links

Nodejs implementation

I developed it in MAC terminal. If it is Windows, some commands are not applicable. You need to replace them with appropriate commands.

  1. The directory structure
/** * images * package.json */ -idcard-index.js-images - ***.jpg - ***.png - .... - package.jsonCopy the code
  1. Install shelljs package NPM install shelljs because simple shell command execution is required

  2. Index.js implementation (relatively simple)

var shell = require('shelljs')
var images = shell.exec('ls ./images/*', {silent: true}).toString()
var arr = images.split('\n')

var api_key = 'xxx'// Apply apikey var api_secret ='xxx'Api_secret var result = []for (var item in arr) {
    (function(item) {
        setTimeout(function() {
            var tmp = The '@' + arr[item]
            if (tmp == The '@') {
                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))
}
Copy the code

Why setTimeout instead of just executing it? If there are too many requests at one time, the API interface returns an error_message saying that there are too many requests on the current interface. Therefore, data will not be lost after each delay

  • performnode index, will be printedThe name + Id number

Is mainly used to closure, easy = _ = | |