【问题标题】:Google Cloud Vision accuracy for each text returns 0.0每个文本的 Google Cloud Vision 准确度返回 0.0
【发布时间】:2021-08-09 22:20:02
【问题描述】:

我正在使用谷歌云视觉 OCR 来检测图像中的文本。我在谷歌提供的文本之后尝试了 .confidence,但它总是返回为 0.0

response = client.document_text_detection(image=image_googlecloud)
texts = response.text_annotations

texts[0].confidence == 0.0

###This is the part of output of the response variable (the last few lines)###
                y: 2657
              }
            }
            text: "E"
            confidence: 1.0
          }
          confidence: 0.9900000095367432
        }
        confidence: 0.9900000095367432
      }
      block_type: TEXT
      confidence: 0.9900000095367432
    }
  }

当我打印响应变量时,它具有所有置信度值(都大于 0.0),但是当我尝试获取某个单词的置信度时(在上述方法中),它返回 0.0。 有没有办法解决这个问题来获得每个单词的信心?

【问题讨论】:

    标签: google-cloud-vision


    【解决方案1】:

    DOCUMENT_TEXT_DETECTION 遵循此层次结构提取文本结构:

    TextAnnotation -> Page -> Block -> Paragraph -> Word -> Symbol.

    因此,要获得每个单词的置信度,您必须遍历结构组件。

    您可以参考下面提到的代码来获取每个单词的置信度。

    我的图片中的文字:“早上好,一千英里的旅程从一步开始。”

    代码:

    def detect_document_uri(uri):
       """Detects document features in the file located in Google Cloud
       Storage."""
       from google.cloud import vision
       client = vision.ImageAnnotatorClient()
       image = vision.Image()
       image.source.image_uri = uri
    
       response = client.document_text_detection(image=image)
    
       for page in response.full_text_annotation.pages:
           for block in page.blocks:
              
               for paragraph in block.paragraphs:
                  
                   for word in paragraph.words:
                       words = ''.join([
                           symbol.text for symbol in word.symbols
                       ])
                       print('Words: {} (confidence: {})'.format(
                           words, word.confidence))
    
       if response.error.message:
           raise Exception(
               '{}\nFor more info on error messages, check: '
               'https://cloud.google.com/apis/design/errors'.format(
                   response.error.message))
    
    detect_document_uri("gs://your_bucket_name/image.jpg")
    
    
    

    输出:

    本地机器代码:

    def detect_document(path):
        """Detects document features in an image."""
        from google.cloud import vision
        import io
        client = vision.ImageAnnotatorClient()
    
        # [START vision_python_migration_document_text_detection]
        with io.open(path, 'rb') as image_file:
            content = image_file.read()
    
        image = vision.Image(content=content)
    
        response = client.document_text_detection(image=image)
    
        for page in response.full_text_annotation.pages:
            for block in page.blocks:
                
                for paragraph in block.paragraphs:
                   
                    for word in paragraph.words:
                        word_text = ''.join([
                            symbol.text for symbol in word.symbols
                        ])
                        print('Word text: {} (confidence: {})'.format(
                            word_text, word.confidence))
    
                        
        if response.error.message:
            raise Exception(
                '{}\nFor more info on error messages, check: '
                'https://cloud.google.com/apis/design/errors'.format(
                    response.error.message))
                    
    detect_document("path of image from local machine")
    
    
    

    输出:

    【讨论】:

    • 感谢您的代码!对于尝试使用本地图像执行相同过程的人,此方法不起作用,因为这需要将图像上传到谷歌云存储。对于本地文档,请查看此链接:[cloud.google.com/vision/docs/samples/vision-fulltext-detection]
    • 嗨@AbhishekRamesh,谢谢你的回复,我也用本地机器的工作代码更新了答案。
    猜你喜欢
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2019-02-27
    • 2019-02-25
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多