-
Notifications
You must be signed in to change notification settings - Fork 26
/
yolov4.go
198 lines (177 loc) · 5.26 KB
/
yolov4.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
package core
import (
"bufio"
"fmt"
"image"
"image/color"
"os"
"sort"
"strconv"
"github.com/pfhds/live-stream-ai/models"
"gocv.io/x/gocv"
)
var YoloMs *YoloManager
// 读取label
func readLabels(path string) []string {
var classes []string
read, _ := os.Open(fmt.Sprintf("%v/coco.names", path))
defer read.Close()
scanner := bufio.NewScanner(read)
for scanner.Scan() {
classes = append(classes, scanner.Text())
}
return classes
}
// getOutputsNames : YOLO Layer
func getOutputsNames(net *gocv.Net) []string {
var outputLayers []string
for _, i := range net.GetUnconnectedOutLayers() {
layer := net.GetLayer(i)
layerName := layer.GetName()
if layerName != "_input" {
outputLayers = append(outputLayers, layerName)
}
}
return outputLayers
}
// 启动yolov4模型初始化工作
func (yolo *YoloManager) StartYolo(config *models.ServerConfig) {
YoloMs = &YoloManager{}
YoloMs.Labels = readLabels(config.YoloPath)
net := gocv.ReadNet(fmt.Sprintf("%v/yolov4.weights", config.YoloPath), fmt.Sprintf("%v//yolov4.cfg", config.YoloPath))
//defer net.Close()
//gocv.NetBackendCUDA
net.SetPreferableBackend(gocv.NetBackendType(gocv.NetBackendCUDA))
// gocv.NetTargetCUDA
net.SetPreferableTarget(gocv.NetTargetType(gocv.NetTargetCUDA))
YoloMs.Net = &net
YoloMs.OutputNames = getOutputsNames(&net)
YoloMs.ScoreThreshold = config.Score
YoloMs.NmsThreshold = config.Nms
YoloMs.LabelData = make(map[string]*LabelResult)
}
// PostProcess : All Detect Box
func PostProcess(frame gocv.Mat, outs *[]gocv.Mat) ([]image.Rectangle, []float32, []int) {
var classIds []int
var confidences []float32
var boxes []image.Rectangle
for _, out := range *outs {
data, _ := out.DataPtrFloat32()
for i := 0; i < out.Rows(); i, data = i+1, data[out.Cols():] {
scoresCol := out.RowRange(i, i+1)
scores := scoresCol.ColRange(5, out.Cols())
_, confidence, _, classIDPoint := gocv.MinMaxLoc(scores)
if confidence > 0.5 {
centerX := int(data[0] * float32(frame.Cols()))
centerY := int(data[1] * float32(frame.Rows()))
width := int(data[2] * float32(frame.Cols()))
height := int(data[3] * float32(frame.Rows()))
left := centerX - width/2
top := centerY - height/2
classIds = append(classIds, classIDPoint.X)
confidences = append(confidences, float32(confidence))
boxes = append(boxes, image.Rect(left, top, width, height))
}
}
}
return boxes, confidences, classIds
}
// drawRect : Detect Class to Draw Rect
func drawRect(img gocv.Mat, boxes []image.Rectangle, classes []string, classIds []int, indices []int, confidences []float32) (gocv.Mat, map[string]LabelResult) {
detectClass := make(map[string]LabelResult)
for _, idx := range indices {
if idx == 0 {
continue
}
labelName := classes[classIds[idx]] + " " + strconv.FormatFloat(float64(confidences[idx]), 'f', 2, 32)
sizeConfig := gocv.GetTextSize(labelName, gocv.FontHersheyComplexSmall, 1.2, 2)
x0 := boxes[idx].Max.X
y0 := boxes[idx].Max.Y
x1 := boxes[idx].Max.X + boxes[idx].Min.X
y1 := boxes[idx].Max.Y + boxes[idx].Min.Y
if x0+2 >= img.Cols() {
x1 = img.Cols() - 1*2
}
if y1+2 >= img.Rows() {
y1 = img.Rows() - 1*2
}
// 画框
gocv.Rectangle(
&img,
image.Rect(x0, y0, x1, y1),
color.RGBA{255, 0, 191, 0},
1)
// 文字填充
gocv.Rectangle(
&img,
image.Rect(x0, y0-sizeConfig.Y, x0+sizeConfig.X, y0),
color.RGBA{255, 0, 191, 0},
-1)
// 文字
gocv.PutText(
&img,
labelName,
image.Point{x0, y0 - 3}, gocv.FontHersheyComplexSmall,
1.2,
color.RGBA{255, 255, 255, 0},
2)
model, ok := detectClass[classes[classIds[idx]]]
if ok {
model.Warn = model.Warn + 1
} else {
model = LabelResult{
LabelName: classes[classIds[idx]],
Warn: 1,
}
}
detectClass[classes[classIds[idx]]] = model
//detectClass = append(detectClass, labelName)
}
return img, detectClass
}
// 计算识别的结果集
func GetWarnData(warnData map[string]LabelResult) []*LabelResult {
if len(warnData) == 0 {
return nil
}
for k, v := range warnData {
model, ok := YoloMs.LabelData[k]
if ok {
model.Warn = model.Warn + v.Warn
} else {
model = &LabelResult{
LabelName: v.LabelName,
Warn: v.Warn,
}
}
YoloMs.LabelData[k] = model
}
var warnResult []*LabelResult
for _, v := range YoloMs.LabelData {
warnResult = append(warnResult, v)
}
sort.Slice(warnResult, func(i, j int) bool {
return warnResult[i].Warn > warnResult[j].Warn
})
return warnResult
}
// 处理单张图像
func RunDetectImg(src gocv.Mat) (gocv.Mat, []*LabelResult) {
img := src.Clone()
img.ConvertTo(&img, gocv.MatTypeCV32F)
// 默认为416,降低这个值可以在CPU架构上提高识别速度,但是会降低识别精度
blob := gocv.BlobFromImage(img, 1/255.0, image.Pt(128, 128), gocv.NewScalar(0, 0, 0, 0), true, false)
YoloMs.Net.SetInput(blob, "")
probs := YoloMs.Net.ForwardLayers(YoloMs.OutputNames)
boxes, confidences, classIds := PostProcess(img, &probs)
img.Close()
blob.Close()
indices := make([]int, len(YoloMs.Labels))
if len(boxes) == 0 { // No Classes
return src, nil
}
gocv.NMSBoxes(boxes, confidences, YoloMs.ScoreThreshold, YoloMs.NmsThreshold, indices)
img2, warnData := drawRect(src, boxes, YoloMs.Labels, classIds, indices, confidences)
warnResult := GetWarnData(warnData)
return img2, warnResult
}