Class: Tesseract::Process

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

Constant Summary collapse

CONVERT_COMMAND =
'convert'
TESSERACT_COMMAND =
'tesseract'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_name, options = {}) ⇒ Process

Returns a new instance of Process.



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

def initialize(image_name, options = {})
  DependencyChecker.check!
  @image = Pathname.new(image_name)
  @hash = Digest::MD5.hexdigest("#{@image}-#{Time.now}")
  @lang = options[:lang].nil? ? 'eng' : options.delete(:lang)
  @options = options
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



3
4
5
# File 'lib/tesseract/process.rb', line 3

def image
  @image
end

#langObject

Returns the value of attribute lang.



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

def lang
  @lang
end

Instance Method Details

#process!Object



20
21
22
23
24
25
# File 'lib/tesseract/process.rb', line 20

def process!
  temp_image = to_tiff
  text = tesseract_translation(temp_image)
  FileHandler.cleanup!
  text.gsub(/^\//, '')
end

#tesseract_translation(image_file) ⇒ Object



33
34
35
36
37
38
# File 'lib/tesseract/process.rb', line 33

def tesseract_translation(image_file)
  temp_text_file = FileHandler.create_temp_file("#{@hash}")
  config_file = write_configs
  system [TESSERACT_COMMAND, image_file, temp_text_file, "-l #{@lang}", config_file, "&> /dev/null"].join(" ")
  File.read("#{temp_text_file}.txt")
end

#to_sObject



16
17
18
# File 'lib/tesseract/process.rb', line 16

def to_s
  @out ||= process!
end

#to_tiffObject



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

def to_tiff
  temp_file = FileHandler.create_temp_file("#{@hash}.tif")
  system [CONVERT_COMMAND, image, temp_file].join(" ")
  temp_file
end

#write_configsObject



40
41
42
43
44
45
46
47
# File 'lib/tesseract/process.rb', line 40

def write_configs
  return '' if @options.empty?
  path = FileHandler.create_temp_file("#{@hash}.config")
  File.open(path, "w+") do |f|
    @options.each { |k,v| f << "#{k} #{v}\n" }
  end
  path
end