-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainTask.cpp
294 lines (268 loc) · 8.13 KB
/
MainTask.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
#include "./MainTask.hpp"
CameraTask::CameraTask(QObject *parent) : QObject(parent)
{
camera = std::make_shared<CameraCV>();
setCamera();
}
void CameraTask::directDislay()
{
// if (if_direct_show == false)
// {
// if_direct_show = true;
// }
// else
// {
// if_direct_show = false;
// }
}
void CameraTask::working()
{
static int idex = 0;
if (camera->isOpened())
{
if (camera->process() == false)
{
std::string error_msg;
camera->getErrorMessage(error_msg);
emit videoLost(error_msg);
}
else
{
//std::cout<<"show? "<<if_direct_show<<std::endl;
camera->getCvFrame(this->frame);
if (this->frame.empty())
{
std::string error_msg;
error_msg="1";
emit videoLost(error_msg);
return;
}
if (if_direct_show == 0)
{
cv::Mat m_frame;
cv::resize(frame, m_frame, cv::Size(DISPLAY_WIGTH, DISPLAY_HEIGHT));
std::vector<TrackingBox> temp_vector;
emit toDisplay(m_frame, temp_vector);
}
idex++;
if (idex == 8)
{
cv::Mat m_frame;
cv::resize(frame, m_frame, cv::Size(DISPLAY_WIGTH, DISPLAY_WIGTH));
emit toDetect(m_frame, this->if_direct_show);
idex = 0;
}
emit toTransport(frame);
}
}
else
{
std::string error_msg = "video error";
emit videoLost(error_msg);
}
}
void CameraTask::setCamera()
{
CameraParam camera_param;
camera_param.width = CAMERA_WIDTH;
camera_param.height = CAMERA_HEIGHT;
camera_param.exporsure = CAMERA_EXPORSURE;
camera_param.fps = CAMERA_FPS * 2;
camera->setCamera(camera_param);
std::string path = camera_path;
if (!camera->openCamera(path))
{
std::string error_msg;
camera->getErrorMessage(error_msg);
emit videoLost(error_msg);
}
}
void CameraTask::reboot()
{
// this->camera->closeCamera();
if (!camera->isOpened())
setCamera();
}
TransVideoTask::TransVideoTask(QObject *parent) : QObject(parent)
{
//transporter = new VideoTransporter;
//std::make_shared<VideoTransporter>();
setTransport();
}
void TransVideoTask::working(cv::Mat &frame)
{
static double timestamp = 0.002;
if (if_open)
{
// qDebug() << "open";
// cv::resize(frame, frame, cv::Size(CAMERA_WIDTH, CAMERA_HEIGHT));
// std::cout << "frame size " << frame.cols << " " << frame.rows << std::endl;
cv::Mat img = frame;
timestamp += 0.0002;
transporter->Write(img, timestamp);
}
else
{
setTransport();
}
}
void TransVideoTask::setTransport()
{
transporter = new VideoTransporter();
transporter->SetSize(CAMERA_WIDTH, CAMERA_HEIGHT);
std::pair<int, int> frame_rate(CAMERA_FPS, 1);
transporter->SetFramerate(frame_rate);
auto ret = transporter->Open(OUTURL, OUTPORT);
if (ret < 0)
{
if_open = false;
std::string error_msg = "set failed";
delete transporter;
emit setFailed(error_msg);
}
else
{
if_open = true;
}
// qDebug()<<"set ";
}
DetectTask::DetectTask(QObject *parent) : QObject(parent)
{
detector = std::make_shared<YoloWorker>("/opt/gx-edge-ai-demo/models/person224-v5n-int8.tflite");
redetect = std::make_shared<ReDetect>("/opt/gx-edge-ai-demo/models/reid16.tflite");
tracking = std::make_shared<Tracking>();
// setDetector();
}
void DetectTask::working(cv::Mat &frame, int if_direct_show)
{
// if(frame.empty()){
// return;
// }
std::chrono::high_resolution_clock::time_point prcoss_start_time = std::chrono::system_clock::now();
uchar input_tensor[INPUT_TESONR_SIZE];
bool prep = detector->preProcess(frame, input_tensor);
if (prep == false)
{
std::string error_msg = "prep failed";
emit setFailed(error_msg);
return;
}
uchar output_tensor[OUTPUT_TESONR_SIZE];
bool proc = detector->tflite_worker->inference(input_tensor, output_tensor);
if (proc == false)
{
std::string error_msg = "proc failed";
emit setFailed(error_msg);
}
DetectbBoxList detect_list;
bool fproc = detector->postProcess(output_tensor, detect_list);
if (fproc == false)
{
std::string error_msg = "fproc failed";
emit setFailed(error_msg);
}
if (detect_list.size() > 6)
{
return;
}
if (if_direct_show)
{
std::vector<cv::Rect2d> box_lists;
for (auto a : detect_list)
{
a.bbox &= cv::Rect2d(0, 0, DISPLAY_WIGTH, DISPLAY_WIGTH);
box_lists.push_back(a.bbox);
}
std::vector<TrackingBox> box;
for (auto a : box_lists)
{
TrackingBox x;
x.id = -1;
x.box = a;
box.push_back(x);
}
if (if_direct_show == 1)
{
emit toDisplay(frame, box);
}
else
{
// std::chrono::high_resolution_clock::time_point s1 = std::chrono::system_clock::now();
this->tracking->track(frame, box_lists);
// std::vector<cv::Rect2d> show_box;
// for(auto a:this->tracking->frameTrackingResult){
// show_box.push_back(a.box);
// std::cout<<"i found the id is"<<a.id<<std::endl;
// }
// std::chrono::high_resolution_clock::time_point e1 = std::chrono::system_clock::now();
// std::cout << " here is tracking" << std::chrono::duration_cast<std::chrono::milliseconds>(e1 - s1).count() << std::endl;
emit toDisplay(frame, this->tracking->frameTrackingResult);
}
}
if (!detect_list.empty())
{
for (auto a : detect_list)
{
auto box = a.bbox;
box &= cv::Rect2d(0, 0, DISPLAY_WIGTH, DISPLAY_WIGTH);
cv::Mat tracking_frame = frame(box).clone();
float vid[256] = {0};
// this->redetect->inference(tracking_frame);
emit toSent(vid, box, 0, 0);
}
}
// std::chrono::high_resolution_clock::time_point prcoss_end_time = std::chrono::system_clock::now();
// std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(prcoss_end_time - prcoss_start_time).count() << std::endl;
// std::cout << "====================================================" << std::endl;
}
TransDataTask::TransDataTask(int port, QObject *parent) : QObject(parent)
{
transporter = std::make_shared<ResultTransporter>(port);
if_open = transporter->init();
if (if_open == false) {
// std::cout << transporter;
}
}
void TransDataTask::transmit(float *id_vector, cv::Rect2d &rect, uint8_t camera_id, uint8_t person_id)
{
if (if_open)
{
int box_int[4] = {rect.x, rect.y, rect.width, rect.height};
int tx = transporter->Transmit(id_vector, box_int, camera_id, person_id);
std::cout << "send" << std::endl;
if (tx <= 0)
{
std::string error_msg = "trans faied";
// std::cout << error_msg << std::endl;
emit sentFailed(error_msg);
}
else
{
std::cout << "send : " << tx << std::endl;
}
receive();
}
else
{
// std::cout << "no init " << std::endl;
}
}
void TransDataTask::receive()
{
if (if_open)
{
// cv::Mat frame_rx;
float vector[256];
int box_rx[4];
uint8_t cam_id;
uint8_t person_id;
int rx = transporter->Receive(vector, box_rx, cam_id, person_id);
if (rx > 0)
{
std::cout << "rerceive =>> box : " << box_rx[0] << " " << box_rx[1] << " " << box_rx[2] << " " << box_rx[3] << " "
<< " cam_id : " << (int)cam_id << " person_id : " << (int)person_id << std::endl;
cv::Rect2d box = cv::Rect2d(box_rx[0], box_rx[1], box_rx[2], box_rx[3]);
emit toReDetect(vector, box, cam_id, person_id);
}
}
}