如何使用Google';什么是视觉API?python
我最近开始使用谷歌的visionapi。我试图注释一批图像,因此从它们的文档中发布了指南 然而,我不清楚如何从一个API调用中注释多个图像。假设我在谷歌云存储桶中存储了10幅图像。如何一次注释所有这些图像并将它们存储在一个JSON文件中?现在,我编写了一个程序,调用了它们的示例函数,它可以工作,但简单地说,为什么我不能说:“查看这个文件夹并注释其中的所有图像。” 提前谢谢如何使用Google';什么是视觉API?python,python,python-3.x,google-cloud-platform,google-vision,Python,Python 3.x,Google Cloud Platform,Google Vision,我最近开始使用谷歌的visionapi。我试图注释一批图像,因此从它们的文档中发布了指南 然而,我不清楚如何从一个API调用中注释多个图像。假设我在谷歌云存储桶中存储了10幅图像。如何一次注释所有这些图像并将它们存储在一个JSON文件中?现在,我编写了一个程序,调用了它们的示例函数,它可以工作,但简单地说,为什么我不能说:“查看这个文件夹并注释其中的所有图像。” 提前谢谢 from batch_image_labeling import sample_async_batch_annotate_i
from batch_image_labeling import sample_async_batch_annotate_images
counter = 0
for file in os.listdir('my_directory'):
filename = file
sample_async_batch_annotate_images('gs://my_bucket/{}'.format(filename), 'gs://my_bucket/{}'.format(counter))
counter += 1
from google.cloud import vision_v1
from google.cloud.vision_v1 import enums
import six
def sample_async_batch_annotate_images(input_image_uri, output_uri):
"""Perform async batch image annotation"""
client = vision_v1.ImageAnnotatorClient()
if isinstance(input_image_uri, six.binary_type):
input_image_uri = input_image_uri.decode('utf-8')
if isinstance(output_uri, six.binary_type):
output_uri = output_uri.decode('utf-8')
source = {'image_uri': input_image_uri}
image = {'source': source}
type_ = enums.Feature.Type.LABEL_DETECTION
features_element = {'type': type_}
type_2 = enums.Feature.Type.IMAGE_PROPERTIES
features_element_2 = {'type': type_2}
features = [features_element, features_element_2]
requests_element = {'image': image, 'features': features}
requests = [requests_element]
gcs_destination = {'uri': output_uri}
# The max number of responses to output in each JSON file
batch_size = 2
output_config = {'gcs_destination': gcs_destination, 'batch_size': batch_size}
operation = client.async_batch_annotate_images(requests, output_config)
print('Waiting for operation to complete...')
response = operation.result()
# The output is written to GCS with the provided output_uri as prefix
gcs_output_uri = response.output_config.gcs_destination.uri
print('Output written to GCS with prefix: {}'.format(gcs_output_uri))
从这个例子中我们不太清楚,但是对
async\u batch\u annotate\u images
的调用使用了一个requests
参数,该参数是多个请求的列表。所以你可以这样做:
rom google.cloud导入vision\u v1
从google.cloud.vision_v1导入枚举
进口六
def生成请求(输入图像uri):
如果isinstance(输入图像uri,六位二进制类型):
input\u image\u uri=input\u image\u uri.decode('utf-8')
如果isinstance(输出uri,六位二进制类型):
output\u uri=output\u uri.decode('utf-8')
source={'image\u uri':输入\u image\u uri}
image={'source':source}
类型=enums.Feature.type.LABEL\u检测
特征元素={'type':类型}
type_2=enums.Feature.type.IMAGE_属性
特征元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素元素
features=[features\u元素,features\u元素\u 2]
请求元素={'image':图像,'features':features}
返回请求\u元素
def示例\u异步\u批处理\u注释\u图像(输入\u uri、输出\u uri):
“”“执行异步批处理图像批注”“”
client=vision\u v1.ImageAnnotatorClient()
请求=[
生成请求(输入uri.format(文件名))
用于os.listdir(“我的目录”)中的文件名
]
gcs_destination={'uri':输出_uri}
#每个JSON文件中输出的最大响应数
批次大小=1
输出_配置={'gcs_destination':gcs_destination'batch_size':batch_size}
操作=client.async\u批处理\u注释\u图像(请求、输出\u配置)
打印('等待操作完成…')
response=operation.result()
#输出以提供的输出uri作为前缀写入GCS
gcs\u output\u uri=response.output\u config.gcs\u destination.uri
打印('以前缀{}写入地面军事系统的输出。'格式(地面军事系统输出uri))
示例\u异步\u批处理\u注释\u图像('gs://my\u bucket/{}','gs://my\u bucket/results')
这可以在一个请求中注释多达2000个图像。唯一的缺点是,您只能指定一个
输出uri
作为目标,因此无法使用计数器将每个结果放入单独的文件中,但您可以设置批处理大小=1
,以确保每个响应都是单独编写的,如果这是您想要的。@Bente很高兴听到这个消息!请不要忘记接受答案。Node.js中是否可能有相同的脚本?我可以通过提供我的GCS URI作为“gs://my_bucket/results/batch-{}-”。format(counter)
,获得以计数器命名的输出文件。文件将在bucket中显示为batch-1-output-x-to-y.json
。我能够使用批处理文件,每个文件有100个响应。