-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocr.go
More file actions
277 lines (242 loc) · 6.36 KB
/
ocr.go
File metadata and controls
277 lines (242 loc) · 6.36 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"image"
"image/color"
"image/draw"
"image/jpeg"
"log"
"os/exec"
"regexp"
"strconv"
"strings"
)
type cropConfig struct {
X0, Y0, X1, Y1 int
}
func parseCropRect(s string) *cropConfig {
s = strings.TrimSpace(s)
if s == "" {
return nil
}
parts := strings.Split(s, ",")
if len(parts) != 4 {
log.Fatalf("crop must be x0,y0,x1,y1 but got: %q", s)
}
vals := make([]int, 4)
for i, p := range parts {
v, err := strconv.Atoi(strings.TrimSpace(p))
if err != nil {
log.Fatalf("crop: invalid integer %q: %v", p, err)
}
vals[i] = v
}
c := &cropConfig{X0: vals[0], Y0: vals[1], X1: vals[2], Y1: vals[3]}
if c.X0 >= c.X1 || c.Y0 >= c.Y1 {
log.Fatalf("crop: invalid rectangle (x0,y0 must be < x1,y1): %v", c)
}
return c
}
func cropImage(data []byte, c *cropConfig) ([]byte, error) {
img, _, err := image.Decode(bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("decode image: %w", err)
}
bounds := img.Bounds()
x0 := max(c.X0, bounds.Min.X)
y0 := max(c.Y0, bounds.Min.Y)
x1 := min(c.X1, bounds.Max.X)
y1 := min(c.Y1, bounds.Max.Y)
if x0 >= x1 || y0 >= y1 {
return nil, fmt.Errorf("crop rect outside image bounds")
}
type subImager interface {
SubImage(r image.Rectangle) image.Image
}
si, ok := img.(subImager)
if !ok {
return nil, fmt.Errorf("image type does not support cropping")
}
cropped := si.SubImage(image.Rect(x0, y0, x1, y1))
var buf bytes.Buffer
if err := jpeg.Encode(&buf, cropped, &jpeg.Options{Quality: 95}); err != nil {
return nil, fmt.Errorf("encode cropped image: %w", err)
}
return buf.Bytes(), nil
}
type maskRegion struct {
Rect image.Rectangle
Color color.RGBA
}
func parseMaskRegions(regions, colors string) []maskRegion {
regions = strings.TrimSpace(regions)
if regions == "" {
return nil
}
coords := strings.Split(regions, ",")
if len(coords)%4 != 0 {
log.Fatalf("ocr-mask-regions: must have groups of 4 coordinates (x1,y1,x2,y2), got %d values", len(coords))
}
vals := make([]int, len(coords))
for i, c := range coords {
v, err := strconv.Atoi(strings.TrimSpace(c))
if err != nil {
log.Fatalf("ocr-mask-regions: invalid integer %q: %v", c, err)
}
vals[i] = v
}
numRects := len(vals) / 4
rectColors := parseMaskColors(colors, numRects)
masks := make([]maskRegion, numRects)
for i := 0; i < numRects; i++ {
x0, y0 := vals[i*4], vals[i*4+1]
x1, y1 := vals[i*4+2], vals[i*4+3]
if x0 >= x1 || y0 >= y1 {
log.Fatalf("ocr-mask-regions: invalid rectangle #%d (x0,y0 must be < x1,y1): %d,%d,%d,%d", i+1, x0, y0, x1, y1)
}
masks[i] = maskRegion{
Rect: image.Rect(x0, y0, x1, y1),
Color: rectColors[i],
}
}
return masks
}
func parseMaskColors(s string, numRects int) []color.RGBA {
s = strings.TrimSpace(s)
defaultColor := color.RGBA{0, 0, 0, 255}
if s == "" {
colors := make([]color.RGBA, numRects)
for i := range colors {
colors[i] = defaultColor
}
return colors
}
parts := strings.Split(s, ",")
parsed := make([]color.RGBA, 0, len(parts))
for _, p := range parts {
p = strings.TrimSpace(p)
if p == "" {
continue
}
parsed = append(parsed, parseHexColor(p))
}
if len(parsed) == 1 {
colors := make([]color.RGBA, numRects)
for i := range colors {
colors[i] = parsed[0]
}
return colors
}
if len(parsed) != numRects {
log.Fatalf("ocr-mask-colors: must specify 1 color or exactly %d (one per rectangle), got %d", numRects, len(parsed))
}
return parsed
}
func parseHexColor(s string) color.RGBA {
s = strings.TrimPrefix(s, "#")
if len(s) != 6 {
log.Fatalf("ocr-mask-colors: invalid hex color %q (must be 6 hex digits like 0099CC)", s)
}
b, err := hex.DecodeString(s)
if err != nil {
log.Fatalf("ocr-mask-colors: invalid hex color %q: %v", s, err)
}
return color.RGBA{R: b[0], G: b[1], B: b[2], A: 255}
}
func maskImage(data []byte, masks []maskRegion) ([]byte, error) {
img, _, err := image.Decode(bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("decode image: %w", err)
}
bounds := img.Bounds()
dst := image.NewRGBA(bounds)
draw.Draw(dst, bounds, img, bounds.Min, draw.Src)
for _, m := range masks {
r := m.Rect.Intersect(bounds)
if r.Empty() {
continue
}
draw.Draw(dst, r, &image.Uniform{m.Color}, image.Point{}, draw.Src)
}
var buf bytes.Buffer
if err := jpeg.Encode(&buf, dst, &jpeg.Options{Quality: 95}); err != nil {
return nil, fmt.Errorf("encode masked image: %w", err)
}
return buf.Bytes(), nil
}
type ocrOutput struct {
Texts []string `json:"texts"`
Scores []float64 `json:"scores"`
}
type ocrFixRule struct {
Pattern *regexp.Regexp
Replacement string
}
func parseOCRFixRules(s string) []ocrFixRule {
s = strings.TrimSpace(s)
if s == "" {
return nil
}
parts := strings.Split(s, ",")
rules := make([]ocrFixRule, 0, len(parts))
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
idx := strings.Index(part, "=")
if idx < 0 {
log.Fatalf("ocr-fix-regex: each rule must be pattern=replacement, got: %q", part)
}
pattern := part[:idx]
replacement := part[idx+1:]
re, err := regexp.Compile(pattern)
if err != nil {
log.Fatalf("ocr-fix-regex: invalid pattern %q: %v", pattern, err)
}
rules = append(rules, ocrFixRule{Pattern: re, Replacement: replacement})
}
return rules
}
func applyFixRules(s string, rules []ocrFixRule) string {
for _, r := range rules {
s = r.Pattern.ReplaceAllString(s, r.Replacement)
}
return s
}
func extractReading(texts []string, matchRe *regexp.Regexp, fixRules []ocrFixRule) string {
// First pass: apply fix rules, then match.
for _, t := range texts {
t = applyFixRules(strings.TrimSpace(t), fixRules)
if matchRe.MatchString(t) {
return t
}
}
// Fallback: longest all-digit string.
digitsOnly := regexp.MustCompile(`^\d+$`)
best := ""
for _, t := range texts {
t = applyFixRules(strings.TrimSpace(t), fixRules)
if digitsOnly.MatchString(t) && len(t) > len(best) {
best = t
}
}
return best
}
func runOCR(imagePath string) (*ocrOutput, error) {
cmd := exec.Command(pythonBin, ocrScript, imagePath)
var stderr bytes.Buffer
cmd.Stderr = &stderr
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("%w: %s", err, stderr.String())
}
var out ocrOutput
if err := json.Unmarshal(output, &out); err != nil {
return nil, fmt.Errorf("parse output: %w (raw: %s)", err, string(output))
}
return &out, nil
}