如何使用存储在 Google Cloud Storage 中的图像调用 Google Vision API?
How do I call the Google Vision API with an image stored in Google Cloud Storage?
我在 GCS 上有一堆图片,想知道它们是什么?
可以通过 REST API 调用访问 Vision API。您将嵌入图像的 JSON 请求或 link 传递给 GCS 中的图像。然后,您可以在图像上传递您想要 运行 的功能。这是作为 JSON 请求传入的,响应对象包含注释。这是调用 Vision API 的 Python 代码片段。
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
对于 GCS 集成 - 我将修改上面的主体以通过将内容属性替换为 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()
我在 GCS 上有一堆图片,想知道它们是什么?
可以通过 REST API 调用访问 Vision API。您将嵌入图像的 JSON 请求或 link 传递给 GCS 中的图像。然后,您可以在图像上传递您想要 运行 的功能。这是作为 JSON 请求传入的,响应对象包含注释。这是调用 Vision API 的 Python 代码片段。
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
对于 GCS 集成 - 我将修改上面的主体以通过将内容属性替换为 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()