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

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

Overview

Text

The results from either the TEXT_DETECTION feature (OCR for shorter documents with sparse text) or the DOCUMENT_TEXT_DETECTION feature (OCR for longer documents with dense text). Optional. Contains structured representations of OCR extracted text, as well as the entire UTF-8 text as a string.

Examples:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

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

text = image.text

text.text
# "Google Cloud Client for Ruby an idiomatic, intuitive... "

text.words[0].text #=> "Google"
text.words[0].bounds.count #=> 4
vertex = text.words[0].bounds.first
vertex.x #=> 13
vertex.y #=> 8

# Use `pages` to access a full structural representation
text.pages[0].blocks[0].paragraphs[0].words[0].symbols[0].text
#=> "G"

Defined Under Namespace

Classes: Page, Word

Instance Method Summary collapse

Instance Method Details

#boundsArray<Vertex>

The bounds for the detected text in the image.

Returns:



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

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

#localeString

The language code detected for text.

Returns:



81
82
83
# File 'lib/google/cloud/vision/annotation/text.rb', line 81

def locale
  @grpc.locale
end

#pagesArray<Page>

Each page in the detected text, with the metadata for each page. Contains a structured representation of OCR extracted text. The hierarchy of an OCR extracted text structure is like this: Page -> Block -> Paragraph -> Word -> Symbol Each structural component, starting from Page, may further have its own properties. Properties describe detected languages, breaks etc..

Returns:



116
117
118
# File 'lib/google/cloud/vision/annotation/text.rb', line 116

def pages
  @pages
end

#textString

The text detected in an image.

Returns:

  • (String)

    The entire text including newline characters.



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

def text
  @grpc.description
end

#to_hHash

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

Returns:

  • (Hash)


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

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

#wordsArray<Word>

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

Returns:



102
103
104
# File 'lib/google/cloud/vision/annotation/text.rb', line 102

def words
  @words
end