-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
103 lines (89 loc) · 1.78 KB
/
main.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
package main
import (
"bytes"
"image"
"image/png"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/disintegration/gift"
"github.com/otiai10/gosseract/v2"
)
func main() {
if len(os.Args) < 2 {
panic("missing image path")
}
if len(os.Args) < 3 {
panic("missing blur level number [1-10]")
}
if len(os.Args) < 4 {
panic("missing text confidence level number [1-100]")
}
path := os.Args[1]
blurLevel := os.Args[2]
minConfidence := os.Args[3]
err := blur(path, blurLevel, minConfidence)
if err != nil {
panic(err)
}
}
func blur(path string, blurLevelStr, minConfidenceStr string) error {
blurLevel, err := strconv.Atoi(blurLevelStr)
if err != nil {
return err
}
minConfidence, err := strconv.Atoi(minConfidenceStr)
if err != nil {
return err
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return err
}
img, err := png.Decode(bytes.NewBuffer(data))
if err != nil {
return err
}
rgba := image.NewRGBA(img.Bounds())
gift.New().Draw(rgba, img)
client := gosseract.NewClient()
defer client.Close()
client.SetImageFromBytes(data)
boxes, err := client.GetBoundingBoxes(gosseract.RIL_WORD)
if err != nil {
return err
}
for _, box := range boxes {
if box.Confidence < float64(minConfidence) {
continue
}
bounds := box.Box.Bounds()
gift.New(gift.GaussianBlur(float32(blurLevel))).DrawAt(
rgba,
rgba.SubImage(box.Box),
image.Pt(bounds.Min.X, bounds.Min.Y),
gift.OverOperator,
)
}
newPath := strings.TrimSuffix(path, filepath.Ext(path)) + "_blurred.png"
f, err := os.Create(newPath)
if err != nil {
return err
}
err = png.Encode(f, rgba)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
return nil
}