【问题标题】:using google storage and google vision API使用谷歌存储和谷歌视觉 API
【发布时间】: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


    【解决方案1】:

    你的函数只是期待 gcs uri

    detect_text_uri('gs://ocr_storage/meter_reader.jpg')
    

    【讨论】:

      【解决方案2】:

      该函数正在等待的是来自 google 存储的 URI。但在运行该单元之前,您需要使用包含您的凭据的 json 文件登录:

      os.environ['GOOGLE_APPLICATION_CREDENTIALS']=r"credentials.json"
      

      您可以使用类似的功能从本地图像中读取文本。

      代码在这个链接:https://cloud.google.com/vision/docs/ocr#vision_text_detection_gcs-python

      【讨论】:

        猜你喜欢
        • 2019-03-22
        • 2017-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多