-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
161 lines (140 loc) · 3.51 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
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
package main
import (
"encoding/base64"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"math/rand"
"os"
"strings"
"time"
"github.com/Kardbord/hfapigo/v3"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
)
const HuggingFaceTokenEnv = "HUGGING_FACE_TOKEN"
func init() {
rand.Seed(time.Now().UnixNano())
key := os.Getenv(HuggingFaceTokenEnv)
if key != "" {
hfapigo.SetAPIKey(key)
}
}
const (
inputImage = "./test-image.png"
outputImage = "./test-image-output.png"
)
func main() {
resps, err := SendRequest()
if err != nil {
fmt.Println("Error:", err)
return
}
srcImg, err := OpenImg(inputImage)
if err != nil {
fmt.Println("Error:", err)
return
}
err = DrawMasks(srcImg, resps)
if err != nil {
fmt.Println("Error:", err)
return
}
err = WriteOutputFile(srcImg, outputImage)
if err != nil {
fmt.Println("Error:", err)
return
}
}
func OpenImg(imgFile string) (draw.Image, error) {
fmt.Printf("Opening image: %s\n", imgFile)
f, err := os.Open(inputImage)
if err != nil {
return nil, err
}
defer f.Close()
img, err := png.Decode(f)
dimg, ok := img.(draw.Image)
if !ok {
return nil, fmt.Errorf("%T is not a drawable image type", img)
}
return dimg, err
}
func SendRequest() ([]*hfapigo.ImageSegmentationResponse, error) {
fmt.Printf("Requesting segmentation of image: %s\n", inputImage)
return hfapigo.SendImageSegmentationRequest(hfapigo.RecommendedImageSegmentationModel, inputImage)
}
// See https://go.dev/blog/image-draw
func DrawMasks(srcImg draw.Image, resps []*hfapigo.ImageSegmentationResponse) error {
type LabelCoords struct {
X int
Y int
}
labelCoords := []LabelCoords{}
for segmentN, r := range resps {
reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(r.Mask))
mask, _, err := image.Decode(reader)
if err != nil {
return err
}
WriteOutputFile(mask, fmt.Sprintf("mask%d.png", segmentN))
fmt.Printf("Label: %s, Score: %f\n", r.Label, r.Score)
segment := image.NewRGBA(mask.Bounds())
segmentColor := color.RGBA{
R: uint8(rand.Intn(266)),
G: uint8(rand.Intn(266)),
B: uint8(rand.Intn(266)),
A: 210,
}
// There's probably a better way to do this, but image
// processing is not my forte.
for x := 0; x < segment.Bounds().Dx(); x++ {
for y := 0; y < segment.Bounds().Dy(); y++ {
mr, mg, mb, ma := mask.At(x, y).RGBA()
if mr+mg+mb == ma*3 {
segment.SetRGBA(x, y, segmentColor)
if len(labelCoords) == segmentN {
labelCoords = append(labelCoords, LabelCoords{
X: x + 5,
Y: y + 20,
})
}
}
}
}
WriteOutputFile(segment, fmt.Sprintf("segment%d.png", segmentN))
draw.DrawMask(srcImg, srcImg.Bounds(), segment, image.Point{}, mask, image.Point{}, draw.Over)
}
// Add labels last so they show through.
for i := range resps {
AddLabel(srcImg, labelCoords[i].X, labelCoords[i].Y, fmt.Sprintf("%s (%.2f%%)", resps[i].Label, resps[i].Score*100))
}
return nil
}
func WriteOutputFile(img image.Image, filename string) error {
outf, err := os.Create(filename)
if err != nil {
return err
}
defer outf.Close()
err = png.Encode(outf, img)
if err != nil {
return err
}
fmt.Println("Output image written to", outf.Name())
return nil
}
func AddLabel(img draw.Image, x, y int, label string) {
col := color.RGBA{0, 0, 0, 255}
point := fixed.Point26_6{X: fixed.Int26_6(x * 64), Y: fixed.Int26_6(y * 64)}
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(col),
Face: basicfont.Face7x13,
Dot: point,
}
d.DrawString(label)
}