-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSimpleOCR.lpr
301 lines (276 loc) · 13.6 KB
/
SimpleOCR.lpr
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
library SimpleOCR;
{==============================================================================]
Copyright (c) 2021, Jarl `slacky` Holta
Project: SimpleOCR
Project URL: https://github.com/slackydev/SimpleOCR
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
[==============================================================================}
{$i simpleocr.inc}
uses
classes, sysutils,
simpleocr.types, simpleocr.engine, simpleocr.filters;
{$i simbaplugin.inc}
procedure TFontSet_Load(const Params: PParamArray); cdecl;
begin
PFontSet(Params^[0])^.Load(PString(Params^[1])^, PInteger(Params^[2])^);
end;
procedure TSimpleOCR_TextToMatrix(const Params: PParamArray; const Result: Pointer); cdecl;
begin
PIntegerMatrix(Result)^ := PSimpleOCR(Params^[0])^.TextToMatrix(PString(Params^[1])^, PFontSet(Params^[2])^);
end;
procedure TSimpleOCR_TextToTPA(const Params: PParamArray; const Result: Pointer); cdecl;
begin
PPointArray(Result)^ := PSimpleOCR(Params^[0])^.TextToTPA(PString(Params^[1])^, PFontSet(Params^[2])^);
end;
procedure TSimpleOCR_LocateText1(const Params: PParamArray; const Result: Pointer); cdecl;
begin
PSingle(Result)^ := PSimpleOCR(Params^[0])^.LocateText(PIntegerMatrix(Params^[1])^, PString(Params^[2])^, PFontSet(Params^[3])^, POCRFilter(Params^[4])^, PBox(Params^[5])^);
end;
procedure TSimpleOCR_LocateText2(const Params: PParamArray; const Result: Pointer); cdecl;
begin
PBoolean(Result)^ := PSimpleOCR(Params^[0])^.LocateText(PIntegerMatrix(Params^[1])^, PString(Params^[2])^, PFontSet(Params^[3])^, POCRFilter(Params^[4])^, PSingle(Params^[5])^);
end;
procedure TSimpleOCR_Recognize(const Params: PParamArray; const Result: Pointer); cdecl;
begin
PString(Result)^ := PSimpleOCR(Params^[0])^.Recognize(PIntegerMatrix(Params^[1])^, POCRFilter(Params^[2])^, PFontSet(Params^[3])^);
end;
procedure TSimpleOCR_RecognizeStatic(const Params: PParamArray; const Result: Pointer); cdecl;
begin
PString(Result)^ := PSimpleOCR(Params^[0])^.RecognizeStatic(PIntegerMatrix(Params^[1])^, POCRFilter(Params^[2])^, PFontSet(Params^[3])^, PInteger(Params^[4])^);
end;
procedure TSimpleOCR_RecognizeLines(const Params: PParamArray; const Result: Pointer); cdecl;
begin
PStringArray(Result)^ := PSimpleOCR(Params^[0])^.RecognizeLines(PIntegerMatrix(Params^[1])^, POCRFilter(Params^[2])^, PFontSet(Params^[3])^, PBoxArray(Params^[4])^);
end;
procedure TSimpleOCR_RecognizeLinesEx(const Params: PParamArray; const Result: Pointer); cdecl;
begin
PStringArray(Result)^ := PSimpleOCR(Params^[0])^.RecognizeLines(PIntegerMatrix(Params^[1])^, POCRFilter(Params^[2])^, PFontSet(Params^[3])^);
end;
var
InternalDataSize: Integer;
initialization
addGlobalType(
'packed record ' + LineEnding +
' ImageWidth, ImageHeight: Integer; ' + LineEnding +
' Width, Height: Integer; ' + LineEnding +
' CharacterBounds: TBox; ' + LineEnding +
' CharacterPoints: TPointArray; ' + LineEnding +
' CharacterPointsLength: Integer; ' + LineEnding +
' ShadowPoints: TPointArray; ' + LineEnding +
' BackgroundPoints: TPointArray; ' + LineEnding +
' BackgroundPointsLength: Integer; ' + LineEnding +
' TotalBounds: TBox; ' + LineEnding +
' Value: Char; ' + LineEnding +
'end;',
'TFontCharacter');
addGlobalType(
'packed record ' + LineEnding +
' Name: String; ' + LineEnding +
' Characters: array[32..126] of TFontCharacter; ' + LineEnding +
' SpaceWidth: Integer; ' + LineEnding +
' MaxWidth: Integer; ' + LineEnding +
' MaxHeight: Integer; ' + LineEnding +
'end;',
'TFontSet');
addGlobalType(
'packed record ' + LineEnding +
' Rule: Integer; ' + LineEnding +
' ' + LineEnding +
' AnyColorFilter: packed record ' + LineEnding +
' MaxShadowValue: Integer; ' + LineEnding +
' Tolerance: Integer; ' + LineEnding +
' end; ' + LineEnding +
' ' + LineEnding +
' ColorFilter: packed record ' + LineEnding +
' Colors: array of packed record ' + LineEnding +
' Color: Integer; ' + LineEnding +
' Tolerance: Integer; ' + LineEnding +
' end; ' + LineEnding +
' Invert: Boolean; ' + LineEnding +
' end; ' + LineEnding +
' ' + LineEnding +
' ThresholdFilter: packed record ' + LineEnding +
' Amount: Integer; ' + LineEnding +
' Invert: Boolean; ' + LineEnding +
' end; ' + LineEnding +
' ' + LineEnding +
' ShadowFilter: packed record ' + LineEnding +
' MaxShadowValue: Integer; ' + LineEnding +
' Tolerance: Integer; ' + LineEnding +
' end; ' + LineEnding +
' ' + LineEnding +
' MinCharacterMatch: Char; ' + LineEnding +
'end;',
'TOCRFilter');
InternalDataSize := SizeOf(TSimpleOCR) - (SizeOf(TFontSet) + SizeOf(TIntegerMatrix));
addGlobalType(
'packed record ' + LineEnding +
' FontSet: TFontSet; ' + LineEnding +
' Client: TIntegerMatrix; ' + LineEnding +
' InternalData: array[1..' + IntToStr(InternalDataSize) + '] of Byte;' + LineEnding +
'end;',
'TSimpleOCR');
addGlobalFunc('procedure TFontSet.Load(FileName: String; Space: Integer = 4); native;', @TFontSet_Load);
addGlobalFunc('function TSimpleOCR.TextToMatrix(Text: String; constref Font: TFontSet): TIntegerMatrix; native;', @TSimpleOCR_TextToMatrix);
addGlobalFunc('function TSimpleOCR.TextToTPA(Text: String; constref Font: TFontSet): TPointArray; native;', @TSimpleOCR_TextToTPA);
addGlobalFunc('function TSimpleOCR._LocateText(Matrix: TIntegerMatrix; Text: String; constref Font: TFontSet; Filter: TOCRFilter; out Bounds: TBox): Single; overload; native;', @TSimpleOCR_LocateText1);
addGlobalFunc('function TSimpleOCR._LocateText(Matrix: TIntegerMatrix; Text: String; constref Font: TFontSet; Filter: TOCRFilter; MinMatch: Single = 1): Boolean; overload; native;', @TSimpleOCR_LocateText2);
addGlobalFunc('function TSimpleOCR._Recognize(Matrix: TIntegerMatrix; Filter: TOCRFilter; constref Font: TFontSet): String; native;', @TSimpleOCR_Recognize);
addGlobalFunc('function TSimpleOCR._RecognizeLines(Matrix: TIntegerMatrix; Filter: TOCRFilter; constref Font: TFontSet; out Bounds: TBoxArray): TStringArray; overload; native;', @TSimpleOCR_RecognizeLines);
addGlobalFunc('function TSimpleOCR._RecognizeLines(Matrix: TIntegerMatrix; Filter: TOCRFilter; constref Font: TFontSet): TStringArray; overload; native;', @TSimpleOCR_RecognizeLinesEx);
addGlobalFunc('function TSimpleOCR._RecognizeStatic(Matrix: TIntegerMatrix; Filter: TOCRFilter; constref Font: TFontSet; MaxWalk: Integer = 20): String; native;', @TSimpleOCR_RecognizeStatic);
addCode([
'function TSimpleOCR._GetColorsMatrix(B: TBox): TIntegerMatrix; static;',
'begin',
' {$IFDEF SIMBAMAJOR2000}',
' Result := Target.GetColorsMatrix(B);',
' {$ELSE}',
' Result := GetColorsMatrix(B.X1, B.Y1, B.X2, B.Y2);',
' {$ENDIF}',
'end;',
'',
'type TOCRAnyColorFilter = type TOCRFilter; // 0',
'type TOCRColorFilter = type TOCRFilter; // 1',
'type TOCRThresholdFilter = type TOCRFilter; // 2',
'type TOCRShadowFilter = type TOCRFilter; // 3',
'type TOCRInvertColorFilter = type TOCRFilter; // 4',
'',
'function TOCRAnyColorFilter.Create(Tolerance: Integer; MaxShadowValue: Integer): TOCRAnyColorFilter; static;',
'begin',
' Result.Rule := 0;',
' Result.AnyColorFilter.Tolerance := Tolerance;',
' Result.AnyColorFilter.MaxShadowValue := MaxShadowValue;',
'end;',
'',
'function TOCRColorFilter.Create(Colors, Tolerances: TIntegerArray): TOCRColorFilter; static; overload;',
'var',
' I: Integer;',
'begin',
' if Length(Colors) <> Length(Tolerances) then',
' raise "TOCRColorFilter.Create: Length(Colors) <> Length(Tolerances)";',
'',
' Result.Rule := 1;',
'',
' SetLength(Result.ColorFilter.Colors, Length(Colors));',
' for I := 0 to High(Colors) do',
' begin',
' Result.ColorFilter.Colors[I].Color := Colors[I];',
' Result.ColorFilter.Colors[I].Tolerance := Tolerances[I];',
' end;',
'end;',
'',
'function TOCRColorFilter.Create(Colors: TIntegerArray): TOCRColorFilter; static; overload;',
'var',
' I: Integer;',
'begin',
' Result.Rule := 1;',
'',
' SetLength(Result.ColorFilter.Colors, Length(Colors));',
' for I := 0 to High(Colors) do',
' Result.ColorFilter.Colors[I].Color := Colors[I];',
'end;',
'',
'function TOCRInvertColorFilter.Create(Colors, Tolerances: TIntegerArray): TOCRInvertColorFilter; static; overload;',
'var',
' I: Integer;',
'begin',
' if Length(Colors) <> Length(Tolerances) then',
' raise "TOCRInvertColorFilter.Create: Length(Colors) <> Length(Tolerances)";',
'',
' Result.Rule := 4;',
' Result.ColorFilter.Invert := True;',
'',
' SetLength(Result.ColorFilter.Colors, Length(Colors));',
' for I := 0 to High(Colors) do',
' begin',
' Result.ColorFilter.Colors[I].Color := Colors[I];',
' Result.ColorFilter.Colors[I].Tolerance := Tolerances[I];',
' end;',
'end;',
'',
'function TOCRInvertColorFilter.Create(Colors: TIntegerArray): TOCRInvertColorFilter; static; overload;',
'var',
' I: Integer;',
'begin',
' Result.Rule := 4;',
' Result.ColorFilter.Invert := True;',
'',
' SetLength(Result.ColorFilter.Colors, Length(Colors));',
' for I := 0 to High(Colors) do',
' Result.ColorFilter.Colors[I].Color := Colors[I];',
'end;',
'',
'function TOCRThresholdFilter.Create(Amount: Integer; Invert: Boolean = False): TOCRThresholdFilter; static;',
'begin',
' Result.Rule := 2;',
' Result.ThresholdFilter.Amount := Amount;',
' Result.ThresholdFilter.Invert := Invert;',
'end;',
'',
'function TOCRShadowFilter.Create(MaxShadowValue: Integer = 25; Tolerance: Integer = 5): TOCRShadowFilter; static;',
'begin',
' Result.Rule := 3;',
' Result.ShadowFilter.MaxShadowValue := MaxShadowValue;',
' Result.ShadowFilter.Tolerance := Tolerance;',
'end;',
'',
'function TSimpleOCR.Recognize(Area: TBox; Filter: TOCRFilter; constref Font: TFontSet): String;',
'begin',
' Result := Self._Recognize(TSimpleOCR._GetColorsMatrix(Area), Filter, Font);',
'end;',
'',
'function TSimpleOCR.RecognizeStatic(Area: TBox; Filter: TOCRFilter; constref Font: TFontSet; MaxWalk: Integer = 20): String;',
'begin',
' Result := Self._RecognizeStatic(TSimpleOCR._GetColorsMatrix(Area), Filter, Font, MaxWalk);',
'end;',
'',
'function TSimpleOCR.RecognizeLines(Area: TBox; Filter: TOCRFilter; constref Font: TFontSet; out Bounds: TBoxArray): TStringArray; overload;',
'var',
' I: Integer;',
'begin',
' Result := Self._RecognizeLines(TSimpleOCR._GetColorsMatrix(Area), Filter, Font, Bounds);',
' for I := 0 to High(Bounds) do',
' begin',
' Bounds[I].X1 += Area.X1;',
' Bounds[I].Y1 += Area.Y1;',
' Bounds[I].X2 += Area.X1;',
' Bounds[I].Y2 += Area.Y1;',
' end;',
'end;',
'',
'function TSimpleOCR.RecognizeLines(Area: TBox; Filter: TOCRFilter; constref Font: TFontSet): TStringArray; overload;',
'begin',
' Result := Self._RecognizeLines(TSimpleOCR._GetColorsMatrix(Area), Filter, Font);',
'end;',
'',
'function TSimpleOCR.RecognizeNumber(Area: TBox; Filter: TOCRFilter; constref Font: TFontSet): Int64;',
'var',
' Text: String;',
' Character: Char;',
'begin',
' for Character in Self.Recognize(Area, Filter, Font) do',
' case Character of',
' #48..#57: Text += Character;',
' #79: Text += #48;',
' end;',
'',
' if (Text <> "") then',
' Result := StrToInt(Text);',
'end;',
'',
'function TSimpleOCR.LocateText(Area: TBox; Text: String; constref Font: TFontSet; Filter: TOCRFilter; out Bounds: TBox): Single; overload;',
'begin',
' Result := Self._LocateText(TSimpleOCR._GetColorsMatrix(Area), Text, Font, Filter, Bounds);',
'',
' Bounds.X1 += Area.X1;',
' Bounds.Y1 += Area.Y1;',
' Bounds.X2 += Area.X1;',
' Bounds.Y2 += Area.Y1;',
'end;',
'',
'function TSimpleOCR.LocateText(Area: TBox; Text: String; constref Font: TFontSet; Filter: TOCRFilter; MinMatch: Single): Boolean; overload;',
'begin',
' Result := Self._LocateText(TSimpleOCR._GetColorsMatrix(Area), Text, Font, Filter, MinMatch);',
'end;'
]);
end.