본문 바로가기

개발

Google vision api 를 이용한 OCR with NODE

728x90

OCR 은 Optical character recognition 로 이미지화 된 문자를 문자로 변환하는 것이다.

 

아래 코드를 따라하면 바로 Google Vision API 를 이용한 OCR 을 이용 할 수 있다.
(Google Vision API OCR 참고 : https://cloud.google.com/vision/docs/ocr)

 

const axios = require('axios');
const fs = require('fs');


const API_KEY = [GOOGLE VISION API KEY]

if (!API_KEY) {
    console.log('No API key provided')
}

function base64_encode(file) {
    var bitmap = fs.readFileSync(file);
    return new Buffer(bitmap).toString('base64');
}

var base64str = base64_encode('test_image.jpg');

const apiCall = `https://vision.googleapis.com/v1/images:annotate?key=${API_KEY}`;

const reqObj = {
    requests: [
        {
            "image": {
                "content": base64str
            },
            "features": [
                {
                    "type": "DOCUMENT_TEXT_DETECTION"
                }
            ]
        }
    ]
}

axios.post(apiCall, reqObj).then((response) => {
    console.log(response);
    console.log(JSON.stringify(response.data.responses, undefined, 4));
}).catch((e) => {
    console.log(e.response);
});

'개발' 카테고리의 다른 글

Precision, Recall, Accureacy 지표의 의미  (0) 2020.06.07
react-native Linux causes a ENOSPC error  (0) 2020.06.07
python matplolib/networkx image to byteIO  (0) 2020.06.07
osx ldconfig command not found error  (0) 2020.06.04
OPENCV with OSX  (0) 2020.06.04