【发布时间】:2019-04-26 13:42:12
【问题描述】:
我有以下函数,它将图像 url 传递给谷歌视觉服务并返回图像中的字母和数字(字符)。它适用于一般网址,但我调用它来访问存储在 Google 存储中的文件,它不起作用。我怎样才能让它工作?我看过谷歌搜索的例子,但我不知道该怎么做?
如果无法使用谷歌存储,有没有办法可以只上传图像而不是存储在文件系统中?我不需要存储图像,我只关心返回的字符。
def detect_text_uri(uri):
"""Detects text in the file located in Google Cloud Storage or on the Web.
"""
from google.cloud import vision
client = vision.ImageAnnotatorClient()
image = vision.types.Image()
image.source.image_uri = uri
image.source.gcs_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)))
return texts
{
这行不起作用,它应该读取我放在谷歌存储中的图像,所有返回的都是空白响应:
detect_text_uri("'source': {'image_uri': 'gs://ocr_storage/meter_reader.jpg'}")
这条线工作正常:
detect_text_uri('https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Transparent_Electricity_Meter_found_in_Israel.JPG/220px-Transparent_Electricity_Meter_found_in_Israel.JPG')
【问题讨论】:
标签: python google-cloud-platform google-api google-cloud-storage google-vision