Annotate a batch of images asynchronously: Core

Annotate a batch of image files in Cloud Storage while off-line.

Code sample


Before trying this sample, follow the Ruby setup instructions in the Vision Quickstart Using Client Libraries. For more information, see the Vision Ruby API reference documentation.

# Instantiate a client
image_annotator_client = Google::Cloud::Vision.image_annotator

# input_image_uri = "gs://cloud-samples-data/vision/label/wakeupcat.jpg"
# output_uri = "gs://your-bucket/prefix/"
image = { source: { image_uri: input_image_uri } }
features = [{ type: :LABEL_DETECTION }, { type: :IMAGE_PROPERTIES }]

# Each requests element corresponds to a single image.  To annotate more
# images, create a request element for each image and add it to
# the array of requests
request = { image: image, features: features }
gcs_destination = { uri: output_uri }

# The max number of responses to output in each JSON file
output_config = { gcs_destination: gcs_destination, batch_size: 2 }

# Make the long-running operation request
operation = image_annotator_client.async_batch_annotate_images \
  requests: [request], output_config: output_config

# Block until operation complete

raise operation.results.message if operation.error?

response = operation.response

# The output is written to GCS with the provided output_uri as prefix
gcs_output_uri = response.output_config.gcs_destination.uri
puts "Output written to GCS with prefix: #{gcs_output_uri}"

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser