Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Revert "Revert "restructed gem to be more consistant with the standar…

…d directory structure and version bumped""

This reverts commit 2a8a428.
  • Loading branch information...
commit 1246b64a659e4a0a21a5e1384eed37399ea59e75 1 parent 2a8a428
@scottdavis authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+*.gem
+*.DS_Store
View
10 LICENSE
@@ -0,0 +1,10 @@
+Copyright (c) 2011 Scott Davis
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
+Tesseract is released under the Apache License v2.0 more info here (http://code.google.com/p/tesseract-ocr)
View
10 lib/tesseract.rb
@@ -0,0 +1,10 @@
+require 'tesseract/dependency_checker'
+require 'tesseract/file_handler'
+require 'tesseract/process'
+require 'pathname'
+require 'digest/md5'
+
+
+module Tesseract
+
+end
View
40 lib/tesseract/dependency_checker.rb
@@ -0,0 +1,40 @@
+module Tesseract
+ class DependencyChecker
+ #putting these here so its easyer to test
+ IMAGE_MAGICK_ERROR = "ImageMagick \"convert\" command not found! Make sure ImageMagick is installed and in the system path"
+ TESSERACT_ERROR = "\"tesseract\" command not found! Make sure tesseract is installed and in the system path"
+ OS_ERROR = "Only Unix Based enviroments are supported Mac, Linux, etc."
+
+ def self.check!
+ check_os!
+ check_for_tesseract!
+ check_for_imagemagick!
+ true
+ end
+
+ private
+ #for easy mocking
+ def self.run_cmd(cmd)
+ `#{cmd}`
+ end
+
+ def self.check_os!
+ case ::RUBY_PLATFORM
+ when /darwin/
+ return true
+ when /linux/, /unix/
+ return true
+ end
+ raise Exception, OS_ERROR
+ end
+
+ def self.check_for_imagemagick!
+ raise Exception, IMAGE_MAGICK_ERROR if run_cmd('which convert').empty?
+ end
+
+ def self.check_for_tesseract!
+ raise Exception, TESSERACT_ERROR if run_cmd('which tesseract').empty?
+ end
+
+ end
+end
View
20 lib/tesseract/file_handler.rb
@@ -0,0 +1,20 @@
+require 'pathname'
+require 'tempfile'
+module Tesseract
+ class FileHandler
+ @tempfiles = []
+
+ def self.create_temp_file(filename)
+ file = Pathname.new(Dir::tmpdir).join(filename)
+ @tempfiles << file
+ return file
+ end
+
+ def self.cleanup!
+ @tempfiles.each do |file|
+ File.unlink(file.to_s) if File.exists?(file.to_s)
+ end
+ end
+
+ end
+end
View
51 lib/tesseract/process.rb
@@ -0,0 +1,51 @@
+module Tesseract
+ class Process
+ attr_reader :image
+ attr_accessor :lang
+ CONVERT_COMMAND = 'convert'
+ TESSERACT_COMMAND = 'tesseract'
+
+ 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
+
+ def to_s
+ @out ||= process!
+ end
+
+ def process!
+ temp_image = to_tiff
+ text = tesseract_translation(temp_image)
+ FileHandler.cleanup!
+ text.gsub(/^\//, '')
+ end
+
+ def to_tiff
+ temp_file = FileHandler.create_temp_file("#{@hash}.tif")
+ system [CONVERT_COMMAND, image, temp_file].join(" ")
+ temp_file
+ end
+
+ 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
+
+ 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
+
+ end
+
+end
View
3  lib/tesseract/version.rb
@@ -0,0 +1,3 @@
+module Tesseract
+ VERSION = '0.1.0'
+end
Please sign in to comment.
Something went wrong with that request. Please try again.