Blame view

vpt/src/VPT/VPT.cpp 19.3 KB
144cd8c4   Hu Chunming   v6.0.0 替换模型
1
2
3
4
5
6
7
8
9
10
11
12
  #include <time.h>
  #include "ErrorInfo.h"
  #include "VPT.h"
  #include "ObjCls.h"
  #include "utools.h"
  
  //model trt onnx 20210512
  //#include "ga_vpt_onnx_net.h"
  //#include "ga_trt_yolo_vpt_calibrator.h"
  
  //model trt onnx 0715
  //model pytorch_12cls_RenCheWu_yolov5m_hw640
6ca63d90   Hu Chunming   提交v4.0.0
13
14
  #include "ga_vpt_det_yolo_640x640.h"
  #include "ga_trt_yolo_vpt_calibrator.h"
144cd8c4   Hu Chunming   v6.0.0 替换模型
15
16
  
  //9
6ca63d90   Hu Chunming   提交v4.0.0
17
  //老底层 模型
144cd8c4   Hu Chunming   v6.0.0 替换模型
18
19
20
  //#include "ga_vpt_predict_net.h"
  //#include "ga_vpt_init_net.h"
  
6ca63d90   Hu Chunming   提交v4.0.0
21
  //trt的底层 caffe2的模型
144cd8c4   Hu Chunming   v6.0.0 替换模型
22
23
24
25
26
27
28
  //#include "ga_vpt_predict_net.h"
  //#include "ga_vpt_init_net.h"
  
  //13
  //#include "ga_model.h"
  //#include "ga_model_init.h"
  
144cd8c4   Hu Chunming   v6.0.0 替换模型
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  //#define AUTHORIZATION
  
  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 || (nYear == 2020 && nMonth < 4))
  		return 1;
  	else
  	{
6ca63d90   Hu Chunming   提交v4.0.0
46
  		printf("版本过期,请联系开发商!中科院自动化所模式识别实验室图像视频组\n");
144cd8c4   Hu Chunming   v6.0.0 替换模型
47
48
49
50
51
  		return -1;
  	}
  }
  
  #include "authority.h"
6ca63d90   Hu Chunming   提交v4.0.0
52
53
  //#define productSN "92DAF36635F7492EB40D4C10DC67832F"//人车物SDK_linux_v4.0.4.190701_cuda10.0_cudnn7.5.0
  #define productSN "7CF8B4797F9E441686145BED07F662DE"//人车物SDK_linux_v4.0.4.190701_cuda10.0_cudnn7.5.0
144cd8c4   Hu Chunming   v6.0.0 替换模型
54
55
56
57
58
59
60
61
62
63
  
  map<int, int> index_mp= {make_pair(0,0), make_pair(1,2), make_pair(2,4), make_pair(4,2), make_pair(5,5), make_pair(6,6), make_pair(7,8), make_pair(8,3), make_pair(10,1) , make_pair(11,2), make_pair(12,7)};
  
  void check_thread(objDetector * handle);
  
  int VPT_Init(void **handle, VPT_PARAM vparam, char* resDir, VIDEO_OBJECT_SNAPSHOT_CALLBACK VideoObjSnapshotCallBack/* = NULL*/)
  {
  	//if (checkTime() != 1)
  	//	return FAILED;
  
6ca63d90   Hu Chunming   提交v4.0.0
64
65
  	//int res = SUCCESS;
    int ret = SUCCESS;
144cd8c4   Hu Chunming   v6.0.0 替换模型
66
67
68
69
70
71
72
73
74
75
76
  #ifdef AUTHORIZATION
  #ifdef _WIN32
    if(SUCCESS == (ret = sy_licence(productSN)))
  #elif __linux__
  			char wtime[15];
  	memset(wtime, 0, 15);
  	char * time = wtime;
  	if (SUCCESS == (ret = sy_licence(productSN, &time)))
  #endif
  #endif
    {
6ca63d90   Hu Chunming   提交v4.0.0
77
78
    
    	*handle = new objDetector;	//申请对象
144cd8c4   Hu Chunming   v6.0.0 替换模型
79
80
    	memcpy((void *)&((objDetector *)*handle)->param, (void *)&vparam, sizeof(VPT_PARAM));
    
6ca63d90   Hu Chunming   提交v4.0.0
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
    	int flag = 0;	//返回值 默认为SUCCEEDED0
    
    
    //----------------- 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";
     */
     //----------------- 猫和狗结构化 ---------------------//
    
144cd8c4   Hu Chunming   v6.0.0 替换模型
122
    
6ca63d90   Hu Chunming   提交v4.0.0
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
    
    //----------------- 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_ = (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"
    		;
    
    	ret = ctools_init(&(((objDetector *)handle)->detector), &vpt_param);
     */
     /*
144cd8c4   Hu Chunming   v6.0.0 替换模型
168
      ctools_init_params vpt_param;
6ca63d90   Hu Chunming   提交v4.0.0
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
  		vpt_param.thres_ = 0.8;
  		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_ = NULL;
  		vpt_param.net_file_ = NULL;
  #else
  		vpt_param.weight_file_ = NULL;
  		vpt_param.net_file_ = NULL;
  #endif
  
  		
  		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"
  		;
  		vpt_param.need_im_info_ = 1; // true
  		
  		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;
  	  ret = ctools_init(&(((objDetector *)handle)->detector), &vpt_param);
   */
   
  
      //----------------- 人车物检测新模型YOLO ---------------------//
      /*
   	  ctools_init_params vpt_param;
  		vpt_param.thres_ = 0.5;//vparam.thres;
  		vpt_param.log_level_ = 0;
  		vpt_param.device_type_ =DEVICE_GPU;
  		vpt_param.device_id_ =  vparam.gpuid;
  		vpt_param.engine_type_ = ENGINE_TENSORRT;
  
  
  		vpt_param.data_process_str_ = 
      "CopyData_CPU2GPU_U8;"
144cd8c4   Hu Chunming   v6.0.0 替换模型
215
216
217
218
219
220
221
222
  		"TypeConvert_U8_F32;"
  		"ResizeMaxMidPad_F32_F32,test_size,640,test_max_size,640,max_height,640,max_width,640,"
  		"submean_b,0,submean_g,0,submean_r,0,"
  		"variance_rev_b,0.00392,variance_rev_g,0.00392,variance_rev_r,0.00392;"
  		"BGR2RGB_F32_F32;"
  		"NHWC2NCHW_F32"
  		;
      //param.preprocess_param;
6ca63d90   Hu Chunming   提交v4.0.0
223
224
225
226
227
228
  		
  		if (vpt_param.engine_type_ == ENGINE_MCAFFE2)
  		{
  			printf("ENGINE_MCAFFE2 not support!\n");
        vpt_param.engine_type_ = ENGINE_TENSORRT;
  		}
144cd8c4   Hu Chunming   v6.0.0 替换模型
229
  	 
6ca63d90   Hu Chunming   提交v4.0.0
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
     if (vpt_param.engine_type_ == ENGINE_TENSORRT)
  		{
  			vpt_param.need_im_info_ = 0; // true
  			vpt_param.model_type_ = MODEL_YOLOV5;
  			
  			vpt_param.net_array_ = (uint8_t*)ga_vpt_det_yolo_640x640;
  			vpt_param.net_array_len_ = ga_vpt_det_yolo_640x640_len;
  
  			vpt_param.trt_serialize_file_ = "VPT_DET";// NULL;// "FPN_VPT_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  
  			memset(vpt_param.tensorrt_param_str_, 0, sizeof(vpt_param.tensorrt_param_str_));
  
  			int batch_size = 20;
  			std::string g_data_mode = "INT8";
  			bool g_is_create_calibrator = false;
  			int g_is_onnx_model = 1;
  
  			sprintf(vpt_param.tensorrt_param_str_, "max_batchsize %d,"
  				"data_mode %s,"
  				"is_create_calibrator %d,"
  				"is_onnx_model %d,"
  				"input_names images,"
  				"output_names output",
  				batch_size, g_data_mode.c_str(), g_is_create_calibrator, g_is_onnx_model);
  
  			//vpt_param.tensorrt_calibrator_file_ = "trt_yolo_vpt_calibrator";
  			vpt_param.tensorrt_calibrator_array_len_ = ga_trt_yolo_vpt_calibrator_len;// "trt_fpn_vpt_calibrator";
  			vpt_param.tensorrt_calibrator_array_ = (unsigned char*)ga_trt_yolo_vpt_calibrator;// "trt_fpn_vpt_calibrator";
  		}
    */
    
      //pytorch_12cls_RenCheWu_yolov5m_hw640,模型类别有变化,process直接映射回之前的index
      ctools_init_params vpt_param;
  		vpt_param.thres_ = 0.5;//vparam.thres;
  		vpt_param.log_level_ = 0;
  		vpt_param.device_type_ =DEVICE_GPU;
  		vpt_param.device_id_ =  vparam.gpuid;
  		vpt_param.engine_type_ = ENGINE_TENSORRT;
  
  
  		vpt_param.data_process_str_ = 
      "CopyData_CPU2GPU_U8;"
  		"TypeConvert_U8_F32;"
  		"ResizeMaxMidPad_F32_F32,test_size,640,test_max_size,640,max_height,640,max_width,640,"
  		"submean_b,0,submean_g,0,submean_r,0,"
  		"variance_rev_b,0.00392,variance_rev_g,0.00392,variance_rev_r,0.00392;"
  		"BGR2RGB_F32_F32;"
  		"NHWC2NCHW_F32"
  		;
      //param.preprocess_param;
  		
  		if (vpt_param.engine_type_ == ENGINE_MCAFFE2)
  		{
  			printf("ENGINE_MCAFFE2 not support!\n");
        vpt_param.engine_type_ = ENGINE_TENSORRT;
  		}
  	 
     if (vpt_param.engine_type_ == ENGINE_TENSORRT)
  		{
  			vpt_param.need_im_info_ = 0; // true
  			vpt_param.model_type_ = MODEL_YOLOV5;
144cd8c4   Hu Chunming   v6.0.0 替换模型
291
  			
6ca63d90   Hu Chunming   提交v4.0.0
292
293
294
295
296
297
298
299
  			vpt_param.net_array_ = (uint8_t*)ga_vpt_det_yolo_640x640;
  			vpt_param.net_array_len_ = ga_vpt_det_yolo_640x640_len;
  
        char trt_serialize_file_[1024];
        {
          if(vparam.serialize_file == nullptr) vparam.serialize_file = ".";
          string dir_path =  vparam.serialize_file;
          int iLen = dir_path.length();
144cd8c4   Hu Chunming   v6.0.0 替换模型
300
         
6ca63d90   Hu Chunming   提交v4.0.0
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
          if (dir_path[iLen - 1] != '\\' && dir_path[iLen - 1] != '/')
  				{
  					dir_path += '/';
  				}
          sprintf(trt_serialize_file_, "%sVPT_DET_MODEL20220211", dir_path.c_str());
        }
       
        
        printf("serialize_file path: %s\n",trt_serialize_file_ );
  			vpt_param.trt_serialize_file_ = trt_serialize_file_;//"VPT_DET";
  
  			memset(vpt_param.tensorrt_param_str_, 0, sizeof(vpt_param.tensorrt_param_str_));
  
  			int batch_size = 20;
  			std::string g_data_mode = "INT8";
  			bool g_is_create_calibrator = false;
  			int g_is_onnx_model = 1;
  
  			sprintf(vpt_param.tensorrt_param_str_, "max_batchsize %d,"
  				"data_mode %s,"
  				"is_create_calibrator %d,"
  				"is_onnx_model %d,"
  				"input_names images,"
  				"output_names output",
  				batch_size, g_data_mode.c_str(), g_is_create_calibrator, g_is_onnx_model);
  
  			//vpt_param.tensorrt_calibrator_file_ = "trt_yolo_vpt_calibrator";
  			vpt_param.tensorrt_calibrator_array_len_ = ga_trt_yolo_vpt_calibrator_len;// "trt_fpn_vpt_calibrator";
  			vpt_param.tensorrt_calibrator_array_ = (unsigned char*)ga_trt_yolo_vpt_calibrator;// "trt_fpn_vpt_calibrator";
  		}
  		ret = ctools_init(&(((objDetector *)*handle)->detector), &vpt_param);
144cd8c4   Hu Chunming   v6.0.0 替换模型
332
  
6ca63d90   Hu Chunming   提交v4.0.0
333
   
144cd8c4   Hu Chunming   v6.0.0 替换模型
334
335
      if(ret == SUCCESS)
      {
6ca63d90   Hu Chunming   提交v4.0.0
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    /*
    cv::Mat img = cv::imread("test.jpg");
    
    
    sy_img cur_img;
    		cur_img.set_data(img.cols, img.rows, img.channels(), img.data);
    
    		ctools_result *detresult;
    
    int count = 1000;
    vector< vector <float>> detectResult;	//转化为跟踪所需要的输入
    while(count--)
    		int res_status = ctools_process((((objDetector *)handle)->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 < 100; 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);
    			}
    		}
    
    
    printf("============ finish det\n");
    */
    
    
      	//----------------- 9类结构化(自用单路人车物) ---------------------//
      		//拌线初始化
      	((objDetector *)*handle)->tracker = new Sort(/*lineArray, linecount, DETECTTYPE*/);
144cd8c4   Hu Chunming   v6.0.0 替换模型
382
      	((objDetector *)*handle)->snapshotHelper = new SnapShot(resDir, VideoObjSnapshotCallBack, vparam.SnaoshotParameter);
144cd8c4   Hu Chunming   v6.0.0 替换模型
383
384
385
386
      
      	((objDetector *)*handle)->frameCounter = FusionInterval;
       	((objDetector *)*handle)->isInitFrame = true;
        
6ca63d90   Hu Chunming   提交v4.0.0
387
388
389
390
391
392
393
        //----------------- 授权线程初始化 ---------------------//
       #ifdef AUTHORIZATION
        ((objDetector *)*handle)->licence_status = 0;
     		((objDetector *)*handle)->thrd = boost::thread(check_thread, ((objDetector *)*handle));
        ((objDetector *)*handle)->thrd_status = 0;
        #endif
      }
144cd8c4   Hu Chunming   v6.0.0 替换模型
394
      }
144cd8c4   Hu Chunming   v6.0.0 替换模型
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
  /*
    }
    else
    {
      ret =  AUTHOR_ERROR;	
    }
  #ifdef __linux__
    if(wtime)
    {
  		delete[] wtime;
  		wtime = NULL;
    }
  #endif
  */
    return ret;
  }
    
144cd8c4   Hu Chunming   v6.0.0 替换模型
412
  void VPT_ResetTracker(void * handle/*, int maxResultCountVPT_Line * lineArray = NULL, int linecount = 0*/)
6ca63d90   Hu Chunming   提交v4.0.0
413
  {
144cd8c4   Hu Chunming   v6.0.0 替换模型
414
  	objDetector* tools = (objDetector*)handle;
6ca63d90   Hu Chunming   提交v4.0.0
415
416
417
  	/*if (tools->ssdResult)
  	{
  	delete[] tools->ssdResult;
144cd8c4   Hu Chunming   v6.0.0 替换模型
418
  	}
6ca63d90   Hu Chunming   提交v4.0.0
419
420
421
422
423
424
425
426
  	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*/);	//新接口 无需流量统计和快照
144cd8c4   Hu Chunming   v6.0.0 替换模型
427
428
429
  }
  
  
6ca63d90   Hu Chunming   提交v4.0.0
430
  //辅助函数:数据预处理
144cd8c4   Hu Chunming   v6.0.0 替换模型
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
  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;
6ca63d90   Hu Chunming   提交v4.0.0
451
  	//CDTOOLS_Result ssdResult[MAX_OBJ_COUNT_TEST];
144cd8c4   Hu Chunming   v6.0.0 替换模型
452
  
6ca63d90   Hu Chunming   提交v4.0.0
453
  	vector< vector <float>> detectResult;	//转化为跟踪所需要的输入
144cd8c4   Hu Chunming   v6.0.0 替换模型
454
455
456
457
458
459
460
461
  	int index = 0;
  	bool isUseDet = false;
  	vector<int> deleteTrackers;
  
  	int ObjCount = tools->tracker->updateLastFrame(deleteTrackers);
  
  	//void SaveSnapshot(vector<int> deleteTrackers);
  	tools->snapshotHelper->SaveSnapshot(deleteTrackers);
6ca63d90   Hu Chunming   提交v4.0.0
462
          sleep(10); //靠靠靠靠 220824 byzsh
144cd8c4   Hu Chunming   v6.0.0 替换模型
463
464
465
466
  	vector<vector<float>>().swap(detectResult);
  	detectResult.clear();
  
  	vector<int>().swap(deleteTrackers);
6ca63d90   Hu Chunming   提交v4.0.0
467
  	deleteTrackers.clear();
144cd8c4   Hu Chunming   v6.0.0 替换模型
468
  
144cd8c4   Hu Chunming   v6.0.0 替换模型
469
  
144cd8c4   Hu Chunming   v6.0.0 替换模型
470
471
472
473
474
475
476
477
478
479
480
481
482
  	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];
    
    #ifdef AUTHORIZATION
    if (tools->licence_status > -3)
    #endif
    {
6ca63d90   Hu Chunming   提交v4.0.0
483
    	Mat img(height, width, CV_8UC3, bgr);
144cd8c4   Hu Chunming   v6.0.0 替换模型
484
485
      cv::Mat tmp_img;
      img.copyTo(tmp_img);
6ca63d90   Hu Chunming   提交v4.0.0
486
    	vector< vector <float>> detectResult;	//转化为跟踪所需要的输入
144cd8c4   Hu Chunming   v6.0.0 替换模型
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
    	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(img.cols, img.rows, img.channels(), img.data);
    
    		ctools_result *detresult;
    
    		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];
6ca63d90   Hu Chunming   提交v4.0.0
512
          int old_index = cur_result.obj_results_[c].data_[0];
144cd8c4   Hu Chunming   v6.0.0 替换模型
513
        
6ca63d90   Hu Chunming   提交v4.0.0
514
    			if (score >= THRESHOLD && (old_index!=3 && old_index!=9)/*index 为pytorch_12cls_RenCheWu_yolov5m_hw640模型添加index映射的操作*/)  //更改为left top right bottom score index(-1的原始数据 sort内部进行-1操作)
144cd8c4   Hu Chunming   v6.0.0 替换模型
515
516
517
518
519
520
521
    			{
    				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]);
6ca63d90   Hu Chunming   提交v4.0.0
522
            //obj.push_back(cur_result.obj_results_[c].data_[0] + 1);  //针对YOLO模型 内部-1 外部得+1
144cd8c4   Hu Chunming   v6.0.0 替换模型
523
524
            
    				int new_index = index_mp[old_index] + 1;
6ca63d90   Hu Chunming   提交v4.0.0
525
            obj.push_back(new_index);
144cd8c4   Hu Chunming   v6.0.0 替换模型
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
          
    				detectResult.push_back(obj);
    			}
    		}
    
    		//dResult->objCount = index;
    		result->objCount = detectResult.size();
       
    		//VPT_Result temp;
    		tools->isInitFrame = false;
    		tools->frameCounter = 0;
    		isUseDet = true;
    
    	}
    	tools->frameCounter++;
    
    
6ca63d90   Hu Chunming   提交v4.0.0
543
    	//for (int j = 0; j < FusionInterval; j++)	//跳帧:检测一帧,跟踪 (FusionInterval - 1) 
144cd8c4   Hu Chunming   v6.0.0 替换模型
544
545
    	//{
     
6ca63d90   Hu Chunming   提交v4.0.0
546
547
  
    	int objCount = tools->tracker->update(width, height, isUseDet, detectResult, result->obj, deleteTrackers);
144cd8c4   Hu Chunming   v6.0.0 替换模型
548
549
    
    	result->objCount = objCount;
144cd8c4   Hu Chunming   v6.0.0 替换模型
550
    	
6ca63d90   Hu Chunming   提交v4.0.0
551
    	tools->snapshotHelper->SnapshotProcess(result, tmp_img, deleteTrackers, frameCount);
144cd8c4   Hu Chunming   v6.0.0 替换模型
552
553
554
555
556
557
558
559
560
561
    
      vector<vector<float>>().swap(detectResult);
    	detectResult.clear();
    	
    	vector<int>().swap(deleteTrackers);
    	deleteTrackers.clear();
    	//}
    //}
    
    
6ca63d90   Hu Chunming   提交v4.0.0
562
563
    	if (showTrack)
    		tools->tracker->addTracker(&tmp_img);
144cd8c4   Hu Chunming   v6.0.0 替换模型
564
565
566
567
568
569
570
571
572
573
574
   
    	return 0;
    }
    /*
  	else
  	{
      return AUTHOR_ERROR;
  	}
   */
  }
  
6ca63d90   Hu Chunming   提交v4.0.0
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
  //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;
  //}
144cd8c4   Hu Chunming   v6.0.0 替换模型
606
607
608
609
610
611
612
613
614
615
616
617
  
  void VPT_Release(void **handle)
  {
  	objDetector* tools = (objDetector*)*handle;
  
  	VPT_ProcessLastFrame(tools);
  
  	if (tools && tools->detector)
  	{
  		ctools_release(&tools->detector);
  		tools->detector = NULL;
  	}
144cd8c4   Hu Chunming   v6.0.0 替换模型
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
   
   #ifdef AUTHORIZATION
    if(tools->thrd_status == 0)
    {
  			tools->thrd.interrupt();
  			tools->thrd.join();
  			tools->thrd_status = -1;
  	}
   #endif
  	if (tools)
  	{
  		delete tools;
  		tools = NULL;
  	}
  }
  
  	void check_thread(objDetector * handle)
  		{
  			int res = -1;
  #ifdef __linux__
  			char wtime[15];
  	memset(wtime, 0, 15);
  	char * time = wtime;
  #endif
  			while (1)
  			{
  				//printf("xxx check status on process...\n");
  #ifdef _WIN32
  				res = sy_licence(productSN);
  #elif __linux__
  				res = sy_licence(productSN, &time);
  				//printf("--------------wtime in thread: %s, status: %d\n", wtime, licence_status);
  #endif
  				if (res < 0)
  				{
  					handle->licence_status= handle->licence_status - 1;
  				}
  				else
  				{
  					if (handle->licence_status < 0)
  					{
  						handle->licence_status = 0;
  					}
  				}
  				boost::this_thread::sleep(boost::posix_time::seconds(300)); //5min
  				//boost::this_thread::sleep(boost::posix_time::milliseconds(10);
  			}
6ca63d90   Hu Chunming   提交v4.0.0
665
  		}