Class: Gcloud::Vision::Annotation::Text::Word

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/vision/annotation/text.rb

Overview

# Word

A word within a detected text (OCR). See Gcloud::Vision::Annotation::Text.

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

image = vision.image "path/to/text.png"
text = image.text

words = text.words
words.count #=> 28

word = words.first
word.text #=> "Google"
word.bounds.count #=> 4
word.bounds.first #=> #<Vertex (x: 13, y: 8)>

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWord

Returns a new instance of Word.



169
170
171
# File 'lib/gcloud/vision/annotation/text.rb', line 169

def initialize
  @gapi = {}
end

Instance Attribute Details

#gapiObject



165
166
167
# File 'lib/gcloud/vision/annotation/text.rb', line 165

def gapi
  @gapi
end

Class Method Details

.from_gapi(gapi) ⇒ Object



229
230
231
# File 'lib/gcloud/vision/annotation/text.rb', line 229

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

Instance Method Details

#boundsArray<Vertex>

The bounds of the word within the detected text.

Returns:



187
188
189
190
191
192
# File 'lib/gcloud/vision/annotation/text.rb', line 187

def bounds
  return [] unless @gapi.bounding_poly
  @bounds ||= Array(@gapi.bounding_poly.vertices).map do |v|
    Vertex.from_gapi v
  end
end

#inspectObject



223
224
225
# File 'lib/gcloud/vision/annotation/text.rb', line 223

def inspect
  format "#<Word text: %s, bounds: %i>", text.inspect, bounds.count
end

#textString

The text of the word.

Returns:

  • (String)


178
179
180
# File 'lib/gcloud/vision/annotation/text.rb', line 178

def text
  @gapi.description
end

#to_hHash

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

Returns:

  • (Hash)


199
200
201
# File 'lib/gcloud/vision/annotation/text.rb', line 199

def to_h
  to_hash
end

#to_hashHash

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

Returns:

  • (Hash)


208
209
210
# File 'lib/gcloud/vision/annotation/text.rb', line 208

def to_hash
  { text: text, bounds: bounds.map(&:to_h) }
end

#to_sObject



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

def to_s
  to_str
end

#to_strObject



218
219
220
# File 'lib/gcloud/vision/annotation/text.rb', line 218

def to_str
  text
end