-
Notifications
You must be signed in to change notification settings - Fork 224
/
stroke_width_transform.go
89 lines (69 loc) · 1.89 KB
/
stroke_width_transform.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
package ocrworker
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"github.com/couchbaselabs/logg"
)
type StrokeWidthTransformer struct {
}
func (s StrokeWidthTransformer) preprocess(ocrRequest *OcrRequest) error {
// write bytes to a temp file
tmpFileNameInput, err := createTempFileName()
tmpFileNameInput = fmt.Sprintf("%s.png", tmpFileNameInput)
if err != nil {
return err
}
defer os.Remove(tmpFileNameInput)
tmpFileNameOutput, err := createTempFileName()
tmpFileNameOutput = fmt.Sprintf("%s.png", tmpFileNameOutput)
if err != nil {
return err
}
defer os.Remove(tmpFileNameOutput)
err = saveBytesToFileName(ocrRequest.ImgBytes, tmpFileNameInput)
if err != nil {
return err
}
// run DecodeText binary on it (if not in path, print warning and do nothing)
darkOnLightSetting := s.extractDarkOnLightParam(*ocrRequest)
logg.LogTo(
"PREPROCESSOR_WORKER",
"DetectText on %s -> %s with %s",
tmpFileNameInput,
tmpFileNameOutput,
darkOnLightSetting,
)
out, err := exec.Command(
"DetectText",
tmpFileNameInput,
tmpFileNameOutput,
darkOnLightSetting,
).CombinedOutput()
if err != nil {
logg.LogFatal("Error running command: %s. out: %s", err, out)
}
logg.LogTo("PREPROCESSOR_WORKER", "output: %v", string(out))
// read bytes from output file into ocrRequest.ImgBytes
resultBytes, err := ioutil.ReadFile(tmpFileNameOutput)
if err != nil {
return err
}
ocrRequest.ImgBytes = resultBytes
return nil
}
func (s StrokeWidthTransformer) extractDarkOnLightParam(ocrRequest OcrRequest) string {
logg.LogTo("PREPROCESSOR_WORKER", "extract dark on light param")
val := "1"
preprocessorArgs := ocrRequest.PreprocessorArgs
swtArgs := preprocessorArgs[PreprocessorStrokeWidthTransform]
if swtArgs != nil {
swtArg, ok := swtArgs.(string)
if ok && (swtArg == "0" || swtArg == "1") {
val = swtArg
}
}
logg.LogTo("PREPROCESSOR_WORKER", "return val: %s", val)
return val
}