forked from deluan/lookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocr_test.go
94 lines (78 loc) · 2.16 KB
/
ocr_test.go
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
package lookup
import (
"image"
_ "image/png"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestOCR(t *testing.T) {
Convey("Given an OCR object", t, func() {
ocr := NewOCR(0.8)
Convey("When I try to load an invalid font directory", func() {
err := ocr.LoadFont("testdata/NON_EXISTENT")
Convey("It returns an error", func() {
So(err.Error(), ShouldNotBeNil)
})
})
Convey("When I load a valid font on it", func() {
err := ocr.LoadFont("testdata/font_1")
Convey("It loads the fonts successfully", func() {
So(err, ShouldBeNil)
})
Convey("It stores the font family", func() {
So(ocr.fontFamilies, ShouldContainKey, "font_1")
So(ocr.fontFamilies, ShouldHaveLength, 1)
So(ocr.fontFamilies["font_1"], ShouldHaveLength, 13)
})
Convey("It updates the totalSymbols", func() {
So(ocr.allSymbols, ShouldHaveLength, 13)
})
Convey("And when I pass an image to be recognized", func() {
img := loadImageColor("testdata/test3.png")
text, _ := ocr.Recognize(img)
Convey("It recognizes the text in the image", func() {
So(text, ShouldEqual, "3662\n32€/€")
})
})
Convey("And when I pass an subimage to be recognized", func() {
img := loadImageColor("testdata/full.png")
text, _ := ocr.Recognize(img.(*image.NRGBA).SubImage(image.Rect(1280, 646, 1280+61, 646+31)))
Convey("It only recognizes the text inside the subimage", func() {
So(text, ShouldEqual, "4339")
})
})
})
})
}
func BenchmarkOCR(b *testing.B) {
b.StopTimer()
ocr := NewOCR(0.7)
if err := ocr.LoadFont("testdata/font_1"); err != nil {
panic(err)
}
img := loadImageGray("testdata/test3.png")
if _, err := ocr.Recognize(img); err != nil {
panic(err)
}
b.StartTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = ocr.Recognize(img)
}
}
func BenchmarkOCRParallel(b *testing.B) {
b.StopTimer()
ocr := NewOCR(0.7, 5)
if err := ocr.LoadFont("testdata/font_1"); err != nil {
panic(err)
}
img := loadImageGray("testdata/test3.png")
if _, err := ocr.Recognize(img); err != nil {
panic(err)
}
b.StartTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = ocr.Recognize(img)
}
}