Migrating to Ruby Cloud Vision API Client Library v0.32.0

The Ruby Cloud Vision API Client Library v0.32.0 has significant design differences from previous client libraries.

The following changes have been made to the Cloud Vision API library:

  • The client library is more consistent with the RPC API.

  • Content method parameters now distinguish between a local image and an image on Google Cloud Storage.

  • Requests are batched by default rather than being made on a per-image basis.

This page provides details on the changes that you need to make to your Ruby code for the Cloud Vision API client libraries in order to use the v0.32.0 Ruby client library.

Running previous versions of the client library

You are not required to upgrade your Ruby client library to v0.32. However, new functionality in the Cloud Vision API is only supported in the v0.32 and later versions.

If you want to continue using a previous version of the Ruby client library and do not want to migrate your code, then you should specify the version of the Ruby client library used by your app. To specify a specific library version, edit the Gemfile file as shown following:

gem "google-cloud-vision", "~> 0.31.0"

Removed Classes

The following classes have been removed in the Ruby Cloud Vision API v0.32.0 gem.

  • Google::Cloud::Vision::Project

  • Google::Cloud::Vision::Location

  • Google::Cloud::Vision::Image

  • Google::Cloud::Vision::Annotate

  • Google::Cloud::Vision::Annotation

Moved Classes

The following classes will now be namespaced for each version of Cloud Vision API which allows for updates per version of the API.

  • Google::Cloud::Vision::Credentials is now namespaced under the API version Google::Cloud::Vision::V[API_VERSION]::Credentials

This class can be accessed from Google::Cloud::Vision and a specific version of the Cloud Vision API can be used by initializing the client in the following way:

# Instantiates a client with a specified version
image_annotator = Google::Cloud::Vision::ImageAnnotator.new version: :v1

Required Code Changes

Gem dependency

There are no changes to the require path for the Ruby Cloud Vision API Client Library v0.32.0 gem.

# Imports the Google Cloud client library
require "google/cloud/vision"

Create a client

When you create a Cloud Vision API client using the Ruby Cloud Vision API Client Library v0.32.0, you must make the following changes:

  • Remove the project parameter. The project parameter has been removed because the Cloud Vision API doesn't include a project name in requests.
  • Rather than constructing a vision client, make either a image_annotator client, or a product_search client. The image_annotator client contains the functionality of the old vision client.
image_annotator = Google::Cloud::Vision::ImageAnnotator.new

Constructing requests

The following sections cover constructing requests using the Ruby Cloud Vision API Client Library v0.32.0. The examples make requests using both a local image and a URI for an image from Google Cloud Storage.

Constructing objects that represent image content from local file

The following example shows the new way to request label detection for a local file.

Previous versions of the client library:

project_id = "YOUR_PROJECT_ID"
vision = Google::Cloud::Vision.new project: project_id
file_name = "./resources/cat.jpg"
labels = vision.image(file_name).labels

puts "Labels:"
labels.each do |label|
  puts label.description
end

Ruby Client Library v0.32:

image_annotator = Google::Cloud::Vision::ImageAnnotator.new

# The name of the image file to annotate
file_name = "./resources/cat.jpg"

# Performs label detection on the image file
response = image_annotator.label_detection image: file_name
response.responses.each do |res|
  puts "Labels:"
  res.label_annotations.each do |label|
    puts label.description
  end
end

Constructing objects that represent image content from Google Cloud Storage URI

The following example shows the new way to request label detection for an image stored on Google Cloud Storage.

Previous versions of the client library:

project_id = "YOUR_PROJECT_ID"
vision = Google::Cloud::Vision.new project: project_id
storage_uri = "gs://gapic-toolkit/President_Barack_Obama.jpg"
labels = vision.image(storage_uri).labels

puts "Labels:"
labels.each do |label|
  puts label.description
end

Ruby Client Library v0.32:

image_annotator = Google::Cloud::Vision::ImageAnnotator.new

response = image_annotator.label_detection(
  image: "gs://gapic-toolkit/President_Barack_Obama.jpg",
  max_results: 15 # optional, defaults to 10
)

puts "Labels:"
response.responses.each do |res|
  res.label_annotations.each do |label|
    puts label.description
  end
end

Crop hints detection

The following example shows the new way to request crop hints detection and process the response.

Previous versions of the client library:

image  = vision_client.image image_path

crop_hints = image.crop_hints

crop_hints.each do |crop_hint|
  puts "Crop hint bounds:"
  crop_hint.bounds.each do |bound|
    puts "#{bound.x}, #{bound.y}"
  end
end

Ruby Client Library v0.32:

response = image_annotator.crop_hints_detection image: image_path

response.responses.each do |res|
  puts "Crop hint bounds:"
  res.crop_hints_annotation.crop_hints.each do |crop_hint|
    crop_hint.bounding_poly.vertices.each do |bound|
      puts "#{bound.x}, #{bound.y}"
    end
  end
end

Document text detection

The following example shows the new way to request document text detection and process the response.

Previous versions of the client library:

image  = vision_client.image image_path

document = image.document

puts document.text

Ruby Client Library v0.32:

response = image_annotator.document_text_detection image: image_path

text = ""
response.responses.each do |res|
  res.text_annotations.each do |annotation|
    text << annotation.description
  end
end

puts text

Face detection

The following example shows the new way to request document text detection and process the response.

Previous versions of the client library:

image  = vision_client.image image_path

image.faces.each do |face|
  puts "Joy:      #{face.likelihood.joy?}"
  puts "Anger:    #{face.likelihood.anger?}"
  puts "Sorrow:   #{face.likelihood.sorrow?}"
  puts "Surprise: #{face.likelihood.surprise?}"
end

Ruby Client Library v0.32:

response = image_annotator.face_detection image: image_path

response.responses.each do |res|
  res.face_annotations.each do |face|
    puts "Joy:      #{face.joy_likelihood}"
    puts "Anger:    #{face.anger_likelihood}"
    puts "Sorrow:   #{face.sorrow_likelihood}"
    puts "Surprise: #{face.surprise_likelihood}"
  end
end

Image properties detection

The following example shows the new way to request image properties detection and process the response.

Previous versions of the client library:

image  = vision_client.image image_path

image.properties.colors.each do |color|
  puts "Color #{color.red}, #{color.green}, #{color.blue}"
end

Ruby Client Library v0.32:

response = image_annotator.image_properties_detection image: image_path

response.responses.each do |res|
  res.image_properties_annotation.dominant_colors.colors.each do |color_info|
    color = color_info.color
    puts "Color #{color.red}, #{color.green}, #{color.blue}"
  end
end

Label detection

The following example shows the new way to request label detection and process the response.

Previous versions of the client library:

image  = vision_client.image image_path

image.labels.each do |label|
  puts label.description
end

Ruby Client Library v0.32:

response = image_annotator.label_detection(
  image: image_path,
  max_results: 15 # optional, defaults to 10
)

response.responses.each do |res|
  res.label_annotations.each do |label|
    puts label.description
  end
end

Landmark detection

The following example shows the new way to request landmark detection and process the response.

Previous versions of the client library:

image  = vision_client.image image_path

image.landmarks.each do |landmark|
  puts landmark.description

  landmark.locations.each do |location|
    puts "#{location.latitude}, #{location.longitude}"
  end
end

Ruby Client Library v0.32:

response = image_annotator.landmark_detection image: image_path

response.responses.each do |res|
  res.landmark_annotations.each do |landmark|
    puts landmark.description

    landmark.locations.each do |location|
      puts "#{location.lat_lng.latitude}, #{location.lat_lng.longitude}"
    end
  end
end

Logo detection

The following example shows the new way to request logo detection and process the response.

Previous versions of the client library:

image  = vision_client.image image_path

image.logos.each do |logo|
  puts logo.description
end

Ruby Client Library v0.32:

response = image_annotator.logo_detection image: image_path

response.responses.each do |res|
  res.logo_annotations.each do |logo|
    puts logo.description
  end
end

Safe search detection

The following example shows the new way to request safe search detection and process the response.

Previous versions of the client library:

image  = vision_client.image image_path

safe_search = image.safe_search

puts "Adult:    #{safe_search.adult?}"
puts "Spoof:    #{safe_search.spoof?}"
puts "Medical:  #{safe_search.medical?}"
puts "Violence: #{safe_search.violence?}"

Ruby Client Library v0.32:

response = image_annotator.safe_search_detection image: image_path

response.responses.each do |res|
  safe_search = res.safe_search_annotation

  puts "Adult:    #{safe_search.adult}"
  puts "Spoof:    #{safe_search.spoof}"
  puts "Medical:  #{safe_search.medical}"
  puts "Violence: #{safe_search.violence}"
end

Text detection

The following example shows the new way to request text detection and process the response.

Previous versions of the client library:

image  = vision_client.image image_path

puts image.text

Ruby Client Library v0.32:

response = image_annotator.text_detection(
  image: image_path,
  max_results: 1 # optional, defaults to 10
)

response.responses.each do |res|
  res.text_annotations.each do |text|
    puts text.description
  end
end

Web detection

The following example shows the new way to request web detection and process the response.

Previous versions of the client library:

image  = vision_client.image image_path

web = image.web

web.entities.each do |entity|
  puts entity.description
end

web.full_matching_images.each do |image|
  puts image.url
end

Ruby Client Library v0.32:

response = image_annotator.web_detection(
  image: image_path,
  max_results: 15 # optional, defaults to 10
)

response.responses.each do |res|
  res.web_detection.web_entities.each do |entity|
    puts entity.description
  end

  res.web_detection.full_matching_images.each do |match|
    puts match.url
  end
end

Object localization detection

The following example shows the new way to request object localization detection and process the response.

Previous versions of the client library:

image  = vision_client.image image_path

image.object_localizations.each do |object|
  puts "#{object.name} (confidence: #{object.score})"
  puts "Normalized bounding polygon vertices:"
  object.bounds.each do |vertex|
    puts " - (#{vertex.x}, #{vertex.y})"
  end
end

Ruby Client Library v0.32:

response = image_annotator.object_localization_detection image: image_path

response.responses.each do |res|
  res.localized_object_annotations.each do |object|
    puts "#{object.name} (confidence: #{object.score})"
    puts "Normalized bounding polygon vertices:"
    object.bounding_poly.normalized_vertices.each do |vertex|
      puts " - (#{vertex.x}, #{vertex.y})"
    end
  end
end

PDF text detection

The async_batch_annotate_files method remains unchanged. A new document_text_detection method has been added to help simplify the process. The following example shows the new way to request pdf text detection. The Operation returned remains the same.

# gcs_source_uri = "Google Cloud Storage URI, eg. 'gs://my-bucket/example.pdf'"
# gcs_destination_uri = "Google Cloud Storage URI, eg. 'gs://my-bucket/prefix_'"

require "google/cloud/vision"
require "google/cloud/storage"

vision = Google::Cloud::Vision.new

# Supported mime_types are: 'application/pdf' and 'image/tiff'
input_config = {
  gcs_source: { uri: gcs_source_uri },
  mime_type:  "application/pdf"
}

output_config = {
  gcs_destination: { uri: gcs_destination_uri },
  batch_size:      2  # number of pages to group per json output file
}

async_request = {
  input_config:  input_config,
  features:      [{ type: "DOCUMENT_TEXT_DETECTION" }],
  output_config: output_config
}

operation = vision.async_batch_annotate_files [async_request]

puts "Waiting for the operation to finish."
operation.wait_until_done!

Ruby Client Library v0.32:

# gcs_source_uri = "Google Cloud Storage URI, eg. 'gs://my-bucket/example.pdf'"
# gcs_destination_uri = "Google Cloud Storage URI, eg. 'gs://my-bucket/prefix_'"

require "google/cloud/vision"
require "google/cloud/storage"

image_annotator = Google::Cloud::Vision::ImageAnnotator.new

operation = image_annotator.document_text_detection(
  image: gcs_source_uri,
  mime_type: "application/pdf",
  batch_size: 2,
  destination: gcs_destination_uri,
  async: true
)

puts "Waiting for the operation to finish."
operation.wait_until_done!
Hai trovato utile questa pagina? Facci sapere cosa ne pensi:

Invia feedback per...

Cloud Vision API Documentation
Hai bisogno di assistenza? Visita la nostra pagina di assistenza.