Class: Tesseract::Process
- Inherits:
-
Object
- Object
- Tesseract::Process
- Defined in:
- lib/tesseract/process.rb
Constant Summary collapse
- CONVERT_COMMAND =
'convert'
- TESSERACT_COMMAND =
'tesseract'
Instance Attribute Summary collapse
-
#image ⇒ Object
readonly
Returns the value of attribute image.
-
#lang ⇒ Object
Returns the value of attribute lang.
Instance Method Summary collapse
-
#initialize(image_name, options = {}) ⇒ Process
constructor
A new instance of Process.
- #process! ⇒ Object
- #tesseract_translation(image_file) ⇒ Object
- #to_s ⇒ Object
- #to_tiff ⇒ Object
- #write_configs ⇒ Object
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, = {}) DependencyChecker.check! @image = Pathname.new(image_name) @hash = Digest::MD5.hexdigest("#{@image}-#{Time.now}") @lang = [:lang].nil? ? 'eng' : .delete(:lang) @options = end |
Instance Attribute Details
#image ⇒ Object (readonly)
Returns the value of attribute image.
3 4 5 |
# File 'lib/tesseract/process.rb', line 3 def image @image end |
#lang ⇒ Object
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_s ⇒ Object
16 17 18 |
# File 'lib/tesseract/process.rb', line 16 def to_s @out ||= process! end |
#to_tiff ⇒ Object
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_configs ⇒ Object
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 |