forked from tleyden/open-ocr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr_request.go
65 lines (51 loc) · 1.74 KB
/
ocr_request.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
package ocrworker
import "fmt"
import "encoding/base64"
type OcrRequest struct {
ImgUrl string `json:"img_url"`
ImgBase64 string `json:"img_base64"`
EngineType OcrEngineType `json:"engine"`
ImgBytes []byte `json:"img_bytes"`
PreprocessorChain []string `json:"preprocessors"`
PreprocessorArgs map[string]interface{} `json:"preprocessor-args"`
EngineArgs map[string]interface{} `json:"engine_args"`
// decode ocr in http handler rather than putting in queue
InplaceDecode bool `json:"inplace_decode"`
}
// figure out the next pre-processor routing key to use (if any).
// if we have finished with the pre-processors, then use the processorRoutingKey
func (ocrRequest *OcrRequest) nextPreprocessor(processorRoutingKey string) string {
if len(ocrRequest.PreprocessorChain) == 0 {
return processorRoutingKey
} else {
var x string
s := ocrRequest.PreprocessorChain
x, s = s[len(s)-1], s[:len(s)-1]
ocrRequest.PreprocessorChain = s
return x
}
}
func (ocrRequest *OcrRequest) decodeBase64() error {
bytes, decodeError := base64.StdEncoding.DecodeString(ocrRequest.ImgBase64)
if decodeError != nil {
return decodeError
}
ocrRequest.ImgBytes = bytes
ocrRequest.ImgBase64 = ""
return nil
}
func (ocrRequest *OcrRequest) hasBase64() bool {
return ocrRequest.ImgBase64 != ""
}
func (ocrRequest *OcrRequest) downloadImgUrl() error {
bytes, err := url2bytes(ocrRequest.ImgUrl)
if err != nil {
return err
}
ocrRequest.ImgBytes = bytes
ocrRequest.ImgUrl = ""
return nil
}
func (o OcrRequest) String() string {
return fmt.Sprintf("ImgUrl: %s, EngineType: %s, Preprocessors: %s", o.ImgUrl, o.EngineType, o.PreprocessorChain)
}