Google 浏览器演示中的 Cloud Vision OCR 与通过 python 的区别

Differences between Google Cloud Vision OCR in browser demo and via python

我对 Google Cloud Vision API 还很陌生,所以如果有明显的答案,我深表歉意。我注意到对于某些图像,我在 Google Cloud Vision API 拖放 (https://cloud.google.com/vision/docs/drag-and-drop) 和 python 中的本地图像检测之间得到了不同的 OCR 结果。 =15=]

我的代码如下

import io
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types

# Instantiates a client
client = vision.ImageAnnotatorClient()

# The name of the image file to annotate
file_name = "./test0004a.jpg"

# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
    content = image_file.read()

image = types.Image(content=content)

response = client.text_detection(image=image)
texts = response.text_annotations

print('Texts:')
for text in texts:
#    print('\n"{}"'.format(text.description.encode('utf-8')))
    print('\n"{}"'.format(text.description.encode('ascii','ignore')))

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

    print('bounds: {}'.format(','.join(vertices)))

附上突出显示的示例图片Sample Image

上面的 python 代码没有 return 任何内容,但在浏览器中使用拖放它正确地将“2340”识别为文本。 python 和浏览器 return 不应该是相同的结果吗?如果没有,为什么不呢?我是否需要在代码中包含其他参数?。

这里的问题是您使用的是 TEXT_DETECTION 而不是 DOCUMENT_TEXT_DETECTION,这是您共享的 Drag and Drop example page 中使用的功能。

通过更改方法(改为document_text_detection()),您应该会得到想要的结果(我已经用您的代码测试过,确实有效):

# Using TEXT_DETECTION
response = client.text_detection(image=image)

# Using DOCUMENT_TEXT_DETECTION
response = client.document_text_detection(image=image)

虽然这两种方法都可用于 OCR,如 the documentation 中所述,DOCUMENT_TEXT_DETECTION 针对密集文本和文档进行了优化。你分享的图片质量不是很高,文字也不是很清楚,所以可能对于这种类型的图片,DOCUMENT_TEXT_DETECTIONTEXT_DETECTION.[=25的表现更好=]

查看其他示例,其中 DOCUMENT_TEXT_DETECTIONTEXT_DETECTION 效果更好。无论如何,请注意,情况可能并非总是如此,TEXT_DETECTION在某些条件下仍可能有更好的结果: