Class: Google::Cloud::Vision::Annotation::Text::Page::Paragraph

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

Overview

Paragraph

Structural unit of text representing a number of words in certain order. See Block.

Examples:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

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

paragraph = text.pages[0].blocks[0].paragraphs.first

paragraph.languages.first.code #=> "en"
paragraph.bounds.count #=> 4
paragraph.words.count #=> 10

Instance Method Summary collapse

Instance Method Details

#boundsArray<Vertex>

The bounding box for the paragraph. The vertices are in the order of top-left, top-right, bottom-right, bottom-left. When a rotation of the bounding box is detected the rotation is represented as around the top-left corner as defined when the text is read in the 'natural' orientation. For example:

  • when the text is horizontal it might look like: 0----1 | | 3----2
  • when rotated 180 degrees around the top-left corner it becomes: 2----3 | | 1----0 and the vertice order will still be (0, 1, 2, 3).

Returns:



614
615
616
617
618
619
# File 'lib/google/cloud/vision/annotation/text.rb', line 614

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

#break_type::Symbol

The type of a detected break at the start or end of the page.

Returns:

  • (::Symbol)


576
577
578
579
580
# File 'lib/google/cloud/vision/annotation/text.rb', line 576

def break_type
  return nil if @grpc.property.nil?
  @grpc.property.detected_break &&
    @grpc.property.detected_break.type.to_sym
end

#languagesArray<Language>

A list of detected languages together with confidence.

Returns:



563
564
565
566
567
568
569
# File 'lib/google/cloud/vision/annotation/text.rb', line 563

def languages
  return [] if @grpc.property.nil?
  detected_languages = @grpc.property.detected_languages
  @languages ||= Array(detected_languages).map do |l|
    Language.from_grpc l
  end
end

#prefix_break?Boolean

True if a detected break prepends the page.

Returns:

  • (Boolean)


587
588
589
590
591
# File 'lib/google/cloud/vision/annotation/text.rb', line 587

def prefix_break?
  return nil if @grpc.property.nil?
  @grpc.property.detected_break &&
    @grpc.property.detected_break.is_prefix
end

#to_hHash

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

Returns:

  • (Hash)


637
638
639
640
641
# File 'lib/google/cloud/vision/annotation/text.rb', line 637

def to_h
  { languages: languages.map(&:to_h), break_type: break_type,
    prefix_break: prefix_break?, bounds: bounds.map(&:to_h),
    words: words.map(&:to_h) }
end

#wordsArray<Word>

List of words in this paragraph.

Returns:



626
627
628
629
630
# File 'lib/google/cloud/vision/annotation/text.rb', line 626

def words
  @words ||= Array(@grpc.words).map do |b|
    Word.from_grpc b
  end
end