【问题标题】:How do I call the Google Vision API with an image stored in Google Cloud Storage?如何使用存储在 Google Cloud Storage 中的图像调用 Google Vision API?
【发布时间】:2016-02-05 01:08:44
【问题描述】:

我在 GCS 上有一堆图片,想弄清楚它们是什么?

【问题讨论】:

    标签: python google-cloud-vision


    【解决方案1】:

    对于 GCS 集成 - 我只需将上面的正文修改为通过将 content 属性替换为 gcs_image_uri 来指向 GCS 位置

        batch_request = [{
        'image': {
            'source': {
                'gcs_image_uri': "gs://bucket_name/object_path"
            }
        },
        'features': [{
            'type': 'LANDMARK_DETECTION',
            'maxResults': max_results,
            }]
        }]
    service = get_vision_service()
    request = service.images().annotate(body={
        'requests': batch_request,
        })
    response = request.execute()
    

    【讨论】:

      【解决方案2】:

      可以通过 REST API 调用访问 Vision API。您传入一个 JSON 请求,其中包含嵌入的图像或 GCS 中图像的链接。然后,您可以传入要在图像上运行的功能。这是作为 JSON 请求传入的,响应对象包含注释。这是调用 Vision API 的 Python 代码的 sn-p。

      DISCOVERY_URL='https://{api}.googleapis.com/$discovery/rest?version={apiVersion}'
      
      credentials = GoogleCredentials.get_application_default()
      service = discovery.build('vision', 'v1', credentials=credentials,
                                discoveryServiceUrl=DISCOVERY_URL)
      
      with open(photo_file, 'rb') as image:
        image_content = base64.b64encode(image.read())    
        service_request = service.images().annotate(
          body={
            'requests': [{
              'image': {
                'content': image_content
               },
              'features': [{
                'type': 'LABEL_DETECTION',  # Feature to detect
                'maxResults': 1,
               }]
             }]
          })
        response = service_request.execute()
        label = response['responses'][0]['labelAnnotations'][0]['description']
      

      有关更多信息,您可能希望查看Label Detection Tutorial

      【讨论】:

        猜你喜欢
        • 2016-06-11
        • 2016-06-10
        • 1970-01-01
        • 2017-04-11
        • 2018-05-23
        • 2018-02-21
        • 2017-06-26
        • 1970-01-01
        • 2020-01-17
        相关资源
        最近更新 更多