forked from yonghenglh6/LHTracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
147 lines (130 loc) · 5.99 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
#include <stdio.h>
#include <stdlib.h>
#include "data_loader.h"
#include "lh_tracking.h"
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <map>
#include <iostream>
#include <fstream>
#include <glog/logging.h>
using namespace std;
using namespace cv;
const unsigned char LabelColors[][3] = {{251, 144, 17},
{2, 224, 17},
{
247, 13, 145},
{206, 36, 255},
{0, 78, 255}};
int main(int argn, char **arg) {
string dataset_id("n6");
string base_dir("/home/liuhao/workspace/1_dgvehicle/LHTracking/");
string dataset_name = dataset_id;
string imagelist_file = base_dir + "data/" + dataset_name + ".list";
string detectresult_file = base_dir + "data/detect_gd_" + dataset_name;
string track_output_file = base_dir + "track_" + dataset_name + ".txt";
string track_image_output_directory = base_dir + "track_" + dataset_name + "/";
if (access(track_image_output_directory.c_str(), F_OK) != 0) {
CHECK(mkdir(track_image_output_directory.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0)
<< "Can not create the image folder.";
}
ofstream track_output(track_output_file.c_str());
int fps = 15;
int skip_frames = 180;
skip_frames = int(skip_frames / fps) * fps + 1;
LOG(INFO) << "SKIP FRAME: " << skip_frames;
dataloader::DataReader *data_reader = new dataloader::ImagelistDataReader(imagelist_file, skip_frames);
map<int, vector<dataloader::DetectObject> > detect_result;
dataloader::read_detection_result(detectresult_file, detect_result);
deque<Mat> all_imgs;
bool display = true;
LHTracker *tracker = new LHTracker();
Mat frame;
int frame_index;
bool stop = false;
while (!stop) {
frame_index = data_reader->readNextImage(frame);
if (frame_index == -1) {
LOG(FATAL) << "Error: can not read frames.\n";
} else if (frame_index == -2) {
LOG(INFO) << "Image done.";
break;
// delete data_reader;
// data_reader = new dataloader::ImagelistDataReader(imagelist_file);
// continue;
}
// LOG(INFO) << "FrameIndex: " << frame_index << endl;
all_imgs.push_back(frame.clone());
TrackingResult result;
if ((frame_index - 1) % fps == 0) {
vector<Rect> pos;
vector<unsigned char> type;
vector<unsigned long> kill_id;
vector<dataloader::DetectObject> &det_vec = detect_result[int(frame_index)];
for (int i = 0; i < det_vec.size(); i++) {
pos.push_back(det_vec[i].position);
type.push_back(det_vec[i].typeID);
}
unsigned long long tt, tt2;
struct timeval start;
gettimeofday(&start, NULL);
tt = (start.tv_sec) * 1000000 + start.tv_usec;
// sleep(1);
tracker->Update(frame, frame_index, true, pos, type, result, kill_id);
struct timeval end;
gettimeofday(&end, NULL);
tt2 = (end.tv_sec) * 1000000 + end.tv_usec;
LOG(INFO) << "time:" << float((tt2 - tt) / 1000.f);
LOG(INFO) << "FrameIndex: " << frame_index << " , Detect Size: " << det_vec.size() << " , Track Size: "
<< (result.size() > 0 ? result[0].obj.size() : 0) << endl;
}
if (display) {
for (int i = 0; i < result.size(); i++) {
if (((result[i].frm_id - 1) % fps >= 0)) {
char tmp_char[512];
for (int j = 0; j < result[i].obj.size(); j++) {
int color_idx = result[i].obj[j].obj_id % 5;
rectangle(all_imgs[0], result[i].obj[j].loc,
Scalar(LabelColors[color_idx][2],
LabelColors[color_idx][1],
LabelColors[color_idx][0]), 3, 8, 0);
sprintf(tmp_char, "%lu type-%u score-%.0f sl-%d dir-%d-%d",
result[i].obj[j].obj_id, result[i].obj[j].type,
result[i].obj[j].score, result[i].obj[j].sl,
result[i].obj[j].dir.up_down_dir,
result[i].obj[j].dir.left_right_dir);
cv::putText(all_imgs[0], tmp_char,
cv::Point(result[i].obj[j].loc.x,
result[i].obj[j].loc.y - 12),
CV_FONT_HERSHEY_COMPLEX, 0.7,
Scalar(LabelColors[color_idx][2],
LabelColors[color_idx][1],
LabelColors[color_idx][0]), 2);
track_output << result[i].frm_id << " " << result[i].obj[j].obj_id << " "
<< int(result[i].obj[j].type) << " "\
<< result[i].obj[j].loc.x << " " << result[i].obj[j].loc.y << " " << result[i].obj[j].loc.width << \
" " << result[i].obj[j].loc.height << "\n";
}
sprintf(tmp_char, (track_image_output_directory + "/%lu.jpg").c_str(),
result[i].frm_id);
imshow("Tracking Debug", all_imgs[0]);
if ((result[i].frm_id - 1) % fps == 0)
imwrite(tmp_char, all_imgs[0]);
int c = waitKey(1);
if ((char) c == 27) {
stop = true;
}
}
// if ((result[i].frm_id - 1) % fps == 0)
all_imgs.pop_front();
}
}
}
track_output.close();
delete tracker;
delete data_reader;
return EXIT_SUCCESS;
}