【问题标题】:Google text detection api - Web demo result is different from using apiGoogle 文本检测 api - Web 演示结果与使用 api 不同
【发布时间】:2019-11-14 12:27:14
【问题描述】:

我尝试使用 Google Vision API 文本检测功能和 Google 的网络演示对我的图像进行 OCR。两个结果不一样。

首先,我在 url https://cloud.google.com/vision/docs/drag-and-drop 上进行了演示。最后,我通过python语言使用google api代码进行了尝试。两个结果不一样,我不知道为什么。你能帮我解决这个问题吗?

我的python代码在这里:

client = vision.ImageAnnotatorClient()
raw_byte = cv2.imencode('.jpg', image)[1].tostring()
post_image = types.Image(content=raw_byte)
image_context = vision.types.ImageContext()
response = client.text_detection(image=post_image, image_context=image_context)

【问题讨论】:

  • 尝试使用document_text_detection看看输出有没有区别cloud.google.com/vision/docs/…
  • 我试过了,结果和我的预期完全不同。和网页演示不一样

标签: python google-cloud-platform google-cloud-functions google-cloud-vision


【解决方案1】:

这是打字稿代码。

但这个想法不是使用text_detection,而是使用document_text_detection 之类的东西(不确定python API 具体提供了什么)。

使用documentTextDetection() 而不是textDetection() 为我解决了完全相同的问题。

const fs = require("fs");
const path = require("path");
const vision = require("@google-cloud/vision");

async function quickstart() {
  let text = '';
  const fileName = "j056vt-_800w_800h_sb.jpg";
  const imageFile = fs.readFileSync(fileName);
  const image = Buffer.from(imageFile).toString("base64");
  const client = new vision.ImageAnnotatorClient();

  const request = {
    image: {
      content: image
    },
    imageContext: {
      languageHints: ["vi-VN"]
    }
  };

  const [result] = await client.documentTextDetection(request);

  // OUTPUT METHOD A

  for (const tmp of result.textAnnotations) {
      text += tmp.description + "\n";
  }

  console.log(text);

  const out = path.basename(fileName, path.extname(fileName)) + ".txt";
  fs.writeFileSync(out, text);

  // OUTPUT METHOD B

  const fullTextAnnotation = result.fullTextAnnotation;
  console.log(`Full text: ${fullTextAnnotation.text}`);
  fullTextAnnotation.pages.forEach(page => {
    page.blocks.forEach(block => {
      console.log(`Block confidence: ${block.confidence}`);
      block.paragraphs.forEach(paragraph => {
        console.log(`Paragraph confidence: ${paragraph.confidence}`);
        paragraph.words.forEach(word => {
          const wordText = word.symbols.map(s => s.text).join("");
          console.log(`Word text: ${wordText}`);
          console.log(`Word confidence: ${word.confidence}`);
          word.symbols.forEach(symbol => {
            console.log(`Symbol text: ${symbol.text}`);
            console.log(`Symbol confidence: ${symbol.confidence}`);
          });
        });
      });
    });
  });

}

quickstart();

【讨论】:

    【解决方案2】:

    实际上,比较您的两个结果,我看到的唯一区别是结果的显示方式。 Google Cloud 拖放站点显示带有边界框的结果并尝试查找文本区域。

    您使用 Python 脚本获得的响应包含相同的信息。几个例子:

    texts = response.text_annotations
    print([i.description for i in texts])
    # prints all the words that were found in the image
    
    print([i.bounding_poly.vertices for i in texts])
    # prints all boxes around detected words
    

    请随时提出更多问题以进行澄清。

    其他一些想法:

    • 您是否对图像进行预处理?
    • 图片的尺寸是多少?

    【讨论】:

      猜你喜欢
      • 2020-08-30
      • 2018-06-01
      • 2017-08-05
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多