微软服务 - OCR

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": "-"
                        }
                    ]
                }
            ]
        }
    ]
}

猜你喜欢

转载自blog.csdn.net/seaalan/article/details/89182877
OCR
今日推荐