Class: Google::Cloud::Vision::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/vision/image.rb,
lib/google/cloud/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 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 for current file size limits.

Examples:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

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

Instance Method Summary collapse

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.



61
62
63
# File 'lib/google/cloud/vision/image.rb', line 61

def context
  @context
end

Instance Method Details

#faceAnnotation::Face

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

Returns:



122
123
124
# File 'lib/google/cloud/vision/image.rb', line 122

def face
  faces(1).first
end

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

Performs the FACE_DETECTION feature on the image.

Examples:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new
image = vision.image "path/to/face.jpg"

faces = image.faces

face = faces.first
face.bounds.face.count #=> 4
vertex = face.bounds.face.first
vertex.x #=> 28
vertex.y #=> 40

Parameters:

Returns:

See Also:



110
111
112
113
114
# File 'lib/google/cloud/vision/image.rb', line 110

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

#labelAnnotation::Entity

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

Returns:



240
241
242
# File 'lib/google/cloud/vision/image.rb', line 240

def label
  labels(1).first
end

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

Performs the LABEL_DETECTION feature on the image.

Examples:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new
image = vision.image "path/to/landmark.jpg"

labels = image.labels

labels.count #=> 4
label = labels.first
label.score #=> 0.9481348991394043
label.description #=> "stone carving"
label.mid #=> "/m/02wtjj"

Parameters:

Returns:

See Also:



228
229
230
231
232
# File 'lib/google/cloud/vision/image.rb', line 228

def labels max_results = Google::Cloud::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:



161
162
163
# File 'lib/google/cloud/vision/image.rb', line 161

def landmark
  landmarks(1).first
end

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

Performs the LANDMARK_DETECTION feature on the image.

Examples:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new
image = vision.image "path/to/landmark.jpg"

landmarks = image.landmarks

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

Parameters:

Returns:

See Also:



149
150
151
152
153
# File 'lib/google/cloud/vision/image.rb', line 149

def landmarks max_results = Google::Cloud::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:



200
201
202
# File 'lib/google/cloud/vision/image.rb', line 200

def 
  logos(1).first
end

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

Performs the LOGO_DETECTION feature on the image.

Examples:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new
image = vision.image "path/to/logo.jpg"

logos = image.logos

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

Parameters:

Returns:

See Also:



188
189
190
191
192
# File 'lib/google/cloud/vision/image.rb', line 188

def logos max_results = Google::Cloud::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 "google/cloud/vision"

vision = Google::Cloud::Vision.new
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:



316
317
318
319
320
# File 'lib/google/cloud/vision/image.rb', line 316

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 "google/cloud/vision"

vision = Google::Cloud::Vision.new
image = vision.image "path/to/face.jpg"

safe_search = image.safe_search

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

Returns:

See Also:



288
289
290
291
292
# File 'lib/google/cloud/vision/image.rb', line 288

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 "google/cloud/vision"

vision = Google::Cloud::Vision.new
image = vision.image "path/to/text.png"

text = image.text

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

Returns:

See Also:



264
265
266
267
268
# File 'lib/google/cloud/vision/image.rb', line 264

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