Google 视觉 API 仅检测 GCP 上 public 图像的标签

Google Vision API detects labels for only public images on GCP

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

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

例如

私有时:

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

当我标记为'public'时:

# ./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 访问,它甚至应该适用于我的私人图像。

你能帮忙吗?

引用云存储对象时,使用 URI 模式 gs://bucket_name/object_name

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

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

这对我来说适用于非public 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)