-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
99 lines (76 loc) · 2.25 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const fs = require('fs');
const path = require('path');
const https = require('https');
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
const API_KEY = config.api_key;
const imagesFolderPath = './images/';
const textsFolderPath = './texts/';
createDirIfNotExists(imagesFolderPath);
createDirIfNotExists(textsFolderPath);
fs.readdirSync(imagesFolderPath).forEach((fileName) => {
var extName = path.extname(fileName);
if (extName !== '.jpg') {
console.log(fileName + ' is not a jpg file, skipping');
return;
}
console.log(fileName + ' processing');
var filePath = imagesFolderPath + fileName;
fs.open(filePath, 'r', function (status, fd) {
if (status) {
console.log(status.message);
return;
}
var data = fs.readFileSync(filePath);
console.log(filePath + ' converted to base64');
var queryString = 'key=' + API_KEY;
var options = {
'method': 'POST',
'hostname': 'vision.googleapis.com',
'path': '/v1/images:annotate?' + queryString,
'headers': {
'Content-Type': 'application/json'
}
};
var req = https.request(options, function (res) {
var chunks = [];
res.on('data', function (chunk) {
chunks.push(chunk);
});
res.on('end', function () {
var body = Buffer.concat(chunks);
var responseBody = JSON.parse(body);
var text = responseBody.responses[0].fullTextAnnotation.text;
console.log(filePath + ' text extracted');
var textFileDestPath = textsFolderPath + fileName + '.txt';
fs.writeFile(textFileDestPath, text, function (err) {
if (err) {
console.error(err);
return;
}
console.log(textFileDestPath + ' text file saved');
});
});
});
req.on('error', function (error) {
console.log(error);
});
var imageContentBae64 = data.toString('base64');
req.write(JSON.stringify({
requests: [{
image: {
content: imageContentBae64
},
features: [{
type: 'DOCUMENT_TEXT_DETECTION',
maxResults: 50
}]
}]
}));
req.end();
});
});
function createDirIfNotExists (path) {
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
}