-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
196 lines (151 loc) · 3.49 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
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
package main
import (
"encoding/json"
"fmt"
"gocv.io/x/gocv"
"image"
)
type output struct {
Desc string `json:"desc"`
Points []image.Point `json:"points"`
}
var net *gocv.Net
var images chan *gocv.Mat
var poses chan [][]image.Point
var pose [][]image.Point
var deviceID = 0
var proto = "./src/models/pose_iter_440000.caffemodel"
var model = "./src/models/openpose_pose_coco.prototxt"
var backend = gocv.NetBackendDefault
var target = gocv.NetTargetCPU
func main() {
webcam, err := gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Printf("Error opening device %v\n", deviceID)
return
}
defer webcam.Close()
img := gocv.NewMat()
defer img.Close()
n := gocv.ReadNet(model, proto)
net = &n
if net.Empty() {
fmt.Printf("Error reading network model from : %v %v\n", model, proto)
return
}
defer net.Close()
net.SetPreferableBackend(gocv.NetBackendType(backend))
net.SetPreferableTarget(gocv.NetTargetType(target))
images = make(chan *gocv.Mat, 1)
poses = make(chan [][]image.Point)
if ok := webcam.Read(&img); !ok {
fmt.Printf("Error cannot read device %v\n", deviceID)
return
}
processFrame(&img)
go performDetection()
for {
if ok := webcam.Read(&img); !ok {
fmt.Printf("Device closed: %v\n", deviceID)
return
}
if img.Empty() {
continue
}
select {
case pose = <-poses:
processFrame(&img)
default:
}
printOutput(&img)
}
}
func processFrame(i *gocv.Mat) {
frame := gocv.NewMat()
i.CopyTo(&frame)
images <- &frame
}
func performDetection() {
for {
frame := <-images
blob := gocv.BlobFromImage(*frame, 1.0/255.0, image.Pt(368, 368), gocv.NewScalar(0, 0, 0, 0), false, false)
net.SetInput(blob, "")
prob := net.Forward("")
var midx int
s := prob.Size()
nparts, h, w := s[1], s[2], s[3]
switch nparts {
case 19:
midx = 0
nparts = 18
case 16:
midx = 1
nparts = 15
case 22:
midx = 2
default:
fmt.Println("there should be 19 parts for the COCO model, 16 for MPI, or 22 for the hand model")
return
}
pts := make([]image.Point, 22)
for i := 0; i < nparts; i++ {
pts[i] = image.Pt(-1, -1)
heatmap, _ := prob.FromPtr(h, w, gocv.MatTypeCV32F, 0, i)
_, maxVal, _, maxLoc := gocv.MinMaxLoc(heatmap)
if maxVal > 0.1 {
pts[i] = maxLoc
}
heatmap.Close()
}
sX := int(float32(frame.Cols()) / float32(w))
sY := int(float32(frame.Rows()) / float32(h))
results := [][]image.Point{}
for _, p := range PosePairs[midx] {
a := pts[p[0]]
b := pts[p[1]]
if a.X <= 0 || a.Y <= 0 || b.X <= 0 || b.Y <= 0 {
continue
}
a.X *= sX
a.Y *= sY
b.X *= sX
b.Y *= sY
results = append(results, []image.Point{a, b})
}
prob.Close()
blob.Close()
frame.Close()
poses <- results
}
}
func printOutput(frame *gocv.Mat) {
for _, pts := range pose {
res := &output{
Desc: "Output",
Points: []image.Point{ pts[0], pts[1] }}
response, _ := json.Marshal(res)
fmt.Println(response)
}
}
var PosePairs = [3][20][2]int{
{
{1, 2}, {1, 5}, {2, 3},
{3, 4}, {5, 6}, {6, 7},
{1, 8}, {8, 9}, {9, 10},
{1, 11}, {11, 12}, {12, 13},
{1, 0}, {0, 14},
{14, 16}, {0, 15}, {15, 17},
},
{
{0, 1}, {1, 2}, {2, 3},
{3, 4}, {1, 5}, {5, 6},
{6, 7}, {1, 14}, {14, 8}, {8, 9},
{9, 10}, {14, 11}, {11, 12}, {12, 13},
},
{
{0, 1}, {1, 2}, {2, 3}, {3, 4},
{0, 5}, {5, 6}, {6, 7}, {7, 8},
{0, 9}, {9, 10}, {10, 11}, {11, 12},
{0, 13}, {13, 14}, {14, 15}, {15, 16},
{0, 17}, {17, 18}, {18, 19}, {19, 20},
}}