forked from tleyden/open-ocr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tesseract_engine.go
262 lines (213 loc) · 7.15 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"
"os"
"os/exec"
"github.com/rs/zerolog/log"
)
// 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"`
saveFiles bool
}
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 {
log.Info().Str("component", "OCR_TESSERACT").
Interface("configVarsMapInterfaceOrig", configVarsMapInterfaceOrig).
Interface("configVarsMapInterfaceOrig", configVarsMapInterfaceOrig).Msg("got configVarsMap")
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
}
// Export 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 {
var 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", t.pageSegMode)
}
if t.lang != "" {
result = append(result, "-l", t.lang)
}
return result
}
// ProcessRequest will process incoming OCR request by routing it through the whole process chain
func (t TesseractEngine) ProcessRequest(ocrRequest *OcrRequest, _ *WorkerConfig) (OcrResult, error) {
tmpFileName, err := func() (string, error) {
switch {
case ocrRequest.ImgBase64 != "":
return t.tmpFileFromImageBase64(ocrRequest.ImgBase64)
case ocrRequest.ImgUrl != "":
return t.tmpFileFromImageUrl(ocrRequest.ImgUrl)
default:
return t.tmpFileFromImageBytes(ocrRequest.ImgBytes)
}
}()
if err != nil {
log.Error().Err(err).Str("component", "OCR_TESSERACT").Msg("error getting tmpFileName")
return OcrResult{}, err
}
engineArgs, err := NewTesseractEngineArgs(ocrRequest)
if err != nil {
log.Error().Err(err).Str("component", "OCR_TESSERACT").Caller().Msg("error getting engineArgs")
return OcrResult{}, err
}
if engineArgs.saveFiles {
defer func(name string) {
err := os.Remove(name)
if err != nil {
log.Warn().Err(err).Str("component", "OCR_TESSERACT").Caller().Msg(name + " could not be removed")
}
}(tmpFileName)
}
ocrResult, err := t.processImageFile(tmpFileName, *engineArgs)
return ocrResult, err
}
func (TesseractEngine) tmpFileFromImageBytes(imgBytes []byte) (string, error) {
log.Info().Str("component", "OCR_TESSERACT").Msg("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 (TesseractEngine) tmpFileFromImageBase64(base64Image string) (string, error) {
log.Info().Str("component", "OCR_TESSERACT").Msg("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 (TesseractEngine) tmpFileFromImageUrl(imgUrl string) (string, error) {
log.Info().Str("component", "OCR_TESSERACT").Msg("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 (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", "json"}
// build args array
cflags := engineArgs.Export()
cmdArgs := []string{inputFilename, tmpOutFileBaseName}
cmdArgs = append(cmdArgs, cflags...)
log.Info().Str("component", "OCR_TESSERACT").Interface("cmdArgs", cmdArgs)
// exec tesseract
cmd := exec.Command("tesseract", cmdArgs...)
output, err := cmd.CombinedOutput()
if err != nil {
log.Error().Err(err).Str("component", "OCR_TESSERACT").Str("component", "OCR_TESSERACT").
Msg(string(output))
return OcrResult{Status: "error"}, err
}
outBytes, outFile, err := findAndReadOutfile(tmpOutFileBaseName, fileExtensions)
// delete output file when we are done
if engineArgs.saveFiles {
defer func(name string) {
err := os.Remove(name)
if err != nil {
log.Warn().Err(err).Str("component", "OCR_TESSERACT").Caller().Msg(name + " could not be removed")
}
}(outFile)
}
if err != nil {
log.Error().Err(err).Str("component", "OCR_TESSERACT").
Str("file_name", tmpOutFileBaseName).Msg("Error getting data from out file")
return OcrResult{Status: "error"}, err
}
return OcrResult{
Text: string(outBytes),
Status: "done",
}, nil
}
func findOutfile(outfileBaseName string, fileExtensions []string) (string, error) {
for _, fileExtension := range fileExtensions {
outFile := fmt.Sprintf("%v.%v", outfileBaseName, fileExtension)
log.Info().Str("component", "OCR_TESSERACT").Str("outFile", outFile).
Msg("check if file exists")
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) (outBytes []byte, outfile string, err error) {
outfile, err = findOutfile(outfileBaseName, fileExtensions)
if err != nil {
return nil, "", err
}
outBytes, err = os.ReadFile(outfile)
if err != nil {
return nil, "", err
}
return outBytes, outfile, nil
}