Class: Google::Cloud::Vision::Annotation

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

Overview

Annotation

The results of all requested image annotations.

See Project#annotate and Image.

Examples:

require "google/cloud/vision"

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

Instance Method Details

#faceFace

The first face result, if there is one.

Examples:

require "google/cloud/vision"

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

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

Returns:



89
90
91
# File 'lib/google/cloud/vision/annotation.rb', line 89

def face
  faces.first
end

#face?Boolean

Whether there is at least one result from face detection.

Examples:

require "google/cloud/vision"

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

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

Returns:

  • (Boolean)


107
108
109
# File 'lib/google/cloud/vision/annotation.rb', line 107

def face?
  faces.count > 0
end

#facesArray<Face>

The results of face detection.

Examples:

require "google/cloud/vision"

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

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

Returns:



69
70
71
72
73
# File 'lib/google/cloud/vision/annotation.rb', line 69

def faces
  @faces ||= Array(@grpc.face_annotations).map do |fa|
    Face.from_grpc fa
  end
end

#labelEntity

The first label result, if there is one.

Examples:

require "google/cloud/vision"

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

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

Returns:



262
263
264
# File 'lib/google/cloud/vision/annotation.rb', line 262

def label
  labels.first
end

#label?Boolean

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

Examples:

require "google/cloud/vision"

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

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

Returns:

  • (Boolean)


281
282
283
# File 'lib/google/cloud/vision/annotation.rb', line 281

def label?
  labels.count > 0
end

#labelsArray<Entity>

The results of label detection.

Examples:

require "google/cloud/vision"

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

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

Returns:



242
243
244
245
246
# File 'lib/google/cloud/vision/annotation.rb', line 242

def labels
  @labels ||= Array(@grpc.label_annotations).map do |lb|
    Entity.from_grpc lb
  end
end

#landmarkEntity

The first landmark result, if there is one.

Examples:

require "google/cloud/vision"

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

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

Returns:



146
147
148
# File 'lib/google/cloud/vision/annotation.rb', line 146

def landmark
  landmarks.first
end

#landmark?Boolean

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

Examples:

require "google/cloud/vision"

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

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

Returns:

  • (Boolean)


165
166
167
# File 'lib/google/cloud/vision/annotation.rb', line 165

def landmark?
  landmarks.count > 0
end

#landmarksArray<Entity>

The results of landmark detection.

Examples:

require "google/cloud/vision"

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

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

Returns:



126
127
128
129
130
# File 'lib/google/cloud/vision/annotation.rb', line 126

def landmarks
  @landmarks ||= Array(@grpc.landmark_annotations).map do |lm|
    Entity.from_grpc lm
  end
end

#logoEntity

The first logo result, if there is one.

Examples:

require "google/cloud/vision"

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

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

Returns:



204
205
206
# File 'lib/google/cloud/vision/annotation.rb', line 204

def 
  logos.first
end

#logo?Boolean

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

Examples:

require "google/cloud/vision"

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

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

Returns:

  • (Boolean)


223
224
225
# File 'lib/google/cloud/vision/annotation.rb', line 223

def logo?
  logos.count > 0
end

#logosArray<Entity>

The results of logo detection.

Examples:

require "google/cloud/vision"

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

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

Returns:



184
185
186
187
188
# File 'lib/google/cloud/vision/annotation.rb', line 184

def logos
  @logos ||= Array(@grpc.logo_annotations).map do |lg|
    Entity.from_grpc lg
  end
end

#propertiesProperties

The results of properties detection.

Examples:

require "google/cloud/vision"

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

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

Returns:



373
374
375
376
377
# File 'lib/google/cloud/vision/annotation.rb', line 373

def properties
  return nil unless @grpc.image_properties_annotation
  @properties ||= Properties.from_grpc(
    @grpc.image_properties_annotation)
end

#properties?Boolean

Whether there is a result for properties detection.

Examples:

require "google/cloud/vision"

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

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

Returns:

  • (Boolean)


393
394
395
# File 'lib/google/cloud/vision/annotation.rb', line 393

def properties?
  !properties.nil?
end

#safe_searchSafeSearch

The results of safe_search detection.

Examples:

require "google/cloud/vision"

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

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

Returns:



335
336
337
338
# File 'lib/google/cloud/vision/annotation.rb', line 335

def safe_search
  return nil unless @grpc.safe_search_annotation
  @safe_search ||= SafeSearch.from_grpc(@grpc.safe_search_annotation)
end

#safe_search?Boolean

Whether there is a result for safe_search detection. detection.

Examples:

require "google/cloud/vision"

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

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

Returns:

  • (Boolean)


355
356
357
# File 'lib/google/cloud/vision/annotation.rb', line 355

def safe_search?
  !safe_search.nil?
end

#textText

The results of text (OCR) detection.

Examples:

require "google/cloud/vision"

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

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

Returns:



299
300
301
# File 'lib/google/cloud/vision/annotation.rb', line 299

def text
  @text ||= Text.from_grpc(@grpc.text_annotations)
end

#text?Boolean

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

Examples:

require "google/cloud/vision"

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

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

Returns:

  • (Boolean)


317
318
319
# File 'lib/google/cloud/vision/annotation.rb', line 317

def text?
  !text.nil?
end

#to_hHash

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

Returns:

  • (Hash)


402
403
404
405
406
407
# File 'lib/google/cloud/vision/annotation.rb', line 402

def to_h
  { 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