-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
141 lines (103 loc) · 2.75 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
package main
import (
"gocv.io/x/gocv"
"fmt"
"image"
"github.com/hybridgroup/go-ncs"
"io/ioutil"
)
type DetectionResult struct {
boxes []BBox
img gocv.Mat
}
type BBox struct {
topleft image.Point
bottomright image.Point
center image.Point
label int
confidence float32
}
func main() {
useCaffeOrNCS := "ncs" // "ncs" or "caffe"
// Setup Channels
img_chan := make(chan *gocv.Mat, 1)
res_chan := make(chan DetectionResult, 1)
// Start Camera / Image Producer
go producer(img_chan)
// Start Worker (caffe or ncs)
if useCaffeOrNCS == "caffe" {
go caffeWorker(img_chan, res_chan)
}
if useCaffeOrNCS == "ncs" {
// bootstrapping NCS devices
const numNCSDevices= 2
const graphFileName= "tiny-yolov2-voc.ncsmodel"
for deviceID := 0; deviceID < numNCSDevices; deviceID++ {
res, name := ncs.GetDeviceName(deviceID)
if res != ncs.StatusOK {
fmt.Printf("NCS Error: %v\n", res)
return
}
fmt.Println(deviceID, "NCS: " + name)
// open NCS device
fmt.Println("Opening NCS device " + name + "...")
status, s := ncs.OpenDevice(name)
if status != ncs.StatusOK {
fmt.Printf("NCS Error: %v\n", status)
return
}
defer s.CloseDevice()
// load precompiled graph file in NCS format
data, err := ioutil.ReadFile(graphFileName)
if err != nil {
fmt.Println("Error opening graph file:", err)
return
}
// allocate graph on NCS stick
fmt.Println("Allocating graph...")
allocateStatus, graph := s.AllocateGraph(data)
if allocateStatus != ncs.StatusOK {
fmt.Printf("NCS Error: %v\n", allocateStatus)
return
}
defer graph.DeallocateGraph()
// Start NCS Worker
go ncsWorker(graph, img_chan, res_chan)
}
}
// Display Detections
showImg := gocv.NewMat()
window := gocv.NewWindow("Hello")
defer window.Close()
imgCounter := 0
for res := range(res_chan) {
showImg = res.img
fmt.Println(fmt.Sprintf("\nImage #%d", imgCounter))
imgCounter++
for _, box := range(res.boxes) {
b := image.Rectangle{image.Point{X:box.topleft.X, Y:box.topleft.Y}, image.Point{X:box.bottomright.X, Y:box.bottomright.Y}}
gocv.Rectangle(&showImg, b, colors[box.label % len(colors)], 2)
textPos := box.topleft
textPos.Y = textPos.Y-10
gocv.PutText(&showImg, classNames[box.label], textPos, 1, 1.0, colors[box.label % len(colors)], 2)
//fmt.Println(classNames[box.label], box.confidence)
}
window.IMShow(showImg)
if window.WaitKey(1) >= 0 {
break
}
}
}
func producer(img_chan chan <- *gocv.Mat) {
webcam, _ := gocv.VideoCaptureDevice(0)
webcam.Set(3, 800)
webcam.Set(4, 450)
webcam.Set(5, 30)
for {
img := gocv.NewMat()
webcam.Read(&img)
// Just for Debugging: Override Camera with image from disk
//img = gocv.IMRead("person.jpg", 1)
img_chan <- &img
}
}