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

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

Overview

Block

A logical element on the page. See Google::Cloud::Vision::Annotation::Text::Page.

Examples:

require "google/cloud/vision"

vision = Google::Cloud::Vision.new

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

block = text.pages[0].blocks.first

block.languages.first.code #=> "en"
block.bounds.count #=> 4
block.paragraphs.count #=> 1

Instance Method Summary collapse

Instance Method Details

#block_type::Symbol

Detected block type (text, image etc) for the block.

Returns:

  • (::Symbol)


485
486
487
# File 'lib/google/cloud/vision/annotation/text.rb', line 485

def block_type
  @grpc.block_type.to_sym
end

#boundsArray<Vertex>

The bounding box for the block. 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:



461
462
463
464
465
466
# File 'lib/google/cloud/vision/annotation/text.rb', line 461

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)


425
426
427
428
# File 'lib/google/cloud/vision/annotation/text.rb', line 425

def break_type
  @grpc.property.detected_break &&
    @grpc.property.detected_break.type.to_sym
end

#languagesArray<Language>

A list of detected languages together with confidence.

Returns:



413
414
415
416
417
418
# File 'lib/google/cloud/vision/annotation/text.rb', line 413

def languages
  detected_languages = @grpc.property.detected_languages
  @languages ||= Array(detected_languages).map do |l|
    Language.from_grpc l
  end
end

#paragraphsArray<Paragraph>

List of paragraphs in this block (if this block is of type text).

Returns:



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

def paragraphs
  @paragraphs ||= Array(@grpc.paragraphs).map do |b|
    Paragraph.from_grpc b
  end
end

#prefix_break?Boolean

True if a detected break prepends the page.

Returns:

  • (Boolean)


435
436
437
438
# File 'lib/google/cloud/vision/annotation/text.rb', line 435

def prefix_break?
  @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)


494
495
496
497
498
# File 'lib/google/cloud/vision/annotation/text.rb', line 494

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