我的问题是,是否可以从axios响应流中识别tesseract.js
const axios = require('axios');
const { TesseractWorker } = require('tesseract.js');
const worker = new TesseractWorker();
axios({
method: 'get',
url: 'https://lh3.googleusercontent.com/iXmJ9aWblkGDpg-_jpcqaY10KmA8HthjZ7F15U7mJ9PQK6vZEStMlathz1FfQQWV5XeeF-A1tZ0UpDjx3q6vEm2BWZn5k1btVSuBk9ad=s660',
responseType: 'stream'
})
.then(function (response) {
//this doesn't work
worker.recognize(response.data).then(result => {
console.log(result);
});
});我看到了一些https://ourcodeworld.com/articles/read/580/how-to-convert-images-to-text-with-pure-javascript-using-tesseract-js和https://ourcodeworld.com/articles/read/348/getting-started-with-optical-character-recognition-ocr-with-tesseract-in-node-js的例子。
但是我不能从这个例子中找出。
经过调试,我发现这不是tesseract.js的问题,因为它正在调用本机node.js fs readFile函数https://github.com/naptha/tesseract.js/blob/master/src/node/index.js#L37
所以现在readFile面临的问题是如何从axios响应中readFile。这也是不可能的。As readFile只接受路径,不接受数据。因此将会给tesseract.js造成一个问题,以便在识别readFile时可以绕过它。
发布于 2019-06-25 09:50:12
我从未使用过这个库,但从给定的示例和对源代码的快速检查来看,worker.recognize似乎不接受流作为参数,相反,它需要一个图像url或实际的图像,并且它在内部处理网络调用(如果需要)。
https://github.com/naptha/tesseract.js/blob/master/src/common/TesseractWorker.js#L74
const { TesseractWorker } = require( 'tesseract.js' );
const worker = new TesseractWorker();
worker.recognize('https://lh3.googleusercontent.com/iXmJ9aWblkGDpg-_jpcqaY10KmA8HthjZ7F15U7mJ9PQK6vZEStMlathz1FfQQWV5XeeF-A1tZ0UpDjx3q6vEm2BWZn5k1btVSuBk9ad=s660')
.then(console.log)
.catch(console.error)发布于 2020-08-31 14:35:53
在axios中,如果是Node.js,可以将responseType更改为arraybuffer,如果是浏览器,则可以将其更改为blob。并将结果传递给Tesseract.recognize
例如,
const img = await axios({
method: 'get',
url: 'your img url',
responseType: 'arraybuffer' //for me it's node.js
});
const imgeDataAsString = await Tesseract.recognize(
img.data,
'eng',
{ logger: m => console.log(m) }
).then(({ data: { text } }) => text);您可以参考axios文档here
https://stackoverflow.com/questions/56742191
复制相似问题