VPT.cppbk
9.94 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <time.h>
#include "ErrorInfo.h"
#include "VPT.h"
#include "ObjCls.h"
#include "utools.h"
//9
#include "ga_vpt_predict_net.h"
#include "ga_vpt_init_net.h"
//13
//#include "ga_model.h"
//#include "ga_model_init.h"
int checkTime()
{
struct tm* info;
int nYear, nMonth, nDay;
time_t raw;
time(&raw);
info = localtime(&raw);
nYear = info->tm_year + 1900;
nMonth = info->tm_mon + 1;
nDay = info->tm_mday;
if ((nYear == 2019 && nMonth<10))
return 1;
else
{
printf("版本过期,请联系开发商!中科院自动化所模式识别实验室图像视频组\n");
return -1;
}
}
int VPT_Init(void *&handle, VPT_PARAM vparam, char* resDir, VIDEO_OBJECT_SNAPSHOT_CALLBACK VideoObjSnapshotCallBack/* = NULL*/)
{
if (checkTime() != 1)
return FAILED;
int res = SUCCESS;
handle = new objDetector; //申请对象
memcpy((void *)&((objDetector *)handle)->param, (void *)&vparam, sizeof(VPT_PARAM));
int flag = 0; //返回值 默认为SUCCEEDED:0
//----------------- 13类结构化 ---------------------//
/*
param.initnetArray = (unsigned char*)ga_model_init;
param.initnetArrayLen = ga_model_init_len;
param.predictnetArray = (unsigned char*)ga_model;
param.predictnetArrayLen = ga_model_len;
param.mode = GPU_MODE;
param.gpuid = vparam.gpuid;
param.flag_glog = 1;
param.test_size = 360;
param.test_max_size = 640;
param.fpn_coarsest_stride = 32;
param.submean = "3 0 0 0";
param.variance = "3 255.0 255.0 255.0";
*/
//----------------- 13类结构化 ---------------------//
//----------------- 猫和狗结构化 ---------------------//
/*
param.initnetArray = (unsigned char*)ga_model_init;
param.initnetArrayLen = ga_model_init_len;
param.predictnetArray = (unsigned char*)ga_model;
param.predictnetArrayLen = ga_model_len;
param.mode = GPU_MODE;
param.gpuid = 0;
param.flag_glog = 1;
param.test_size = 800;
param.test_max_size = 1333;
param.fpn_coarsest_stride = 32;
param.submean = "3 102.9801 115.9465 122.7717";
param.variance = "3 1.0 1.0 1.0";
*/
//----------------- 猫和狗结构化 ---------------------//
//----------------- 9类结构化(自用单路人车物) ---------------------//
ctools_init_params vpt_param;
vpt_param.log_level_ = 0;
vpt_param.device_type_ = DEVICE_GPU;
vpt_param.device_id_ = vparam.gpuid;
vpt_param.engine_type_ = ENGINE_MCAFFE2;
vpt_param.model_type_ = MODEL_FPN;
#ifdef _MSC_VER
//vpt_param.weight_file_ ="../../../model/vptModel/vpt_init_net";
//vpt_param.net_file_ = "../../../model/vptModel/vpt_predict_net";
#else
vpt_param.weight_file_ = NULL;
vpt_param.net_file_ = NULL;
//vpt_param.weight_file_ = "vpt_model/init_net";
//vpt_param.net_file_ = "vpt_model/predict_net";
#endif
/*
vpt_param.weight_array_ = NULL;
vpt_param.weight_array_len_ = 0;
vpt_param.net_array_ = NULL;
vpt_param.net_array_len_ = 0;
*/
vpt_param.weight_array_ = (unsigned char*)ga_vpt_init_net;
vpt_param.weight_array_len_ = ga_vpt_init_net_len;
vpt_param.net_array_ = (unsigned char*)ga_vpt_predict_net;
vpt_param.net_array_len_ = ga_vpt_predict_net_len;
vpt_param.need_im_info_ = 1; // true
//"ResizePad_F32_F32,test_size,720,test_max_size,1280,fpn_coarsest_stride,32,"
//"NV12ToRGB_U8_U8,channel,3;"
//"ResizePad_U8_F32,test_size,540,test_max_size,1280,fpn_coarsest_stride,32,"
//"NV12ToRGB_U8_F32,channel,3;"
//"ResizePad_F32_F32,test_size,540,test_max_size,1280,fpn_coarsest_stride,32,"
vpt_param.data_process_str_ =
"CopyData_CPU2GPU_U8;"
"TypeConvert_U8_F32;"
"ResizePad_F32_F32,test_size,720,test_max_size,1280,fpn_coarsest_stride,32,"
"submean_b,103.94,submean_g,116.78,submean_r,123.68,"
"variance_rev_b,0.017,variance_rev_g,0.017,variance_rev_r,0.017;"
"NHWC2NCHW_F32"
;
res = ctools_init(&((objDetector *)handle)->detector, &vpt_param);
//----------------- 9类结构化(自用单路人车物) ---------------------//
//拌线初始化
((objDetector *)handle)->tracker = new Sort(/*lineArray, linecount, DETECTTYPE*/);
((objDetector *)handle)->snapshotHelper = new SnapShot(resDir, VideoObjSnapshotCallBack, vparam.SnaoshotParameter);
((objDetector *)handle)->frameCounter = FusionInterval;
return res;
}
void VPT_ResetTracker(void * handle/*, int maxResultCountVPT_Line * lineArray = NULL, int linecount = 0*/)
{
objDetector* tools = (objDetector*)handle;
/*if (tools->ssdResult)
{
delete[] tools->ssdResult;
}
tools->param.maxResultCount = maxResultCount;
tools->ssdResult = new CDTOOLS_Result[maxResultCount];*/
tools->tracker->Release();
delete tools->tracker;
tools->tracker = NULL;
tools->tracker = new Sort(/*lineArray, linecount, DETECTTYPE*/); //新接口 无需流量统计和快照
}
//辅助函数:数据预处理
float VPTmeanSub[3] = { 103.52, 116.28, 123.675 };
float VPTVariance[3] = { 57.375, 57.12, 58.395 };
void datapreprocess(float * blob, int width, int height, int channels, int batchsize)
{
int datasize_ = width * height;
for (int c = 0; c < 3; c++)
{
for (int i = 0; i < width * height; i++)
{
blob[datasize_ * c + i] = ((blob[datasize_ * c + i]) - VPTmeanSub[c]) / VPTVariance[c];
}
}
}
int VPT_ProcessLastFrame(void * handle/*, unsigned char * rgb, int width, int height, FT_Result * result*/)
{
objDetector* tools = (objDetector*)handle;
//CDTOOLS_Result ssdResult[MAX_OBJ_COUNT_TEST];
vector< vector <float>> detectResult; //转化为跟踪所需要的输入
int index = 0;
bool isUseDet = false;
vector<int> deleteTrackers;
int ObjCount = tools->tracker->updateLastFrame(deleteTrackers);
//void SaveSnapshot(vector<int> deleteTrackers);
tools->snapshotHelper->SaveSnapshot(deleteTrackers);
vector<vector<float>>().swap(detectResult);
detectResult.clear();
vector<int>().swap(deleteTrackers);
deleteTrackers.clear();
return 0;
}
//int VPT_Process(void * handle, unsigned char *bgr, int width, int height, int channels, int frameCount, VPT_Result *result, vector<int> &deleteTrackers, VPT_TrafficResult* traffic, bool showTrack)
int VPT_Process(void * handle, unsigned char *bgr, int width, int height, int channels, int frameCount, VPT_Result *result,/* int maxResultCount,*/ bool showTrack)
{
objDetector* tools = (objDetector*)handle;
//CDTOOLS_Result ssdResult[MAX_OBJ_COUNT_TEST];
Mat img(height, width, CV_8UC3, bgr);
vector< vector <float>> detectResult; //转化为跟踪所需要的输入
int index = 0;
bool isUseDet = false;
vector<int> deleteTrackers;
if (tools->frameCounter == FusionInterval || tools->isInitFrame)
{
cout << "begin Ctools_Detector" <<endl;
sy_img cur_img;
cur_img.set_data(width, height, channels, bgr);
ctools_result *detresult;
int count = 20;
while(count--)
int res_status = ctools_process(tools->detector, &cur_img, 1, &detresult);
cout << "finish Ctools_Detector" <<endl;
ctools_result &cur_result = detresult[0];
//(*result)[b].obj_count_ = cur_result.obj_count_ > MAX_DET_COUNT ? MAX_DET_COUNT : cur_result.obj_count_;
for (int c = 0; c < cur_result.obj_count_ && c<tools->param.maxResultCount; c++)
{
float score = cur_result.obj_results_[c].data_[1];
if (score >=THRESHOLD) //更改为left top right bottom score index
{
vector <float> obj;
obj.push_back(cur_result.obj_results_[c].data_[2]);
obj.push_back(cur_result.obj_results_[c].data_[3]);
obj.push_back(cur_result.obj_results_[c].data_[4]);
obj.push_back(cur_result.obj_results_[c].data_[5]);
obj.push_back(cur_result.obj_results_[c].data_[1]);
obj.push_back(cur_result.obj_results_[c].data_[0] - 1);
detectResult.push_back(obj);
}
}
//dResult->objCount = index;
result->objCount = detectResult.size();
printf("det count: %d\n", result->objCount);
//VPT_Result temp;
tools->isInitFrame = false;
tools->frameCounter = 0;
isUseDet = true;
}
tools->frameCounter++;
//for (int j = 0; j < FusionInterval; j++) //跳帧:检测一帧,跟踪 (FusionInterval - 1)帧
//{
int objCount = tools->tracker->update(width, height, isUseDet, detectResult, result->obj, deleteTrackers);
cout <<"sort objcount: " << objCount << endl;
/*if (j == 0)
{*/
//memcpy(result->obj, temp.obj, sizeof(VPT_Result)); //只返回第一帧带检测的结果
result->objCount = objCount;
printf("1\n");
tools->snapshotHelper->SnapshotProcess(result, img, deleteTrackers, frameCount);
printf("2\n");
vector<vector<float>>().swap(detectResult);
detectResult.clear();
printf("3\n");
vector<int>().swap(deleteTrackers);
deleteTrackers.clear();
printf("4\n");
//}
//}
if (showTrack)
tools->tracker->addTracker(&img);
return 0;
}
//int VPT_ProcessImage(void * handle, unsigned char * bgr, int width, int height, int channels, VPT_Result * result)
//{
// objDetector* tools = (objDetector*)handle;
//
// if (bgr == NULL || width == 0 || height == 0) //图像数据错误
// return -1;
//
// clock_t begin = clock();
//
// CDTOOLS_Result ssdResult[50];
//
// int count = Ctools_Detector(tools->detector, bgr, width, height, 3, ssdResult, datapreprocess); //
// clock_t end = clock();
//
// for (int i = 0; i < count; i++)
// {
// const CDTOOLS_Result& d = ssdResult[i];// Detection format: [image_id, label, score, xmin, ymin, xmax, ymax)
// const float score = d.boxinfo[5];
//
// result->obj[i].id = i;
// result->obj[i].left = d.boxinfo[0]; // bbout[i][0];
// result->obj[i].top = d.boxinfo[1]; //bbout[i][1];
// result->obj[i].right = d.boxinfo[2]; //bbout[i][2];
// result->obj[i].bottom = d.boxinfo[3]; //bbout[i][3];
// result->obj[i].confidence = d.boxinfo[4];
// result->obj[i].index = d.boxinfo[5] - 1; // bbout[i][4];
// }
//
// result->objCount = count;
// return count;
//}
void VPT_Release(void *&handle)
{
objDetector* tools = (objDetector*)handle;
VPT_ProcessLastFrame(tools);
if (tools && tools->detector)
{
ctools_release(&tools->detector);
tools->detector = NULL;
}
if (tools)
{
delete tools;
tools = NULL;
}
}