从Vision API文本检测和填充中读取适当的字段

时间:2016-06-10 06:27:32

标签: javascript html5 ocr vision

一直试图从政府那里读取数据。发布身份证并使用谷歌的Vision Api填写表格的字段如下..

Government ID

我已经成功地从远景API中读取数据,但现在面临填写表格的问题,如下面的相应数据......

Form

我怎样才能实现这个目标?

What i Wish to get

Vision API的回复:

{
"responses": [
    {
        "textAnnotations": [
            {
                "locale": "en",
                "description": "amagas faATST\nINCOME TAX DEPARTMENT\nMAHENDRAKUMARRBAGUL\nRAMKRISHNA NATTHU BAGUL\n01/06/1981\n4Permanent Account Number\nANSAB4834E\nSignature\nGOVT OF INDIA\n",
                "boundingPoly": {
                    "vertices": [
                        {
                            "x": 2,
                            "y": 64
                        },
                        {
                            "x": 4308,
                            "y": 64
                        },
                        {
                            "x": 4308,
                            "y": 2701
                        },
                        {
                            "x": 2,
                            "y": 2701
                        }
                    ]
                }
            },
            {
                "description": "amagas",
                "boundingPoly": {
                    "vertices": [
                        {
                            "x": 6,
                            "y": 64
                        },
                        {
                            "x": 774,
                            "y": 65
                        },
                        {
                            "x": 774,
                            "y": 374
                        },
                        {
                            "x": 6,
                            "y": 373
                        }
                    ]
                }
            },

请帮助

2 个答案:

答案 0 :(得分:3)

您可以使用Node.js执行此操作。我使用Microsoft的Computer Vision API使用Node.js完成了它。获取JSON字符串后,将其解析为JSON对象并运行循环以从中提取数据。之后使用split函数将数据存储到数组中。

//Load the request module
var request = require('request');

var str="";
//Lets configure and request
request({
    url: 'https://api.projectoxford.ai/vision/v1.0/ocr?', //URL to hit
    qs: {"language": "unk",
         "detectOrientation ": "true"
         }, //Query string data

    method: 'POST', //Specify the method

    headers: { //We can define headers too
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key':'xxxxxxxxxxxxxxxx'
    },

    body: "{'url':'LINK TO THE IMAGE'}",

}, function(error, response, body){
    if(error) {
        console.log(error);
    } else {
        var jsonObj = JSON.parse(body);

        var ob = jsonObj;
            for(i=0;i<ob.regions.length;i++){
                for(j=0;j<ob.regions[i].lines.length;j++){
                    for(k=0;k<ob.regions[i].lines[j].words.length;k++){
                            var str = str + " "+ob.regions[i].lines[j].words[k].text;
                    }
                    str = str + "\n";
                }
            }


            var arr = str.split("\n");

            console.log("Name: " + arr[1]);
            console.log("Father's Name: " + arr[2]);
            console.log("Date of Birth: " + arr[3]);
            console.log("Permanent Account Number: " + arr[5]);

    }
});

只需使用您自己的Microsoft Computer Vision API订阅密钥即可。如果您想使用自己的Google Vision API生成的JSON文件,只需删除上面的代码并使用代码下半部分的算法。它会工作! :)干杯

答案 1 :(得分:1)

根据您提供的示例,我们可以假设 1.所有回复中都将提供部门和签名 2.您需要的所有信息都在不同的行上

基于这些假设:

const regExpression = new Regex(' /(DEPARTMENT\\n(.*)\\nSignature)/');
const str = response.responses[0].textAnnotations[0].description; // this will be from the api response form vision
const match = str.match(regExpression)[2].split(“\\n”);

/* Output of above script will be like below
[
  "MAHENDRAKUMARRBAGUL",
  "RAMKRISHNA NATTHU BAGUL",
  "01/06/1981",
  "4Permanent Account Number",
  "ANSAB4834E"
]
*/