-
Notifications
You must be signed in to change notification settings - Fork 5
/
yolov4.cpp
270 lines (202 loc) · 8.07 KB
/
yolov4.cpp
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
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#pragma warning(disable:4996)
#include "net.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#if CV_MAJOR_VERSION >= 3
#include <opencv2/videoio/videoio.hpp>
#endif
#include <vector>
#include <stdio.h>
#define NCNN_PROFILING
#define YOLOV4_TINY //Using yolov4_tiny, if undef, using original yolov4
#ifdef NCNN_PROFILING
#include "benchmark.h"
#endif
struct Object
{
cv::Rect_<float> rect;
int label;
float prob;
};
static int init_yolov4(ncnn::Net* yolov4, int* target_size)
{
/* --> Set the params you need for the ncnn inference <-- */
yolov4->opt.num_threads = 8; //You need to compile with libgomp for multi thread support
yolov4->opt.use_vulkan_compute = true; //You need to compile with libvulkan for gpu support
yolov4->opt.use_winograd_convolution = true;
yolov4->opt.use_sgemm_convolution = true;
yolov4->opt.use_int8_inference = true;
//yolov4->opt.use_fp16_packed = true;
//yolov4->opt.use_fp16_storage = true;
//yolov4->opt.use_fp16_arithmetic = true;
yolov4->opt.use_packing_layout = true;
yolov4->opt.use_shader_pack8 = false;
yolov4->opt.use_image_storage = false;
/* --> End of setting params <-- */
int ret = 0;
// original pretrained model from https://github.com/AlexeyAB/darknet
// the ncnn model https://drive.google.com/drive/folders/1YzILvh0SKQPS_lrb33dmGNq7aVTKPWS0?usp=sharing
// the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
#ifdef YOLOV4_TINY
const char* yolov4_param = "E:/ncnn/yolov4/yolov4-tiny-opt-int8.param";
const char* yolov4_model = "E:/ncnn/yolov4/yolov4-tiny-opt-int8.bin";
*target_size = 416;
#else
const char* yolov4_param = "yolov4-opt.param";
const char* yolov4_model = "yolov4-opt.bin";
*target_size = 608;
#endif
ret = yolov4->load_param(yolov4_param);
if (ret != 0)
{
return ret;
}
ret = yolov4->load_model(yolov4_model);
if (ret != 0)
{
return ret;
}
return 0;
}
static int detect_yolov4(const cv::Mat& bgr, std::vector<Object>& objects, int target_size, ncnn::Net* yolov4)
{
int img_w = bgr.cols;
int img_h = bgr.rows;
ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, bgr.cols, bgr.rows, target_size, target_size);
const float mean_vals[3] = { 0, 0, 0 };
const float norm_vals[3] = { 1 / 255.f, 1 / 255.f, 1 / 255.f };
in.substract_mean_normalize(mean_vals, norm_vals);
ncnn::Extractor ex = yolov4->create_extractor();
ex.input("data", in);
ncnn::Mat out;
ex.extract("output", out);
objects.clear();
for (int i = 0; i < out.h; i++)
{
const float* values = out.row(i);
Object object;
object.label = values[0];
object.prob = values[1];
object.rect.x = values[2] * img_w;
object.rect.y = values[3] * img_h;
object.rect.width = values[4] * img_w - object.rect.x;
object.rect.height = values[5] * img_h - object.rect.y;
objects.push_back(object);
}
return 0;
}
static int draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects, int is_streaming)
{
static const char* class_names[] = { "background", "person", "bicycle",
"car", "motorbike", "aeroplane", "bus", "train", "truck",
"boat", "traffic light", "fire hydrant", "stop sign",
"parking meter", "bench", "bird", "cat", "dog", "horse",
"sheep", "cow", "elephant", "bear", "zebra", "giraffe",
"backpack", "umbrella", "handbag", "tie", "suitcase",
"frisbee", "skis", "snowboard", "sports ball", "kite",
"baseball bat", "baseball glove", "skateboard", "surfboard",
"tennis racket", "bottle", "wine glass", "cup", "fork",
"knife", "spoon", "bowl", "banana", "apple", "sandwich",
"orange", "broccoli", "carrot", "hot dog", "pizza", "donut",
"cake", "chair", "sofa", "pottedplant", "bed", "diningtable",
"toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard",
"cell phone", "microwave", "oven", "toaster", "sink",
"refrigerator", "book", "clock", "vase", "scissors",
"teddy bear", "hair drier", "toothbrush"
};
cv::Mat image = bgr.clone();
for (size_t i = 0; i < objects.size(); i++)
{
const Object& obj = objects[i];
fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));
char text[256];
sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
int baseLine = 0;
cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
int x = obj.rect.x;
int y = obj.rect.y - label_size.height - baseLine;
if (y < 0)
y = 0;
if (x + label_size.width > image.cols)
x = image.cols - label_size.width;
cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
cv::Scalar(255, 255, 255), -1);
cv::putText(image, text, cv::Point(x, y + label_size.height),
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
}
cv::imshow("image", image);
if (is_streaming)
{
cv::waitKey(1);
}
else
{
cv::waitKey(0);
}
return 0;
}
int main(int argc, char** argv)
{
cv::Mat frame;
std::vector<Object> objects;
cv::VideoCapture cap;
ncnn::Net yolov4;
const char* devicepath;
int target_size = 160;
int is_streaming = 0;
/*
const char* imagepath = "E:/ncnn/yolov5/person.jpg";
cv::Mat m = cv::imread(imagepath, 1);
if (m.empty())
{
fprintf(stderr, "cv::imread %s failed\n", imagepath);
return -1;
}
double start = GetTickCount();
std::vector<Object> objects;
detect_yolov5(m, objects);
double end = GetTickCount();
fprintf(stderr, "cost time: %.5f\n ms", (end - start)/1000);
draw_objects(m, objects);
*/
int ret = init_yolov4(&yolov4, &target_size); //We load model and param first!
if (ret != 0)
{
fprintf(stderr, "Failed to load model or param, error %d", ret);
return -1;
}
cv::VideoCapture capture;
capture.open(0); //修改这个参数可以选择打开想要用的摄像头
//cv::Mat frame;
while (true)
{
capture >> frame;
cv::Mat m = frame;
double start = GetTickCount();
std::vector<Object> objects;
detect_yolov4(frame, objects, 160, &yolov4);
double end = GetTickCount();
fprintf(stderr, "cost time: %.5f ms \n", (end - start-10));
// imshow("外接摄像头", m); //remember, imshow() needs a window name for its first parameter
draw_objects(m, objects, 8);
if (cv::waitKey(30) >= 0)
break;
}
return 0;
}