Google Vision API中的OCR置信度得分

5
我正在使用Google Vision OCR在Python中从图像中提取文本。使用以下代码片段。然而,置信度得分总是显示为0.0,这明显是不正确的。
如何从Google响应中提取单个字符或单词的OCR置信度得分?
 content = cv2.imencode('.jpg', cv2.imread(file_name))[1].tostring()
 img = types.Image(content=content)
 response1 = client.text_detection(image=img, image_context={"language_hints": ["en"]})
 response_annotations = response1.text_annotations
 for x in response1.text_annotations:
      print(x)
      print(f'confidence:{x.confidence}')

例如:一次迭代的输出结果

description: "Date:"
bounding_poly {
  vertices {
    x: 127
    y: 11
  }
  vertices {
    x: 181
    y: 10
  }
  vertices {
    x: 181
    y: 29
  }
  vertices {
    x: 127
    y: 30
  }
}

confidence:0.0

尝试在演示API中发布图像?结果不同?也许删除语言提示会产生一些影响。 - InUser
演示API?可以进一步详细说明吗?它的OCR表现完美,甚至找到了每个字符的大小写和空格数量。但是它的置信度为零,这不符合实际。 - letsBeePolite
尝试在这里 -> https://cloud.google.com/vision,置信度是否相同? - InUser
使用 google-cloud-vision==1.0.0 时遇到了相同的问题。 - hzitoun
@letsBeePolite请问问题的最新消息是什么吗? - hzitoun
请问这个问题有任何最新消息吗? - Alfredo Maussa
3个回答

2

我成功地复现了你的问题。我使用了以下函数,并且对于所有项目获得了置信度为0.0。

from google.cloud import vision

def detect_text_uri(uri):
    client = vision.ImageAnnotatorClient()
    image = vision.types.Image()
    image.source.image_uri = uri

    response = client.text_detection(image=image)
    texts = response.text_annotations
    print('Texts:')

    for text in texts:
        print('\n"{}"'.format(text.description))

        vertices = (['({},{})'.format(vertex.x, vertex.y)
                    for vertex in text.bounding_poly.vertices])

        print('bounds: {}'.format(','.join(vertices)))
        print("confidence: {}".format(text.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))

然而,在使用相同的图像并选择“尝试API”选项时,我在文档中获得了一些置信度非0的结果。从本地图像检测文本时也发生了这种情况。

人们应该期望两种方法使用相同的值来计算置信度。我已经打开了一个问题跟踪器,请在此处查看。


1
请注意,"尝试API"似乎使用的是DOCUMENT_TEXT_DETECTION功能,而不是TEXT_DETECTION。在代码中调用时,使用document_text_detection()而不是text_detection()似乎可以保持置信度。 - Klesun

2
工作代码,用于检索GOCR响应的正确置信度值。
(使用document_text_detection()而不是text_detection()
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.types.Image(content=content)

    response = client.document_text_detection(image=image)

    for page in response.full_text_annotation.pages:
        for block in page.blocks:
            print('\nBlock confidence: {}\n'.format(block.confidence))

            for paragraph in block.paragraphs:
                print('Paragraph confidence: {}'.format(
                    paragraph.confidence))

                for word in paragraph.words:
                    word_text = ''.join([
                        symbol.text for symbol in word.symbols
                    ])
                    print('Word text: {} (confidence: {})'.format(
                        word_text, word.confidence))

                    for symbol in word.symbols:
                        print('\tSymbol: {} (confidence: {})'.format(
                            symbol.text, symbol.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))
    # [END vision_python_migration_document_text_detection]
# [END vision_fulltext_detection]

# add your own path
path = "gocr_vision.png"
detect_document(path)

更改方法并不是解决方案。document_text_detection的目标并不是文本检测。 - hzitoun

0
置信度得分未填充?
试试这个。
from google.cloud import vision_v1
    image = vision_v1.Image(content=my_image)
    text_detection_params = vision_v1.TextDetectionParams(enable_text_detection_confidence_score=True)
    image_context = vision_v1.ImageContext(text_detection_params=text_detection_params)
    response = client.text_detection(image=image, image_context=image_context)
    
 

你的回答可以通过提供更多支持性信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的回答是否正确。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接