/
tesseract.simba
380 lines (278 loc) · 8.45 KB
/
tesseract.simba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
(*
Tesseract OCR
=============
The Tesseract file stores the functions related tesseract OCR
The source for this file can be found `here <https://github.com/SRL/SRL-6/blob/master/lib/core/tesseract.simba>`_.
*)
{$loadlib libtesseract2_}
{$f-}
(*
type TTesseractFilter
~~~~~~~~~~~~~~~~~~~~~
A record that stores filtering to apply onto a image.
Example:
.. code-block:: pascal
var
myTesseractFilter: TTesseractFilter;
begin
with (myTesseractFilter) do
begin
multiplerW := 3;
multiplerH := 3;
with (thresholdSettings) do
begin
invert := False;
amount := 20;
method := TM_Mean;
end;
end;
writeln(Tesseract_GetText(100, 100, 200, 200, myTesseractFilter));
end;
*)
type
TTesseractFilter = record
multiplerW, multiplerH: integer;
thresholdSettings: record invert: boolean; amount: byte; method: TBmpThreshMethod; end;
end;
(*
const Tesseract
~~~~~~~~~~~~~~~
String constants that store the data path, language and whitelist presets.
Example:
.. code-block:: pascal
print(TESS_DATA_PATH);
*)
const
TESS_DATA_PATH = AppPath + 'tessdata';
TESS_LANGUAGE = 'eng';
const
TESS_WHITELIST_NUMBERS = '0123456789';
TESS_WHITELIST_LETTERS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
TESS_WHITELIST_NONE = '';
(*
const Filter presets
~~~~~~~~~~~~~~~~~~~~
Constants of TTesseractFilter that are filter presets
Example:
.. code-block:: pascal
Tesseract_GetText(100, 100, 200, 200, TESS_FILTER_SMALL_CHARS);
*)
const
TESS_FILTER_SMALL_CHARS: TTesseractFilter = [3, 3, [false, 50, TM_Mean]];
TESS_FILTER_SMALL_CHARS_2: TTesseractFilter = [5, 5, [false, 55, TM_Mean]];
TESS_FILTER_NPC_CHAT_CHARS: TTesseractFilter = [3, 3, [true, -50, TM_Mean]];
{*
var Tesseract
~~~~~~~~~~~~~
Internal variables for use by the Tesseract.
*}
var
__tesseractPtr: pointer := nil;
__tesseractIsSetup: boolean = false;
(*
_Tesseract_Setup
~~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure _Tesseract_Setup();
Setups our instance of tesseract-ocr. Automaticlly called if using functions
from this file.
.. note::
- by Olly
- Last updated: 26 July 2014 by Olly
Example:
.. code-block:: pascal
_Tesseract_Setup();
*)
procedure _Tesseract_Setup();
begin
if __tesseractIsSetup then
exit();
__tesseractPtr := Tesseract_Create();
if (__tesseractPtr = nil) then
print('Failed to create a tesseract instance', TDebug.FATAL);
if (Tesseract_Init(__tesseractPtr, TESS_DATA_PATH, TESS_LANGUAGE) <> 0) then
print('Unable to initizalse tesseract, check Simba console for more infomation', TDebug.FATAL);
addOnTerminate('_Tesseract_Free');
__tesseractIsSetup := true;
end;
(*
_Tesseract_Free
~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure _Tesseract_Free();
Frees our instance of tesseract-ocr. Is automaticly called on terminate if
_Tesseract_Setup() has been called.
.. note::
- by Olly
- Last updated: 26 July 2014 by Olly
Example:
.. code-block:: pascal
_Tesseract_Free();
*)
procedure _Tesseract_Free();
begin
if (__tesseractPtr = nil) or (not __tesseractIsSetup) then
exit();
Tesseract_End(__tesseractPtr);
Tesseract_Delete(__tesseractPtr);
__tesseractPtr := nil;
__tesseractIsSetup := false;
end;
(*
Tesseract_ApplyFilter
~~~~~~~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure Tesseract_ApplyFilter(const BMP: integer; const filter: TTesseractFilter);
Applys the tesseract filter onto the bitmap.
.. note::
- by Olly
- Last updated: 9 September 2014 by Olly
Example:
.. code-block:: pascal
Tesseract_ApplyFilter(BMP, MyTesseractFilter);
*)
procedure Tesseract_ApplyFilter(const BMP: integer; const filter: TTesseractFilter);
var
w, h: integer;
begin
if (not BitmapExists(BMP)) then
Exit();
GetBitmapSize(BMP, w, h);
if (Filter.multiplerW > 0) and (Filter.multiplerH > 0) then
ResizeBitmapEx(BMP, RM_Bilinear, Filter.multiplerW * w, Filter.multiplerH * h);
ThresholdAdaptiveBitmap(bmp, 0, 255, Filter.thresholdSettings.invert,
Filter.thresholdSettings.method,
Filter.thresholdSettings.amount);
end;
(*
Tesseract_GetText
~~~~~~~~~~~~~~~~~
.. code-block:: pascal
function Tesseract_GetText(const bmp: integer): string;
Runs tesseract on the bitmap 'bmp'.
.. note::
- by Olly
- Last updated: 26 July 2014 by Olly
Example:
.. code-block:: pascal
writeln(Tesseract_GetText(bmp));
*)
function Tesseract_GetText(const bmp: integer; const Whitelist: String = TESS_WHITELIST_NONE): string;
var
w, h, i: integer;
textLen: UInt32;
textPtr: PChar = nil;
begin
if (not __tesseractIsSetup) then
_Tesseract_Setup();
if (Whitelist <> TESS_WHITELIST_NONE) then
Tesseract_SetVariable(__tesseractPtr, 'tessedit_char_whitelist', WhiteList);
result := '';
getBitmapSize(bmp, w, h);
if (w < 10) or (h < 10) then
Exit;
try
Tesseract_SetImage(__tesseractPtr, getMufasaBitmap(bmp).getData(), w, h, 4, Integer(w * 4));
try
textPtr := Tesseract_GetUTF8Text(__tesseractPtr, textLen);
except
TextPtr := nil;
end;
if (TextPtr <> nil) then
begin
setLength(result, textLen);
if (textLen > 0) then
begin
for i := 1 to textLen do
result[i] := textPtr[i - 1]^;
result := trim(stringReplace(result, #10, #13#10, [rfReplaceAll]));
end;
end;
finally
if (textPtr <> nil) then
begin
Tesseract_FreeUTF8Text(textPtr);
Tesseract_Clear(__tesseractPtr);
end;
if (Whitelist <> TESS_WHITELIST_NONE) then // reset to default
begin
_Tesseract_Free();
_Tesseract_Setup();
end;
end;
end;
(*
Tesseract_GetText; overload
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: pascal
function Tesseract_GetText(const xs, ys, xe, ye: integer; filter: TTesseractFilter): string; overload;
Overloaded method that searches on the client with a filter to read text.
.. note::
- by Olly
- Last updated: 22 January 2016 by Daniel
Example:
.. code-block:: pascal
writeln(Tesseract_GetText(100, 100, 200, 200, TESS_FILTER_SMALL_CHARS));
*)
function Tesseract_GetText(const xs, ys, xe, ye: integer; filter: TTesseractFilter; Whitelist: String = TESS_WHITELIST_NONE): string; overload;
var
bmp: Integer;
t, tt: LongWord;
begin
if((xe - xs) < 10) or ((ye - ys) < 10) then
Exit('');
bmp := bitmapFromClient(xs, ys, xe, ye);
try
t := getSystemTime();
Tesseract_ApplyFilter(bmp, filter);
t := (getSystemTime() - t);
tt := getSystemTime();
result := Tesseract_GetText(bmp, Whitelist);
{$IFDEF TESSERACT_DEBUG}
printf('Tesseract_GetText(): Filtering took: %d ms, Tesseract Took: %d ms, Total Time: %d ms.', [t, (getSystemTime() - tt), (getSystemTime() - tt) + t]);
printf('Tesseract_GetText(): Text found: %s', [result]);
{$ENDIF}
finally
{$IFDEF TESSERACT_DEBUG}
clearDebugImg();
displayDebugImgWindow(getMufasaBitmap(bmp).getWidth(), getMufasaBitmap(bmp).getHeight());
drawBitmapDebugImg(bmp);
{$ENDIF}
end;
freeBitmap(bmp);
end;
(*
Tesseract_GetText; overload
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: pascal
function Tesseract_GetText(const area: TBox; filter: TTesseractFilter): string; overload;
Overloaded method that searches on the client with a filter and accepts a TBox
as an area parameter rather than x1, y1, x2, y2.
.. note::
- by Olly
- Last updated: 26 July 2014 by Olly
Example:
.. code-block:: pascal
writeln(Tesseract_GetText(box, TESS_FILTER_SMALL_CHARS));
*)
function Tesseract_GetText(const area: TBox; filter: TTesseractFilter; Whitelist: String = TESS_WHITELIST_NONE): string; overload;
begin
result := Tesseract_GetText(area.x1, area.y1, area.x2, area.y2, filter, Whitelist);
end;
// keep old versions working
// deprecated use the TTesseractFilter presets
const
FILTER_SMALL_CHARS: TTesseractFilter = TESS_FILTER_SMALL_CHARS;
FILTER_NPC_CHAT_CHARS: TTesseractFilter = TESS_FILTER_NPC_CHAT_CHARS;
FILTER_SMALL_CHARS_2: TTesseractFilter = TESS_FILTER_SMALL_CHARS_2;
// deprecated use Tesseract_GetText().
function tesseractGetText(const searchBox: tbox; filterMethod: TTesseractFilter): string;
begin
result := Tesseract_GetText(searchBox, filterMethod);
end;
// deprecated use Tesseract_GetText().
function tesseractGetText(const x1, y1, x2, y2: integer; filterMethod: TTesseractFilter; config: string = ''): string; overload;
begin
result := Tesseract_GetText(x1, y1, x2, y2, filterMethod, config);
end;
{$f+}