gpt4 book ai didi

python - 浏览器演示中的 Google Cloud Vision OCR 与通过 Python 进行的 Google Cloud Vision OCR 之间的差异

转载 作者:行者123 更新时间:2023-12-01 09:20:03 26 4
gpt4 key购买 nike

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

我的代码如下

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 代码不会返回任何内容,但在浏览器中使用拖放操作可以正确地将“2340”识别为文本。python 和浏览器不应该返回相同的结果吗?如果不是,为什么不呢?我需要在代码中包含额外的参数吗?

最佳答案

这里的问题是您正在使用 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_DETECTION可能是这样的。提供比 TEXT_DETECTION 更好的性能.

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

关于python - 浏览器演示中的 Google Cloud Vision OCR 与通过 Python 进行的 Google Cloud Vision OCR 之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50868017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com