forked from Xetro/tarkov-AH-scrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr.js
executable file
·73 lines (60 loc) · 1.88 KB
/
ocr.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
const OCR = require('tesseract.js-node');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const fs = require('fs');
let worker;
const initOCR = async () => {
worker = await OCR({
tessdata: './',
languages: ['bender']
});
return;
}
const processImage = async (fileName, filePath) => {
const processedImgPath = `./data/images/processed/${fileName}.png`;
await prepareImage(processedImgPath, filePath);
if (fs.existsSync(processedImgPath)) {
const text = worker.recognize(processedImgPath, 'bender');
let price_array = text.replace(/[^\S\r\n]/g, '').split('\n').filter(string => string.length);
price_array = price_array.map(price => price.slice(0, -1));
return price_array;
} else {
console.log('RETURNNING NULL: ', processedImgPath)
return null;
}
};
const prepareImage = async (processedImgPath, filePath) => {
try {
if (fs.existsSync(filePath)) {
const execString = `convert` + ' ' +
filePath + ' ' +
`-crop 190x1025+1215+40` + ' ' +
`-colorspace Gray ` + ' ' +
`-channel Gray` + ' ' +
`-threshold 50%` + ' ' +
`-negate` + ' ' +
processedImgPath;
const { stdout, stderr } = await exec(execString);
if (stderr) {
throw new Error(stderr);
}
} else {
console.log('IMG DOESNT EXIST: ', processedImgPath)
}
} catch (error) {
console.log('Error preparing image: ', error);
}
}
const prepareAll = async () => {
let files = fs.readdirSync('./data/images/raw');
files.forEach(async (file) => {
const processedImgPath = `./data/images/processed/${file.slice(0, -3)}tif`;
const filePath = `./data/images/raw/${file}`;
await prepareImage(processedImgPath, filePath);
});
}
module.exports = {
processImage,
initOCR,
prepareAll
};