From b944fdeb452972c30d853dfe5e02110427c343b9 Mon Sep 17 00:00:00 2001 From: cmhu <2657262686@qq.com> Date: Thu, 6 Jun 2024 17:53:01 +0800 Subject: [PATCH] 同步代码 --- vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.cpp | 65 +++++++++++++++++++++++++++++++++++++---------------------------- vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.h | 7 +++++-- vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.cpp | 381 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.h | 94 ---------------------------------------------------------------------------------------------- vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/VPTProcess.cpp | 381 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/VPTProcess.h | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 517 insertions(+), 505 deletions(-) delete mode 100644 vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.cpp delete mode 100644 vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.h create mode 100644 vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/VPTProcess.cpp create mode 100644 vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/VPTProcess.h diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.cpp b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.cpp index f00b4b5..d35fab9 100644 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.cpp +++ b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.cpp @@ -182,7 +182,7 @@ int CMutliSourceVideoProcess::InitAlgorthim(mvpt_param vptParam, VIDEO_OBJECT_IN m_face_det_config = vptParam.face_det_config; - m_snaphot_helper.snapshot_helper_init(vptParam.gpuid, gpu_total_memory, vptParam.vrdbpath, vptParam.auth_license, vptParam.wait_framecount, cmdConfig); + m_snaphot_helper.snapshot_helper_init(vptParam.gpuid, gpu_total_memory, vptParam.vrdbpath, AUTH_LICENSE, vptParam.wait_framecount, cmdConfig); m_bProcessExit = false; ProcessThread = std::thread(algorthim_process_thread, this); @@ -202,7 +202,12 @@ int CMutliSourceVideoProcess::InitAlgorthim(mvpt_param vptParam, VIDEO_OBJECT_IN void CMutliSourceVideoProcess::FinishTask(const int taskID) { - if (m_taskMap.find(taskID) == m_taskMap.end()){ + task_over(taskID, true); +} + +void CMutliSourceVideoProcess::task_over(const int taskID, bool bOver) { + + if (m_taskMap.find(taskID) == m_taskMap.end()) { return; } @@ -235,18 +240,20 @@ void CMutliSourceVideoProcess::FinishTask(const int taskID) m_snaphot_helper.waitSaveAnalysisInfo(task.taskID); - task.taskTcuvid->DxCloseDecoder(); - delete task.taskTcuvid; - task.taskTcuvid = NULL; + if (task.taskTcuvid) + { + task.taskTcuvid->DxCloseDecoder(); + delete task.taskTcuvid; + task.taskTcuvid = NULL; + } //回调通知上层任务结束 - if (task.taskFinishCallbackFunc != nullptr) { + if (bOver && task.taskFinishCallbackFunc != nullptr) { std::lock_guard l(m_snaphot_helper.callback_tx); task.taskFinishCallbackFunc(task.taskID); } LOG_INFO("task {} is finished. timeusing: {}", taskID, get_cur_time_ms() - task.timestamp); - } void CMutliSourceVideoProcess::PauseTask(const int taskID) @@ -327,8 +334,6 @@ bool CMutliSourceVideoProcess::AddTask(task_param tparam) //debug by zsh return false; } - cout << "AddTask: " << new_task.taskFileSource << endl; - DxConfig cfg = { 0 }; cfg.devId = mgpuid; cfg.forceTcp = false; @@ -471,8 +476,6 @@ int CMutliSourceVideoProcess::AddOperator(task_param tparam) memcpy(newOper.minBoxsize, tparam.minBoxsize, sizeof(sy_rect)* DETECTTYPE); TaskOperatorQ.push_back(newOper); taskCondVar.wait_for(l, std::chrono::seconds(20)); - - cout << "AddOperator: " << newOper.videoFileName << endl; } int addRes = -1; @@ -494,7 +497,7 @@ int CMutliSourceVideoProcess::AddOperator(task_param tparam) { TaskOperatorQ.pop_back(); } - cout << "Failed Add New Task! Algorithm Process Error! " << endl; + LOG_ERROR("Failed Add New Task! Algorithm Process Error: {}", tparam.video_filename); } return addRes; @@ -550,8 +553,6 @@ void CMutliSourceVideoProcess::OperatorTask() tparam.log = newOperator.taskSfxLogFunc; tparam.userPtr = newOperator.logUserPtr; - cout << "before AddTask: " << tparam.video_filename << endl; - AddTask(tparam); } break; @@ -568,7 +569,6 @@ void CMutliSourceVideoProcess::OperatorTask() if (iter != m_taskMap.end()) { m_taskMap.erase(iter); } - } break; default: @@ -639,7 +639,7 @@ void CMutliSourceVideoProcess::algorthim_process() while (!m_bProcessExit) { if (licence_status <= -3) { - printf("authority failed!\n"); + LOG_ERROR("authority failed!\n"); break; } @@ -653,7 +653,14 @@ void CMutliSourceVideoProcess::algorthim_process() for (auto it=m_taskMap.begin(); it!=m_taskMap.end(); ) { Task& task = it->second; if (!task.taskTcuvid->DxDecoderIsRun() && task.taskTcuvid->DxFrameIsEmpty()) { - FinishTask(task.taskID); + //if (task.taskTcuvid->IsHandleClose()) + //{ + // task_over(task.taskID, false); + //} + //else + { + task_over(task.taskID, true); + } it = m_taskMap.erase(it); } else { ++it; @@ -678,6 +685,8 @@ void CMutliSourceVideoProcess::algorthim_process() if(task.taskTotalFrameCount <= 1) { task.taskTotalFrameCount = task.taskTcuvid->DxGetFrameCount(); } + + task.taskFrameCount = dxFrame.timestamp; } if (vec_dxGpuFrame.size() <= 0) { @@ -710,9 +719,7 @@ void CMutliSourceVideoProcess::algorthim_process() vector vec_vptResult = m_vptProcess.process(vec_data); -#ifdef LOG_INFO2 - std::cout << "VPT_Process_GPU time_using: " << get_cur_time_ms() - start_time_vpt << std::endl; -#endif + //LOG_DEBUG("VPT_Process_GPU time_using: {}", get_cur_time_ms() - start_time_vpt); long long result_analysis_time = get_cur_time_ms(); @@ -722,8 +729,6 @@ void CMutliSourceVideoProcess::algorthim_process() Task& task = m_taskMap[task_id]; DxGPUFrame& task_algorithm_data = vec_dxGpuFrame[i]; - - task.taskFrameCount = task_algorithm_data.timestamp; //实时查看模块,若存在实时查看,把当前视频画面cp回内存 bool view = false; @@ -841,17 +846,21 @@ void CMutliSourceVideoProcess::algorthim_process() { //需要做人脸检测 int human_count = human_idx.size(); - sy_img human_img[human_count]; - sy_point ori_points[human_count]; + vector vec_human_img; + vector< sy_point> vec_points; for (int idx = 0; idx < human_count; idx++) { int ii = human_idx[idx]; - human_img[idx].set_data(snapshot_dst_width[ii], snapshot_dst_height[ii], 3, snapshot_image_data[ii]); - ori_points[idx].x_ = (snapshot_right[ii] - snapshot_left[ii]); - ori_points[idx].y_ = (snapshot_bottom[ii] - snapshot_top[ii]); + sy_img human_img; + human_img.set_data(snapshot_dst_width[ii], snapshot_dst_height[ii], 3, snapshot_image_data[ii]); + sy_point ori_point; + ori_point.x_ = (snapshot_right[ii] - snapshot_left[ii]); + ori_point.y_ = (snapshot_bottom[ii] - snapshot_top[ii]); + vec_points.push_back(ori_point); + vec_human_img.push_back(human_img); } - m_snaphot_helper.cacheFaceSnapshotInfo(human_img, human_count, ori_points, human_idx, human_obj_keys, snapshot_left, snapshot_top, task_algorithm_data); + m_snaphot_helper.cacheFaceSnapshotInfo(vec_human_img.data(), human_count, vec_points.data(), human_idx, human_obj_keys, snapshot_left, snapshot_top, task_algorithm_data); } } diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.h b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.h index 6f2b4da..5856955 100644 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.h +++ b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.h @@ -10,7 +10,6 @@ #include "time.h" #include "opencv2/opencv.hpp" #include "opencv2/highgui/highgui.hpp" -#include "VPTProcess.h" #include #include #include @@ -18,7 +17,8 @@ #include "../cuda_src/ImageSaveGPU.h" #include "mvpt_process_assist.h" -#include "snapshot_helper.h" +#include "./snapshot_analysis/VPTProcess.h" +#include "./snapshot_analysis/snapshot_helper.h" #include @@ -132,6 +132,9 @@ public: void algorthim_process(); +private: + void task_over(const int taskID, bool bOver); + public: int section_batch_size; int licence_status; diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.cpp b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.cpp deleted file mode 100644 index 60bfcd2..0000000 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.cpp +++ /dev/null @@ -1,381 +0,0 @@ -#include "VPTProcess.h" - -#include -#include -#include "ErrorInfo.h" - -#include "../utils/logger.hpp" - -#define THRESHOLD 0.5 - -#define USE_YOLOV5 - - -// #include "../../model/vptModeTrt/ga_vpt_init_net.h" -// #include "../../model/vptModeTrt/ga_vpt_predict_net.h" -// //#include "../../model/hidemodel_caffe_1108/ga_vpt_init_net_caffe2.h" -// //#include "../../model/hidemodel_caffe_1108/ga_vpt_predict_net_caffe2.h" - -//model trt onnx 20210715 -#include "../../model/vptModeTrt/ga_vpt_det_yolo_640x640.h" // debug by zsh -#include "../../model/vptModeTrt/ga_trt_yolo_vpt_calibrator.h" // debug by zsh - -// // fpn -// #include "vpt_fpn_plugin_factory.h" -// #include "../../model/vptModeTrt/ga_trt_fpn_vpt_calibrator.h" - - -struct DetectResultInfo { - vector< vector > det_result; - int task_id; - int width; - int height; - unsigned long long ts; -}; - - - -static long long get_cur_time_ms(){ - chrono::time_point tpMicro - = chrono::time_point_cast(chrono::system_clock::now()); - - return tpMicro.time_since_epoch().count(); -} - -VPTProcess::VPTProcess(/* args */) -{ - m_taskTrackerMap.clear(); -} - -VPTProcess::~VPTProcess() -{ -} - -void VPTProcess::config_fpn_param(ctools_init_params& param, int batch_size) { - // param.model_type_ = MODEL_FPN; - - // param.weight_file_ = NULL; - // param.net_file_ = NULL; - - // param.data_process_str_ = - // //"CopyData_CPU2GPU_U8;" - // "TypeConvert_U8_F32;" - // "ResizeMaxPad_F32_F32,test_size,720,test_max_size,1280,max_height,736,max_width,1280," - // "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" - // ; - // param.need_im_info_ = 1; // true - - // if (param.engine_type_ == ENGINE_MCAFFE2) - // { - // /*param.weight_array_ = (unsigned char*)ga_vpt_init_net_caffe2; - // param.weight_array_len_ = ga_vpt_init_net_len_caffe2; - // param.net_array_ = (unsigned char*)ga_vpt_predict_net_caffe2; - // param.net_array_len_ = ga_vpt_predict_net_len_caffe2;*/ - // } - // else if (param.engine_type_ == ENGINE_TENSORRT) - // { - // param.weight_array_ = (uint8_t*)ga_vpt_init_net; - // param.weight_array_len_ = ga_vpt_init_net_len; - // param.net_array_ = (uint8_t*)ga_vpt_predict_net; - // param.net_array_len_ = ga_vpt_predict_net_len; - - // memset(param.tensorrt_param_str_, 0, sizeof(param.tensorrt_param_str_)); - - // std::string g_data_mode = "FP32"; - // bool g_is_create_calibrator = false; - - // sprintf(param.tensorrt_param_str_, "max_batchsize %d," - // "data_mode %s," - // "is_create_calibrator %d," - // "input_names data im_info," - // "output_names cls_prob bbox_pred_final rois_count_each_img", - // batch_size, g_data_mode.c_str(), g_is_create_calibrator); - - // param.tensorrt_calibrator_file_ = NULL;// "trt_fpn_vpt_calibrator"; - // param.tensorrt_calibrator_array_len_ = ga_trt_fpn_vpt_calibrator_len;// "trt_fpn_vpt_calibrator"; - // param.tensorrt_calibrator_array_ = (unsigned char*)ga_trt_fpn_vpt_calibrator;// "trt_fpn_vpt_calibrator"; - - // param.tensorrt_plugin_factory_ptr_ = &(tensorrt_plugin_factory); - // } -} - -void VPTProcess::config_yolo_param(ctools_init_params& param, int batch_size) { - - param.model_type_ = MODEL_YOLOV5; - param.weight_file_ = NULL; - param.net_file_ = NULL; - - 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.need_im_info_ = 0; - - if (param.engine_type_ == ENGINE_MCAFFE2) - { - /*param.weight_array_ = (unsigned char*)ga_vpt_init_net_caffe2; - param.weight_array_len_ = ga_vpt_init_net_len_caffe2; - param.net_array_ = (unsigned char*)ga_vpt_predict_net_caffe2; - param.net_array_len_ = ga_vpt_predict_net_len_caffe2;*/ - } - else if (param.engine_type_ == ENGINE_TENSORRT) - { - param.net_array_ = (uint8_t*)ga_vpt_det_yolo_640x640; - param.net_array_len_ = ga_vpt_det_yolo_640x640_len; //debug by zsh - - memset(param.tensorrt_param_str_, 0, sizeof(param.tensorrt_param_str_)); - - std::string g_data_mode = "FP32"; - bool g_is_create_calibrator = false; - - int g_is_onnx_model = 1; - sprintf(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); - - param.tensorrt_calibrator_array_len_ = ga_trt_yolo_vpt_calibrator_len; - param.tensorrt_calibrator_array_ = (unsigned char*)ga_trt_yolo_vpt_calibrator; - - } -} - -int VPTProcess::init(int gpuid, int max_batch_size) -{ - ctools_init_params param; - param.thres_ = 0.5; - param.log_level_ = 0; - param.device_type_ = DEVICE_GPU; - param.device_id_ = gpuid; - param.engine_type_ = ENGINE_TENSORRT; - param.trt_serialize_file_ = "./serialize_file/VPT"; - - m_max_batch_size = max_batch_size; - -#ifdef USE_YOLOV5 - config_yolo_param(param, m_max_batch_size); -#else - config_fpn_param(param, m_max_batch_size); -#endif - - int flag = ctools_init(&det_handle, ¶m); - if (SUCCESS != flag) { - LOG_ERROR("VPTProcess init failed!"); - } - - return flag; -} - -void VPTProcess::check_VPT_Result(VPT_Result & vResult) { - int index = 0; - for (int i = 0; i < vResult.objCount; i++) { - if ((vResult.obj[i].right - vResult.obj[i].left) > 10 && (vResult.obj[i].bottom - vResult.obj[i].top) > 10) - { - if (index == i) { - index++; - continue; - } - vResult.obj[index].left = vResult.obj[i].left; - vResult.obj[index].top = vResult.obj[i].top; - vResult.obj[index].right = vResult.obj[i].right; - vResult.obj[index].bottom = vResult.obj[i].bottom; - vResult.obj[index].center_x = vResult.obj[i].center_x; - vResult.obj[index].center_y = vResult.obj[i].center_y; - vResult.obj[index].index = vResult.obj[i].index; - vResult.obj[index].id = vResult.obj[i].id; - vResult.obj[index].num = vResult.obj[i].num; - vResult.obj[index].confidence = vResult.obj[i].confidence; - vResult.obj[index].snap_flag = vResult.obj[i].snap_flag; - index++; - } - } - - vResult.objCount = index; -} - -vector VPTProcess::process(vector vec_data) { - - vector vec_result; - - if(nullptr == det_handle){ - return vec_result; - } - - long long t1 = get_cur_time_ms(); - - int batchsize = vec_data.size(); - - vector vec_detectResult; - - int cycle_time = batchsize / m_max_batch_size; - cycle_time = (batchsize % m_max_batch_size) == 0 ? cycle_time : (cycle_time + 1) ; - - for (int i = 0; i < cycle_time; i++) { - int start_index = i * m_max_batch_size; - int end_index = start_index + m_max_batch_size; - if(end_index >= batchsize) { - end_index = batchsize; - } - - vector vec_img; - vector vec_task_id; - vector vec_ts; - for (int j = start_index; j < end_index; j++) { - vec_img.push_back(vec_data[j].img); - vec_task_id.push_back(vec_data[j].task_id); - vec_ts.push_back(vec_data[j].frameIndex); - } - - ctools_result *detresult; - int res_status = ctools_process(det_handle, vec_img.data(), vec_img.size(), &detresult); - - for (size_t b = 0; b < vec_img.size(); b++) { - ctools_result &cur_result = detresult[b]; - - DetectResultInfo result_info; - result_info.task_id = vec_task_id[b]; - result_info.ts = vec_ts[b]; - result_info.width = vec_img[b].w_; - result_info.height = vec_img[b].h_; - for (int c = 0; c < cur_result.obj_count_ && c < MAX_OBJ_COUNT; c++) - { - float x1 = cur_result.obj_results_[c].data_[2]; - float y1 = cur_result.obj_results_[c].data_[3]; - float x2 = cur_result.obj_results_[c].data_[4]; - float y2 = cur_result.obj_results_[c].data_[5]; - - float class_id = cur_result.obj_results_[c].data_[0]; - float score = cur_result.obj_results_[c].data_[1]; - - if (score >= THRESHOLD) - { - vector obj; - - obj.push_back(x1); - obj.push_back(y1); - obj.push_back(x2); - obj.push_back(y2); - obj.push_back(score); - obj.push_back(class_id); - // detectResult[real_index].push_back(obj); - result_info.det_result.push_back(obj); - } - } - - vec_detectResult.push_back(result_info); - } - } - - for (int i = 0; i < vec_detectResult.size(); i++) { - DetectResultInfo& det_result_info = vec_detectResult[i]; - TaskTracker& task_tracker = m_taskTrackerMap[det_result_info.task_id]; - - // TaskTracker& task_tracker = tools->taskTrackers[i]; - if (!task_tracker.tracker.GetState()) { - continue; - } - - int widht = det_result_info.width; - int height = det_result_info.height; - - VPTProcessResult oneResult; - oneResult.task_id = det_result_info.task_id; - if (task_tracker.lastFrameIndex > 0) { - // 非第一帧 - int update_times = det_result_info.ts - task_tracker.lastFrameIndex - 1; - if (update_times < 0) { - cout << "FrameIndex error !! lastFrameIndex= " << task_tracker.lastFrameIndex << " cur_frameindex = " << det_result_info.ts << endl; - } - - cout << "update_times " << update_times << endl; - - for (int j = 0; j < update_times; j++) { // 无检测框跟踪 - VPT_Result unresult; - unresult.objCount = task_tracker.tracker.update(widht, height, false, task_tracker.lastDetectResult, unresult.obj, task_tracker.lastDeleteObjectID); - check_VPT_Result(unresult); - oneResult.vecUnUsedResult.push_back(unresult); - } - } - oneResult.vptResult.objCount = task_tracker.tracker.update(widht, height, true, det_result_info.det_result, oneResult.vptResult.obj, oneResult.vecDeleteObj); - - check_VPT_Result(oneResult.vptResult); - - task_tracker.lastDetectResult = det_result_info.det_result; - task_tracker.lastDeleteObjectID = oneResult.vecDeleteObj; - task_tracker.lastFrameIndex = det_result_info.ts; - - oneResult.ts = det_result_info.ts; - - vec_result.push_back(oneResult); - } - - return vec_result; -} - -void VPTProcess::release() { - if (det_handle) { - ctools_release(&det_handle); - det_handle = NULL; - } - - m_taskTrackerMap.clear(); -} - -void VPTProcess::AddTaskTracker(const int taskID) -{ - TaskTracker t; - t.TaskID = taskID; - t.lastFrameIndex = 0; - t.tracker.setYOLOv5(true); // YOLOv5 要设为true, fpn 要设为false - - m_taskTrackerMap[taskID] = t; -} - -void VPTProcess::FinishTaskTracker(const int taskID) -{ - if (m_taskTrackerMap.find(taskID) == m_taskTrackerMap.end()) { - return; - } - - m_taskTrackerMap.erase(taskID); -} - -void VPTProcess::PauseTaskTracker(const int taskID) -{ - if (m_taskTrackerMap.find(taskID) == m_taskTrackerMap.end()) { - return; - } - - TaskTracker& t = m_taskTrackerMap[taskID]; - t.tracker.Pause(); -} - -void VPTProcess::RestartTaskTraker(const int taskID) -{ - if (m_taskTrackerMap.find(taskID) == m_taskTrackerMap.end()) { - return; - } - - TaskTracker& t = m_taskTrackerMap[taskID]; - t.tracker.ReSet(); -} - -void VPTProcess::DrawTracker(const int taskID, cv::Mat *img) -{ - if (m_taskTrackerMap.find(taskID) == m_taskTrackerMap.end()) { - return; - } - - TaskTracker& t = m_taskTrackerMap[taskID]; - t.tracker.addTracker(img); -} \ No newline at end of file diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.h b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.h deleted file mode 100644 index c757b12..0000000 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.h +++ /dev/null @@ -1,94 +0,0 @@ -/******************************************************************************************* -* Version: VPT_x64_V2.0.0_20170705 -* CopyRight: �п�Ժ�Զ����о���ģʽʶ��ʵ����ͼ����Ƶ�� -* UpdateDate: 20170705 -* Content: �˳�������� -********************************************************************************************/ -#pragma once -#include "opencv2/highgui/highgui.hpp" -#include -#include "utools.h" -#include -#include -#include "common.h" - -#include "./sort/Sort.h" //tracker - -using namespace std; - - -struct TaskTracker -{ - int TaskID; - double ratioWidth; - double ratioHeight; - Sort tracker; - unsigned int lastFrameIndex; - - vector< vector > lastDetectResult; - vector lastDeleteObjectID; -}; - -struct DataInfo { - int task_id {-1}; - sy_img img; - unsigned long long frameIndex; -}; - -struct VPTProcessResult { - int task_id {-1}; - VPT_Result vptResult; - vector vecDeleteObj; - vector vecUnUsedResult; - unsigned long long ts; -}; - - -class VPTProcess -{ -public: - VPTProcess(/* args */); - ~VPTProcess(); - - /************************************************************************* - * PURPOSE: 初始化 - * PARAM: - [in] vparam 参数 - * RETURN: handle - * NOTES: - *************************************************************************/ - int init(int gpuid, int max_batch_size); - - /************************************************************************* - * PURPOSE: 人车物检测跟踪 - *************************************************************************/ - - vector process(vector vec_data); - - /************************************************************************* - * PURPOSE: 资源释放 - * PARAM: - [in] handle - 处理句柄 - * RETURN: NULL - * NOTES: - *************************************************************************/ - void release(); - - void AddTaskTracker(const int taskID); - void FinishTaskTracker(const int taskID); - void PauseTaskTracker(const int taskID); - void RestartTaskTraker(const int taskID); - void DrawTracker(const int taskID, cv::Mat *img); - -private: - void config_fpn_param(ctools_init_params& param, int batch_size); - void config_yolo_param(ctools_init_params& param, int batch_size); - void check_VPT_Result(VPT_Result & vResult); - -private: - int m_max_batch_size {20}; - - void* det_handle {nullptr}; - vector taskTrackers; - map m_taskTrackerMap; -}; diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/VPTProcess.cpp b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/VPTProcess.cpp new file mode 100644 index 0000000..60bfcd2 --- /dev/null +++ b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/VPTProcess.cpp @@ -0,0 +1,381 @@ +#include "VPTProcess.h" + +#include +#include +#include "ErrorInfo.h" + +#include "../utils/logger.hpp" + +#define THRESHOLD 0.5 + +#define USE_YOLOV5 + + +// #include "../../model/vptModeTrt/ga_vpt_init_net.h" +// #include "../../model/vptModeTrt/ga_vpt_predict_net.h" +// //#include "../../model/hidemodel_caffe_1108/ga_vpt_init_net_caffe2.h" +// //#include "../../model/hidemodel_caffe_1108/ga_vpt_predict_net_caffe2.h" + +//model trt onnx 20210715 +#include "../../model/vptModeTrt/ga_vpt_det_yolo_640x640.h" // debug by zsh +#include "../../model/vptModeTrt/ga_trt_yolo_vpt_calibrator.h" // debug by zsh + +// // fpn +// #include "vpt_fpn_plugin_factory.h" +// #include "../../model/vptModeTrt/ga_trt_fpn_vpt_calibrator.h" + + +struct DetectResultInfo { + vector< vector > det_result; + int task_id; + int width; + int height; + unsigned long long ts; +}; + + + +static long long get_cur_time_ms(){ + chrono::time_point tpMicro + = chrono::time_point_cast(chrono::system_clock::now()); + + return tpMicro.time_since_epoch().count(); +} + +VPTProcess::VPTProcess(/* args */) +{ + m_taskTrackerMap.clear(); +} + +VPTProcess::~VPTProcess() +{ +} + +void VPTProcess::config_fpn_param(ctools_init_params& param, int batch_size) { + // param.model_type_ = MODEL_FPN; + + // param.weight_file_ = NULL; + // param.net_file_ = NULL; + + // param.data_process_str_ = + // //"CopyData_CPU2GPU_U8;" + // "TypeConvert_U8_F32;" + // "ResizeMaxPad_F32_F32,test_size,720,test_max_size,1280,max_height,736,max_width,1280," + // "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" + // ; + // param.need_im_info_ = 1; // true + + // if (param.engine_type_ == ENGINE_MCAFFE2) + // { + // /*param.weight_array_ = (unsigned char*)ga_vpt_init_net_caffe2; + // param.weight_array_len_ = ga_vpt_init_net_len_caffe2; + // param.net_array_ = (unsigned char*)ga_vpt_predict_net_caffe2; + // param.net_array_len_ = ga_vpt_predict_net_len_caffe2;*/ + // } + // else if (param.engine_type_ == ENGINE_TENSORRT) + // { + // param.weight_array_ = (uint8_t*)ga_vpt_init_net; + // param.weight_array_len_ = ga_vpt_init_net_len; + // param.net_array_ = (uint8_t*)ga_vpt_predict_net; + // param.net_array_len_ = ga_vpt_predict_net_len; + + // memset(param.tensorrt_param_str_, 0, sizeof(param.tensorrt_param_str_)); + + // std::string g_data_mode = "FP32"; + // bool g_is_create_calibrator = false; + + // sprintf(param.tensorrt_param_str_, "max_batchsize %d," + // "data_mode %s," + // "is_create_calibrator %d," + // "input_names data im_info," + // "output_names cls_prob bbox_pred_final rois_count_each_img", + // batch_size, g_data_mode.c_str(), g_is_create_calibrator); + + // param.tensorrt_calibrator_file_ = NULL;// "trt_fpn_vpt_calibrator"; + // param.tensorrt_calibrator_array_len_ = ga_trt_fpn_vpt_calibrator_len;// "trt_fpn_vpt_calibrator"; + // param.tensorrt_calibrator_array_ = (unsigned char*)ga_trt_fpn_vpt_calibrator;// "trt_fpn_vpt_calibrator"; + + // param.tensorrt_plugin_factory_ptr_ = &(tensorrt_plugin_factory); + // } +} + +void VPTProcess::config_yolo_param(ctools_init_params& param, int batch_size) { + + param.model_type_ = MODEL_YOLOV5; + param.weight_file_ = NULL; + param.net_file_ = NULL; + + 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.need_im_info_ = 0; + + if (param.engine_type_ == ENGINE_MCAFFE2) + { + /*param.weight_array_ = (unsigned char*)ga_vpt_init_net_caffe2; + param.weight_array_len_ = ga_vpt_init_net_len_caffe2; + param.net_array_ = (unsigned char*)ga_vpt_predict_net_caffe2; + param.net_array_len_ = ga_vpt_predict_net_len_caffe2;*/ + } + else if (param.engine_type_ == ENGINE_TENSORRT) + { + param.net_array_ = (uint8_t*)ga_vpt_det_yolo_640x640; + param.net_array_len_ = ga_vpt_det_yolo_640x640_len; //debug by zsh + + memset(param.tensorrt_param_str_, 0, sizeof(param.tensorrt_param_str_)); + + std::string g_data_mode = "FP32"; + bool g_is_create_calibrator = false; + + int g_is_onnx_model = 1; + sprintf(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); + + param.tensorrt_calibrator_array_len_ = ga_trt_yolo_vpt_calibrator_len; + param.tensorrt_calibrator_array_ = (unsigned char*)ga_trt_yolo_vpt_calibrator; + + } +} + +int VPTProcess::init(int gpuid, int max_batch_size) +{ + ctools_init_params param; + param.thres_ = 0.5; + param.log_level_ = 0; + param.device_type_ = DEVICE_GPU; + param.device_id_ = gpuid; + param.engine_type_ = ENGINE_TENSORRT; + param.trt_serialize_file_ = "./serialize_file/VPT"; + + m_max_batch_size = max_batch_size; + +#ifdef USE_YOLOV5 + config_yolo_param(param, m_max_batch_size); +#else + config_fpn_param(param, m_max_batch_size); +#endif + + int flag = ctools_init(&det_handle, ¶m); + if (SUCCESS != flag) { + LOG_ERROR("VPTProcess init failed!"); + } + + return flag; +} + +void VPTProcess::check_VPT_Result(VPT_Result & vResult) { + int index = 0; + for (int i = 0; i < vResult.objCount; i++) { + if ((vResult.obj[i].right - vResult.obj[i].left) > 10 && (vResult.obj[i].bottom - vResult.obj[i].top) > 10) + { + if (index == i) { + index++; + continue; + } + vResult.obj[index].left = vResult.obj[i].left; + vResult.obj[index].top = vResult.obj[i].top; + vResult.obj[index].right = vResult.obj[i].right; + vResult.obj[index].bottom = vResult.obj[i].bottom; + vResult.obj[index].center_x = vResult.obj[i].center_x; + vResult.obj[index].center_y = vResult.obj[i].center_y; + vResult.obj[index].index = vResult.obj[i].index; + vResult.obj[index].id = vResult.obj[i].id; + vResult.obj[index].num = vResult.obj[i].num; + vResult.obj[index].confidence = vResult.obj[i].confidence; + vResult.obj[index].snap_flag = vResult.obj[i].snap_flag; + index++; + } + } + + vResult.objCount = index; +} + +vector VPTProcess::process(vector vec_data) { + + vector vec_result; + + if(nullptr == det_handle){ + return vec_result; + } + + long long t1 = get_cur_time_ms(); + + int batchsize = vec_data.size(); + + vector vec_detectResult; + + int cycle_time = batchsize / m_max_batch_size; + cycle_time = (batchsize % m_max_batch_size) == 0 ? cycle_time : (cycle_time + 1) ; + + for (int i = 0; i < cycle_time; i++) { + int start_index = i * m_max_batch_size; + int end_index = start_index + m_max_batch_size; + if(end_index >= batchsize) { + end_index = batchsize; + } + + vector vec_img; + vector vec_task_id; + vector vec_ts; + for (int j = start_index; j < end_index; j++) { + vec_img.push_back(vec_data[j].img); + vec_task_id.push_back(vec_data[j].task_id); + vec_ts.push_back(vec_data[j].frameIndex); + } + + ctools_result *detresult; + int res_status = ctools_process(det_handle, vec_img.data(), vec_img.size(), &detresult); + + for (size_t b = 0; b < vec_img.size(); b++) { + ctools_result &cur_result = detresult[b]; + + DetectResultInfo result_info; + result_info.task_id = vec_task_id[b]; + result_info.ts = vec_ts[b]; + result_info.width = vec_img[b].w_; + result_info.height = vec_img[b].h_; + for (int c = 0; c < cur_result.obj_count_ && c < MAX_OBJ_COUNT; c++) + { + float x1 = cur_result.obj_results_[c].data_[2]; + float y1 = cur_result.obj_results_[c].data_[3]; + float x2 = cur_result.obj_results_[c].data_[4]; + float y2 = cur_result.obj_results_[c].data_[5]; + + float class_id = cur_result.obj_results_[c].data_[0]; + float score = cur_result.obj_results_[c].data_[1]; + + if (score >= THRESHOLD) + { + vector obj; + + obj.push_back(x1); + obj.push_back(y1); + obj.push_back(x2); + obj.push_back(y2); + obj.push_back(score); + obj.push_back(class_id); + // detectResult[real_index].push_back(obj); + result_info.det_result.push_back(obj); + } + } + + vec_detectResult.push_back(result_info); + } + } + + for (int i = 0; i < vec_detectResult.size(); i++) { + DetectResultInfo& det_result_info = vec_detectResult[i]; + TaskTracker& task_tracker = m_taskTrackerMap[det_result_info.task_id]; + + // TaskTracker& task_tracker = tools->taskTrackers[i]; + if (!task_tracker.tracker.GetState()) { + continue; + } + + int widht = det_result_info.width; + int height = det_result_info.height; + + VPTProcessResult oneResult; + oneResult.task_id = det_result_info.task_id; + if (task_tracker.lastFrameIndex > 0) { + // 非第一帧 + int update_times = det_result_info.ts - task_tracker.lastFrameIndex - 1; + if (update_times < 0) { + cout << "FrameIndex error !! lastFrameIndex= " << task_tracker.lastFrameIndex << " cur_frameindex = " << det_result_info.ts << endl; + } + + cout << "update_times " << update_times << endl; + + for (int j = 0; j < update_times; j++) { // 无检测框跟踪 + VPT_Result unresult; + unresult.objCount = task_tracker.tracker.update(widht, height, false, task_tracker.lastDetectResult, unresult.obj, task_tracker.lastDeleteObjectID); + check_VPT_Result(unresult); + oneResult.vecUnUsedResult.push_back(unresult); + } + } + oneResult.vptResult.objCount = task_tracker.tracker.update(widht, height, true, det_result_info.det_result, oneResult.vptResult.obj, oneResult.vecDeleteObj); + + check_VPT_Result(oneResult.vptResult); + + task_tracker.lastDetectResult = det_result_info.det_result; + task_tracker.lastDeleteObjectID = oneResult.vecDeleteObj; + task_tracker.lastFrameIndex = det_result_info.ts; + + oneResult.ts = det_result_info.ts; + + vec_result.push_back(oneResult); + } + + return vec_result; +} + +void VPTProcess::release() { + if (det_handle) { + ctools_release(&det_handle); + det_handle = NULL; + } + + m_taskTrackerMap.clear(); +} + +void VPTProcess::AddTaskTracker(const int taskID) +{ + TaskTracker t; + t.TaskID = taskID; + t.lastFrameIndex = 0; + t.tracker.setYOLOv5(true); // YOLOv5 要设为true, fpn 要设为false + + m_taskTrackerMap[taskID] = t; +} + +void VPTProcess::FinishTaskTracker(const int taskID) +{ + if (m_taskTrackerMap.find(taskID) == m_taskTrackerMap.end()) { + return; + } + + m_taskTrackerMap.erase(taskID); +} + +void VPTProcess::PauseTaskTracker(const int taskID) +{ + if (m_taskTrackerMap.find(taskID) == m_taskTrackerMap.end()) { + return; + } + + TaskTracker& t = m_taskTrackerMap[taskID]; + t.tracker.Pause(); +} + +void VPTProcess::RestartTaskTraker(const int taskID) +{ + if (m_taskTrackerMap.find(taskID) == m_taskTrackerMap.end()) { + return; + } + + TaskTracker& t = m_taskTrackerMap[taskID]; + t.tracker.ReSet(); +} + +void VPTProcess::DrawTracker(const int taskID, cv::Mat *img) +{ + if (m_taskTrackerMap.find(taskID) == m_taskTrackerMap.end()) { + return; + } + + TaskTracker& t = m_taskTrackerMap[taskID]; + t.tracker.addTracker(img); +} \ No newline at end of file diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/VPTProcess.h b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/VPTProcess.h new file mode 100644 index 0000000..c757b12 --- /dev/null +++ b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/VPTProcess.h @@ -0,0 +1,94 @@ +/******************************************************************************************* +* Version: VPT_x64_V2.0.0_20170705 +* CopyRight: �п�Ժ�Զ����о���ģʽʶ��ʵ����ͼ����Ƶ�� +* UpdateDate: 20170705 +* Content: �˳�������� +********************************************************************************************/ +#pragma once +#include "opencv2/highgui/highgui.hpp" +#include +#include "utools.h" +#include +#include +#include "common.h" + +#include "./sort/Sort.h" //tracker + +using namespace std; + + +struct TaskTracker +{ + int TaskID; + double ratioWidth; + double ratioHeight; + Sort tracker; + unsigned int lastFrameIndex; + + vector< vector > lastDetectResult; + vector lastDeleteObjectID; +}; + +struct DataInfo { + int task_id {-1}; + sy_img img; + unsigned long long frameIndex; +}; + +struct VPTProcessResult { + int task_id {-1}; + VPT_Result vptResult; + vector vecDeleteObj; + vector vecUnUsedResult; + unsigned long long ts; +}; + + +class VPTProcess +{ +public: + VPTProcess(/* args */); + ~VPTProcess(); + + /************************************************************************* + * PURPOSE: 初始化 + * PARAM: + [in] vparam 参数 + * RETURN: handle + * NOTES: + *************************************************************************/ + int init(int gpuid, int max_batch_size); + + /************************************************************************* + * PURPOSE: 人车物检测跟踪 + *************************************************************************/ + + vector process(vector vec_data); + + /************************************************************************* + * PURPOSE: 资源释放 + * PARAM: + [in] handle - 处理句柄 + * RETURN: NULL + * NOTES: + *************************************************************************/ + void release(); + + void AddTaskTracker(const int taskID); + void FinishTaskTracker(const int taskID); + void PauseTaskTracker(const int taskID); + void RestartTaskTraker(const int taskID); + void DrawTracker(const int taskID, cv::Mat *img); + +private: + void config_fpn_param(ctools_init_params& param, int batch_size); + void config_yolo_param(ctools_init_params& param, int batch_size); + void check_VPT_Result(VPT_Result & vResult); + +private: + int m_max_batch_size {20}; + + void* det_handle {nullptr}; + vector taskTrackers; + map m_taskTrackerMap; +}; -- libgit2 0.21.4