Skip to content

Commit 2fa58e3

Browse files
authored
Initial commit
1 parent a4fc4e0 commit 2fa58e3

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

nodejs/Vision/RecognizeText.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
'use strict';
2+
3+
const fetch = require('node-fetch');
4+
const fs = require('fs');
5+
const util = require('util');
6+
const delay = require('delay');
7+
8+
const readFile = util.promisify(fs.readFile);
9+
10+
// This sample uses the Recognize Text API call from Azure Computer Vision
11+
// This call can recognize printed or handwritten text, just change the image and mode to specify.
12+
// Computer Vision API - v2.0:
13+
// https://westus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/587f2c6a154055056008f200
14+
15+
let key = process.env['COMPUTER_VISION_SUBSCRIPTION_KEY'];
16+
let baseUrl= process.env['COMPUTER_VISION_ENDPOINT']
17+
let mode = 'Printed'; // Printed or Handwritten depending on what we want to detect
18+
19+
// Use a local file downloaded from here:
20+
// https://github.com/Azure-Samples/cognitive-services-sample-data-files/tree/master/ComputerVision/Images
21+
// Or add your own image to your project folder
22+
let file = 'printed_text.jpg' // local
23+
24+
// Get the binary data of an image
25+
async function readImageByPath(imagePath) {
26+
const imageBinary = await readFile(imagePath);
27+
const fileData = imageBinary.toString('hex');
28+
const result = [];
29+
for (let i = 0; i < fileData.length; i += 2) {
30+
result.push(parseInt(`${fileData[i]}${fileData[i + 1]}`, 16))
31+
}
32+
return Buffer.from(result);
33+
}
34+
35+
async function checkAnalyzeImageJob(operationLocationUrl) {
36+
return new Promise(async (resolve, reject) => {
37+
try {
38+
const response = await fetch(operationLocationUrl, {
39+
method: 'GET',
40+
headers: {
41+
"Content-Type": "application/json",
42+
"Ocp-Apim-Subscription-Key": key
43+
}
44+
})
45+
46+
const json = await response.json();
47+
return resolve(json);
48+
} catch (e) {
49+
return reject(e);
50+
}
51+
})
52+
}
53+
54+
async function submitAnalyzeImageJob(imageBinary) {
55+
return new Promise(async (resolve, reject) => {
56+
const url = baseUrl + "/vision/v2.0/recognizeText" + "?" + `mode=${mode}`;
57+
58+
try {
59+
const response = await fetch(url, {
60+
method: 'POST',
61+
headers: {
62+
"Content-Type": "application/octet-stream",
63+
"Ocp-Apim-Subscription-Key": key
64+
},
65+
body: imageBinary
66+
})
67+
68+
if (response.headers.has('operation-location')) {
69+
return resolve(response.headers.get('operation-location'));
70+
} else {
71+
return reject(response.headers);
72+
}
73+
} catch (e) {
74+
return reject(e);
75+
}
76+
});
77+
}
78+
79+
async function analyzeImage(imageBinary) {
80+
return new Promise(async (resolve, reject) => {
81+
const jobLocation = await submitAnalyzeImageJob(imageBinary);
82+
let isDone = false;
83+
84+
while (!isDone) {
85+
const result = await checkAnalyzeImageJob(jobLocation);
86+
if (result.status == 'NotStarted') {
87+
console.log('Recognition is not started yet');
88+
isDone = false;
89+
await delay(1000);
90+
} else if (result.status == 'Succeeded') {
91+
return resolve(result.recognitionResult);
92+
} else if (result.status == 'Running') {
93+
console.log('Recognition is running, awaiting result');
94+
isDone = false;
95+
await delay(1000);
96+
} else {
97+
return reject('Unknown status: ' + JSON.stringify(result));
98+
}
99+
}
100+
101+
return reject('SHOULD NOT REACH');
102+
});
103+
}
104+
105+
const start = async () => {
106+
const imageData = await readImageByPath(file);
107+
const result = await analyzeImage(imageData);
108+
console.log(result);
109+
};
110+
111+
start();

0 commit comments

Comments
 (0)