Class: RTesseract
- Inherits:
-
Object
- Object
- RTesseract
- Defined in:
- lib/rtesseract.rb,
lib/rtesseract/box.rb,
lib/rtesseract/mixed.rb,
lib/rtesseract/errors.rb,
lib/rtesseract/box_char.rb
Overview
Ruby wrapper for Tesseract OCR
Direct Known Subclasses
Defined Under Namespace
Classes: Box, BoxChar, Configuration, ConversionError, ErrorWithMemory, ImageNotSelectedError, Mixed, TempFilesNotRemovedError
Constant Summary collapse
- OPTIONS =
%w(command lang psm processor debug clear_console_output options)
- LANGUAGES =
Aliases to languages names
{ 'eng' => %w(en en-us english), 'ita' => %w(it), 'por' => %w(pt pt-br portuguese), 'spa' => %w(sp) }
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
Instance Attribute Summary collapse
-
#image_object ⇒ Object
Returns the value of attribute image_object.
-
#lang ⇒ Object
Select the language ===Languages * eng - English * deu - German * deu-f - German fraktur * fra - French * ita - Italian * nld - Dutch * por - Portuguese * spa - Spanish * vie - Vietnamese Note: Make sure you have installed the language to tesseract.
-
#options ⇒ Object
Returns the value of attribute options.
-
#options_cmd ⇒ Object
Returns the value of attribute options_cmd.
-
#processor ⇒ Object
readonly
Returns the value of attribute processor.
-
#psm ⇒ Object
Page Segment Mode.
-
#source ⇒ Object
Returns the value of attribute source.
Class Method Summary collapse
- .choose_processor!(processor) ⇒ Object
- .configure {|configuration| ... } ⇒ Object
- .read(src = nil, options = {}) {|image| ... } ⇒ Object
Instance Method Summary collapse
- #after_convert_hook ⇒ Object
-
#clear_console_output ⇒ Object
TODO: Clear console for MacOS or Windows.
- #command_line_options(options) ⇒ Object
- #config ⇒ Object
- #config_file ⇒ Object
- #config_hook ⇒ Object
-
#convert ⇒ Object
Convert image to string.
- #convert_command ⇒ Object
- #convert_text ⇒ Object
-
#crop!(x, y, width, height) ⇒ Object
Crop image to convert.
- #default_command ⇒ Object
- #file_ext ⇒ Object
-
#from_blob(blob, ext = '') ⇒ Object
Read image from memory blob.
- #image ⇒ Object
-
#initialize(src = '', options = {}) ⇒ RTesseract
constructor
A new instance of RTesseract.
- #initialize_hook ⇒ Object
- #read ⇒ Object
-
#remove_file(files = []) ⇒ Object
Remove files.
- #text_file ⇒ Object
- #text_file_with_ext(ext = nil) ⇒ Object
-
#to_s ⇒ Object
Output value.
-
#to_s_without_spaces ⇒ Object
Remove spaces and break-lines.
Constructor Details
#initialize(src = '', options = {}) ⇒ RTesseract
Returns a new instance of RTesseract.
57 58 59 60 61 62 63 |
# File 'lib/rtesseract.rb', line 57 def initialize(src = '', = {}) () @value, @x, @y, @w, @h = [nil] @processor = RTesseract.choose_processor!(@processor) @source = @processor.image?(src) ? src : Pathname.new(src) initialize_hook end |
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
37 38 39 |
# File 'lib/rtesseract.rb', line 37 def configuration @configuration end |
Instance Attribute Details
#image_object ⇒ Object
Returns the value of attribute image_object.
19 20 21 |
# File 'lib/rtesseract.rb', line 19 def image_object @image_object end |
#lang ⇒ Object
Select the language
Languages
-
eng - English
-
deu - German
-
deu-f - German fraktur
-
fra - French
-
ita - Italian
-
nld - Dutch
-
por - Portuguese
-
spa - Spanish
-
vie - Vietnamese
Note: Make sure you have installed the language to tesseract
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rtesseract.rb', line 143 def lang language = "#{@lang}".strip.downcase LANGUAGES.each do |value, names| return " -l #{value} " if names.include? language end return " -l #{language} " if language.size > 0 '' rescue '' end |
#options ⇒ Object
Returns the value of attribute options.
20 21 22 |
# File 'lib/rtesseract.rb', line 20 def @options end |
#options_cmd ⇒ Object
Returns the value of attribute options_cmd.
21 22 23 |
# File 'lib/rtesseract.rb', line 21 def @options_cmd end |
#processor ⇒ Object (readonly)
Returns the value of attribute processor.
24 25 26 |
# File 'lib/rtesseract.rb', line 24 def processor @processor end |
#psm ⇒ Object
Page Segment Mode
155 156 157 158 159 |
# File 'lib/rtesseract.rb', line 155 def psm (@psm.nil? ? '' : " -psm #{@psm} ") rescue '' end |
#source ⇒ Object
Returns the value of attribute source.
25 26 27 |
# File 'lib/rtesseract.rb', line 25 def source @source end |
Class Method Details
.choose_processor!(processor) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/rtesseract.rb', line 253 def self.choose_processor!(processor) processor = if MiniMagickProcessor.a_name?(processor.to_s) MiniMagickProcessor elsif QuickMagickProcessor.a_name?(processor.to_s) QuickMagickProcessor elsif NoneProcessor.a_name?(processor.to_s) NoneProcessor else RMagickProcessor end processor.setup processor end |
.configure {|configuration| ... } ⇒ Object
40 41 42 43 |
# File 'lib/rtesseract.rb', line 40 def self.configure self.configuration ||= Configuration.new yield(configuration) end |
.read(src = nil, options = {}) {|image| ... } ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/rtesseract.rb', line 88 def self.read(src = nil, = {}) fail RTesseract::ImageNotSelectedError if src.nil? processor = RTesseract.choose_processor!(.option(:processor, nil)) image = processor.read_with_processor(src.to_s) yield(image) object = RTesseract.new('', ).from_blob(image.to_blob) object end |
Instance Method Details
#after_convert_hook ⇒ Object
209 210 |
# File 'lib/rtesseract.rb', line 209 def after_convert_hook end |
#clear_console_output ⇒ Object
TODO: Clear console for MacOS or Windows
180 181 182 183 |
# File 'lib/rtesseract.rb', line 180 def clear_console_output return '' unless @clear_console_output return '2>/dev/null' if File.exist?('/dev/null') # Linux console clear end |
#command_line_options(options) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rtesseract.rb', line 68 def () default_config = RTesseract.configuration ? RTesseract.configuration.to_hash : {} @options = default_config.merge() @command = @options.option(:command, default_command) @lang = @options.option(:lang, '') @psm = @options.option(:psm, nil) @processor = @options.option(:processor, 'rmagick') @debug = @options.option(:debug, false) @options_cmd = @options.option(:options, []) @options_cmd = [@options_cmd] unless @options_cmd.is_a?(Array) # Disable clear console if debug mode @clear_console_output = @debug ? false : .option(:clear_console_output, true) end |
#config ⇒ Object
164 165 166 167 168 |
# File 'lib/rtesseract.rb', line 164 def config @options ||= {} config_hook @options.map { |k, v| "#{k} #{v}" }.join("\n") end |
#config_file ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/rtesseract.rb', line 170 def config_file config_hook return '' if @options == {} conf = Tempfile.new('config') conf.write(config) conf.flush conf.path end |
#config_hook ⇒ Object
161 162 |
# File 'lib/rtesseract.rb', line 161 def config_hook end |
#convert ⇒ Object
Convert image to string
213 214 215 216 217 218 219 220 |
# File 'lib/rtesseract.rb', line 213 def convert convert_command after_convert_hook convert_text remove_file([@image, text_file_with_ext]) rescue => error raise RTesseract::ConversionError.new(error), error, caller end |
#convert_command ⇒ Object
201 202 203 |
# File 'lib/rtesseract.rb', line 201 def convert_command `#{@command} "#{image}" "#{text_file}" #{lang} #{psm} #{config_file} #{clear_console_output} #{@options_cmd.join(' ')}` end |
#convert_text ⇒ Object
205 206 207 |
# File 'lib/rtesseract.rb', line 205 def convert_text @value = File.read(text_file_with_ext).to_s end |
#crop!(x, y, width, height) ⇒ Object
Crop image to convert
110 111 112 113 114 |
# File 'lib/rtesseract.rb', line 110 def crop!(x, y, width, height) @value = nil @x, @y, @w, @h = x.to_i, y.to_i, width.to_i, height.to_i self end |
#default_command ⇒ Object
82 83 84 85 86 |
# File 'lib/rtesseract.rb', line 82 def default_command TesseractBin::Executables[:tesseract] || 'tesseract' rescue 'tesseract' end |
#file_ext ⇒ Object
189 190 191 |
# File 'lib/rtesseract.rb', line 189 def file_ext '.txt' end |
#from_blob(blob, ext = '') ⇒ Object
Read image from memory blob
223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/rtesseract.rb', line 223 def from_blob(blob, ext = '') blob_file = Tempfile.new(['blob', ext], encoding: 'ascii-8bit') blob_file.binmode.write(blob) blob_file.rewind blob_file.flush self.source = blob_file.path convert remove_file([blob_file]) self rescue => error raise RTesseract::ConversionError.new(error), error, caller end |
#image ⇒ Object
185 186 187 |
# File 'lib/rtesseract.rb', line 185 def image (@image = @processor.image_to_tif(@source, @x, @y, @w, @h)).path end |
#initialize_hook ⇒ Object
65 66 |
# File 'lib/rtesseract.rb', line 65 def initialize_hook end |
#read ⇒ Object
97 98 99 100 101 102 |
# File 'lib/rtesseract.rb', line 97 def read image = @processor.read_with_processor(@source.to_s) new_image = yield(image) from_blob(new_image.to_blob, File.extname(@source.to_s)) self end |
#remove_file(files = []) ⇒ Object
Remove files
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rtesseract.rb', line 117 def remove_file(files = []) files.each do |file| if file.is_a?(Tempfile) file.close file.unlink else File.unlink(file) end end true rescue => error raise RTesseract::TempFilesNotRemovedError.new(error: error, files: files) end |
#text_file ⇒ Object
193 194 195 |
# File 'lib/rtesseract.rb', line 193 def text_file @text_file = Pathname.new(Dir.tmpdir).join("#{Time.now.to_f}#{rand(1500)}").to_s end |
#text_file_with_ext(ext = nil) ⇒ Object
197 198 199 |
# File 'lib/rtesseract.rb', line 197 def text_file_with_ext(ext = nil) [@text_file, ext || file_ext].join('') end |
#to_s ⇒ Object
Output value
237 238 239 240 241 242 243 244 245 246 |
# File 'lib/rtesseract.rb', line 237 def to_s return @value if @value != nil if @processor.image?(@source) || @source.file? convert @value else fail RTesseract::ImageNotSelectedError.new(@source) end end |
#to_s_without_spaces ⇒ Object
Remove spaces and break-lines
249 250 251 |
# File 'lib/rtesseract.rb', line 249 def to_s_without_spaces to_s.gsub(' ', '').gsub("\n", '').gsub("\r", '') end |