Module: TesseractFFI::Rectangles

Included in:
Tesseract
Defined in:
lib/tesseract_ffi/rectangles.rb

Overview

module Rectangles mixin for recognizing text blocks defined by rectangles

Instance Method Summary collapse

Instance Method Details

#recognize_rectangle(x_coord, y_coord, width, height) ⇒ Object



10
11
12
13
14
15
# File 'lib/tesseract_ffi/rectangles.rb', line 10

def recognize_rectangle(x_coord, y_coord, width, height)
  setup do
    set_rectangle(x_coord, y_coord, width, height)
    ocr
  end
end

#recognize_rectangles(rectangle_list) ⇒ Object

rubocop:disable Metrics/MethodLength



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tesseract_ffi/rectangles.rb', line 18

def recognize_rectangles(rectangle_list)
  @texts = []
  @rectangle_list = nil
  if valid_rectangle_list? rectangle_list
    @rectangle_list = rectangle_list
    setup do
      @rectangle_list.each do |r|
        set_rectangle(r[0], r[1], r[2], r[3])
        ocr
        @texts << @utf8_text.strip
      end
    end
  end
  @texts
end

#set_rectangle(x_coord, y_coord, width, height) ⇒ Object



6
7
8
# File 'lib/tesseract_ffi/rectangles.rb', line 6

def set_rectangle(x_coord, y_coord, width, height)
  tess_set_rectangle(@handle, x_coord, y_coord, width, height)
end

#valid_rectangle?(rectangle) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tesseract_ffi/rectangles.rb', line 45

def valid_rectangle?(rectangle)
  if rectangle.is_a?(Array) &&
     rectangle.length == 4 &&
     rectangle.all? { |r| r.is_a?(Integer) }
    true
  else
    msg = 'Tesseract Error Argument must be array of 4 Integer'
    @errors << msg
    raise TessException.new(error_msg: msg)
  end
end

#valid_rectangle_list?(list) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
# File 'lib/tesseract_ffi/rectangles.rb', line 34

def valid_rectangle_list?(list)
  if list.is_a?(Array) && list.all? { |r| valid_rectangle?(r) }
    true
  else
    msg = 'Tess Error Argument must be a list'
    # copy the error message as we are not going to Setup
    @errors << msg
    raise TessException.new(error_msg: msg)
  end
end