-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.cpp
304 lines (267 loc) · 9.1 KB
/
main.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
/* face tracking PC application SDK */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include "detection/face_detector.h"
#include "face_tracker.h"
#include <string>
#include <sstream>
using std::vector;
using namespace cv;
using namespace std;
typedef struct {
std::vector<int> id_vector;
std::vector<cv::Mat> img_vector;
} TrackData;
void DrawDetection(cv::Mat& img, std::vector<FaceInfo> boxes){
if (boxes.size() == 0) {
//cv::imshow("image", img);
}
else {
for (int i = 0; i < boxes.size(); i++) {
cv::Point topleft;
cv::Point bottomright;
topleft.x = boxes[i].x1;
topleft.y = boxes[i].y1;
bottomright.x = boxes[i].x2;
bottomright.y = boxes[i].y2;
std::cout << topleft.x << std::endl;
cv::rectangle(img, topleft, bottomright, cv::Scalar(255, 0, 0), 2, cv::LINE_8);
cv::imshow("image", img);
}
}
}
TrackData GetTrackData(cv::Mat& img, std::vector<Track> tracks, TrackData& track_buf) {
TrackData data;
if (tracks.size() != 0) {
if (track_buf.id_vector.size() == 0) {
for (int i = 0; i < tracks.size(); i++) {
cv::Point topleft;
cv::Point bottomright;
topleft.x = tracks[i].x1;
topleft.y = tracks[i].y1;
bottomright.x = tracks[i].x2;
bottomright.y = tracks[i].y2;
cv::Mat croppedImg;
int width = (int)(bottomright.x - topleft.x);
int height = (int)(bottomright.y - topleft.y);
if (width != 0 || height != 0) {
if ((bottomright.x < img.cols) && (bottomright.y < img.rows) && (topleft.x > 0 && topleft.y > 0)) {
img(cv::Rect(topleft.x, topleft.y, width, height)).copyTo(croppedImg);
data.id_vector.push_back(tracks[i].id);
data.img_vector.push_back(croppedImg);
}
}
}
for (int i = 0; i < data.id_vector.size(); i++) {
track_buf.id_vector.push_back(data.id_vector[i]);
track_buf.img_vector.push_back(data.img_vector[i]);
}
}
else {
for (int i = 0; i < tracks.size(); i++) {
if (tracks[i].id != track_buf.id_vector[i]) {
cv::Point topleft;
cv::Point bottomright;
topleft.x = tracks[i].x1;
topleft.y = tracks[i].y1;
bottomright.x = tracks[i].x2;
bottomright.y = tracks[i].y2;
cv::Mat croppedImg;
int width = (int)(bottomright.x - topleft.x);
int height = (int)(bottomright.y - topleft.y);
if (width != 0 || height != 0) {
if ((bottomright.x < img.cols) && (bottomright.y < img.rows) && (topleft.x > 0 && topleft.y > 0)) {
img(cv::Rect(topleft.x, topleft.y, width, height)).copyTo(croppedImg);
data.id_vector.push_back(tracks[i].id);
data.img_vector.push_back(croppedImg);
}
}
}
}
track_buf.id_vector.clear();
track_buf.img_vector.clear();
for (int i = 0; i < data.id_vector.size(); i++) {
track_buf.id_vector.push_back(data.id_vector[i]);
track_buf.img_vector.push_back(data.img_vector[i]);
}
}
}
return data;
}
void WriteToFile(TrackData& data) {
if (data.id_vector.size() != 0) {
for (int i = 0; i < data.id_vector.size(); i++) {
cv::String filename = "./" + std::to_string(data.id_vector[i]) + ".jpg";
cv::imwrite(filename, data.img_vector[i]);
}
}
}
void DrawTrack(cv::Mat& img, std::vector<Track> tracks) {
if (tracks.size() == 0) {
cv::imshow("image", img);
}
else {
for (int i = 0; i < tracks.size(); i++) {
cv::Point topleft;
cv::Point bottomright;
topleft.x = tracks[i].x1;
topleft.y = tracks[i].y1;
bottomright.x = tracks[i].x2;
bottomright.y = tracks[i].y2;
std::cout << topleft.x << std::endl;
cv::rectangle(img, topleft, bottomright, cv::Scalar(255, 255, 0), 2, cv::LINE_8);
cv::putText(img, std::to_string(tracks[i].id), topleft,
cv::FONT_HERSHEY_PLAIN, 2.0,
cv::Scalar(0, 255, 0), 2, cv::LINE_AA);
cv::imshow("image", img);
}
}
}
void SaveTrack(cv::Mat& img, std::vector<Track> tracks, cv::VideoWriter& oVideoWriter) {
if (tracks.size() == 0) {
oVideoWriter.write(img);
}
else {
for (int i = 0; i < tracks.size(); i++) {
cv::Point topleft;
cv::Point bottomright;
topleft.x = tracks[i].x1;
topleft.y = tracks[i].y1;
bottomright.x = tracks[i].x2;
bottomright.y = tracks[i].y2;
std::cout << topleft.x << std::endl;
cv::rectangle(img, topleft, bottomright, cv::Scalar(255, 255, 0), 2, cv::LINE_8);
cv::putText(img, std::to_string(tracks[i].id), topleft,
cv::FONT_HERSHEY_PLAIN, 2.0,
cv::Scalar(0, 255, 0), 2, cv::LINE_AA);
oVideoWriter.write(img);
}
}
}
int VideoFileInferenceRealTimeView(int total_frame, std::vector<TrackData>& track_data) {
cv::VideoCapture cap("./video4.mp4");
if (!cap.isOpened()) {
std::cout << "No video stream detected" << std::endl;
system("pause");
return -1;
}
//get the frames rate of the video
double fps = cap.get(cv::CAP_PROP_FPS);
std::cout << "Frames per seconds : " << fps << std::endl;
UltraFace detector("./weights/detection/RFB-320.bin", "./weights/detection/RFB-320.param", 320, 240, 1, 0.7);
FaceTracker tracker;
int __ = tracker.LoadThirdPartyModels();
int frame_cnt = 0;
TrackData track_buf;
while (true) {
cv::Mat frame;
std::vector<FaceInfo> boxes;
bool bSuccess = cap.read(frame); // read a new frame from video
if (bSuccess == false){
std::cout << "Found the end of the video" << std::endl;
break;
}
frame_cnt++;
ncnn::Mat inmat = ncnn::Mat::from_pixels(frame.data, ncnn::Mat::PIXEL_BGR2RGB, frame.cols, frame.rows);
detector.detect(inmat, boxes);
//DrawDetection(frame, boxes);
std::vector<Track> tracks;
tracker.Get_Track(frame, boxes, tracks);
//TrackData contains one cropped face image per person, this can be used later for recognition purpose
if (boxes.size() == tracks.size()) {
track_data.push_back(GetTrackData(frame, tracks, track_buf));
}
//DrawTrack(frame, tracks);
if (frame_cnt == total_frame)
break;
char c = (char)cv::waitKey(25);//Allowing 25 milliseconds frame processing time and initiating break condition
if (c == 27) { //If 'Esc' is entered break the loop//
break;
}
}
cap.release();//Releasing the buffer memory//
return 0;
}
int VideoFileInferenceSaveVideo() {
cv::VideoCapture cap("./video4.mp4");
if (!cap.isOpened()) {
std::cout << "No video stream detected" << std::endl;
system("pause");
return -1;
}
//get the frames rate of the video
double fps = cap.get(cv::CAP_PROP_FPS);
std::cout << "Frames per seconds : " << fps << std::endl;
int frame_width = static_cast<int>(cap.get(CAP_PROP_FRAME_WIDTH)); //get the width of frames of the video
int frame_height = static_cast<int>(cap.get(CAP_PROP_FRAME_HEIGHT)); //get the height of frames of the video
int frames_per_second = 25;
Size frame_size(frame_width, frame_height);
//Create and initialize the VideoWriter object
cv::VideoWriter oVideoWriter("./TrackingVideo.avi", cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
frames_per_second, frame_size, true);
//If the VideoWriter object is not initialized successfully, exit the program
if (oVideoWriter.isOpened() == false)
{
cout << "Cannot save the video to a file" << endl;
cin.get(); //wait for any key press
return -1;
}
UltraFace detector("./weights/detection/RFB-320.bin", "./weights/detection/RFB-320.param", 320, 240, 1, 0.7);
FaceTracker tracker;
int __ = tracker.LoadThirdPartyModels();
int frame_cnt = 0;
while (true) {
cv::Mat frame;
std::vector<FaceInfo> boxes;
bool bSuccess = cap.read(frame); // read a new frame from video
if (bSuccess == false){
std::cout << "Found the end of the video" << std::endl;
break;
}
frame_cnt++;
ncnn::Mat inmat = ncnn::Mat::from_pixels(frame.data, ncnn::Mat::PIXEL_BGR2RGB, frame.cols, frame.rows);
detector.detect(inmat, boxes);
std::vector<Track> tracks;
tracker.Get_Track(frame, boxes, tracks);
SaveTrack(frame, tracks, oVideoWriter);
char c = (char)cv::waitKey(25);//Allowing 25 milliseconds frame processing time and initiating break condition
if (c == 27) { //If 'Esc' is entered break the loop//
break;
}
}
cap.release();//Releasing the buffer memory//
oVideoWriter.release();//Flush and close the video file
return 0;
}
int main(int argc, char** argv){
int total_frame = 100;
int video_out_index = 0;
std::cout << "**************************************************" << std::endl;
std::cout << "Have " << argc << " arguments:" << std::endl;
for (int i = 0; i < argc; ++i)
std::cout << i << " " << argv[i] << std::endl;
std::cout << "**************************************************\n" << std::endl;
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "--video_out")){
if (!strcmp(argv[i + 1], "true"))
video_out_index = 1;
else
video_out_index = 0;
if (!strcmp(argv[i], "--frame_num"))
total_frame = std::atoi(argv[i + 1]);
}
}
std::vector<TrackData> track_data;
if (video_out_index == 1)
int _ = VideoFileInferenceSaveVideo();
else
int _ = VideoFileInferenceRealTimeView(total_frame, track_data);
for(int i = 0; i < track_data.size(); i++)
WriteToFile(track_data[i]);
return 0;
}