-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathocr.js
92 lines (84 loc) · 2.76 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class OCR {
constructor() {
this.worker = null; // classical tesseract only
this.AIworker = null; // default with AI
}
async init() {
this.worker = Tesseract.createWorker();
await this.worker.load();
await this.worker.loadLanguage('eng');
await this.worker.initialize('eng', Tesseract.OEM.TESSERACT_ONLY);
this.AIworker = Tesseract.createWorker({
logger: m => console.debug(m)
});
await this.AIworker.load();
await this.AIworker.loadLanguage('eng');
await this.AIworker.initialize('eng');
}
async itemCount(domElem, points) {
const params = {
//'tessedit_ocr_engine_mode': 0,
//'tessedit_pageseg_mode': 8,
//'tessedit_ocr_engine_mode': Tesseract.OEM.TESSERACT_ONLY,
'tessedit_pageseg_mode': Tesseract.PSM.SINGLE_WORD,
'tessedit_char_whitelist': '0123456789k+',
'tessjs_create_hocr': '0',
'tessjs_create_tsv': '0'
//'tessjs_create_osd': '1'
};
await this.worker.setParameters(params);
const options = { rectangle: {
top: points.x0,
left: points.y0,
width: Math.abs(points.x1 - points.x0),
height: Math.abs(points.y1 - points.y0)
}};
const result = await this.worker.recognize(domElem); //, options);
console.debug(result);
console.debug(result.data.text);
return parseNKInt(result.data.text);
}
async detectSeaport(domElem) {
const params = {
//'tessedit_ocr_engine_mode': 0,
//'tessedit_pageseg_mode': 8,
//'tessedit_ocr_engine_mode': Tesseract.OEM.TESSERACT_ONLY,
//'tessedit_pageseg_mode': Tesseract.PSM.SPARSE_TEXT,
//'tessedit_char_whitelist': '0123456789',
'tessjs_create_hocr': '0',
'tessjs_create_tsv': '0'
//'tessjs_create_osd': '1'
};
await this.AIworker.setParameters(params);
const result = await this.AIworker.recognize(domElem);
console.debug(result);
console.debug(result.data.text);
return result.data.text;
}
}
const parseNKInt = (string) => {
let str = string.replace(/(\r\n|\n|\r| )/g,"");
if (str.endsWith("k+")) {
let thousand = str.slice(0, -2);
return parseInt(thousand) * 1000;
} else {
return parseInt(str);
}
}
// returns: matOut
const postprocessSeaport = async (matIn) => {
let step = new cv.Mat();
let step2 = new cv.Mat();
//cv.cvtColor(matIn, step, cv.COLOR_RGBA2GRAY, 0);
//let lut = [];
//lut += interpolateArray([0, 0], 256/2);
//lut += interpolateArray([0, 7*(256/8)], 7*(256/8));
//lut += interpolateArray([7*(256/8), 255], 1*(256/8));
//console.log(lut);
//cv.LUT(src, lut, dst);
cv.threshold(matIn, step2, 0.75*256, 255, cv.THRESH_BINARY);
//cv.threshold(step, dst, 0.65*256, 0, cv.THRESH_TOZERO);
cv.bitwise_not(step2, step);
step2.delete();
return step;
}