代码之家  ›  专栏  ›  技术社区  ›  ARIF MAHMUD RANA

泰瑟拉克.js从axios流响应识别

  •  0
  • ARIF MAHMUD RANA  · 技术社区  · 7 年前

    我的问题是可以从泰瑟拉克.js从axios响应流

    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 .

    但我不能从这些例子中看出。

    -----------------------------------------------------更新--------------------------------------------------------------------

    https://github.com/naptha/tesseract.js/blob/master/src/node/index.js#L37

    所以现在面临着readFile的问题,如何从axios响应中读取文件。这也是不可能的。因为readFile只接受路径而不接受数据。所以会产生一个问题泰瑟拉克.js以便在识别readFile时可以绕过。

    0 回复  |  直到 7 年前
        1
  •  0
  •   Ahmed Ayoub    7 年前

    我从未使用过这个库,但是从给定的示例和对它们的源代码的快速检查来看,它看起来像 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)
    
        2
  •  0
  •   Fasid Mpm    5 年前

    在axios中,可以更改 responseType arraybuffer 万一节点.js,或 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

    推荐文章