Class: Gcloud::Vision::Annotation

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/vision/annotation.rb,
lib/gcloud/vision/annotation/face.rb,
lib/gcloud/vision/annotation/text.rb,
lib/gcloud/vision/annotation/entity.rb,
lib/gcloud/vision/annotation/vertex.rb,
lib/gcloud/vision/annotation/properties.rb,
lib/gcloud/vision/annotation/safe_search.rb

Overview

# Annotation

The results of all requested image annotations.

See Project#annotate and Image.

Examples:

require "gcloud"

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

annotation = vision.annotate image, faces: true, labels: true
annotation.faces.count #=> 1
annotation.labels.count #=> 4
annotation.text #=> nil

Defined Under Namespace

Classes: Entity, Face, Properties, SafeSearch, Text, Vertex

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAnnotation

Returns a new instance of Annotation.



50
51
52
# File 'lib/gcloud/vision/annotation.rb', line 50

def initialize
  @gapi = nil
end

Instance Attribute Details

#gapiObject



46
47
48
# File 'lib/gcloud/vision/annotation.rb', line 46

def gapi
  @gapi
end

Class Method Details

.from_gapi(gapi) ⇒ Object



450
451
452
# File 'lib/gcloud/vision/annotation.rb', line 450

def self.from_gapi gapi
  new.tap { |a| a.instance_variable_set :@gapi, gapi }
end

Instance Method Details

#faceFace

The first face result, if there is one.

Examples:

require "gcloud"

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

annotation = vision.annotate image, faces: 1
face = annotation.face

Returns:



91
92
93
# File 'lib/gcloud/vision/annotation.rb', line 91

def face
  faces.first
end

#face?Boolean

Whether there is at least one result from face detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, faces: 1
annotation.face? #=> true

Returns:

  • (Boolean)


110
111
112
# File 'lib/gcloud/vision/annotation.rb', line 110

def face?
  faces.count > 0
end

#facesArray<Face>

The results of face detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, faces: true
annotation.faces.count #=> 1
face = annotation.faces.first

Returns:



70
71
72
73
74
# File 'lib/gcloud/vision/annotation.rb', line 70

def faces
  @faces ||= Array(@gapi["faceAnnotations"]).map do |fa|
    Face.from_gapi fa
  end
end

#inspectObject



444
445
446
# File 'lib/gcloud/vision/annotation.rb', line 444

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

#labelEntity

The first label result, if there is one.

Examples:

require "gcloud"

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

annotation = vision.annotate image, labels: 1
label = annotation.label

Returns:



273
274
275
# File 'lib/gcloud/vision/annotation.rb', line 273

def label
  labels.first
end

#label?Boolean

Whether there is at least one result from label detection. detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, labels: 1
annotation.label? #=> true

Returns:

  • (Boolean)


293
294
295
# File 'lib/gcloud/vision/annotation.rb', line 293

def label?
  labels.count > 0
end

#labelsArray<Entity>

The results of label detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, labels: 1
annotation.labels.count #=> 1
label = annotation.labels.first

Returns:



252
253
254
255
256
# File 'lib/gcloud/vision/annotation.rb', line 252

def labels
  @labels ||= Array(@gapi["labelAnnotations"]).map do |lb|
    Entity.from_gapi lb
  end
end

#landmarkEntity

The first landmark result, if there is one.

Examples:

require "gcloud"

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

annotation = vision.annotate image, landmarks: 1
landmark = annotation.landmark

Returns:



151
152
153
# File 'lib/gcloud/vision/annotation.rb', line 151

def landmark
  landmarks.first
end

#landmark?Boolean

Whether there is at least one result from landmark detection. detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, landmarks: 1
annotation.landmark? #=> true

Returns:

  • (Boolean)


171
172
173
# File 'lib/gcloud/vision/annotation.rb', line 171

def landmark?
  landmarks.count > 0
end

#landmarksArray<Entity>

The results of landmark detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, landmarks: 1
annotation.landmarks.count #=> 1
landmark = annotation.landmarks.first

Returns:



130
131
132
133
134
# File 'lib/gcloud/vision/annotation.rb', line 130

def landmarks
  @landmarks ||= Array(@gapi["landmarkAnnotations"]).map do |lm|
    Entity.from_gapi lm
  end
end

#logoEntity

The first logo result, if there is one.

Examples:

require "gcloud"

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

annotation = vision.annotate image, logos: 1
 = annotation.

Returns:



212
213
214
# File 'lib/gcloud/vision/annotation.rb', line 212

def 
  logos.first
end

#logo?Boolean

Whether there is at least one result from logo detection. detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, logos: 1
annotation.logo? #=> true

Returns:

  • (Boolean)


232
233
234
# File 'lib/gcloud/vision/annotation.rb', line 232

def logo?
  logos.count > 0
end

#logosArray<Entity>

The results of logo detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, logos: 1
annotation.logos.count #=> 1
 = annotation.logos.first

Returns:



191
192
193
194
195
# File 'lib/gcloud/vision/annotation.rb', line 191

def logos
  @logos ||= Array(@gapi["logoAnnotations"]).map do |lg|
    Entity.from_gapi lg
  end
end

#propertiesProperties

The results of properties detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, properties: true
properties = annotation.properties

Returns:



390
391
392
393
# File 'lib/gcloud/vision/annotation.rb', line 390

def properties
  return nil unless @gapi["imagePropertiesAnnotation"]
  @properties ||= Properties.from_gapi(@gapi["imagePropertiesAnnotation"])
end

#properties?Boolean

Whether there is a result for properties detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, properties: true
annotation.properties? #=> true

Returns:

  • (Boolean)


410
411
412
# File 'lib/gcloud/vision/annotation.rb', line 410

def properties?
  !properties.nil?
end

#safe_searchSafeSearch

The results of safe_search detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, safe_search: true
safe_search = annotation.safe_search

Returns:



350
351
352
353
# File 'lib/gcloud/vision/annotation.rb', line 350

def safe_search
  return nil unless @gapi["safeSearchAnnotation"]
  @safe_search ||= SafeSearch.from_gapi(@gapi["safeSearchAnnotation"])
end

#safe_search?Boolean

Whether there is a result for safe_search detection. detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, safe_search: true
annotation.safe_search? #=> true

Returns:

  • (Boolean)


371
372
373
# File 'lib/gcloud/vision/annotation.rb', line 371

def safe_search?
  !safe_search.nil?
end

#textText

The results of text (OCR) detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, text: true
text = annotation.text

Returns:



312
313
314
# File 'lib/gcloud/vision/annotation.rb', line 312

def text
  @text ||= Text.from_gapi(@gapi["textAnnotations"])
end

#text?Boolean

Whether there is a result from text (OCR) detection.

Examples:

require "gcloud"

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

annotation = vision.annotate image, text: true
annotation.text? #=> true

Returns:

  • (Boolean)


331
332
333
# File 'lib/gcloud/vision/annotation.rb', line 331

def text?
  !text.nil?
end

#to_hHash

Deeply converts object to a hash. All keys will be symbolized.

Returns:

  • (Hash)


419
420
421
# File 'lib/gcloud/vision/annotation.rb', line 419

def to_h
  to_hash
end

#to_hashHash

Deeply converts object to a hash. All keys will be symbolized.

Returns:

  • (Hash)


428
429
430
431
432
433
# File 'lib/gcloud/vision/annotation.rb', line 428

def to_hash
  { faces: faces.map(&:to_h), landmarks: landmarks.map(&:to_h),
    logos: logos.map(&:to_h), labels: labels.map(&:to_h),
    text: text.map(&:to_h), safe_search: safe_search.to_h,
    properties: properties.to_h }
end

#to_sObject



436
437
438
439
440
441
# File 'lib/gcloud/vision/annotation.rb', line 436

def to_s
  tmplt = "(faces: %i, landmarks: %i, logos: %i, labels: %i, text: %s," \
          " safe_search: %s, properties: %s)"
  format tmplt, faces.count, landmarks.count, logos.count, labels.count,
         text?, safe_search?, properties?
end