-
Notifications
You must be signed in to change notification settings - Fork 224
/
tesseract_engine.go
262 lines (194 loc) · 6.28 KB
/
tesseract_engine.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
package ocrworker
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"os/exec"
"github.com/couchbaselabs/logg"
)
// This variant of the TesseractEngine calls tesseract via exec
type TesseractEngine struct {
}
type TesseractEngineArgs struct {
configVars map[string]string `json:"config_vars"`
pageSegMode string `json:"psm"`
lang string `json:"lang"`
}
func NewTesseractEngineArgs(ocrRequest OcrRequest) (*TesseractEngineArgs, error) {
engineArgs := &TesseractEngineArgs{}
if ocrRequest.EngineArgs == nil {
return engineArgs, nil
}
// config vars
configVarsMapInterfaceOrig := ocrRequest.EngineArgs["config_vars"]
if configVarsMapInterfaceOrig != nil {
logg.LogTo("OCR_TESSERACT", "got configVarsMap: %v type: %T", configVarsMapInterfaceOrig, configVarsMapInterfaceOrig)
configVarsMapInterface := configVarsMapInterfaceOrig.(map[string]interface{})
configVarsMap := make(map[string]string)
for k, v := range configVarsMapInterface {
v, ok := v.(string)
if !ok {
return nil, fmt.Errorf("Could not convert configVar into string: %v", v)
}
configVarsMap[k] = v
}
engineArgs.configVars = configVarsMap
}
// page seg mode
pageSegMode := ocrRequest.EngineArgs["psm"]
if pageSegMode != nil {
pageSegModeStr, ok := pageSegMode.(string)
if !ok {
return nil, fmt.Errorf("Could not convert psm into string: %v", pageSegMode)
}
engineArgs.pageSegMode = pageSegModeStr
}
// language
lang := ocrRequest.EngineArgs["lang"]
if lang != nil {
langStr, ok := lang.(string)
if !ok {
return nil, fmt.Errorf("Could not convert lang into string: %v", lang)
}
engineArgs.lang = langStr
}
return engineArgs, nil
}
// return a slice that can be passed to tesseract binary as command line
// args, eg, ["-c", "tessedit_char_whitelist=0123456789", "-c", "foo=bar"]
func (t TesseractEngineArgs) Export() []string {
result := []string{}
for k, v := range t.configVars {
result = append(result, "-c")
keyValArg := fmt.Sprintf("%s=%s", k, v)
result = append(result, keyValArg)
}
if t.pageSegMode != "" {
result = append(result, "-psm")
result = append(result, t.pageSegMode)
}
if t.lang != "" {
result = append(result, "-l")
result = append(result, t.lang)
}
return result
}
func (t TesseractEngine) ProcessRequest(ocrRequest OcrRequest) (OcrResult, error) {
tmpFileName, err := func() (string, error) {
if ocrRequest.ImgBase64 != "" {
return t.tmpFileFromImageBase64(ocrRequest.ImgBase64)
} else if ocrRequest.ImgUrl != "" {
return t.tmpFileFromImageUrl(ocrRequest.ImgUrl)
} else {
return t.tmpFileFromImageBytes(ocrRequest.ImgBytes)
}
}()
if err != nil {
logg.LogTo("OCR_TESSERACT", "error getting tmpFileName")
return OcrResult{}, err
}
defer os.Remove(tmpFileName)
engineArgs, err := NewTesseractEngineArgs(ocrRequest)
if err != nil {
logg.LogTo("OCR_TESSERACT", "error getting engineArgs")
return OcrResult{}, err
}
ocrResult, err := t.processImageFile(tmpFileName, *engineArgs)
return ocrResult, err
}
func (t TesseractEngine) tmpFileFromImageBytes(imgBytes []byte) (string, error) {
logg.LogTo("OCR_TESSERACT", "Use tesseract with bytes image")
tmpFileName, err := createTempFileName()
if err != nil {
return "", err
}
// we have to write the contents of the image url to a temp
// file, because the leptonica lib can't seem to handle byte arrays
err = saveBytesToFileName(imgBytes, tmpFileName)
if err != nil {
return "", err
}
return tmpFileName, nil
}
func (t TesseractEngine) tmpFileFromImageBase64(base64Image string) (string, error) {
logg.LogTo("OCR_TESSERACT", "Use tesseract with base 64")
tmpFileName, err := createTempFileName()
if err != nil {
return "", err
}
// decoding into bytes the base64 string
decoded, decodeError := base64.StdEncoding.DecodeString(base64Image)
if decodeError != nil {
return "", err
}
err = saveBytesToFileName(decoded, tmpFileName)
if err != nil {
return "", err
}
return tmpFileName, nil
}
func (t TesseractEngine) tmpFileFromImageUrl(imgUrl string) (string, error) {
logg.LogTo("OCR_TESSERACT", "Use tesseract with url")
tmpFileName, err := createTempFileName()
if err != nil {
return "", err
}
// we have to write the contents of the image url to a temp
// file, because the leptonica lib can't seem to handle byte arrays
err = saveUrlContentToFileName(imgUrl, tmpFileName)
if err != nil {
return "", err
}
return tmpFileName, nil
}
func (t TesseractEngine) processImageFile(inputFilename string, engineArgs TesseractEngineArgs) (OcrResult, error) {
// if the input filename is /tmp/ocrimage, set the output file basename
// to /tmp/ocrimage as well, which will produce /tmp/ocrimage.txt output
tmpOutFileBaseName := inputFilename
// possible file extensions
fileExtensions := []string{"txt", "hocr"}
// build args array
cflags := engineArgs.Export()
cmdArgs := []string{inputFilename, tmpOutFileBaseName}
cmdArgs = append(cmdArgs, cflags...)
logg.LogTo("OCR_TESSERACT", "cmdArgs: %v", cmdArgs)
// exec tesseract
cmd := exec.Command("tesseract", cmdArgs...)
output, err := cmd.CombinedOutput()
if err != nil {
logg.LogTo("OCR_TESSERACT", "Error exec tesseract: %v %v", err, string(output))
return OcrResult{}, err
}
outBytes, outFile, err := findAndReadOutfile(tmpOutFileBaseName, fileExtensions)
// delete output file when we are done
defer os.Remove(outFile)
if err != nil {
logg.LogTo("OCR_TESSERACT", "Error getting data from out file: %v", err)
return OcrResult{}, err
}
return OcrResult{
Text: string(outBytes),
}, nil
}
func findOutfile(outfileBaseName string, fileExtensions []string) (string, error) {
for _, fileExtension := range fileExtensions {
outFile := fmt.Sprintf("%v.%v", outfileBaseName, fileExtension)
logg.LogTo("OCR_TESSERACT", "checking if exists: %v", outFile)
if _, err := os.Stat(outFile); err == nil {
return outFile, nil
}
}
return "", fmt.Errorf("Could not find outfile. Basename: %v Extensions: %v", outfileBaseName, fileExtensions)
}
func findAndReadOutfile(outfileBaseName string, fileExtensions []string) ([]byte, string, error) {
outfile, err := findOutfile(outfileBaseName, fileExtensions)
if err != nil {
return nil, "", err
}
outBytes, err := ioutil.ReadFile(outfile)
if err != nil {
return nil, "", err
}
return outBytes, outfile, nil
}