本文介绍了如何使用Google的vision API在一次调用中为多张图像添加注释?Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用Google的视觉API.我正在尝试批注图像,因此发布了'离线批量批注图像从他们的文档中获取指南.

I recently started using Google's vision API. I am trying to annotate a batch of images and therefore issued the 'batch image annotation offline' guide from their documentation.

但是,我不清楚如何从一个API调用中注释多个图像.假设我已经在Google云端存储桶中存储了10张图片.如何立即注释所有这些图像并将它们存储在一个JSON文件中?现在,我编写了一个程序,调用它们的示例函数,并且可以运行,但是简单起见,为什么我不能说:查看此文件夹并注释其中的所有图像."?

However, it is not clear to me how I can annotate MULTIPLE images from one API call. So let's say I have stored 10 images in my google cloud bucket. How can I annotate all these images at once and store them in one JSON file? Right now, I wrote a program that calls their example function and it works, but to put it simple, why can't I say: 'Look in this folder and annotate all images in it.'?

谢谢.

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_batch_annotate_images 的调用需要一个 requests 参数,该参数是多个请求列表.因此,您可以执行以下操作:

It's somewhat unclear from that example, but your call to async_batch_annotate_images takes a requests parameter which is a list of multiple requests. So you can do something like this:

rom google.cloud import vision_v1
from google.cloud.vision_v1 import enums
import six

def generate_request(input_image_uri):
  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}

  return requests_element


def sample_async_batch_annotate_images(input_uri, output_uri):
  """Perform async batch image annotation"""

  client = vision_v1.ImageAnnotatorClient()

  requests = [
    generate_request(input_uri.format(filename))
    for filename in os.listdir('my_directory')
  ]

  gcs_destination = {'uri': output_uri}

  # The max number of responses to output in each JSON file
  batch_size = 1
  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))


sample_async_batch_annotate_images('gs://my_bucket/{}', 'gs://my_bucket/results')

这可以在单个请求中注释多达2,000张图像.唯一的缺点是您只能指定一个 output_uri 作为目标,因此您将无法使用 counter 将每个结果放在单独的文件中,但是您可以设置 batch_size = 1 来确保如果需要的话,分别编写每个响应.

This can annotate up to 2,000 images in a single request. The only downside is that you can only specify a single output_uri as a destination, so you won't be able to use counter to put each result in a separate file, but you can set batch_size = 1 to ensure each response is written separately if this is what you want.

这篇关于如何使用Google的vision API在一次调用中为多张图像添加注释?Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 14:53