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

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

Overview

# Text

The result of text, or optical character recognition (OCR), detection.

Examples:

require "gcloud"

gcloud = Gcloud.new
vision = gcloud.vision

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

text = image.text
text.locale #=> "en"
text.words.count #=> 28
text.text
#=> "Google Cloud Client Library for Ruby an idiomatic, intuitive... "

Defined Under Namespace

Classes: Word

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeText

Returns a new instance of Text.



47
48
49
50
# File 'lib/gcloud/vision/annotation/text.rb', line 47

def initialize
  @gapi = {}
  @words = []
end

Instance Attribute Details

#gapiObject



43
44
45
# File 'lib/gcloud/vision/annotation/text.rb', line 43

def gapi
  @gapi
end

Class Method Details

.from_gapi(gapi_list) ⇒ Object

objects.



131
132
133
134
135
136
137
138
# File 'lib/gcloud/vision/annotation/text.rb', line 131

def self.from_gapi gapi_list
  text, *words = Array gapi_list
  return nil if text.nil?
  new.tap do |t|
    t.instance_variable_set :@gapi, text
    t.instance_variable_set :@words, words.map { |w| Word.from_gapi w }
  end
end

Instance Method Details

#boundsArray<Vertex>

The bounds for the detected text in the image.

Returns:



77
78
79
80
81
82
# File 'lib/gcloud/vision/annotation/text.rb', line 77

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

#inspectObject



123
124
125
126
# File 'lib/gcloud/vision/annotation/text.rb', line 123

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

#localeString

The language code detected for ‘text`.

Returns:



68
69
70
# File 'lib/gcloud/vision/annotation/text.rb', line 68

def locale
  @gapi.locale
end

#textString

The text detected in an image.

Returns:

  • (String)

    The entire text including newline characters.



57
58
59
# File 'lib/gcloud/vision/annotation/text.rb', line 57

def text
  @gapi.description
end

#to_hHash

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

Returns:

  • (Hash)


98
99
100
# File 'lib/gcloud/vision/annotation/text.rb', line 98

def to_h
  to_hash
end

#to_hashHash

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

Returns:

  • (Hash)


107
108
109
110
# File 'lib/gcloud/vision/annotation/text.rb', line 107

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

#to_sObject



113
114
115
# File 'lib/gcloud/vision/annotation/text.rb', line 113

def to_s
  to_str
end

#to_strObject



118
119
120
# File 'lib/gcloud/vision/annotation/text.rb', line 118

def to_str
  text
end

#wordsArray<Word>

Each word in the detected text, with the bounds for each word.

Returns:



89
90
91
# File 'lib/gcloud/vision/annotation/text.rb', line 89

def words
  @words
end