Module: Google::Cloud::Vision

Defined in:
lib/google/cloud/vision.rb,
lib/google/cloud/vision/image.rb,
lib/google/cloud/vision/project.rb,
lib/google/cloud/vision/service.rb,
lib/google/cloud/vision/version.rb,
lib/google/cloud/vision/annotate.rb,
lib/google/cloud/vision/location.rb,
lib/google/cloud/vision/annotation.rb,
lib/google/cloud/vision/credentials.rb,
lib/google/cloud/vision/annotation/web.rb,
lib/google/cloud/vision/v1/geometry_pb.rb,
lib/google/cloud/vision/annotation/face.rb,
lib/google/cloud/vision/annotation/text.rb,
lib/google/cloud/vision/v1/doc/overview.rb,
lib/google/cloud/vision/annotation/entity.rb,
lib/google/cloud/vision/annotation/vertex.rb,
lib/google/cloud/vision/v1/web_detection_pb.rb,
lib/google/cloud/vision/annotation/crop_hint.rb,
lib/google/cloud/vision/annotation/properties.rb,
lib/google/cloud/vision/v1/image_annotator_pb.rb,
lib/google/cloud/vision/v1/text_annotation_pb.rb,
lib/google/cloud/vision/annotation/safe_search.rb,
lib/google/cloud/vision/v1/image_annotator_client.rb,
lib/google/cloud/vision/v1/image_annotator_services_pb.rb,
lib/google/cloud/vision/v1/doc/google/cloud/vision/v1/geometry.rb,
lib/google/cloud/vision/v1/doc/google/cloud/vision/v1/web_detection.rb,
lib/google/cloud/vision/v1/doc/google/cloud/vision/v1/image_annotator.rb,
lib/google/cloud/vision/v1/doc/google/cloud/vision/v1/text_annotation.rb

Overview

Ruby Client for Google Cloud Vision API (Alpha)

Google Cloud Vision API: Integrates Google Vision features, including image labeling, face, logo, and landmark detection, optical character recognition (OCR), and detection of explicit content, into applications.

Quick Start

In order to use this library, you first need to go through the following steps:

  1. Select or create a Cloud Platform project.
  2. Enable the Google Cloud Vision API.
  3. Setup Authentication.

Installation

$ gem install google-cloud-vision

Preview

ImageAnnotatorClient

require "google/cloud/vision"

image_annotator_client = Google::Cloud::Vision.new
gcs_image_uri = "gs://gapic-toolkit/President_Barack_Obama.jpg"
source = { gcs_image_uri: gcs_image_uri }
image = { source: source }
type = :FACE_DETECTION
features_element = { type: type }
features = [features_element]
requests_element = { image: image, features: features }
requests = [requests_element]
response = image_annotator_client.batch_annotate_images(requests)

Next Steps

Defined Under Namespace

Modules: V1 Classes: Annotate, Annotation, Credentials, Image, Location, Project

Constant Summary collapse

VERSION =
"0.27.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_max_crop_hintsObject

The default max results to return for crop hints detection requests. This is used on Google::Cloud::Vision::Project#annotate as well as Google::Cloud::Vision::Image#crop_hints.

The default value is 100.

Examples:

Using the default setting on Google::Cloud::Vision::Project#annotate:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

Google::Cloud::Vision.default_max_crop_hints #=> 100

img = "path/to/landmarks.jpg"
annotation = vision.annotate img, crop_hints: true
# This is the same as calling
# annotation = vision.annotate img, crop_hints: 100

Updating the default setting on Google::Cloud::Vision::Project#annotate:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

# Set a new default
Google::Cloud::Vision.default_max_crop_hints = 5

img = "path/to/landmarks.jpg"
annotation = vision.annotate img, crop_hints: true
# This is the same as calling
# annotation = vision.annotate img, crop_hints: 5

Using the default setting on Google::Cloud::Vision::Image#crop_hints:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

Google::Cloud::Vision.default_max_crop_hints #=> 100

crop_hints = vision.image("path/to/landmarks.jpg").crop_hints
# This is the same as calling
# crop_hints = vision.image("path/to/landmarks.jpg").crop_hints 100

Updating the default setting on Google::Cloud::Vision::Image#crop_hints:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

# Set a new default
Google::Cloud::Vision.default_max_crop_hints = 5

crop_hints = vision.image("path/to/landmarks.jpg").crop_hints
# This is the same as calling
# crop_hints = vision.image("path/to/landmarks.jpg").crop_hints 5


500
501
502
# File 'lib/google/cloud/vision.rb', line 500

def default_max_crop_hints
  @default_max_crop_hints
end

.default_max_facesObject

The default max results to return for facial detection requests. This is used on Google::Cloud::Vision::Project#annotate as well as Google::Cloud::Vision::Image#faces.

The default value is 100.

Examples:

Using the default setting on Google::Cloud::Vision::Project#annotate:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

Google::Cloud::Vision.default_max_faces #=> 100

annotation = vision.annotate "path/to/faces.jpg", faces: true
# This is the same as calling
# annotation = vision.annotate "path/to/faces.jpg", faces: 100

Updating the default setting on Google::Cloud::Vision::Project#annotate:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

# Set a new default
Google::Cloud::Vision.default_max_faces = 5

annotation = vision.annotate "path/to/faces.jpg", faces: true
# This is the same as calling
# annotation = vision.annotate "path/to/faces.jpg", faces: 5

Using the default setting on Google::Cloud::Vision::Image#faces:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

Google::Cloud::Vision.default_max_faces #=> 100

faces = vision.image("path/to/faces.jpg").faces
# This is the same as calling
# faces = vision.image("path/to/faces.jpg").faces 100

Updating the default setting on Google::Cloud::Vision::Image#faces:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

# Set a new default
Google::Cloud::Vision.default_max_faces = 5

faces = vision.image("path/to/faces.jpg").faces
# This is the same as calling
# faces = vision.image("path/to/faces.jpg").faces 5


276
277
278
# File 'lib/google/cloud/vision.rb', line 276

def default_max_faces
  @default_max_faces
end

.default_max_labelsObject

The default max results to return for label detection requests. This is used on Google::Cloud::Vision::Project#annotate as well as Google::Cloud::Vision::Image#labels.

The default value is 100.

Examples:

Using the default setting on Google::Cloud::Vision::Project#annotate:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

Google::Cloud::Vision.default_max_labels #=> 100

annotation = vision.annotate "path/to/labels.jpg", labels: true
# This is the same as calling
# annotation = vision.annotate "path/to/labels.jpg", labels: 100

Updating the default setting on Google::Cloud::Vision::Project#annotate:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

# Set a new default
Google::Cloud::Vision.default_max_labels = 5

annotation = vision.annotate "path/to/labels.jpg", labels: true
# This is the same as calling
# annotation = vision.annotate "path/to/labels.jpg", labels: 5

Using the default setting on Google::Cloud::Vision::Image#labels:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

Google::Cloud::Vision.default_max_labels #=> 100

labels = vision.image("path/to/labels.jpg").labels
# This is the same as calling
# labels = vision.image("path/to/labels.jpg").labels 100

Updating the default setting on Google::Cloud::Vision::Image#labels:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

# Set a new default
Google::Cloud::Vision.default_max_labels = 5

labels = vision.image("path/to/labels.jpg").labels
# This is the same as calling
# labels = vision.image("path/to/labels.jpg").labels 5


443
444
445
# File 'lib/google/cloud/vision.rb', line 443

def default_max_labels
  @default_max_labels
end

.default_max_landmarksObject

The default max results to return for landmark detection requests. This is used on Google::Cloud::Vision::Project#annotate as well as Google::Cloud::Vision::Image#landmarks.

The default value is 100.

Examples:

Using the default setting on Google::Cloud::Vision::Project#annotate:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

Google::Cloud::Vision.default_max_landmarks #=> 100

img = "path/to/landmarks.jpg"
annotation = vision.annotate img, landmarks: true
# This is the same as calling
# annotation = vision.annotate img, landmarks: 100

Updating the default setting on Google::Cloud::Vision::Project#annotate:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

# Set a new default
Google::Cloud::Vision.default_max_landmarks = 5

img = "path/to/landmarks.jpg"
annotation = vision.annotate img, landmarks: true
# This is the same as calling
# annotation = vision.annotate img, landmarks: 5

Using the default setting on Google::Cloud::Vision::Image#landmarks:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

Google::Cloud::Vision.default_max_landmarks #=> 100

landmarks = vision.image("path/to/landmarks.jpg").landmarks
# This is the same as calling
# landmarks = vision.image("path/to/landmarks.jpg").landmarks 100

Updating the default setting on Google::Cloud::Vision::Image#landmarks:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

# Set a new default
Google::Cloud::Vision.default_max_landmarks = 5

landmarks = vision.image("path/to/landmarks.jpg").landmarks
# This is the same as calling
# landmarks = vision.image("path/to/landmarks.jpg").landmarks 5


333
334
335
# File 'lib/google/cloud/vision.rb', line 333

def default_max_landmarks
  @default_max_landmarks
end

.default_max_logosObject

The default max results to return for logo detection requests. This is used on Google::Cloud::Vision::Project#annotate as well as Google::Cloud::Vision::Image#logos.

The default value is 100.

Examples:

Using the default setting on Google::Cloud::Vision::Project#annotate:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

Google::Cloud::Vision.default_max_logos #=> 100

annotation = vision.annotate "path/to/logos.jpg", logos: true
# This is the same as calling
# annotation = vision.annotate "path/to/logos.jpg", logos: 100

Updating the default setting on Google::Cloud::Vision::Project#annotate:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

# Set a new default
Google::Cloud::Vision.default_max_logos = 5

annotation = vision.annotate "path/to/logos.jpg", logos: true
# This is the same as calling
# annotation = vision.annotate "path/to/logos.jpg", logos: 5

Using the default setting on Google::Cloud::Vision::Image#logos:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

Google::Cloud::Vision.default_max_logos #=> 100

logos = vision.image("path/to/logos.jpg").logos
# This is the same as calling
# logos = vision.image("path/to/logos.jpg").logos 100

Updating the default setting on Google::Cloud::Vision::Image#logos:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

# Set a new default
Google::Cloud::Vision.default_max_logos = 5

logos = vision.image("path/to/logos.jpg").logos
# This is the same as calling
# logos = vision.image("path/to/logos.jpg").logos 5


388
389
390
# File 'lib/google/cloud/vision.rb', line 388

def default_max_logos
  @default_max_logos
end

.default_max_webObject

The default max results to return for web detection requests. This is used on Google::Cloud::Vision::Project#annotate as well as Google::Cloud::Vision::Image#web.

The default value is 100.

Examples:

Using the default setting on Google::Cloud::Vision::Project#annotate:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

Google::Cloud::Vision.default_max_web #=> 100

img = "path/to/landmarks.jpg"
annotation = vision.annotate img, web: true
# This is the same as calling
# annotation = vision.annotate img, web: 100

Updating the default setting on Google::Cloud::Vision::Project#annotate:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

# Set a new default
Google::Cloud::Vision.default_max_web = 5

img = "path/to/landmarks.jpg"
annotation = vision.annotate img, web: true
# This is the same as calling
# annotation = vision.annotate img, web: 5

Using the default setting on Google::Cloud::Vision::Image#web:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

Google::Cloud::Vision.default_max_web #=> 100

web = vision.image("path/to/landmarks.jpg").web
# This is the same as calling
# web = vision.image("path/to/landmarks.jpg").web 100

Updating the default setting on Google::Cloud::Vision::Image#web:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

# Set a new default
Google::Cloud::Vision.default_max_web = 5

web = vision.image("path/to/landmarks.jpg").web
# This is the same as calling
# web = vision.image("path/to/landmarks.jpg").web 5


557
558
559
# File 'lib/google/cloud/vision.rb', line 557

def default_max_web
  @default_max_web
end

Class Method Details

.new(project_id: nil, credentials: nil, scope: nil, timeout: nil, client_config: nil, project: nil, keyfile: nil) ⇒ Google::Cloud::Vision::Project

Creates a new object for connecting to the Vision service. Each call creates a new connection.

Examples:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

image = vision.image "path/to/landmark.jpg"

landmark = image.landmark
landmark.description #=> "Mount Rushmore"

Parameters:

  • project_id (String)

    Project identifier for the Vision service you are connecting to. If not present, the default project for the credentials is used.

  • credentials (String, Hash, Google::Auth::Credentials)

    The path to the keyfile as a String, the contents of the keyfile as a Hash, or a Google::Auth::Credentials object. (See Credentials)

  • scope (String, Array<String>)

    The OAuth 2.0 scopes controlling the set of resources and operations that the connection can access. See Using OAuth 2.0 to Access Google APIs.

    The default scope is:

    • https://www.googleapis.com/auth/cloud-platform
  • timeout (Integer)

    Default timeout to use in requests. Optional.

  • client_config (Hash)

    A hash of values to override the default behavior of the API client. Optional.

  • project (String)

    Alias for the project_id argument. Deprecated.

  • keyfile (String)

    Alias for the credentials argument. Deprecated.

Returns:



606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'lib/google/cloud/vision.rb', line 606

def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil,
             client_config: nil, project: nil, keyfile: nil
  project_id ||= (project || Vision::Project.default_project_id)
  project_id = project_id.to_s # Always cast to a string
  fail ArgumentError, "project_id is missing" if project_id.empty?

  credentials ||= (keyfile || Vision::Credentials.default(scope: scope))
  unless credentials.is_a? Google::Auth::Credentials
    credentials = Vision::Credentials.new credentials, scope: scope
  end

  Vision::Project.new(
    Vision::Service.new(
      project_id, credentials, timeout: timeout,
                               client_config: client_config))
end