Class: Gcloud::Vision::Image

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

Overview

# Image

Represents an image for the Vision service.

See Project#image.

The Cloud Vision API supports a variety of image file formats, including JPEG, PNG8, PNG24, Animated GIF (first frame only), and RAW. See [Best Practices - Image Types](cloud.google.com/vision/docs/image-best-practices#image_types) for the list of formats. Be aware that Cloud Vision sets upper limits on file size as well as 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:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

image = vision.image "path/to/text.png"

image.context.languages = ["en"]

text = image.text
text.words.count #=> 28

See Also:

Defined Under Namespace

Classes: Context

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImage

Returns a new instance of Image.



64
65
66
67
68
69
# File 'lib/gcloud/vision/image.rb', line 64

def initialize
  @io = nil
  @url = nil
  @vision = nil
  @context = Context.new
end

Instance Attribute Details

#contextContext (readonly)

Returns the image context for the image, which accepts metadata values such as location and language hints.

Returns:

  • (Context)

    The context instance for the image.



60
61
62
# File 'lib/gcloud/vision/image.rb', line 60

def context
  @context
end

Class Method Details

.from_io(io, vision) ⇒ Object



390
391
392
393
394
395
396
397
398
# File 'lib/gcloud/vision/image.rb', line 390

def self.from_io io, vision
  if !io.respond_to?(:read) && !io.respond_to?(:rewind)
    fail ArgumentError, "Cannot create an Image without an IO object"
  end
  new.tap do |i|
    i.instance_variable_set :@io, io
    i.instance_variable_set :@vision, vision
  end
end

.from_source(source, vision = nil) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/gcloud/vision/image.rb', line 368

def self.from_source source, vision = nil
  if source.respond_to?(:read) && source.respond_to?(:rewind)
    return from_io(source, vision)
  end
  # Convert Storage::File objects to the URL
  source = source.to_gs_url if source.respond_to? :to_gs_url
  # Everything should be a string from now on
  source = String source
  # Create an Image from the Google Storage URL
  return from_url(source, vision) if source.start_with? "gs://"
  # Create an image from a file on the filesystem
  if File.file? source
    unless File.readable? source
      fail ArgumentError, "Cannot read #{source}"
    end
    return from_io(File.open(source, "rb"), vision)
  end
  fail ArgumentError, "Unable to convert #{source} to an Image"
end

.from_url(url, vision) ⇒ Object



402
403
404
405
406
407
408
409
410
411
# File 'lib/gcloud/vision/image.rb', line 402

def self.from_url url, vision
  url = String url
  unless url.start_with? "gs://"
    fail ArgumentError, "Cannot create an Image without a Storage URL"
  end
  new.tap do |i|
    i.instance_variable_set :@url, url
    i.instance_variable_set :@vision, vision
  end
end

Instance Method Details

#faceAnnotation::Face

Performs the ‘FACE_DETECTION` feature on the image and returns only the first result.

Returns:



129
130
131
# File 'lib/gcloud/vision/image.rb', line 129

def face
  faces(1).first
end

#faces(max_results = Gcloud::Vision.default_max_faces) ⇒ Array<Annotation::Face>

Performs the ‘FACE_DETECTION` feature on the image.

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision
image = vision.image "path/to/face.jpg"

faces = image.faces

face = faces.first
face.bounds.face.count #=> 4
face.bounds.face.first #=> #<Vertex (x: 153, y: 34)>

Parameters:

  • max_results (Integer) (defaults to: Gcloud::Vision.default_max_faces)

    The maximum number of results. The default is Gcloud::Vision.default_max_faces. Optional.

Returns:

See Also:



117
118
119
120
121
# File 'lib/gcloud/vision/image.rb', line 117

def faces max_results = Gcloud::Vision.default_max_faces
  ensure_vision!
  annotation = @vision.mark self, faces: max_results
  annotation.faces
end

#inspectObject



349
350
351
# File 'lib/gcloud/vision/image.rb', line 349

def inspect
  "#<#{self.class.name} #{self}>"
end

#io?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/gcloud/vision/image.rb', line 74

def io?
  !@io.nil?
end

#labelAnnotation::Entity

Performs the ‘LABEL_DETECTION` feature on the image and returns only the first result.

Returns:



250
251
252
# File 'lib/gcloud/vision/image.rb', line 250

def label
  labels(1).first
end

#labels(max_results = Gcloud::Vision.default_max_labels) ⇒ Array<Annotation::Entity>

Performs the ‘LABEL_DETECTION` feature on the image.

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision
image = vision.image "path/to/face.jpg"

labels = image.labels

labels.count #=> 4
label = labels.first
label.score #=> 0.9481349
label.description #=> "person"
label.mid #=> "/m/01g317"

Parameters:

  • max_results (Integer) (defaults to: Gcloud::Vision.default_max_labels)

    The maximum number of results. The default is Gcloud::Vision.default_max_labels. Optional.

Returns:

See Also:



238
239
240
241
242
# File 'lib/gcloud/vision/image.rb', line 238

def labels max_results = Gcloud::Vision.default_max_labels
  ensure_vision!
  annotation = @vision.mark self, labels: max_results
  annotation.labels
end

#landmarkAnnotation::Entity

Performs the ‘LANDMARK_DETECTION` feature on the image and returns only the first result.

Returns:



169
170
171
# File 'lib/gcloud/vision/image.rb', line 169

def landmark
  landmarks(1).first
end

#landmarks(max_results = Gcloud::Vision.default_max_landmarks) ⇒ Array<Annotation::Entity>

Performs the ‘LANDMARK_DETECTION` feature on the image.

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision
image = vision.image "path/to/landmark.jpg"

landmarks = image.landmarks

landmark = landmarks.first
landmark.score #=> 0.91912264
landmark.description #=> "Mount Rushmore"
landmark.mid #=> "/m/019dvv"

Parameters:

Returns:

See Also:



157
158
159
160
161
# File 'lib/gcloud/vision/image.rb', line 157

def landmarks max_results = Gcloud::Vision.default_max_landmarks
  ensure_vision!
  annotation = @vision.mark self, landmarks: max_results
  annotation.landmarks
end

#logoAnnotation::Entity

Performs the ‘LOGO_DETECTION` feature on the image and returns only the first result.

Returns:



209
210
211
# File 'lib/gcloud/vision/image.rb', line 209

def 
  logos(1).first
end

#logos(max_results = Gcloud::Vision.default_max_logos) ⇒ Array<Annotation::Entity>

Performs the ‘LOGO_DETECTION` feature on the image.

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision
image = vision.image "path/to/logo.jpg"

logos = image.logos

 = logos.first
.score #=> 0.70057315
.description #=> "Google"
.mid #=> "/m/0b34hf"

Parameters:

  • max_results (Integer) (defaults to: Gcloud::Vision.default_max_logos)

    The maximum number of results. The default is Gcloud::Vision.default_max_logos. Optional.

Returns:

See Also:



197
198
199
200
201
# File 'lib/gcloud/vision/image.rb', line 197

def logos max_results = Gcloud::Vision.default_max_logos
  ensure_vision!
  annotation = @vision.mark self, logos: max_results
  annotation.logos
end

#propertiesAnnotation::Properties

Performs the ‘IMAGE_PROPERTIES` feature on the image.

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision
image = vision.image "path/to/logo.jpg"

properties = image.properties

properties.colors.count #=> 10
color = properties.colors.first
color.red #=> 247.0
color.green #=> 236.0
color.blue #=> 20.0

Returns:

See Also:



330
331
332
333
334
# File 'lib/gcloud/vision/image.rb', line 330

def properties
  ensure_vision!
  annotation = @vision.mark self, properties: true
  annotation.properties
end

#safe_searchAnnotation::SafeSearch

Performs the ‘SAFE_SEARCH_DETECTION` feature on the image.

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision
image = vision.image "path/to/face.jpg"

safe_search = image.safe_search

safe_search.spoof? #=> false
safe_search.spoof #=> "VERY_UNLIKELY"

Returns:

See Also:



301
302
303
304
305
# File 'lib/gcloud/vision/image.rb', line 301

def safe_search
  ensure_vision!
  annotation = @vision.mark self, safe_search: true
  annotation.safe_search
end

#textAnnotation::Text

Performs the ‘TEXT_DETECTION` (OCR) feature on the image.

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision
image = vision.image "path/to/text.png"

text = image.text

text = image.text
text.locale #=> "en"
text.words.count #=> 28
text.text
#=> "Google Cloud Client Library for Ruby an idiomatic, intuitive... "

Returns:

See Also:



276
277
278
279
280
# File 'lib/gcloud/vision/image.rb', line 276

def text
  ensure_vision!
  annotation = @vision.mark self, text: true
  annotation.text
end

#to_gapiObject



355
356
357
358
359
360
361
362
363
364
# File 'lib/gcloud/vision/image.rb', line 355

def to_gapi
  if io?
    @io.rewind
    Google::Apis::VisionV1::Image.new content: @io.read
  elsif url?
    Google::Apis::VisionV1::Image.new source: { gcsImageUri: @url }
  else
    fail ArgumentError, "Unable to use Image with Vision service."
  end
end

#to_sObject



337
338
339
340
341
342
343
344
345
346
# File 'lib/gcloud/vision/image.rb', line 337

def to_s
  @to_s ||= begin
    if io?
      @io.rewind
      "(#{@io.read(16)}...)"
    else
      "(#{url})"
    end
  end
end

#urlString

Returns:

  • (String)


90
91
92
# File 'lib/gcloud/vision/image.rb', line 90

def url
  @url
end

#url?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/gcloud/vision/image.rb', line 81

def url?
  !@url.nil?
end