Class: Gcloud::Vision::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/vision/project.rb

Overview

# Project

Google Cloud Vision allows easy integration of vision detection features within developer applications, including image labeling, face and landmark detection, optical character recognition (OCR), and tagging of explicit content.

See Gcloud#vision

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

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

annotation = vision.annotate image, labels: true

annotation.labels.map &:description
#=> ["stone carving", "ancient history", "statue", "sculpture",
#=>  "monument", "landmark"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ Project



56
57
58
# File 'lib/gcloud/vision/project.rb', line 56

def initialize service
  @service = service
end

Instance Attribute Details

#serviceObject



52
53
54
# File 'lib/gcloud/vision/project.rb', line 52

def service
  @service
end

Class Method Details

.default_projectObject



77
78
79
80
81
82
# File 'lib/gcloud/vision/project.rb', line 77

def self.default_project
  ENV["VISION_PROJECT"] ||
    ENV["GCLOUD_PROJECT"] ||
    ENV["GOOGLE_CLOUD_PROJECT"] ||
    Gcloud::GCE.project_id
end

Instance Method Details

#annotate(*images, faces: false, landmarks: false, logos: false, labels: false, text: false, safe_search: false, properties: false) {|annotate| ... } ⇒ Annotation+ Also known as: mark, detect

Performs detection of Cloud Vision [features](cloud.google.com/vision/reference/rest/v1/images/annotate#Feature) on the given image(s). If no options for features are provided, all image detection features will be performed, with a default of ‘100` results for faces, landmarks, logos, and labels. If any feature option is provided, only the specified feature detections will be performed. Please review [Pricing](cloud.google.com/vision/docs/pricing) before use, as a separate charge is incurred for each feature performed on an image.

Cloud Vision sets upper limits on file size as well as on the total combined size of all images in a request. Reducing your file size can significantly improve throughput; however, be careful not to reduce image quality in the process. See [Best Practices - Image Sizing](cloud.google.com/vision/docs/image-best-practices#image_sizing) for current file size limits.

Examples:

With a single image:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

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

annotation = vision.annotate image, labels: true

annotation.labels.map &:description
#=> ["stone carving", "ancient history", "statue", "sculpture",
#=>  "monument", "landmark"]

With multiple images:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

face_image = vision.image "path/to/face.jpg"
landmark_image = vision.image "path/to/landmark.jpg"

annotations = vision.annotate face_image, landmark_image, labels: true

annotations[0].labels.count #=> 4
annotations[1].labels.count #=> 6

With multiple images and configurations passed in a block:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

face_image = vision.image "path/to/face.jpg"
landmark_image = vision.image "path/to/landmark.jpg"
text_image = vision.image "path/to/text.png"

annotations = vision.annotate do |annotate|
   annotate.annotate face_image, faces: true, labels: true
   annotate.annotate landmark_image, landmarks: true
   annotate.annotate text_image, text: true
end

annotations[0].faces.count #=> 1
annotations[0].labels.count #=> 4
annotations[1].landmarks.count #=> 1
annotations[2].text.words.count #=> 28

Maximum result values can also be provided:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

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

annotation = vision.annotate image, labels: 3

annotation.labels.map &:description
#=> ["stone carving", "ancient history", "statue"]

Yields:

Yield Parameters:

  • annotate (Annotate)

    the Annotate object

See Also:



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/gcloud/vision/project.rb', line 248

def annotate *images, faces: false, landmarks: false, logos: false,
             labels: false, text: false, safe_search: false,
             properties: false
  a = Annotate.new self
  a.annotate(*images, faces: faces, landmarks: landmarks, logos: logos,
                      labels: labels, text: text,
                      safe_search: safe_search, properties: properties)

  yield a if block_given?

  gapi = service.annotate a.requests
  annotations = Array(gapi.responses).map do |g|
    fail Error.from_error(g.error) if g.error
    Annotation.from_gapi g
  end
  return annotations.first if annotations.count == 1
  annotations
end

#image(source) ⇒ Image

Returns a new image from the given source.

Cloud Vision sets upper limits on file size as well as on the total combined size of all images in a request. Reducing your file size can significantly improve throughput; however, be careful not to reduce image quality in the process. See [Best Practices - Image Sizing](cloud.google.com/vision/docs/image-best-practices#image_sizing) for current file size limits.

Note that an object in Google Cloud Storage is a single entity; permissions affect only that object. “Directory permissions” do not exist (though default bucket permissions do exist). Make sure the code which performs your request has access to that image.

Examples:

With a Google Cloud Storage URI:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

image = vision.image "gs://bucket-name/path_to_image_object"

With a local file path:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

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

See Also:



125
126
127
128
# File 'lib/gcloud/vision/project.rb', line 125

def image source
  return source if source.is_a? Image
  Image.from_source source, self
end

#projectObject

The Vision project connected to.

Examples:

require "gcloud"

gcloud = Gcloud.new "my-todo-project",
                    "/path/to/keyfile.json"
vision = gcloud.vision

vision.project #=> "my-todo-project"


71
72
73
# File 'lib/gcloud/vision/project.rb', line 71

def project
  service.project
end