-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetectHandwriting.js
55 lines (50 loc) · 1.28 KB
/
detectHandwriting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Import the Google Cloud client library
const vision = require("@google-cloud/vision");
const fs = require("fs");
async function process(credentials, fileName, lang) {
// Create a client with service account credentials to download from the google cloud console
const client = new vision.ImageAnnotatorClient({
keyFilename: credentials,
});
const content = fs.readFileSync(fileName);
const request = {
image: {
content,
},
imageContext: {
languageHints: prefferedLanguage(lang),
},
};
// Read the local image as a text document
const [result] = await client.documentTextDetection(request);
const output = result.fullTextAnnotation.text;
const outputFileName = `${fileName.split(".")[0]}.txt`;
fs.writeFileSync(outputFileName, output, function (err) {
if (err) {
return console.log(err);
} else {
console.log("Done!");
}
});
}
const prefferedLanguage = (lang) => {
switch (lang) {
// Handwritten hint
case "EN-HAND": {
return ["en-t-i0-handwrit"];
}
case "EN": {
return ["en"];
}
// Handwritten hint
case "RO-HAND": {
return ["ro-t-i0-handwrit"];
}
case "RO": {
return ["ro"];
}
default:
return ["en", "ro"];
}
};
module.exports.process = process;