-
Notifications
You must be signed in to change notification settings - Fork 225
/
ocr_engine.go
75 lines (61 loc) · 1.34 KB
/
ocr_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
package ocrworker
import (
"encoding/json"
"strings"
"github.com/couchbaselabs/logg"
)
type OcrEngineType int
const (
ENGINE_TESSERACT = OcrEngineType(iota)
ENGINE_GO_TESSERACT
ENGINE_MOCK
)
type OcrEngine interface {
ProcessRequest(ocrRequest OcrRequest) (OcrResult, error)
}
func NewOcrEngine(engineType OcrEngineType) OcrEngine {
switch engineType {
case ENGINE_MOCK:
return &MockEngine{}
case ENGINE_TESSERACT:
return &TesseractEngine{}
}
return nil
}
func (e OcrEngineType) String() string {
switch e {
case ENGINE_MOCK:
return "ENGINE_MOCK"
case ENGINE_TESSERACT:
return "ENGINE_TESSERACT"
case ENGINE_GO_TESSERACT:
return "ENGINE_GO_TESSERACT"
}
return ""
}
func (e *OcrEngineType) UnmarshalJSON(b []byte) (err error) {
var engineTypeStr string
if err := json.Unmarshal(b, &engineTypeStr); err == nil {
engineString := strings.ToUpper(engineTypeStr)
switch engineString {
case "TESSERACT":
*e = ENGINE_TESSERACT
case "GO_TESSERACT":
*e = ENGINE_GO_TESSERACT
case "MOCK":
*e = ENGINE_MOCK
default:
logg.LogWarn("Unexpected OcrEngineType json: %v", engineString)
*e = ENGINE_MOCK
}
return nil
}
// not a string .. maybe it's an int
var engineTypeInt int
if err := json.Unmarshal(b, &engineTypeInt); err == nil {
*e = OcrEngineType(engineTypeInt)
return nil
} else {
return err
}
}