- 功能:主要使用OCR服务识别图片中的文字(英文)信息。
- 介绍:https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/concept-recognizing-text
- API文档:https://westus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/56f91f2e778daf14a499e1fc
- 示例:https://azure.microsoft.com/en-us/services/cognitive-services/computer-vision/
- 使用NodeJS的request模块调用代码如下:
const request = require('request');
/**
* get OCR request url
* @returns {String} Full request URL
*/
function getOCRRequestURL() {
return url.format({
protocol: 'https',
host: 'westus.api.cognitive.microsoft.com',
pathname: 'vision/v2.0/ocr',
query: {
language: 'en',
detectOrientation: false
}
});
}
function getOCRRequestOptions(imageUrl) {
return {
method: 'POST',
headers: {
'Ocp-Apim-Subscription-Key': config.microsoft.ocpApimSubscriptionKey,
'Content-Type': 'application/json'
},
body: { url: imageUrl },
uri: getOCRRequestURL(),
json: true
};
}
/**
* get OCR from image
* @param {String} imageUrl Analyze image url
* @param {function(err,{result})} cb Callback
*/
function getOCRFromImage(imageUrl, cb) {
imageUrl = 'https://middle_fc70c230-5b45-11e9-a364-f5713a475f08.png';
if (config.work === 'local') {
return cb(null, {});
}
const requestOptions = getOCRRequestOptions(imageUrl);
request(requestOptions, (error, response, result) => {
if (error) {
let responseError = error.response ? error.response.body || error.response : error;
config.error('getImageInfo error::', responseError, requestOptions);
cb(responseError);
} else {
try {
cb(null, result);
} catch (e) {
config.error('err when parse analyze result', e);
cb(e);
}
}
});
}
- 示例图片:
- 返回的result结果示例如下:
{
"language": "en",
"textAngle": 0,
"orientation": "NotDetected",
"regions": [
{
"boundingBox": "134,61,836,361",
"lines": [
{
"boundingBox": "134,61,803,119",
"words": [
{
"boundingBox": "134,61,96,93",
"text": "\"I"
},
{
"boundingBox": "261,95,184,60",
"text": "was"
},
{
"boundingBox": "482,68,270,112",
"text": "quiet,"
},
{
"boundingBox": "781,67,156,88",
"text": "but"
}
]
},
{
"boundingBox": "184,195,786,94",
"words": [
{
"boundingBox": "184,204,43,84",
"text": "I"
},
{
"boundingBox": "258,229,183,59",
"text": "was"
},
{
"boundingBox": "476,214,156,75",
"text": "not"
},
{
"boundingBox": "661,201,276,88",
"text": "blind."
},
{
"boundingBox": "954,195,16,26",
"text": "'"
}
]
},
{
"boundingBox": "271,351,585,71",
"words": [
{
"boundingBox": "271,393,29,9",
"text": "-"
},
{
"boundingBox": "326,353,175,69",
"text": "Jane"
},
{
"boundingBox": "521,351,278,71",
"text": "Austen"
},
{
"boundingBox": "824,393,32,9",
"text": "-"
}
]
}
]
}
]
}