-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_yolo.cpp
316 lines (259 loc) · 9.46 KB
/
main_yolo.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
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
307
308
309
310
311
312
313
314
315
316
#include "pipert/Channel.h"
#include "pipert/Packet.h"
#include "pipert/Scheduler.h"
#include "pipert/Timer.h"
#include "pipert/Profiler.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <thread>
#include <chrono>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <unistd.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <thread>
#include <string>
#include <unistd.h>
class Yolo
{
public:
Yolo(pipert::PolledChannel<std::vector<cv::Rect>> *pc_to_write) : pc_to_write_(pc_to_write) {}
// confidence threshold
float conf_threshold = 0.6;
// nms threshold
float nms = 0.4;
int width = 416;
int height = 416;
std::vector<std::string> classes;
std::vector<cv::Mat> outs;
cv::dnn::Net net;
cv::Mat blob;
//Camera
int deviceID = 0; // 0 = open default camera
int apiID = cv::CAP_ANY; // 0 = autodetect default API
// remove unnecessary bounding boxes
std::vector<cv::Rect> remove_box(cv::Mat &frame, const std::vector<cv::Mat> &out, cv::dnn::Net *pnet);
// draw bounding boxes
void draw_box(int classId, float conf, int left, int top, int right, int bottom, cv::Mat &frame);
// get output layers
std::vector<std::string> getOutputsNames(const cv::dnn::Net &net);
void Detection(pipert::PacketToProcess<cv::Mat> p)
{
auto frame = p.data();
// convert image to blob
cv::dnn::blobFromImage(frame, blob, 0.00392, cv::Size(width, height), cv::Scalar(0, 0, 0), true, false);
net.setInput(blob);
std::vector<cv::Mat> outs;
net.forward(outs, getOutputsNames(net));
std::vector<cv::Rect> g_boxes = remove_box(frame, outs, &net);
pipert::PacketToFill<std::vector<cv::Rect>> packet_to_fill = pc_to_write_->Acquire(p.timestamp(), g_boxes);
// packet_to_fill.Push();
}
private:
pipert::PolledChannel<std::vector<cv::Rect>> *pc_to_write_;
};
int main(int argc, char **argv)
{
// pipert::Profiler profiler("file:emptylog.txt", 100);
pipert::Scheduler sch;
const int channel_capacity = 10;
pipert::PolledChannel<std::vector<cv::Rect>> pc =
sch.CreatePolledChannel<std::vector<cv::Rect>>("OutChannel", channel_capacity);
Yolo yolo(&pc);
std::string classesFile = "./yolo/cfg/coco.names";
std::ifstream ifs(classesFile.c_str());
std::string line;
while (getline(ifs, line))
yolo.classes.push_back(line);
// load model weights and architecture
std::string configuration = "./yolo/cfg/yolov3-tiny.cfg";
std::string model = "./yolo/cfg/yolov3-tiny.weights";
// Load the network
yolo.net = cv::dnn::readNetFromDarknet(configuration, model);
pipert::ScheduledChannel<cv::Mat> sc1 =
sch.CreateScheduledChannel<cv::Mat>("YoloChannel", channel_capacity, nullptr,
std::bind(&Yolo::Detection, &yolo, std::placeholders::_1));
//--- INITIALIZE VIDEOCAPTURE
cv::VideoCapture cap;
cv::Mat frame;
cap.open(argv[1]);
sch.Start();
int const sleepTime = 10000;
float fps = sleepTime / 100.;
while (true)
{
cap.read(frame);
// check if we succeeded
if (frame.empty())
break;
for (pipert::PacketToProcess<std::vector<cv::Rect>> packet_to_process = pc.Poll();
!packet_to_process.IsEmpty();
packet_to_process = pc.Poll())
{
std::vector<cv::Rect> g_boxes = packet_to_process.data();
std::cout << g_boxes.size() << '\n';
int size = g_boxes.size();
size = cv::min(size, 15);
for (int i = 0; i < size / 2; ++i)
{
cv::Rect box = g_boxes[i];
int left = box.x;
int top = box.y;
int right = box.x + box.width;
int bottom = box.y + box.height;
cv::rectangle(frame, cv::Point(left, top), cv::Point(right, bottom),
cv::Scalar(255, 178, 50), 3);
}
packet_to_process.Release();
}
pipert::Timer::Time time = pipert::Timer::time();
pipert::PacketToFill<cv::Mat> packet_to_fill = sc1.Acquire(time, frame);
packet_to_fill.Push();
std::string label = cv::format("FPS : %.2f", fps);
cv::putText(frame, label, cv::Point(0, 15), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 255));
cv::imshow("Live", frame);
//wait esc
char c = (char)cv::waitKey(25);
if (c == 27)
break;
usleep(sleepTime);
}
usleep(1000);
std::cout << "END" << '\n'
<< '\n';
sch.Stop();
return 0;
}
std::vector<cv::Rect> Yolo::remove_box(cv::Mat &frame, const std::vector<cv::Mat> &outs, cv::dnn::Net *pnet)
{
std::vector<int> classIds;
std::vector<float> confidences;
std::vector<cv::Rect> boxes;
static std::vector<int> outLayers = pnet->getUnconnectedOutLayers();
static std::string outLayerType = pnet->getLayer(outLayers[0])->type;
if (outLayerType == "DetectionOutput")
{
// Network produces output blob with a shape 1x1xNx7 where N is a number of
// detections and an every detection is a vector of values
// [batchId, classId, confidence, left, top, right, bottom]
CV_Assert(outs.size() > 0);
for (size_t k = 0; k < outs.size(); k++)
{
float *data = (float *)outs[k].data;
for (size_t i = 0; i < outs[k].total(); i += 7)
{
float confidence = data[i + 2];
if (confidence > conf_threshold)
{
int left = (int)data[i + 3];
int top = (int)data[i + 4];
int right = (int)data[i + 5];
int bottom = (int)data[i + 6];
int width = right - left + 1;
int height = bottom - top + 1;
if (width * height <= 1)
{
left = (int)(data[i + 3] * frame.cols);
top = (int)(data[i + 4] * frame.rows);
right = (int)(data[i + 5] * frame.cols);
bottom = (int)(data[i + 6] * frame.rows);
width = right - left + 1;
height = bottom - top + 1;
}
classIds.push_back((int)(data[i + 1]) - 1); // Skip 0th background class id.
boxes.push_back(cv::Rect(left, top, width, height));
confidences.push_back(confidence);
}
}
}
}
else if (outLayerType == "Region")
{
for (size_t i = 0; i < outs.size(); ++i)
{
// Network produces output blob with a shape NxC where N is a number of
// detected objects and C is a number of classes + 4 where the first 4
// numbers are [center_x, center_y, width, height]
float *data = (float *)outs[i].data;
for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
{
cv::Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
cv::Point classIdPoint;
double confidence;
cv::minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
if (confidence > conf_threshold)
{
int centerX = (int)(data[0] * frame.cols);
int centerY = (int)(data[1] * frame.rows);
int width = (int)(data[2] * frame.cols);
int height = (int)(data[3] * frame.rows);
int left = centerX - width / 2;
int top = centerY - height / 2;
classIds.push_back(classIdPoint.x);
confidences.push_back((float)confidence);
boxes.push_back(cv::Rect(left, top, width, height));
}
}
}
}
else
CV_Error(cv::Error::StsNotImplemented, "Unknown output layer type: " + outLayerType);
// Perform non maximum suppression to eliminate redundant overlapping boxes with
// lower confidences
std::vector<int> indices;
cv::dnn::NMSBoxes(boxes, confidences, conf_threshold, nms, indices);
std::vector<cv::Rect> g_boxes;
for (size_t i = 0; i < indices.size(); ++i)
{
std::cout << indices.size() << std::endl;
int idx = indices[i];
cv::Rect box = boxes[idx];
g_boxes.push_back(box);
// draw_box(classIds[idx], confidences[idx], box.x, box.y,
// box.x + box.width, box.y + box.height, frame);
}
return g_boxes;
}
// Draw the predicted bounding box
void Yolo::draw_box(int classId, float conf, int left, int top, int right, int bottom, cv::Mat &frame)
{
//Draw a rectangle displaying the bounding box
// rectangle(frame, Point(left, top), Point(right, bottom), Scalar(255, 178, 50), 3);
//Get the label for the class name and its confidence
// std::string label = cv::format("%.2f", conf);
// if (!classes.empty())
// {
// CV_Assert(classId < (int)classes.size());
// label = classes[classId] + ":" + label;
// }
// std::cout << label << '\n';
//Display the label at the top of the bounding box
// int baseLine;
// cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
// top = cv::max(top, labelSize.height);
// rectangle(frame, Point(left, top - round(1.5*labelSize.height)), Point(left + round(1.5*labelSize.width), top + baseLine),
// Scalar(255, 255, 255), FILLED);
// putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0,0,0),1);
}
// Get the names of the output layers
std::vector<std::string> Yolo::getOutputsNames(const cv::dnn::Net &net)
{
static std::vector<std::string> names;
if (names.empty())
{
//Get the indices of the output layers, i.e. the layers with unconnected outputs
std::vector<int> outLayers = net.getUnconnectedOutLayers();
//get the names of all the layers in the network
std::vector<std::string> layersNames = net.getLayerNames();
// Get the names of the output layers in names
names.resize(outLayers.size());
for (size_t i = 0; i < outLayers.size(); ++i)
names[i] = layersNames[outLayers[i] - 1];
}
return names;
}