forked from oylz/DS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
170 lines (154 loc) · 3.93 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
#include <opencv2/opencv.hpp>
#include "NT.h"
#include "StrCommon.h"
NT *_tt = NULL;
void DrawData(cv::Mat mm, cv::Mat frame, const std::map<int, DSResult> &map,
const std::vector<cv::Rect> &outRcs,
bool detect){
std::map<int, DSResult>::const_iterator it;
for(it = map.begin(); it != map.end(); ++it){
CvScalar clr = cvScalar(0, 255, 0);
cv::Rect rc = it->second.rc_;
cv::rectangle(frame, rc, clr);
std::string disp = toStr(it->first);
cv::putText(frame,
disp,
cvPoint(rc.x, rc.y),
CV_FONT_HERSHEY_SIMPLEX,
0.6,
cv::Scalar(0, 0, 255));
}
//
CvScalar clr = cvScalar(0, 0, 255);
for(cv::Rect rc:outRcs){
cv::rectangle(mm, rc, clr);
if(detect){
std::string disp = "detect";
cv::putText(frame,
disp,
cvPoint(100, 100),
CV_FONT_HERSHEY_SIMPLEX,
1,
cv::Scalar(0, 0, 255));
}
}
}
void ReadFileContent(const std::string &file, std::string &content){
FILE *fl = fopen(file.c_str(), "rb");
if(fl == NULL){
return;
}
fseek(fl, 0, SEEK_END);
int len = ftell(fl);
if(len <= 0){
return;
}
fseek(fl, 0, SEEK_SET);
char *buf = new char[len+1];
memset(buf, 0, len+1);
fread(buf, 1, len, fl);
content = std::string(buf);
delete []buf;
fclose(fl);
}
std::map<int, std::vector<cv::Rect>> _rcMap;
void ReadRcFileTotal(const std::string &file) {
std::string content = "";
ReadFileContent(file, content);
std::vector<std::string> lines;
splitStr(content, "\n", lines);
std::vector<cv::Rect> rcs;
int num = -1;
int tmpNum = -1;
for (int i = 0; i < lines.size(); i++) {
std::vector<std::string> cols;
splitStr(lines[i], ",", cols);
if (cols.size() < 6) {
continue;
}
tmpNum = toInt(trim(cols[0]));
if (num!=-1 && tmpNum!=num) {
_rcMap.insert(std::make_pair(num, rcs));
rcs.clear();
num = tmpNum;
}
if (num == -1) {
num = tmpNum;
}
cv::Rect rc;
rc.x = toInt(trim(cols[2]));
rc.y = toInt(trim(cols[3]));
rc.width = toInt(trim(cols[4]));
rc.height = toInt(trim(cols[5]));
rcs.push_back(rc);
}
if (!rcs.empty()) {
_rcMap.insert(std::make_pair(tmpNum, rcs));
}
}
std::string _rcFile = "";
std::string _imgDir;
cv::VideoWriter *_vw = NULL;
bool _isShow = false;
int _imgCount = 0;
void CB(cv::Mat &frame, int num){
if (_vw == NULL) {
_vw = new cv::VideoWriter("out.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, cv::Size(frame.cols, frame.rows));
}
if (_rcMap.empty()) {
ReadRcFileTotal(_rcFile);
}
std::vector<cv::Rect> rcs;
std::map<int, std::vector<cv::Rect>>::iterator it = _rcMap.find(num);
if (it != _rcMap.end()) {
rcs = it->second;
}
std::vector<cv::Rect> outRcs;
int64_t tm0 = gtm();
std::map<int, DSResult> map = _tt->UpdateAndGet(frame, rcs, num, outRcs);
int64_t tm1 = gtm();
Mat mm = frame.clone();
bool detect = (!rcs.empty());
DrawData(mm, frame, map, outRcs, detect);
printf("finish %d frame, updatecasttime:%ld\n", num, tm1-tm0);
//(*_vw) << frame;
if(_isShow){
std::string disp = "frame";
cv::resize(mm, mm, cv::Size(mm.cols/2, mm.rows/2));
cv::resize(frame, frame, cv::Size(frame.cols/2, frame.rows/2));
cv::imshow("mm", mm);
cv::imshow(disp, frame);
cv::waitKey(1);
}
}
void Go() {
std::string root = _imgDir;
for (int i = 1; i < _imgCount; i++) {
std::string path = root;
path += to6dStr(i);
path += ".jpg";
cv::Mat mat = cv::imread(path);
CB(mat, i);
}
}
int main(int argc, char **argv){
if (argc < 2) {
printf("usage:\n./tt showornot(0/1)\n");
return 0;
}
_isShow = toInt(argv[1]);
_tt = new NT();
if(!_tt->Init()){
return 0;
}
//_imgDir = "e:/code/deep_sort-master/MOT16/tt/xyz/img1/";
//_rcFile = "e:/code/deep_sort-master/MOT16/tt/xyz/det/det.txt";
_imgDir = "/home/xyz/code1/xyz/img1/";
_rcFile = "/home/xyz/code1/xyz/det/det.txt";
//_rcFile = "/home/xyz/code/test/pp/FaceNumGetter/out/102.txt";
//_imgDir = "/home/xyz/code1/GEP/FrameBuffer/imglog/img1/";
//_rcFile = "/home/xyz/code1/GEP/FrameBuffer/imglog/det/det.txt";
_imgCount = 650;// 2001;// 750;// 680;
Go();
return 0;
}