-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
49 lines (44 loc) · 1.17 KB
/
index.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
var fs = require('fs');
var tesseract = require('node-tesseract');
var gm = require('gm');
processImg('1.jpg', 'test_1.jpg')
.then(recognizer)
.then(text => {
console.log(`识别结果:${text}`);
})
.catch((err)=> {
console.error(`识别失败:${err}`);
});
/**
* 处理图片为阈值图片
* @param imgPath
* @param newPath
* @param [thresholdVal=55] 默认阈值
* @returns {Promise}
*/
function processImg (imgPath, newPath, thresholdVal) {
return new Promise((resolve, reject) => {
gm(imgPath)
.threshold(thresholdVal || 55)
.write(newPath, (err)=> {
if (err) return reject(err);
resolve(newPath);
});
});
}
/**
* 识别图片
* @param imgPath
* @param options tesseract options
* @returns {Promise}
*/
function recognizer (imgPath, options) {
options = Object.assign({psm: 7}, options);
return new Promise((resolve, reject) => {
tesseract
.process(imgPath, options, (err, text) => {
if (err) return reject(err);
resolve(text.replace(/[\r\n\s]/gm, ''));
});
});
}