【问题标题】:Google Vision API Text Detection Display Words by BlockGoogle Vision API 文本检测按块显示单词
【发布时间】:2019-07-17 08:35:27
【问题描述】:

有没有办法按块对 Google 的 Document Text Detection API 的文本响应进行分组?如果有提供的解决方案,我可能在文档中忽略了它。我目前正在使用 node.js 从用户提供的图像中获取文本。这是我的代码:

const vision = require('@google-cloud/vision');
const client = new vision.ImageAnnotatorClient({
  keyFilename: 'APIKey.json'
});
client
  .documentTextDetection('image.jpg')
  .then(results => {
    res.send(results);
  })
  .catch(err => {
    res.send(err);
  });

提前致谢。

【问题讨论】:

    标签: node.js express google-vision


    【解决方案1】:

    我不确定是否有标准化的方法来执行此操作,但 Vision API 确实为我们提供了编写块文本所需的一切,包括相关的中断(请参阅Vision API break Types)。所以我们可以枚举每个块并从中创建文本。

    我没有考虑其他几个中断类型(HYPHEN、SURE_SPACE),但我认为添加这些应该很容易。

    例如:

    const vision = require('@google-cloud/vision');
    const client = new vision.ImageAnnotatorClient({
        keyFilename: 'APIKey.json'
    });
    
    client
    .documentTextDetection('image.jpg')
    .then(results => {
        console.log("Text blocks: ", getTextBlocks(results));
    })
    .catch(err => {
        console.error("An error occurred: ", err);
    });
    
    function getTextBlocks(visionResults) {
        let textBlocks = [];
        let blockIndex = 0;;
        visionResults.forEach(result => {
            result.fullTextAnnotation.pages.forEach(page => {
                textBlocks = textBlocks.concat(page.blocks.map(block => { return { blockIndex: blockIndex++, text: getBlockText(block) }}));
            });
        });
        return textBlocks;
    }
    
    function getBlockText(block) {
        let result = '';
        block.paragraphs.forEach(paragraph => {
            paragraph.words.forEach(word => {
                word.symbols.forEach(symbol => {
                    result += symbol.text;
                    if (symbol.property && symbol.property.detectedBreak) {
                        const breakType = symbol.property.detectedBreak.type;
                        if (['EOL_SURE_SPACE' ,'SPACE'].includes(breakType)) {
                            result += " ";
                        }
                        if (['EOL_SURE_SPACE' ,'LINE_BREAK'].includes(breakType)) {
                            result += "\n"; // Perhaps use os.EOL for correctness.
                        }
                    }
                })
            })
        })
    
        return result;
    }
    

    【讨论】:

    • 谢谢!但我收到以下错误消息:TypeError: Cannot read property 'detectedBreak' of null
    • 显然,symbol.property 也可以为空,所以:if(symbol.property) 在其他任何东西之前。谢谢!
    • @TerryLennox 你能在 php 中做到这一点吗?
    • 当然@overflow-stack.. 会略有不同...你能问一个问题吗,我认为最好为此创建一个新问题?
    • @TerryLennox 你能解决这个问题吗? stackoverflow.com/questions/57817740/…
    猜你喜欢
    • 2017-10-20
    • 2019-04-24
    • 2020-01-09
    • 2017-11-28
    • 2016-10-01
    • 2020-01-27
    • 2018-06-01
    • 1970-01-01
    • 2016-05-08
    相关资源
    最近更新 更多