Class: Gcloud::Vision::Image
- Inherits:
-
Object
- Object
- Gcloud::Vision::Image
- 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.
Defined Under Namespace
Classes: Context
Instance Attribute Summary collapse
-
#context ⇒ Context
readonly
Returns the image context for the image, which accepts metadata values such as location and language hints.
Class Method Summary collapse
- .from_io(io, vision) ⇒ Object
- .from_source(source, vision = nil) ⇒ Object
- .from_url(url, vision) ⇒ Object
Instance Method Summary collapse
-
#face ⇒ Annotation::Face
Performs the
FACE_DETECTIONfeature on the image and returns only the first result. -
#faces(max_results = Gcloud::Vision.default_max_faces) ⇒ Array<Annotation::Face>
Performs the
FACE_DETECTIONfeature on the image. -
#initialize ⇒ Image
constructor
A new instance of Image.
- #inspect ⇒ Object
- #io? ⇒ Boolean
-
#label ⇒ Annotation::Entity
Performs the
LABEL_DETECTIONfeature on the image and returns only the first result. -
#labels(max_results = Gcloud::Vision.default_max_labels) ⇒ Array<Annotation::Entity>
Performs the
LABEL_DETECTIONfeature on the image. -
#landmark ⇒ Annotation::Entity
Performs the
LANDMARK_DETECTIONfeature on the image and returns only the first result. -
#landmarks(max_results = Gcloud::Vision.default_max_landmarks) ⇒ Array<Annotation::Entity>
Performs the
LANDMARK_DETECTIONfeature on the image. -
#logo ⇒ Annotation::Entity
Performs the
LOGO_DETECTIONfeature on the image and returns only the first result. -
#logos(max_results = Gcloud::Vision.default_max_logos) ⇒ Array<Annotation::Entity>
Performs the
LOGO_DETECTIONfeature on the image. -
#properties ⇒ Annotation::Properties
Performs the
IMAGE_PROPERTIESfeature on the image. -
#safe_search ⇒ Annotation::SafeSearch
Performs the
SAFE_SEARCH_DETECTIONfeature on the image. -
#text ⇒ Annotation::Text
Performs the
TEXT_DETECTION(OCR) feature on the image. - #to_gapi ⇒ Object
- #to_s ⇒ Object
- #url ⇒ String
- #url? ⇒ Boolean
Constructor Details
Instance Attribute Details
#context ⇒ Context (readonly)
Returns the image context for the image, which accepts metadata values such as location and language hints.
59 60 61 |
# File 'lib/gcloud/vision/image.rb', line 59 def context @context end |
Class Method Details
.from_io(io, vision) ⇒ Object
393 394 395 396 397 398 399 400 401 |
# File 'lib/gcloud/vision/image.rb', line 393 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
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/gcloud/vision/image.rb', line 371 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
405 406 407 408 409 410 411 412 413 414 |
# File 'lib/gcloud/vision/image.rb', line 405 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
#face ⇒ Annotation::Face
Performs the FACE_DETECTION feature on the image and returns only the first result.
132 133 134 |
# File 'lib/gcloud/vision/image.rb', line 132 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.
120 121 122 123 124 |
# File 'lib/gcloud/vision/image.rb', line 120 def faces max_results = Gcloud::Vision.default_max_faces ensure_vision! annotation = @vision.mark self, faces: max_results annotation.faces end |
#inspect ⇒ Object
352 353 354 |
# File 'lib/gcloud/vision/image.rb', line 352 def inspect "#<#{self.class.name} #{self}>" end |
#io? ⇒ Boolean
75 76 77 |
# File 'lib/gcloud/vision/image.rb', line 75 def io? !@io.nil? end |
#label ⇒ Annotation::Entity
Performs the LABEL_DETECTION feature on the image and returns only the first result.
253 254 255 |
# File 'lib/gcloud/vision/image.rb', line 253 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.
241 242 243 244 245 |
# File 'lib/gcloud/vision/image.rb', line 241 def labels max_results = Gcloud::Vision.default_max_labels ensure_vision! annotation = @vision.mark self, labels: max_results annotation.labels end |
#landmark ⇒ Annotation::Entity
Performs the LANDMARK_DETECTION feature on the image and returns only the first result.
172 173 174 |
# File 'lib/gcloud/vision/image.rb', line 172 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.
160 161 162 163 164 |
# File 'lib/gcloud/vision/image.rb', line 160 def landmarks max_results = Gcloud::Vision.default_max_landmarks ensure_vision! annotation = @vision.mark self, landmarks: max_results annotation.landmarks end |
#logo ⇒ Annotation::Entity
Performs the LOGO_DETECTION feature on the image and returns only the first result.
212 213 214 |
# File 'lib/gcloud/vision/image.rb', line 212 def logo logos(1).first end |
#logos(max_results = Gcloud::Vision.default_max_logos) ⇒ Array<Annotation::Entity>
Performs the LOGO_DETECTION feature on the image.
200 201 202 203 204 |
# File 'lib/gcloud/vision/image.rb', line 200 def logos max_results = Gcloud::Vision.default_max_logos ensure_vision! annotation = @vision.mark self, logos: max_results annotation.logos end |
#properties ⇒ Annotation::Properties
Performs the IMAGE_PROPERTIES feature on the image.
333 334 335 336 337 |
# File 'lib/gcloud/vision/image.rb', line 333 def properties ensure_vision! annotation = @vision.mark self, properties: true annotation.properties end |
#safe_search ⇒ Annotation::SafeSearch
Performs the SAFE_SEARCH_DETECTION feature on the image.
304 305 306 307 308 |
# File 'lib/gcloud/vision/image.rb', line 304 def safe_search ensure_vision! annotation = @vision.mark self, safe_search: true annotation.safe_search end |
#text ⇒ Annotation::Text
Performs the TEXT_DETECTION (OCR) feature on the image.
279 280 281 282 283 |
# File 'lib/gcloud/vision/image.rb', line 279 def text ensure_vision! annotation = @vision.mark self, text: true annotation.text end |
#to_gapi ⇒ Object
358 359 360 361 362 363 364 365 366 367 |
# File 'lib/gcloud/vision/image.rb', line 358 def to_gapi if io? @io.rewind { content: Base64.strict_encode64(@io.read) } elsif url? { source: { gcsImageUri: @url } } else fail ArgumentError, "Unable to use Image with Vision service." end end |
#to_s ⇒ Object
340 341 342 343 344 345 346 347 348 349 |
# File 'lib/gcloud/vision/image.rb', line 340 def to_s @to_s ||= begin if io? @io.rewind "(#{@io.read(16)}...)" else "(#{url})" end end end |
#url ⇒ String
93 94 95 |
# File 'lib/gcloud/vision/image.rb', line 93 def url @url end |
#url? ⇒ Boolean
84 85 86 |
# File 'lib/gcloud/vision/image.rb', line 84 def url? !@url.nil? end |