Class: Tesseract::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/tesseract/engine.rb,
lib/tesseract/engine/baseline.rb,
lib/tesseract/engine/iterator.rb,
lib/tesseract/engine/orientation.rb,
lib/tesseract/engine/bounding_box.rb,
lib/tesseract/engine/font_attributes.rb

Defined Under Namespace

Classes: Baseline, BoundingBox, FontAttributes, Iterator, Orientation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, language = :eng, mode = :DEFAULT, variables = {}, config = []) {|_self| ... } ⇒ Engine

:yields: self

Yields:

  • (_self)

Yield Parameters:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tesseract/engine.rb', line 37

def initialize (path = nil, language = :eng, mode = :DEFAULT, variables = {}, config = [], &block) # :yields: self
	@api = API.new

	@initializing = true

	@init      = block
	@path      = path
	@language  = language
	@mode      = mode
	@variables = variables
	@config    = config
	@rectangle = []

	yield self if block_given?

	@initializing = false

	_init
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#blacklistObject



108
109
110
# File 'lib/tesseract/engine.rb', line 108

def blacklist
	get('tessedit_char_blacklist').chars.to_a
end

#blacklist=(value) ⇒ Object



112
113
114
# File 'lib/tesseract/engine.rb', line 112

def blacklist= (value)
	set('tessedit_char_blacklist', value.respond_to?(:to_a) ? value.to_a.join : value.to_s)
end

#get(name) ⇒ Object



92
93
94
# File 'lib/tesseract/engine.rb', line 92

def get (name)
	@api.get_variable(name.to_s) || @variables[name]
end

#hocrObject



190
191
192
# File 'lib/tesseract/engine.rb', line 190

def hocr
	hocr_at
end

#hocr_at(x = nil, y = nil, width = nil, height = nil, page = nil) ⇒ Object



186
187
188
# File 'lib/tesseract/engine.rb', line 186

def hocr_at (x = nil, y = nil, width = nil, height = nil, page = nil)
	hocr_for(nil, x, y, width, height, page)
end

#hocr_for(image = nil, x = nil, y = nil, width = nil, height = nil, page = nil) ⇒ Object



177
178
179
180
181
# File 'lib/tesseract/engine.rb', line 177

def hocr_for (image = nil, x = nil, y = nil, width = nil, height = nil, page = nil)
	_setup(image, x, y, width, height)

	@api.get_hocr(page || 0)
end

#image=(image) ⇒ Object



134
135
136
# File 'lib/tesseract/engine.rb', line 134

def image= (image)
	@image = image
end

#input=(name) ⇒ Object



78
79
80
# File 'lib/tesseract/engine.rb', line 78

def input= (name)
	@api.set_input_name(name)
end

#load_config(*config) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/tesseract/engine.rb', line 61

def load_config (*config)
	@config.concat config.flatten.compact.uniq

	unless @initializing
		@config.each {|conf|
			@api.read_config_file(conf)
		}
	end
end

#output=(name) ⇒ Object



82
83
84
# File 'lib/tesseract/engine.rb', line 82

def output= (name)
	@api.set_output_name(name)
end

#page_segmentation_modeObject



124
125
126
# File 'lib/tesseract/engine.rb', line 124

def page_segmentation_mode
	@api.get_page_seg_mode
end

#page_segmentation_mode=(value) ⇒ Object



128
129
130
131
132
# File 'lib/tesseract/engine.rb', line 128

def page_segmentation_mode= (value)
	@psm = C.for_enum(value)

	@api.set_page_seg_mode @psm
end

#process(image, page = nil) ⇒ Object



240
241
242
243
244
245
246
247
248
# File 'lib/tesseract/engine.rb', line 240

def process (image, page = nil)
	if page
		@api.process_page(API.image_for(image), page)
	else
		raise ArgumentError, 'the path does not exist' unless File.exists?(image)

		@api.process_pages(image)
	end
end

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



141
142
143
# File 'lib/tesseract/engine.rb', line 141

def select (x = nil, y = nil, width = nil, height = nil)
	@rectangle = [x, y, width, height]
end

#set(name, value) ⇒ Object



86
87
88
89
90
# File 'lib/tesseract/engine.rb', line 86

def set (name, value)
	@variables[name] = value

	@api.set_variable(name.to_s, value.to_s)
end

#textObject



170
171
172
# File 'lib/tesseract/engine.rb', line 170

def text
	text_at
end

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



166
167
168
# File 'lib/tesseract/engine.rb', line 166

def text_at (x = nil, y = nil, width = nil, height = nil)
	text_for(nil, x, y, width, height)
end

#text_for(image = nil, x = nil, y = nil, width = nil, height = nil) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/tesseract/engine.rb', line 148

def text_for (image = nil, x = nil, y = nil, width = nil, height = nil)
	_setup(image, x, y, width, height)

	@api.get_text.tap {|text|
		text.instance_exec(@api) {|api|
			@unlv       = api.get_unlv
			@confidence = api.mean_text_confidence

			class << self
				attr_reader :unlv, :confidence
			end
		}
	}
end

#versionObject



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

def version
	@api.version
end

#whitelistObject



116
117
118
# File 'lib/tesseract/engine.rb', line 116

def whitelist
	get('tessedit_char_whitelist').chars.to_a
end

#whitelist=(value) ⇒ Object



120
121
122
# File 'lib/tesseract/engine.rb', line 120

def whitelist= (value)
	set('tessedit_char_whitelist', value.respond_to?(:to_a) ? value.to_a.join : value.to_s)
end

#with(&block) ⇒ Object

:yields: self



71
72
73
74
75
76
# File 'lib/tesseract/engine.rb', line 71

def with (&block) # :yields: self
	self.class.new(@path, @language, @mode, @variables.clone, @config.clone) {|e|
		@init.call(e) if @init
		block.call(e) if block
	}
end