forked from otiai10/gosseract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_test.go
195 lines (155 loc) · 4.75 KB
/
all_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
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
package gosseract
import (
"io/ioutil"
"os"
"strings"
"testing"
"golang.org/x/net/html"
. "github.com/otiai10/mint"
)
func TestVersion(t *testing.T) {
version := Version()
Expect(t, version).Match("[0-9]{1}.[0-9]{2}(.[0-9a-z]*)?")
}
func TestClearPersistentCache(t *testing.T) {
client := NewClient()
defer client.Close()
client.init()
ClearPersistentCache()
}
func TestNewClient(t *testing.T) {
client := NewClient()
defer client.Close()
Expect(t, client).TypeOf("*gosseract.Client")
}
func TestClient_SetImage(t *testing.T) {
client := NewClient()
defer client.Close()
client.Trim = true
client.SetImage("./test/data/001-helloworld.png")
client.SetPageSegMode(PSM_SINGLE_BLOCK)
text, err := client.Text()
if client.pixImage == nil {
t.Errorf("could not set image")
}
Expect(t, err).ToBe(nil)
Expect(t, text).ToBe("Hello, World!")
client.SetImage("./test/data/001-helloworld.png")
if client.pixImage != nil {
t.Errorf("could not destory pix image")
}
client.SetImage("somewhere/fake/fakeimage.png")
_, err = client.Text()
Expect(t, err).Not().ToBe(nil)
}
func TestClient_SetImageFromBytes(t *testing.T) {
client := NewClient()
defer client.Close()
content, err := ioutil.ReadFile("./test/data/001-helloworld.png")
if err != nil {
t.Fatalf("could not read test file")
}
client.Trim = true
client.SetImageFromBytes(content)
client.SetPageSegMode(PSM_SINGLE_BLOCK)
text, err := client.Text()
if client.pixImage == nil {
t.Errorf("could not set image")
}
Expect(t, err).ToBe(nil)
Expect(t, text).ToBe("Hello, World!")
client.SetImageFromBytes(content)
if client.pixImage != nil {
t.Errorf("could not destory pix image")
}
}
func TestClient_SetWhitelist(t *testing.T) {
if os.Getenv("TESS_LSTM") == "1" {
t.Skip("Whitelist with LSTM is not working for now. Please check https://github.com/tesseract-ocr/tesseract/issues/751")
}
client := NewClient()
defer client.Close()
client.Trim = true
client.SetImage("./test/data/001-helloworld.png")
client.Languages = []string{"eng"}
client.SetWhitelist("HeloWrd,")
text, err := client.Text()
Expect(t, err).ToBe(nil)
Expect(t, text).ToBe("Hello, Worldl")
}
func TestClient_SetBlacklist(t *testing.T) {
if os.Getenv("TESS_LSTM") == "1" {
t.Skip("Blacklist with LSTM is not working for now. Please check https://github.com/tesseract-ocr/tesseract/issues/751")
}
client := NewClient()
defer client.Close()
client.Trim = true
client.SetImage("./test/data/001-helloworld.png")
client.Languages = []string{"eng"}
client.SetBlacklist("l")
text, err := client.Text()
Expect(t, err).ToBe(nil)
Expect(t, text).ToBe("He110, WorId!")
}
func TestClient_SetLanguage(t *testing.T) {
client := NewClient()
defer client.Close()
client.SetLanguage("deu")
client.SetImage("./test/data/001-helloworld.png")
_, err := client.Text()
Expect(t, err).Not().ToBe(nil)
}
func TestClient_ConfigFilePath(t *testing.T) {
if os.Getenv("TESS_LSTM") == "1" {
t.Skip("Whitelist with LSTM is not working for now. Please check https://github.com/tesseract-ocr/tesseract/issues/751")
}
client := NewClient()
defer client.Close()
err := client.SetConfigFile("./test/config/01.config")
Expect(t, err).ToBe(nil)
client.SetImage("./test/data/001-helloworld.png")
text, err := client.Text()
Expect(t, err).ToBe(nil)
Expect(t, text).ToBe("H W ")
When(t, "the config file is not found", func(t *testing.T) {
err := client.SetConfigFile("./test/config/not-existing")
Expect(t, err).Not().ToBe(nil)
})
When(t, "the config file path is a directory", func(t *testing.T) {
err := client.SetConfigFile("./test/config/02.config")
Expect(t, err).Not().ToBe(nil)
})
}
func TestClient_HTML(t *testing.T) {
client := NewClient()
defer client.Close()
client.SetImage("./test/data/001-helloworld.png")
client.SetWhitelist("Hello,World!")
out, err := client.HOCRText()
Expect(t, err).ToBe(nil)
tokenizer := html.NewTokenizer(strings.NewReader(out))
texts := []string{}
for ttype := tokenizer.Next(); ttype != html.ErrorToken; ttype = tokenizer.Next() {
token := tokenizer.Token()
if token.Type == html.TextToken && strings.TrimSpace(token.Data) != "" {
texts = append(texts, strings.Trim(token.Data, "\n"))
}
}
Expect(t, texts).ToBe([]string{"Hello,", "World!"})
When(t, "only invalid languages are given", func(t *testing.T) {
client := NewClient()
defer client.Close()
client.SetLanguage("foo")
client.SetImage("./test/data/001-helloworld.png")
_, err := client.HOCRText()
Expect(t, err).Not().ToBe(nil)
})
When(t, "undefined key-value is tried to be set", func(t *testing.T) {
client := NewClient()
defer client.Close()
client.SetVariable("foobar", "hoge")
client.SetImage("./test/data/001-helloworld.png")
_, err := client.HOCRText()
Expect(t, err).Not().ToBe(nil)
})
}