Class: Tesseract

Inherits:
Object
  • Object
show all
Defined in:
lib/tesseract.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Tesseract

Returns a new instance of Tesseract.



8
9
10
11
12
13
14
15
16
17
# File 'lib/tesseract.rb', line 8

def initialize (opts={})
  @lang = opts.delete(:lang) || opts.delete(:language) || 'eng'
  self.src = opts.delete(:src) || opts.delete(:source) || opts.delete(:image)
  self.blob = opts.delete(:blob)
  @editor = opts.delete(:editor) || lambda {|x|x}
  self.strip = opts.delete(:strip)
  @tmp = Tempfile.new(['rbtesseract', '.tiff']).tap {|x| x.close }.path

  ObjectSpace.define_finalizer(self, method(:finalize))
end

Instance Attribute Details

#blobObject

Returns the value of attribute blob.



5
6
7
# File 'lib/tesseract.rb', line 5

def blob
  @blob
end

#editorObject

Returns the value of attribute editor.



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

def editor
  @editor
end

#langObject Also known as: language

Returns the value of attribute lang.



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

def lang
  @lang
end

#srcObject Also known as: source, image

Returns the value of attribute src.



5
6
7
# File 'lib/tesseract.rb', line 5

def src
  @src
end

Instance Method Details

#crops(*areas) ⇒ Object



57
58
59
60
61
# File 'lib/tesseract.rb', line 57

def crops (*areas)
  areas.map {|area|
    solve(*area)
  }.join
end

#finalizeObject Also known as: close



67
68
69
# File 'lib/tesseract.rb', line 67

def finalize
  File.unlink(@tmp) rescue nil
end

#solve(x = 0, y = 0, width = nil, height = nil) ⇒ Object



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

def solve (x=0, y=0, width=nil, height=nil)
  editor.call((@src ? Magick::Image.read(@src) : Magick::Image.from_blob(@blob)).first).write(@tmp)
  img = Magick::Image.read(@tmp).first
  x ||= 0
  y ||= 0
  width ||= img.columns
  height ||= img.rows

  get_text(@lang, @tmp, x, y, width, height).tap {|x|
    x.strip! if strip?
  }
end

#strip=(bool) ⇒ Object



29
30
31
# File 'lib/tesseract.rb', line 29

def strip= (bool)
  @strip = !!bool
end

#strip?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/tesseract.rb', line 33

def strip?
  @strip
end

#to_sObject



63
64
65
# File 'lib/tesseract.rb', line 63

def to_s
  solve
end