-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocr_test.go
More file actions
439 lines (417 loc) · 10.1 KB
/
ocr_test.go
File metadata and controls
439 lines (417 loc) · 10.1 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package main
import (
"bytes"
"image"
"image/color"
"image/jpeg"
"regexp"
"testing"
)
func TestExtractReading(t *testing.T) {
defaultMatchRe := regexp.MustCompile(`^000\d+$`)
tests := []struct {
name string
texts []string
matchRe *regexp.Regexp
fixRules []ocrFixRule
want string
}{
{
name: "exact match with 000 prefix",
texts: []string{"000354225"},
matchRe: defaultMatchRe,
want: "000354225",
},
{
name: "match with leading whitespace",
texts: []string{" 000354225 "},
matchRe: defaultMatchRe,
want: "000354225",
},
{
name: "prefers 000-prefixed over longer digit string",
texts: []string{"9876543210", "000354225"},
matchRe: defaultMatchRe,
want: "000354225",
},
{
name: "fallback to longest digit string when no match",
texts: []string{"12", "12345", "abc"},
matchRe: defaultMatchRe,
want: "12345",
},
{
name: "no digits at all returns empty",
texts: []string{"abc", "def"},
matchRe: defaultMatchRe,
want: "",
},
{
name: "empty input returns empty",
texts: []string{},
matchRe: defaultMatchRe,
want: "",
},
{
name: "nil fix rules behaves like no fix",
texts: []string{"000354225"},
matchRe: defaultMatchRe,
want: "000354225",
},
{
name: "single fix rule corrects 300 prefix to 000",
texts: []string{"300354225"},
matchRe: defaultMatchRe,
fixRules: []ocrFixRule{
{Pattern: regexp.MustCompile(`^300`), Replacement: "000"},
},
want: "000354225",
},
{
name: "fix rule does not affect already correct text",
texts: []string{"000354225"},
matchRe: defaultMatchRe,
fixRules: []ocrFixRule{
{Pattern: regexp.MustCompile(`^300`), Replacement: "000"},
},
want: "000354225",
},
{
name: "fix rule applied in fallback path too",
texts: []string{"abc", "30099"},
matchRe: defaultMatchRe,
fixRules: []ocrFixRule{
{Pattern: regexp.MustCompile(`^300`), Replacement: "000"},
},
want: "00099",
},
{
name: "multiple fix rules applied in order",
texts: []string{"O30354225"},
matchRe: defaultMatchRe,
fixRules: []ocrFixRule{
{Pattern: regexp.MustCompile(`^O`), Replacement: "0"},
{Pattern: regexp.MustCompile(`^030`), Replacement: "000"},
},
want: "000354225",
},
{
name: "later rule depends on earlier rule output",
texts: []string{"X3Y354225"},
matchRe: defaultMatchRe,
fixRules: []ocrFixRule{
{Pattern: regexp.MustCompile(`X`), Replacement: "0"},
{Pattern: regexp.MustCompile(`Y`), Replacement: "0"},
},
want: "030354225",
// After rules: "030354225" — doesn't match ^000\d+$, falls through to fallback as longest digit-only? No, it has no non-digits after fix.
// Actually 030354225 is all digits so it's the fallback best.
},
{
name: "custom match regex",
texts: []string{"WM-12345", "67890"},
matchRe: regexp.MustCompile(`^WM-\d+$`),
want: "WM-12345",
},
{
name: "empty fix rules slice same as nil",
texts: []string{"000354225"},
matchRe: defaultMatchRe,
fixRules: []ocrFixRule{},
want: "000354225",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := extractReading(tt.texts, tt.matchRe, tt.fixRules, false)
if got != tt.want {
t.Errorf("extractReading() = %q, want %q", got, tt.want)
}
})
}
}
func TestExtractReadingMerge(t *testing.T) {
defaultMatchRe := regexp.MustCompile(`^000\d+$`)
tests := []struct {
name string
texts []string
matchRe *regexp.Regexp
fixRules []ocrFixRule
want string
}{
{
name: "merge splits into single reading",
texts: []string{"00036", "128"},
matchRe: regexp.MustCompile(`^\d+$`),
want: "00036128",
},
{
name: "merge with match regex",
texts: []string{"000", "354225"},
matchRe: defaultMatchRe,
want: "000354225",
},
{
name: "merge with fix rules",
texts: []string{"O30", "354225"},
matchRe: defaultMatchRe,
fixRules: []ocrFixRule{
{Pattern: regexp.MustCompile(`^O`), Replacement: "0"},
{Pattern: regexp.MustCompile(`^030`), Replacement: "000"},
},
want: "000354225",
},
{
name: "merge single element unchanged",
texts: []string{"000354225"},
matchRe: defaultMatchRe,
want: "000354225",
},
{
name: "merge trims whitespace from each part",
texts: []string{" 000 ", " 354225 "},
matchRe: defaultMatchRe,
want: "000354225",
},
{
name: "merge empty input returns empty",
texts: []string{},
matchRe: defaultMatchRe,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := extractReading(tt.texts, tt.matchRe, tt.fixRules, true)
if got != tt.want {
t.Errorf("extractReading(merge=true) = %q, want %q", got, tt.want)
}
})
}
}
func TestParseOCRFixRules(t *testing.T) {
tests := []struct {
name string
input string
wantNil bool
wantCount int
text string
want string
}{
{
name: "empty returns nil",
input: "",
wantNil: true,
},
{
name: "single rule",
input: "^300=000",
wantCount: 1,
text: "300354225",
want: "000354225",
},
{
name: "two comma-separated rules applied in order",
input: "^O=0,^030=000",
wantCount: 2,
text: "O30354225",
want: "000354225",
},
{
name: "replacement can be empty",
input: "^X=",
wantCount: 1,
text: "X12345",
want: "12345",
},
{
name: "whitespace around rules is trimmed",
input: " ^300=000 , ^O=0 ",
wantCount: 2,
text: "300354225",
want: "000354225",
},
{
name: "trailing comma ignored",
input: "^300=000,",
wantCount: 1,
text: "300354225",
want: "000354225",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rules := parseOCRFixRules(tt.input)
if tt.wantNil {
if rules != nil {
t.Fatalf("expected nil, got %+v", rules)
}
return
}
if len(rules) != tt.wantCount {
t.Fatalf("expected %d rules, got %d", tt.wantCount, len(rules))
}
got := applyFixRules(tt.text, rules)
if got != tt.want {
t.Errorf("applyFixRules(%q) = %q, want %q", tt.text, got, tt.want)
}
})
}
}
func TestParseMaskRegions(t *testing.T) {
tests := []struct {
name string
regions string
colors string
wantCount int
wantNil bool
}{
{
name: "empty returns nil",
regions: "",
wantNil: true,
},
{
name: "single region default color",
regions: "10,20,30,40",
wantCount: 1,
},
{
name: "two regions default color",
regions: "10,20,30,40,50,60,70,80",
wantCount: 2,
},
{
name: "single color applies to all regions",
regions: "10,20,30,40,50,60,70,80",
colors: "FF0000",
wantCount: 2,
},
{
name: "one color per region",
regions: "10,20,30,40,50,60,70,80",
colors: "FF0000,00FF00",
wantCount: 2,
},
{
name: "whitespace trimmed",
regions: " 10 , 20 , 30 , 40 ",
colors: " 0099CC ",
wantCount: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
masks := parseMaskRegions(tt.regions, tt.colors)
if tt.wantNil {
if masks != nil {
t.Fatalf("expected nil, got %+v", masks)
}
return
}
if len(masks) != tt.wantCount {
t.Fatalf("expected %d masks, got %d", tt.wantCount, len(masks))
}
})
}
}
func TestParseMaskColors(t *testing.T) {
tests := []struct {
name string
input string
numRects int
wantR []uint8
}{
{
name: "empty defaults to black",
input: "",
numRects: 2,
wantR: []uint8{0, 0},
},
{
name: "single color broadcast to all",
input: "FF0000",
numRects: 3,
wantR: []uint8{255, 255, 255},
},
{
name: "per-region colors",
input: "FF0000,00FF00",
numRects: 2,
wantR: []uint8{255, 0},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
colors := parseMaskColors(tt.input, tt.numRects)
if len(colors) != tt.numRects {
t.Fatalf("expected %d colors, got %d", tt.numRects, len(colors))
}
for i, c := range colors {
if c.R != tt.wantR[i] {
t.Errorf("color[%d].R = %d, want %d", i, c.R, tt.wantR[i])
}
}
})
}
}
func TestParseHexColor(t *testing.T) {
tests := []struct {
input string
want color.RGBA
}{
{"000000", color.RGBA{0, 0, 0, 255}},
{"FFFFFF", color.RGBA{255, 255, 255, 255}},
{"0099CC", color.RGBA{0, 153, 204, 255}},
{"#FF8800", color.RGBA{255, 136, 0, 255}},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got := parseHexColor(tt.input)
if got != tt.want {
t.Errorf("parseHexColor(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestMaskImage(t *testing.T) {
// Create a 100x100 white JPEG.
img := image.NewRGBA(image.Rect(0, 0, 100, 100))
for y := 0; y < 100; y++ {
for x := 0; x < 100; x++ {
img.Set(x, y, color.RGBA{255, 255, 255, 255})
}
}
var buf bytes.Buffer
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 100}); err != nil {
t.Fatal(err)
}
data := buf.Bytes()
masks := []maskRegion{
{Rect: image.Rect(10, 10, 20, 20), Color: color.RGBA{255, 0, 0, 255}},
{Rect: image.Rect(50, 50, 60, 60), Color: color.RGBA{0, 0, 255, 255}},
}
result, err := maskImage(data, masks)
if err != nil {
t.Fatal(err)
}
masked, _, err := image.Decode(bytes.NewReader(result))
if err != nil {
t.Fatal(err)
}
// Check that the masked region at (15,15) is approximately red.
r, _, _, _ := masked.At(15, 15).RGBA()
if r>>8 < 200 {
t.Errorf("expected red at (15,15), got R=%d", r>>8)
}
// Check that the masked region at (55,55) is approximately blue.
_, _, b, _ := masked.At(55, 55).RGBA()
if b>>8 < 200 {
t.Errorf("expected blue at (55,55), got B=%d", b>>8)
}
// Check that an unmasked region at (0,0) is still approximately white.
ur, ug, ub, _ := masked.At(0, 0).RGBA()
if ur>>8 < 200 || ug>>8 < 200 || ub>>8 < 200 {
t.Errorf("expected white at (0,0), got R=%d G=%d B=%d", ur>>8, ug>>8, ub>>8)
}
}