-
-
Notifications
You must be signed in to change notification settings - Fork 289
/
all_test.go
329 lines (264 loc) · 7.93 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
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
package gosseract
import (
"encoding/xml"
"image"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
. "github.com/otiai10/mint"
)
func TestMain(m *testing.M) {
beforeTest()
code := m.Run()
os.Exit(code)
}
func beforeTest() {
if strings.HasPrefix(Version(), "4.0") {
os.Setenv("TESS_LSTM_DISABLED", "1")
}
switch os.Getenv("TESTCASE") {
case "archlinux", "centos", "debian", "fedora", "mingw":
os.Setenv("TESS_BOX_DISABLED", "1")
}
}
func TestVersion(t *testing.T) {
version := Version()
Expect(t, version).Match("[0-9]{1}.[0-9]{1,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 TestDoubleClose(t *testing.T) {
client := NewClient()
client.Close()
client.Close()
}
func TestClient_SetTessdataPrefix(t *testing.T) {
client := NewClient()
defer client.Close()
cwd, err := os.Getwd()
Expect(t, err).ToBe(nil)
testModelDir := filepath.Join(cwd, "test-model", "tessdata")
err = os.MkdirAll(testModelDir, 0770)
Expect(t, err).ToBe(nil)
defer os.RemoveAll(filepath.Dir(testModelDir))
src, err := os.Open(filepath.Join(getDataPath(), "eng.traineddata"))
Expect(t, err).ToBe(nil)
defer src.Close()
dst, err := os.Create(filepath.Join(testModelDir, "eng.traineddata"))
Expect(t, err).ToBe(nil)
_, err = io.Copy(dst, src)
Expect(t, err).ToBe(nil)
dst.Close()
client.Trim = true
client.SetImage("./test/data/001-helloworld.png")
client.SetLanguage("eng")
client.SetTessdataPrefix(testModelDir)
text, err := client.Text()
Expect(t, err).ToBe(nil)
Expect(t, text).ToBe("Hello, World!")
}
func TestClient_Version(t *testing.T) {
client := NewClient()
defer client.Close()
version := client.Version()
Expect(t, version).Match("[0-9]{1}.[0-9]{1,2}(.[0-9a-z_-]*)?")
}
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!")
err = client.SetImage("./test/data/001-helloworld.png")
Expect(t, err).ToBe(nil)
err = client.SetImage("")
Expect(t, err).Not().ToBe(nil)
err = client.SetImage("somewhere/fake/fakeimage.png")
Expect(t, err).Not().ToBe(nil)
_, err = client.Text()
Expect(t, err).ToBe(nil)
Because(t, "api must be initialized beforehand", func(t *testing.T) {
client := &Client{}
err := client.SetImage("./test/data/001-helloworld.png")
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!")
err = client.SetImageFromBytes(content)
Expect(t, err).ToBe(nil)
err = client.SetImageFromBytes(nil)
Expect(t, err).Not().ToBe(nil)
Because(t, "api must be initialized beforehand", func(t *testing.T) {
client := &Client{}
err := client.SetImageFromBytes(content)
Expect(t, err).Not().ToBe(nil)
})
}
func TestClient_SetWhitelist(t *testing.T) {
if os.Getenv("TESS_LSTM_DISABLED") == "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")
Expect(t, text).Match("Hello, ?Worldl?")
}
func TestClient_SetBlacklist(t *testing.T) {
if os.Getenv("TESS_LSTM_DISABLED") == "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
err := client.SetImage("./test/data/001-helloworld.png")
Expect(t, err).ToBe(nil)
client.Languages = []string{"eng"}
err = client.SetBlacklist("l")
Expect(t, err).ToBe(nil)
text, err := client.Text()
Expect(t, err).ToBe(nil)
Expect(t, text).Match("He(110|tto|o), Wor(I|t)?d!")
}
func TestClient_SetLanguage(t *testing.T) {
client := NewClient()
defer client.Close()
err := client.SetLanguage("undefined-language")
Expect(t, err).ToBe(nil)
err = client.SetLanguage()
Expect(t, err).Not().ToBe(nil)
client.SetImage("./test/data/001-helloworld.png")
_, err = client.Text()
Expect(t, err).Not().ToBe(nil)
if os.Getenv("GOSSERACT_CPPSTDERR_NOT_CAPTURED") != "1" {
Expect(t, err).Match("Failed loading language 'undefined-language'")
}
}
func TestClient_ConfigFilePath(t *testing.T) {
if os.Getenv("TESS_LSTM_DISABLED") == "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).Match("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 TestClientBoundingBox(t *testing.T) {
if os.Getenv("TESS_BOX_DISABLED") == "1" {
t.Skip()
}
client := NewClient()
defer client.Close()
client.SetImage("./test/data/001-helloworld.png")
client.SetWhitelist("Hello,World!")
boxes, err := client.GetBoundingBoxes(RIL_WORD)
Expect(t, err).ToBe(nil)
Because(t, "api must be initialized beforehand", func(t *testing.T) {
client := &Client{}
_, err := client.GetBoundingBoxes(RIL_WORD)
Expect(t, err).Not().ToBe(nil)
})
words := []string{"Hello,World!"}
coords := []image.Rectangle{
image.Rect(74, 64, 1099, 190),
}
if os.Getenv("TESS_LSTM_DISABLED") == "1" {
t.Skip()
}
for i, box := range boxes {
Expect(t, box.Word).ToBe(words[i])
Expect(t, box.Box).ToBe(coords[i])
}
}
func TestClient_HTML(t *testing.T) {
if os.Getenv("TESS_BOX_DISABLED") == "1" {
t.Skip()
}
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)
page := new(Page)
err = xml.Unmarshal([]byte(out), page)
Expect(t, err).ToBe(nil)
Expect(t, len(page.Content.Par.Lines)).ToBe(1)
if os.Getenv("TESS_LSTM_DISABLED") != "1" {
Expect(t, len(page.Content.Par.Lines[0].Words)).ToBe(1)
Expect(t, page.Content.Par.Lines[0].Words[0].Characters).ToBe("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)
})
Because(t, "unknown key is validated when `init` is called", func(t *testing.T) {
client := NewClient()
defer client.Close()
err := client.SetVariable("foobar", "hoge")
Expect(t, err).ToBe(nil)
client.SetImage("./test/data/001-helloworld.png")
_, err = client.Text()
Expect(t, err).Not().ToBe(nil)
})
}
func TestGetAvailableLangs(t *testing.T) {
t.Skip("TODO")
// langs, err := GetAvailableLanguages()
// Expect(t, err).ToBe(nil)
// Expect(t, len(langs)).ToBe(1) // eng only
}