-
Notifications
You must be signed in to change notification settings - Fork 871
/
main.go
148 lines (125 loc) · 3.79 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
// What it does:
//
// This example uses the Caffe (http://caffe.berkeleyvision.org/) deep learning framework
// to classify whatever is in front of the camera.
//
// Download the Caffe model file from:
// http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel
//
// Also, you will need the prototxt file:
// https://raw.githubusercontent.com/opencv/opencv_extra/master/testdata/dnn/bvlc_googlenet.prototxt
//
// And the words text file with the descriptions:
// https://raw.githubusercontent.com/opencv/opencv/master/samples/data/dnn/classification_classes_ILSVRC2012.txt
//
// How to run:
//
// go run ./cmd/caffe-classifier/main.go 0 ~/Downloads/bvlc_googlenet.caffemodel ~/Downloads/bvlc_googlenet.prototxt ~/Downloads/classification_classes_ILSVRC2012.txt
//
// You can also use this sample with the Intel OpenVINO Inference Engine, if you have it installed.
//
// go run ./cmd/caffe-classifier/main.go 0 ~/Downloads/bvlc_googlenet.caffemodel ~/Downloads/bvlc_googlenet.prototxt ~/Downloads/classification_classes_ILSVRC2012.txt openvino fp16
//
package main
import (
"bufio"
"fmt"
"image"
"image/color"
"os"
"path/filepath"
"gocv.io/x/gocv"
)
func main() {
if len(os.Args) < 5 {
fmt.Println("How to run:\ncaffe-classifier [camera ID] [modelfile] [configfile] [descriptionsfile] ([backend] [device])")
return
}
// parse args
deviceID := os.Args[1]
model := os.Args[2]
config := os.Args[3]
descr := os.Args[4]
descriptions, err := readDescriptions(descr)
if err != nil {
fmt.Printf("Error reading descriptions file: %v\n", descr)
return
}
backend := gocv.NetBackendDefault
if len(os.Args) > 5 {
backend = gocv.ParseNetBackend(os.Args[5])
}
target := gocv.NetTargetCPU
if len(os.Args) > 6 {
target = gocv.ParseNetTarget(os.Args[6])
}
// open capture device
webcam, err := gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Printf("Error opening video capture device: %v\n", deviceID)
return
}
defer webcam.Close()
window := gocv.NewWindow("Caffe Classifier")
defer window.Close()
img := gocv.NewMat()
defer img.Close()
// open DNN classifier
net := gocv.ReadNet(model, config)
if net.Empty() {
fmt.Printf("Error reading network model from : %v %v\n", model, config)
return
}
defer net.Close()
net.SetPreferableBackend(gocv.NetBackendType(backend))
net.SetPreferableTarget(gocv.NetTargetType(target))
status := "Ready"
statusColor := color.RGBA{0, 255, 0, 0}
fmt.Printf("Start reading device: %v\n", deviceID)
for {
if ok := webcam.Read(&img); !ok {
fmt.Printf("Device closed: %v\n", deviceID)
return
}
if img.Empty() {
continue
}
// convert image Mat to 224x224 blob that the classifier can analyze
blob := gocv.BlobFromImage(img, 1.0, image.Pt(224, 224), gocv.NewScalar(104, 117, 123, 0), false, false)
// feed the blob into the classifier
net.SetInput(blob, "")
// run a forward pass thru the network
prob := net.Forward("")
// reshape the results into a 1x1000 matrix
probMat := prob.Reshape(1, 1)
// determine the most probable classification
_, maxVal, _, maxLoc := gocv.MinMaxLoc(probMat)
// display classification
status = fmt.Sprintf("description: %v, maxVal: %v\n", descriptions[maxLoc.X], maxVal)
gocv.PutText(&img, status, image.Pt(10, 20), gocv.FontHersheyPlain, 1.2, statusColor, 2)
blob.Close()
prob.Close()
probMat.Close()
window.IMShow(img)
if window.WaitKey(1) >= 0 {
break
}
}
}
// readDescriptions reads the descriptions from a file
// and returns a slice of its lines.
func readDescriptions(path string) ([]string, error) {
file, err := os.Open(filepath.Clean(path))
if err != nil {
return nil, err
}
defer func() {
_ = file.Close()
}()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}