1.下載tesseract,github上那個我沒搞明白,我是百度搜了個安裝包裝上的
2.裝好後配一下環境變量就可以用了,命令行輸入tesseract -v進行測試
3.上兩步都是必須的,否則使用模塊會報錯大概是<?>的bug
4.在項目中引入
const tesseract = require("node-tesseract");
5.到這步就差不多了,附一個官方的demo
tesseract.process(__dirname + '/path/to/image.jpg',function(err, text) {
if(err) {
console.error(err);
} else {
console.log(text);
}
});
// Recognize German text in a single uniform block of text and set the binary path
var options = {
l: 'deu',
psm: 6,
binary: '/usr/local/bin/tesseract'
};
tesseract.process(__dirname + '/path/to/image.jpg', options, function(err, text) {
if(err) {
console.error(err);
} else {
console.log(text);
}
});
再附一個我用來識別驗證碼的
tesseract.process("code.png", { l: "eng", psm: 9 }, function(err, text) {
if (err) {
console.error(err);
} else {
console.log(text);
}
});
6.大功告成
ps:如果使用過程中出了這種bug
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
at makeCallback (fs.js:136:11)
at Object.unlink (fs.js:943:14)
at C:\Users\Administrator\Desktop\cust_lib\node_modules\[email protected]@node-tesseract\lib\tesseract.js:97:24
at FSReqWrap.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:53:3)
那就手動打開C:\Users\Administrator\Desktop\cust_lib\node_modules\[email protected]@node-tesseract\lib\tesseract.js:97:24這裏,將fs.unlink(files[0]);變更爲fs.unlink(files[0], function() {});