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

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/vision/annotation.rb,
lib/google/cloud/vision/annotation/web.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/crop_hint.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: CropHint, Entity, Face, Properties, SafeSearch, Text, Vertex, Web

Instance Method Summary collapse

Instance Method Details

#crop_hintsArray<CropHint>

The results of crop hints detection.

Examples:

require "google/cloud/vision"

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

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

Returns:



415
416
417
418
419
420
421
# File 'lib/google/cloud/vision/annotation.rb', line 415

def crop_hints
  return [] unless @grpc.crop_hints_annotation
  grpc_crop_hints = @grpc.crop_hints_annotation.crop_hints
  @crop_hints ||= Array(grpc_crop_hints).map do |ch|
    CropHint.from_grpc ch
  end
end

#crop_hints?Boolean

Whether there is a result for crop hints detection.

Examples:

require "google/cloud/vision"

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

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

Returns:

  • (Boolean)


437
438
439
# File 'lib/google/cloud/vision/annotation.rb', line 437

def crop_hints?
  crop_hints.any?
end

#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:



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

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)


109
110
111
# File 'lib/google/cloud/vision/annotation.rb', line 109

def face?
  faces.any?
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:



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

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:



264
265
266
# File 'lib/google/cloud/vision/annotation.rb', line 264

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)


283
284
285
# File 'lib/google/cloud/vision/annotation.rb', line 283

def label?
  labels.any?
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:



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

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:



148
149
150
# File 'lib/google/cloud/vision/annotation.rb', line 148

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)


167
168
169
# File 'lib/google/cloud/vision/annotation.rb', line 167

def landmark?
  landmarks.any?
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:



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

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:



206
207
208
# File 'lib/google/cloud/vision/annotation.rb', line 206

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)


225
226
227
# File 'lib/google/cloud/vision/annotation.rb', line 225

def logo?
  logos.any?
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:



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

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:



376
377
378
379
380
381
# File 'lib/google/cloud/vision/annotation.rb', line 376

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)


397
398
399
# File 'lib/google/cloud/vision/annotation.rb', line 397

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:



338
339
340
341
# File 'lib/google/cloud/vision/annotation.rb', line 338

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)


358
359
360
# File 'lib/google/cloud/vision/annotation.rb', line 358

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:



301
302
303
304
# File 'lib/google/cloud/vision/annotation.rb', line 301

def text
  @text ||= \
    Text.from_grpc(@grpc.text_annotations, @grpc.full_text_annotation)
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)


320
321
322
# File 'lib/google/cloud/vision/annotation.rb', line 320

def text?
  !text.nil?
end

#to_hHash

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

Returns:

  • (Hash)


483
484
485
486
487
488
489
# File 'lib/google/cloud/vision/annotation.rb', line 483

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.to_h, safe_search: safe_search.to_h,
    properties: properties.to_h, crop_hints: crop_hints.map(&:to_h),
    web: web.to_h }
end

#webWeb

The results of web detection.

Examples:

require "google/cloud/vision"

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

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

Returns:



455
456
457
458
# File 'lib/google/cloud/vision/annotation.rb', line 455

def web
  return nil unless @grpc.web_detection
  @web ||= Web.from_grpc(@grpc.web_detection)
end

#web?Boolean

Whether there is a result for web detection.

Examples:

require "google/cloud/vision"

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

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

Returns:

  • (Boolean)


474
475
476
# File 'lib/google/cloud/vision/annotation.rb', line 474

def web?
  !web.nil?
end