【问题标题】:Google Vision API detects labels for only public images on GCPGoogle Vision API 仅检测 GCP 上公共图像的标签
【发布时间】:2017-11-28 14:30:23
【问题描述】:

我将图像存储在 Google Cloud Storage 上,并使用 Google Vision API 来检测这些图像的标签。我为这两个目的使用相同的帐户和凭据。 我正在使用以下给出的示例程序: 'https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/vision/cloud-client/detect/detect.py'

我可以成功检测到本地图像和互联网上可公开访问的图像的标签。 当我将以下内容与存储在 GCP 存储桶中的图像一起使用时,除非我将数据(图像)标记为公开,否则程序不会检测到任何标签。

例如

私有时:

# ./detect.py labels-uri 
'https://www.googleapis.com/download/storage/v1/b/mybucket/o/Penguins.jpg?
generation=1510548912343529&alt=media'
Labels:

当我将其标记为“公开”时:

# ./detect.py labels-uri 
'https://www.googleapis.com/download/storage/v1/b/mybucket/o/Penguins.jpg?
generation=1510548912343529&alt=media'
Labels:
penguin
bird
king penguin
flightless bird
beak
organism

我期待,因为我使用相同的凭据来访问视觉和存储 API,它甚至应该适用于我的私有图像。

你能帮忙吗?

【问题讨论】:

    标签: google-cloud-storage google-cloud-vision


    【解决方案1】:

    引用 Cloud Storage 对象时,请使用 URI 模式 gs://bucket_name/object_name

    试试./detect.py labels-uri gs://mybucket/Penguins.jpg

    Cloud Vision 支持 Cloud Storage 对象以及任意 URL。但是,当您引用 URL 时,Cloud Vision 不会在那里转发您的凭据,这与您直接引用 Cloud Storage 对象时不同。在这里,您指定了一个尝试匿名下载 Cloud Storage 对象的 URL,这不是您想要的。 (但请注意,Cloud Vision 不支持指定 GCS 对象的特定版本——有关更多信息,请参阅https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate)。

    【讨论】:

    • 酷!这很好用。感谢您的快速回答。
    • 嗯,不管是 gs:// 还是 http:// 图像 URI,Google 云视觉仍然需要一个公共图像,它不使用用于调用云视觉 API 的凭据来访问图片。根据stackoverflow.com/questions/42125608/…,您需要发送 OAuth 身份验证信息以及对有权读取 GCS 对象的用户或服务帐户的请求
    【解决方案2】:

    这对我来说适用于非公共 GS 存储桶:

    export GOOGLE_APPLICATION_CREDENTIALS="path-to-your-creds.json"
    

    Python3:

    import json
    
    import google.auth
    import requests
    from google.auth.transport import requests as grequests
    from google.cloud import storage
    
    URL = "https://vision.googleapis.com/v1p4beta1/files:asyncBatchAnnotate"
    
    
    credentials, project_id = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
    credentials.refresh(grequests.Request())
    headers = {'Authorization': 'Bearer ' + credentials.token, "Content-Type": "application/json; charset=utf-8"}
    
    request_json = {
        "requests": [
            {
                "inputConfig": {
                    "gcsSource": {
                        "uri": "gs://your-input-bucket/test.pdf"
                    },
                    "mimeType": "application/pdf"
                },
                "features": [
                    {
                        "type": "DOCUMENT_TEXT_DETECTION"
                    }
                ],
                "outputConfig": {
                    "gcsDestination": {
                        "uri": "gs://your-output-bucket/json"
                    },
                    "batchSize": 1
                }
            }
        ]
    }
    
    response = requests.post(URL, json=request_json, headers=headers)
    print(response.content)
    

    【讨论】:

      猜你喜欢
      • 2016-10-01
      • 2017-12-16
      • 2021-12-08
      • 2021-04-01
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多