-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathyolov3.go
306 lines (264 loc) · 8.88 KB
/
yolov3.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Package yolov3 provides a Go implementation of the YOLO V3 object detection system: https://pjreddie.com/darknet/yolo/.
//
// The yolov3 package leverages gocv(https://github.com/hybridgroup/gocv) for a neural net able to detect object.
//
// In order for the neural net to be able to detect objects, it needs the pre-trained network model
// consisting of a .cfg file and a .weights file. Using the Makefile provied by the library, these models
// can simply be downloaded by running 'make models'.
//
// In order to use the package, make sure you've checked the prerequisites in the README: https://github.com/wimspaargaren/yolov3#prerequisites
package yolov3
import (
"fmt"
"image"
"image/color"
"os"
"strings"
"gocv.io/x/gocv"
"github.com/wimspaargaren/yolov3/internal/ml"
)
// Default constants for initialising the yolov3 net.
const (
DefaultInputWidth = 416
DefaultInputHeight = 416
DefaultConfThreshold float32 = 0.5
DefaultNMSThreshold float32 = 0.4
)
// Config can be used to customise the settings of the neural network used for object detection.
type Config struct {
// InputWidth & InputHeight are used to determine the input size of the image for the network
InputWidth int
InputHeight int
// ConfidenceThreshold can be used to determine the minimum confidence before an object is considered to be "detected"
ConfidenceThreshold float32
// Non-maximum suppression threshold used for removing overlapping bounding boxes
NMSThreshold float32
// Type on which the network will be executed
NetTargetType gocv.NetTargetType
NetBackendType gocv.NetBackendType
// NewNet function can be used to inject a custom neural net
NewNet func(weightsPath, configPath string) ml.NeuralNet
}
// validate ensures that the basic fields of the config are set
func (c *Config) validate() {
if c.NewNet == nil {
c.NewNet = initializeNet
}
if c.InputWidth == 0 {
c.InputWidth = DefaultInputWidth
}
if c.InputHeight == 0 {
c.InputHeight = DefaultInputHeight
}
}
// DefaultConfig used to create a working yolov3 net out of the box.
func DefaultConfig() Config {
return Config{
InputWidth: DefaultInputWidth,
InputHeight: DefaultInputHeight,
ConfidenceThreshold: DefaultConfThreshold,
NMSThreshold: DefaultNMSThreshold,
NetTargetType: gocv.NetTargetCPU,
NetBackendType: gocv.NetBackendDefault,
NewNet: initializeNet,
}
}
// ObjectDetection represents information of an object detected by the neural net.
type ObjectDetection struct {
ClassID int
ClassName string
BoundingBox image.Rectangle
Confidence float32
}
// Net the yolov3 net.
type Net interface {
Close() error
GetDetections(gocv.Mat) ([]ObjectDetection, error)
GetDetectionsWithFilter(gocv.Mat, map[string]bool) ([]ObjectDetection, error)
}
// yoloNet the net implementation.
type yoloNet struct {
net ml.NeuralNet
cocoNames []string
DefaultInputWidth int
DefaultInputHeight int
confidenceThreshold float32
DefaultNMSThreshold float32
}
// NewNet creates new yolo net for given weight path, config and coconames list.
func NewNet(weightsPath, configPath, cocoNamePath string) (Net, error) {
return NewNetWithConfig(weightsPath, configPath, cocoNamePath, DefaultConfig())
}
// NewNetWithConfig creates new yolo net with given config.
func NewNetWithConfig(weightsPath, configPath, cocoNamePath string, config Config) (Net, error) {
if _, err := os.Stat(weightsPath); os.IsNotExist(err) {
return nil, fmt.Errorf("path to net weights not found")
}
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return nil, fmt.Errorf("path to net config not found")
}
cocoNames, err := getCocoNames(cocoNamePath)
if err != nil {
return nil, err
}
config.validate()
net := config.NewNet(weightsPath, configPath)
err = setNetTargetTypes(net, config)
if err != nil {
return nil, err
}
return &yoloNet{
net: net,
cocoNames: cocoNames,
DefaultInputWidth: config.InputWidth,
DefaultInputHeight: config.InputHeight,
confidenceThreshold: config.ConfidenceThreshold,
DefaultNMSThreshold: config.NMSThreshold,
}, nil
}
// initializeNet default method for creating neural network, leveraging gocv.
func initializeNet(weightsPath, configPath string) ml.NeuralNet {
net := gocv.ReadNet(weightsPath, configPath)
return &net
}
func setNetTargetTypes(net ml.NeuralNet, config Config) error {
err := net.SetPreferableBackend(config.NetBackendType)
if err != nil {
return err
}
err = net.SetPreferableTarget(config.NetTargetType)
if err != nil {
return err
}
return nil
}
// Close closes the net.
func (y *yoloNet) Close() error {
return y.net.Close()
}
// GetDetections retrieve predicted detections from given matrix.
func (y *yoloNet) GetDetections(frame gocv.Mat) ([]ObjectDetection, error) {
return y.GetDetectionsWithFilter(frame, make(map[string]bool))
}
// GetDetectionsWithFilter allows you to detect objects, but filter out a given list of coco name ids.
func (y *yoloNet) GetDetectionsWithFilter(frame gocv.Mat, classIDsFilter map[string]bool) ([]ObjectDetection, error) {
fl := []string{"yolo_82", "yolo_94", "yolo_106"}
blob := gocv.BlobFromImage(frame, 1.0/255.0, image.Pt(y.DefaultInputWidth, y.DefaultInputHeight), gocv.NewScalar(0, 0, 0, 0), true, false)
// nolint: errcheck
defer blob.Close()
y.net.SetInput(blob, "data")
outputs := y.net.ForwardLayers(fl)
for i := 0; i < len(outputs); i++ {
// nolint: errcheck
defer outputs[i].Close()
}
detections, err := y.processOutputs(frame, outputs, classIDsFilter)
if err != nil {
return nil, err
}
return detections, nil
}
// processOutputs process detected rows in the outputs.
func (y *yoloNet) processOutputs(frame gocv.Mat, outputs []gocv.Mat, filter map[string]bool) ([]ObjectDetection, error) {
detections := []ObjectDetection{}
bboxes := []image.Rectangle{}
confidences := []float32{}
for i := 0; i < len(outputs); i++ {
data, err := outputs[i].DataPtrFloat32()
if err != nil {
return nil, err
}
for j := 0; j < outputs[i].Total(); j += outputs[i].Cols() {
row := data[j : j+outputs[i].Cols()]
scores := row[5:]
classID, confidence := getClassIDAndConfidence(scores)
if y.isFiltered(classID, filter) {
continue
}
if confidence > y.confidenceThreshold {
confidences = append(confidences, confidence)
boundingBox := calculateBoundingBox(frame, row)
bboxes = append(bboxes, boundingBox)
detections = append(detections, ObjectDetection{
ClassID: classID,
ClassName: y.cocoNames[classID],
BoundingBox: boundingBox,
Confidence: confidence,
})
}
}
}
if len(bboxes) == 0 {
return detections, nil
}
indices := gocv.NMSBoxes(bboxes, confidences, y.confidenceThreshold, y.DefaultNMSThreshold)
result := []ObjectDetection{}
for i, indice := range indices {
// If we encounter value 0 skip the detection
// except for the first indice
if i != 0 && indice == 0 {
continue
}
result = append(result, detections[indice])
}
return result, nil
}
func (y *yoloNet) isFiltered(classID int, classIDs map[string]bool) bool {
if classIDs == nil {
return false
}
return classIDs[y.cocoNames[classID]]
}
// calculateBoundingBox calculate the bounding box of the detected object.
func calculateBoundingBox(frame gocv.Mat, row []float32) image.Rectangle {
if len(row) < 4 {
return image.Rect(0, 0, 0, 0)
}
centerX := int(row[0] * float32(frame.Cols()))
centerY := int(row[1] * float32(frame.Rows()))
width := int(row[2] * float32(frame.Cols()))
height := int(row[3] * float32(frame.Rows()))
left := (centerX - width/2)
top := (centerY - height/2)
return image.Rect(left, top, left+width, top+height)
}
// getClassID retrieve class id from given row.
func getClassIDAndConfidence(x []float32) (int, float32) {
res := 0
max := float32(0.0)
for i, y := range x {
if y > max {
max = y
res = i
}
}
return res, max
}
// getCocoNames read coconames from given path.
func getCocoNames(path string) ([]string, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return strings.Split(string(content), "\n"), nil
}
// DrawDetections draws a given list of object detections on a gocv Matrix.
func DrawDetections(frame *gocv.Mat, detections []ObjectDetection) {
for i := 0; i < len(detections); i++ {
detection := detections[i]
text := fmt.Sprintf("%s:%.2f", detection.ClassName, detection.Confidence)
// Create bounding box of object
blue := color.RGBA{0, 0, 255, 0}
gocv.Rectangle(frame, detection.BoundingBox, blue, 3)
// Add text background
black := color.RGBA{0, 0, 0, 0}
size := gocv.GetTextSize(text, gocv.FontHersheySimplex, 0.5, 1)
r := detection.BoundingBox
textBackground := image.Rect(r.Min.X, r.Min.Y-size.Y-1, r.Min.X+size.X, r.Min.Y)
gocv.Rectangle(frame, textBackground, black, int(gocv.Filled))
// Add text
pt := image.Pt(r.Min.X, r.Min.Y-4)
white := color.RGBA{255, 255, 255, 0}
gocv.PutText(frame, text, pt, gocv.FontHersheySimplex, 0.5, white, 1)
}
}