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 8500801..5c47c95 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 @@ -13,15 +13,8 @@ #include "../FFNvDecoder/logger.hpp" -#include "face_detect.h" #include "CropImg.h" -//#include -//#include -//#include -//#pragma comment(lib, "DbgHelp.Lib") -//#pragma comment(lib, "ShLwApi.lib") -//********************************************************// -//1.Ϊʲô��ʱ�򷵻ص�index=2��ȴδ�����ﳵ�ķ�������Ϊindex�ں�����Ϊ��2�����Ǵ�ʱ�Ŀ��ղ�������֮ǰ�Ŀ��գ����Կ���δ���£�����ͼ���С����Ϊ112*224 + // #define AUTHORIZATION //#define DQ_AUTHORIZATION @@ -46,83 +39,11 @@ #endif #endif -//#define LOG_INFO //�Ƿ������ٴ�ӡ -// #define SKIP_FRMAE 5 //��֡֡�� -#define EXTIME 500 #define AUTH_LICENSE "sy_va_sub_sdk_2023" static int TaskID = 0; -auto show_gpu_syimage_ = [](sy_img& cur_frame) { - - unsigned char* cpu_data = new unsigned char[3 * cur_frame.w_ * cur_frame.h_]; - cudaMemcpy(cpu_data, cur_frame.data_, 3 * cur_frame.w_ * cur_frame.h_ * sizeof(unsigned char), cudaMemcpyDeviceToHost); - cv::Mat img(cur_frame.h_, cur_frame.w_, CV_8UC3, cpu_data); - - cv::imshow("img", img); - cv::waitKey(0); - delete[] cpu_data; -}; - -auto show_gpu_image_ = [](DxGPUFrame& cur_frame) { - - unsigned char* cpu_data = new unsigned char[3 * cur_frame.width * cur_frame.height]; - cudaMemcpy(cpu_data, cur_frame.frame, 3 * cur_frame.width * cur_frame.height * sizeof(unsigned char), cudaMemcpyDeviceToHost); - cv::Mat img(cur_frame.height, cur_frame.width, CV_8UC3, cpu_data); - - cv::imshow("img2", img); - cv::waitKey(0); - delete[] cpu_data; -}; - -auto show_gpu_image_withrect_ = [](DxGPUFrame& cur_frame, sy_rect &rect) { - - unsigned char* cpu_data = new unsigned char[3 * cur_frame.width * cur_frame.height]; - cudaMemcpy(cpu_data, cur_frame.frame, 3 * cur_frame.width * cur_frame.height * sizeof(unsigned char), cudaMemcpyDeviceToHost); - cv::Mat img(cur_frame.height, cur_frame.width, CV_8UC3, cpu_data); - cv::rectangle(img, cv::Rect(rect.left_, rect.top_, rect.width_, rect.height_), cv::Scalar(25, 25, 250), 2, 8, 0); - cv::imshow("ori", img); - cv::waitKey(0); - delete[] cpu_data; -}; - -auto show_gpu_image_withfdinfo_ = [](DxGPUFrame& cur_frame, fd_info &face_info) { - - unsigned char* cpu_data = new unsigned char[3 * cur_frame.width * cur_frame.height]; - cudaMemcpy(cpu_data, cur_frame.frame, 3 * cur_frame.width * cur_frame.height * sizeof(unsigned char), cudaMemcpyDeviceToHost); - cv::Mat img(cur_frame.height, cur_frame.width, CV_8UC3, cpu_data); - - printf("%d %d\n", cur_frame.width , cur_frame.height); - for (int ii = 0; ii < 25; ii++) - { - printf("(%d %d) ", face_info.facial_fea_point[ii].x_, face_info.facial_fea_point[ii].y_); - cv::circle(img, cv::Point(face_info.facial_fea_point[ii].x_, face_info.facial_fea_point[ii].y_), 1, cv::Scalar(0, 255, 0)); - - } - //cv::rectangle(img, cv::Rect(face_info.face_position.left_, face_info.face_position.top_, face_info.face_position.width_, face_info.face_position.height_), cv::Scalar(25, 25, 250), 2, 8, 0); - - cv::Mat big_img; - cv::resize(img, big_img, cv::Size(400, 400)); - - - cv::imshow("ori33", img); - cv::imshow("ori44", big_img); - cv::waitKey(0); - delete[] cpu_data; -}; - -auto show_gpu_image_with2rect_ = [](DxGPUFrame& cur_frame, sy_rect &rect, sy_rect &rect2) { - - unsigned char* cpu_data = new unsigned char[3 * cur_frame.width * cur_frame.height]; - cudaMemcpy(cpu_data, cur_frame.frame, 3 * cur_frame.width * cur_frame.height * sizeof(unsigned char), cudaMemcpyDeviceToHost); - cv::Mat img(cur_frame.height, cur_frame.width, CV_8UC3, cpu_data); - cv::rectangle(img, cv::Rect(rect.left_, rect.top_, rect.width_, rect.height_), cv::Scalar(25, 25, 250), 1, 8, 0); - cv::rectangle(img, cv::Rect(rect2.left_, rect2.top_, rect2.width_, rect2.height_), cv::Scalar(25, 250, 25), 1, 8, 0); - cv::imshow("ori2", img); - cv::waitKey(0); - delete[] cpu_data; -}; static long long get_cur_time_ms(){ chrono::time_point tpMicro @@ -131,7 +52,6 @@ static long long get_cur_time_ms(){ return tpMicro.time_since_epoch().count(); } -std::function show_gpu_img_func = show_gpu_image_; void check_thread(void* handle); @@ -168,8 +88,7 @@ int CMutliSourceVideoProcess::FinishProcessThread() m_bProcessExit = true; ProcessThread.join(); //waiting thread finish - VPT_Release(VPT_Handle); - + m_vptProcess.release(); m_snaphot_helper.snapshot_helper_release(); return 1; @@ -248,19 +167,8 @@ int CMutliSourceVideoProcess::InitAlgorthim(mvpt_param vptParam, VIDEO_OBJECT_IN skip_frame_ = vptParam.skip_frame; } - VPTProcess_PARAM param{}; - //param.c = 3; - param.mode = DEVICE_GPU; - param.gpuid = vptParam.gpuid; - //param.threshold = 0.6; - param.threshold = 0.5; - param.engine = ENGINE_TENSORRT; - param.max_batch = section_batch_size; - param.serialize_file = "./serialize_file/VPT"; - param.auth_license = vptParam.auth_license; mgpuid = vptParam.gpuid; - VPT_Handle = NULL; - ret = VPT_Init(VPT_Handle, param); + ret = m_vptProcess.init(mgpuid, section_batch_size); if (0 != ret) return ret; @@ -364,7 +272,7 @@ void CMutliSourceVideoProcess::FinishTask(const int taskID) tasks[i].frameImage.release(); - FinishTaskTracker(VPT_Handle, taskID); + m_vptProcess.FinishTaskTracker(taskID); if (viewTaskID == taskID) viewTaskID = -1; @@ -382,7 +290,7 @@ void CMutliSourceVideoProcess::PauseTask(const int taskID) { if (tasks[i].taskState == PLAY) TaskinPlay--; tasks[i].taskState = PAUSE; - PauseTaskTracker(VPT_Handle, taskID); + m_vptProcess.PauseTaskTracker(taskID); tasks[i].taskTcuvid->PauseDecoder(); if (viewTaskID == taskID) viewTaskID = -1; printf("-----------------------pasue task: %d-----------------------\n", taskID); @@ -399,7 +307,7 @@ void CMutliSourceVideoProcess::RestartTask(const int taskID) { tasks[i].taskState = PLAY; TaskinPlay++; - RestartTaskTraker(VPT_Handle, taskID); + m_vptProcess.RestartTaskTraker(taskID); tasks[i].taskTcuvid->ResumeDecoder(); printf("-----------------------restart task: %d-----------------------\n", taskID); break; @@ -559,7 +467,7 @@ bool CMutliSourceVideoProcess::AddTask(task_param tparam) //debug by zsh TaskinPlay++; tasks.push_back(new_task); - AddTaskTracker(VPT_Handle, new_task.taskID, width, height); + m_vptProcess.AddTaskTracker(new_task.taskID, width, height); AddTaskSucFlag = 1; @@ -850,7 +758,7 @@ void CMutliSourceVideoProcess::algorthim_process() { tasks[i].taskState = DECODEERROR; curPlayTaskCount--; - FinishTaskTracker(VPT_Handle, tasks[i].taskID); + m_vptProcess.FinishTaskTracker(tasks[i].taskID); } } @@ -923,7 +831,7 @@ void CMutliSourceVideoProcess::algorthim_process() vector> tempDeleteObjectID; tempDeleteObjectID.resize(batchsize); - int flag = VPT_Process_GPU(GetVPT_Handle(), batch_img, startbatch, batchsize, vec_frameIndex, VPTResult, tempDeleteObjectID, unUsedResult); + int flag = m_vptProcess.process(batch_img, startbatch, batchsize, vec_frameIndex, VPTResult, tempDeleteObjectID, unUsedResult); process_times++ ; for (auto iter : tempDeleteObjectID) @@ -1076,7 +984,7 @@ void CMutliSourceVideoProcess::algorthim_process() //实时查看 绘制目标轨迹 回调函数返回 if (view) { - DrawTracker(VPT_Handle, *iter, &task.frameImage); + m_vptProcess.DrawTracker(*iter, &task.frameImage); if (task.taskRealTimeCallbackFunc != nullptr) task.taskRealTimeCallbackFunc(task.frameImage.data, task.frameImage.rows, task.frameImage.cols); } 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 079e904..cc27dcc 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 @@ -20,7 +20,6 @@ #include "snapshot_helper.h" #include -//#include "DxLogInterface.h" #ifdef _MSC_VER #ifdef _DEBUG @@ -60,9 +59,6 @@ using namespace std; #define MAXLENGTH 416 #define MINLENGTH 224 -//#define PROCESSHEIGHT 224 -//#define PROCESSWIDTH 416 -//#define DATASIZE PROCESSWIDTH * PROCESSHEIGHT * 3 #define THREAD_COUNT 30 #define SNAPSHOTFRAME 15 #define LOSTMAXFRAMECCOUNT 4 @@ -113,7 +109,7 @@ public: ~CMutliSourceVideoProcess(); int InitAlgorthim(mvpt_param vptParam, VIDEO_OBJECT_INFO_CALLBACK tObjInfoCallbackFunc, VIDEO_FINISH_CALLBACK tFinishCallbackFunc); - void* GetVPT_Handle(){ return VPT_Handle; }; + int get_task_progress(int taskid, double & progress); void OperatorTask(); bool HasNewTask() { @@ -179,9 +175,11 @@ public: /*��������Ӧ����public�� ������ sy_command m_face_det_config; //�Ƿ���������� snapshot_helper m_snaphot_helper; +private: bool m_bExit{false}; bool m_bProcessExit{false}; + + VPTProcess m_vptProcess; }; -static CMutliSourceVideoProcess mainProcess; #endif diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/ObjCls.h b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/ObjCls.h deleted file mode 100644 index 18a3b7b..0000000 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/ObjCls.h +++ /dev/null @@ -1,88 +0,0 @@ -#include "VPTProcess.h"// detector -#include "utools.h" -#include "proto.h" -#include - -#include "sort/Sort.h" //tracker -#include "MD5/mID.h" -//#define MULTICHANNEL 20 //֧�ֵ����·�� -#define THRESHOLD 0.5 - -//struct objDetector{ -// void* detector; -// //Sort tracker[MULTICHANNEL]; -// VPTProcess_PARAM param; -// int channels;//ֵΪopencv��CV_8UC1 ���� CV_8UC3 -//}; - -struct TaskTracker -{ - int TaskID; - double ratioWidth; - double ratioHeight; - Sort tracker; - unsigned int lastFrameIndex; -}; -vector taskTrackers; - - -class CMemoryRecord -{ - public: - unsigned char * ptr; - CMemoryRecord(int w,int h) - { - cudaMalloc((void **) & ptr, w*h*3*sizeof(unsigned char)); - } - virtual ~CMemoryRecord(){ - cudaFree(ptr); - ptr=NULL; - } - -}; -#if 0 -class CMemoryPool -{ - public: - int m_w,m_h; - std::queue filled_records; - std::queue unfilled_records; - CMemoryPool(int w,int h,int num) - { - m_w = w;m_h=h; - for(int i=0;iptr); - unfilled_records.pop(); - filled_records.push(ptr); - return record; - } - - - virtual ~CMemoryPool() - { - for(int i=0;i().swap( filled_records ); - - } - for(int i=0;i().swap( filled_records ); - } - } -}; -#endif 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 index d2362d7..9d93315 100644 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.cpp +++ b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.cpp @@ -1,18 +1,12 @@ #include "VPTProcess.h" -#include "MutliSourceVideoProcess.h" - -#ifndef _MSC_VER -#include -#endif #include #include -#include "ObjCls.h" -#include #include "ErrorInfo.h" -#include "fstream" +#include "../FFNvDecoder/logger.hpp" +#define THRESHOLD 0.5 #define USE_YOLOV5 @@ -32,24 +26,6 @@ -typedef struct objDetector { - - void* det_handle; - float threshold; - int engine_type; - // VPT_FPNPluginFactory tensorrt_plugin_factory; - - int licence_status = -1; - int thrd_status = -1; - vector taskTrackers; - objDetector() - { - det_handle = NULL; - threshold = 0.6; - } -}objDetector; - - static long long get_cur_time_ms(){ chrono::time_point tpMicro = chrono::time_point_cast(chrono::system_clock::now()); @@ -57,7 +33,15 @@ static long long get_cur_time_ms(){ return tpMicro.time_since_epoch().count(); } -void config_fpn_param(ctools_init_params& param, int batch_size) { +VPTProcess::VPTProcess(/* args */) +{ +} + +VPTProcess::~VPTProcess() +{ +} + +void VPTProcess::config_fpn_param(ctools_init_params& param, int batch_size) { // param.model_type_ = MODEL_FPN; // param.weight_file_ = NULL; @@ -103,11 +87,11 @@ void config_fpn_param(ctools_init_params& param, int batch_size) { // 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_ = &(tools->tensorrt_plugin_factory); + // param.tensorrt_plugin_factory_ptr_ = &(tensorrt_plugin_factory); // } } -void config_yolo_param(ctools_init_params& param, int batch_size) { +void VPTProcess::config_yolo_param(ctools_init_params& param, int batch_size) { param.model_type_ = MODEL_YOLOV5; // debug by zsh @@ -154,157 +138,37 @@ void config_yolo_param(ctools_init_params& param, int batch_size) { param.tensorrt_calibrator_array_len_ = ga_trt_yolo_vpt_calibrator_len;// param.tensorrt_calibrator_array_ = (unsigned char*)ga_trt_yolo_vpt_calibrator;// debug by zsh - // param.tensorrt_plugin_factory_ptr_ = &(tools->tensorrt_plugin_factory); + // param.tensorrt_plugin_factory_ptr_ = &(tensorrt_plugin_factory); } } -int VPT_Init(void *&handle, VPTProcess_PARAM vparam) +int VPTProcess::init(int gpuid, int max_batch_size) { - objDetector *tools = new objDetector; - ctools_init_params param; - param.thres_ = vparam.threshold; + param.thres_ = 0.5; param.log_level_ = 0; - param.device_type_ = vparam.mode; - param.device_id_ = vparam.gpuid; - param.engine_type_ = vparam.engine; + param.device_type_ = DEVICE_GPU; + param.device_id_ = gpuid; + param.engine_type_ = ENGINE_TENSORRT; + param.trt_serialize_file_ = "./serialize_file/VPT"; - param.trt_serialize_file_ = vparam.serialize_file; + m_max_batch_size = max_batch_size; #ifdef USE_YOLOV5 - config_yolo_param(param, vparam.max_batch); + config_yolo_param(param, m_max_batch_size); #else - config_fpn_param(param, vparam.max_batch); + config_fpn_param(param, m_max_batch_size); #endif - tools->threshold = vparam.threshold; - tools->engine_type = vparam.engine; // debug by zsh - int flag = ctools_init(&(tools->det_handle), ¶m); + int flag = ctools_init(&det_handle, ¶m); if (SUCCESS != flag) { - if (tools) { - delete tools; - tools = NULL; - } - } else { - handle = tools; + LOG_ERROR("VPTProcess init failed!"); } return flag; } -// int VPT_Init(void *&handle, VPTProcess_PARAM vparam) -// { -// objDetector *tools = new objDetector; - -// ctools_init_params param; -// param.thres_ = vparam.threshold; -// param.log_level_ = 0; -// param.device_type_ = vparam.mode; -// param.device_id_ = vparam.gpuid; -// param.engine_type_ = vparam.engine; - -// param.trt_serialize_file_ = vparam.serialize_file; - - -// param.model_type_ = MODEL_FPN; -// // param.model_type_ = MODEL_YOLOV5; // debug by zsh - -// 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 - -// // 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.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; - -// // param.net_array_ = (uint8_t*)ga_vpt_det_yolo_640x640; -// // param.net_array_len_ = ga_vpt_det_yolo_640x640_len; //debug by zsh - -// param.trt_serialize_file_ = vparam.serialize_file; - -// memset(param.tensorrt_param_str_, 0, sizeof(param.tensorrt_param_str_)); - -// int batch_size = vparam.max_batch; -// 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); - -// 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_calibrator_array_len_ = ga_trt_yolo_vpt_calibrator_len;// -// // param.tensorrt_calibrator_array_ = (unsigned char*)ga_trt_yolo_vpt_calibrator;// debug by zsh - - -// param.tensorrt_plugin_factory_ptr_ = &(tools->tensorrt_plugin_factory); -// } - -// tools->threshold = vparam.threshold; -// tools->engine_type = vparam.engine; // debug by zsh -// int flag = ctools_init(&(tools->det_handle), ¶m); - -// if (SUCCESS != flag) -// { -// if (tools) -// { -// delete tools; -// tools = NULL; -// } -// } -// else -// { -// handle = tools; -// } -// return flag; -// } - -void check_VPT_Result(VPT_Result & vResult) { +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) @@ -331,34 +195,19 @@ void check_VPT_Result(VPT_Result & vResult) { vResult.objCount = index; } -int VPT_Process_GPU(void * handle, sy_img * batch_img, int startBatch, int batchsize, vector vec_frameIndex, vector& result, vector>& deleteObjectID, vector>& unUsedResult) +int VPTProcess::process(sy_img * batch_img, int startBatch, int batchsize, vector vec_frameIndex, vector& result, vector>& deleteObjectID, vector>& unUsedResult) { - objDetector *tools = (objDetector*)handle; + if(nullptr == det_handle){ + return FAILED; + } bool isUseDet = true; int channels = 3; - // if(batchsize == 5) { - // for (int i = 0; i < batchsize; i++) { - // // test - // printf("k值: %d \n", i); - // int height = batch_img[i].h_; - // int width = batch_img[i].w_; - // int rgb_size = 3 * width * height; - // unsigned char *cpu_data = new unsigned char[rgb_size]; - // cudaError_t cudaStatus = cudaMemcpy(cpu_data, batch_img[i].data_, rgb_size * sizeof(unsigned char), cudaMemcpyDeviceToHost); - // cv::Mat img(height, width, CV_8UC3, cpu_data); - // string filename = "test" + to_string(i) + ".jpg"; - // cv::imwrite(filename.c_str(), img); - // delete[] cpu_data; - // cpu_data = nullptr; - // } - // } - long long t1 = get_cur_time_ms(); ctools_result *detresult; - int res_status = ctools_process(tools->det_handle, batch_img, batchsize, &detresult); + int res_status = ctools_process(det_handle, batch_img, batchsize, &detresult); #ifdef LOG_INFO2 long long t2 = get_cur_time_ms(); @@ -400,9 +249,9 @@ int VPT_Process_GPU(void * handle, sy_img * batch_img, int startBatch, int batch int resIndex = startBatch; int detectIndex = 0; - for (int i = startBatch; i < tools->taskTrackers.size(); i++) + for (int i = startBatch; i < taskTrackers.size(); i++) { - TaskTracker task_tracker = tools->taskTrackers[i]; + TaskTracker task_tracker = taskTrackers[i]; if (!task_tracker.tracker.GetState()) { continue; } @@ -463,225 +312,70 @@ int VPT_Process_GPU(void * handle, sy_img * batch_img, int startBatch, int batch return SUCCESS; } - - -void VPT_Release(void * handle) -{ - objDetector *tools = (objDetector*)handle; - - if (tools) - { - if (tools->det_handle) - { - ctools_release(&tools->det_handle); - tools->det_handle = NULL; - } - - vector().swap(tools->taskTrackers); - - delete tools; - tools = NULL; +void VPTProcess::release() { + if (det_handle) { + ctools_release(&det_handle); + det_handle = NULL; } + + vector().swap(taskTrackers); } -void AddTaskTracker(void * handle, const int taskID, const double rWidth, const double rHeight) +void VPTProcess::AddTaskTracker(const int taskID, const double rWidth, const double rHeight) { - objDetector *tools = (objDetector*)handle; TaskTracker t; t.TaskID = taskID; t.ratioWidth = rWidth; t.ratioHeight = rHeight; t.lastFrameIndex = 0; t.tracker.setYOLOv5(true); // YOLOv5 要设为true, fpn 要设为false - tools->taskTrackers.push_back(t); + taskTrackers.push_back(t); } -void FinishTaskTracker(void * handle, const int taskID) +void VPTProcess::FinishTaskTracker(const int taskID) { - objDetector *tools = (objDetector*)handle; - for (int i = 0; i < tools->taskTrackers.size(); i++) + for (int i = 0; i < taskTrackers.size(); i++) { - if (tools->taskTrackers[i].TaskID == taskID) + if (taskTrackers[i].TaskID == taskID) { - tools->taskTrackers.erase(tools->taskTrackers.begin() + i); + taskTrackers.erase(taskTrackers.begin() + i); break; } } } -void PauseTaskTracker(void * handle, const int taskID) +void VPTProcess::PauseTaskTracker(const int taskID) { - objDetector *tools = (objDetector*)handle; - for (int i = 0; i < tools->taskTrackers.size(); i++) + for (int i = 0; i < taskTrackers.size(); i++) { - if (tools->taskTrackers[i].TaskID == taskID) + if (taskTrackers[i].TaskID == taskID) { - tools->taskTrackers[i].tracker.Pause(); + taskTrackers[i].tracker.Pause(); break; } } } -void RestartTaskTraker(void * handle, const int taskID) +void VPTProcess::RestartTaskTraker(const int taskID) { - objDetector *tools = (objDetector*)handle; - for (int i = 0; i < tools->taskTrackers.size(); i++) + for (int i = 0; i < taskTrackers.size(); i++) { - if (tools->taskTrackers[i].TaskID == taskID) + if (taskTrackers[i].TaskID == taskID) { - tools->taskTrackers[i].tracker.ReSet(); + taskTrackers[i].tracker.ReSet(); break; } } } -void DrawTracker(void * handle, const int taskID, cv::Mat *img) +void VPTProcess::DrawTracker(const int taskID, cv::Mat *img) { - objDetector *tools = (objDetector*)handle; - for (int i = 0; i < tools->taskTrackers.size(); i++) + for (int i = 0; i < taskTrackers.size(); i++) { - if (tools->taskTrackers[i].TaskID == taskID) + if (taskTrackers[i].TaskID == taskID) { - tools->taskTrackers[i].tracker.addTracker(img); + taskTrackers[i].tracker.addTracker(img); break; } } -} - - - -int VPT_Process(void * handle, unsigned char ** bgr, int batchsize, VPT_Result *result) -{ - // isUseDet = false; - // } - - // } - // resIndex++; - // if (resIndex == batchsize) break; - //} - //vector >>().swap(detectResult); - //vector>().swap(ssdResult); - ///*detectResult.clear(); - //ssdResult.clear();*/ - - return SUCCESS; -} - -//#include - - - - -void permute(float * image, int testWidth, int testHeight) -{ - //cv::Mat host_image; - float * host_image; - //host_image.create(testHeight, testWidth, CV_32FC3); - - host_image = (float *)malloc(testHeight*testWidth * 3 * sizeof(float));; - - float *Host_img = new float[3 * testWidth * testHeight]{};//?????ڴ? - - float* image_data_original = image; - //NPP_CHECK_CUDA(cudaMemcpy(Host_img, image_data_original, testWidth*testHeight * 3 * sizeof(float), cudaMemcpyDeviceToHost));//?????Կ???????ͼ???????? - cudaMemcpy(Host_img, image_data_original, testWidth*testHeight * 3 * sizeof(float), cudaMemcpyDeviceToHost);//?????Կ???????ͼ???????? - - for (int j = 0; j < testHeight; j++) - { - float *pts = host_image + j * testWidth * 3; - for (int i = 0; i < testWidth; i++) - { - //pts[3 * i] = cv::saturate_cast(Host_img[3 * (j*host_image.cols + i) + 0]); //b - //pts[3 * i + 1] = cv::saturate_cast(Host_img[3 * (j*host_image.cols + i) + 1]); //g - //pts[3 * i + 2] = cv::saturate_cast(Host_img[3 * (j*host_image.cols + i) + 2]); //r - - pts[3 * i] = (Host_img[j * testWidth + i]); //b - pts[3 * i + 1] = (Host_img[testWidth * testHeight + j * testWidth + i]); //g - pts[3 * i + 2] = (Host_img[2 * testWidth * testHeight + j * testWidth + i]); //r - } - } - - cudaMemcpy(image_data_original, host_image, testWidth*testHeight * 3 * sizeof(float), cudaMemcpyHostToDevice); - free(host_image); - //cv::Mat showImg; - //cv::resize(host_image, showImg, cv::Size(640, 480)); - //cv::imshow("image", showImg); - //cv::waitKey(0); -} - -cv::Mat GpuMat2OpencvMat(unsigned char* image, int width, int height) -{ - int testWidth = width; - int testHeight = height; - cv::Mat host_image; - host_image.create(testHeight, testWidth, CV_8UC3); - unsigned char *Host_img = new unsigned char[3 * testWidth * testHeight]{};//?????ڴ? - unsigned char* image_data_original = image; - - cudaError_t code = cudaMemcpy(Host_img, image_data_original, testWidth*testHeight * 3 * sizeof(unsigned char), cudaMemcpyDeviceToHost);//?????Կ???????ͼ???????? - if (code != 0) - { - printf("==========================================================error"); - } - std::ofstream outfile("decode.bin", ios::out | ios::binary); - outfile.write((char*)Host_img, int(sizeof(char) * 1080 * 1920 * 3)); - outfile.close(); - - cudaMemcpy(host_image.data, image_data_original, 1920 * testHeight * 3 * sizeof(unsigned char), cudaMemcpyDeviceToHost); - cv::imwrite("input3.jpg", host_image); - return host_image; -} - - -int VPT_ProcessImage(void * handle, unsigned char ** bgr, int batchsize, VPT_Result * result) -{ - //objDetector* tools = (objDetector*)handle; - //if (bgr == NULL) //ͼ?????ݴ??? - // return IMG_DATA_ERROR; - - //vector imgs; - ////int datasize = tools->param.w * tools->param.h * tools->param.c; - //for (int i = 0; i < batchsize; i++) - //{ - // Mat img(tools->param.h, tools->param.w, tools->param.c, bgr[i]); - // imgs.push_back(img); - //} - - //vector> ssdResult; - ////clock_t begin = clock(); - //ssdResult = SSD_Detect(tools->detector, batchsize, imgs); // - //vector >> detectResult(batchsize); //ת??Ϊ????????Ҫ?????? - //for (int i = 0; i < ssdResult.size(); ++i) - //{ - // const vector& d = ssdResult[i];// Detection format: [image_id, label, score, xmin, ymin, xmax, ymax) - // const float score = d[2]; - // int imgid = d[0]; - // if (score >= THRESHOLD) //????Ϊleft top right bottom score id - // { - // vector obj; - // obj.push_back(d[3] * imgs[d[0]].cols); - // obj.push_back(d[4] * imgs[d[0]].rows); - // obj.push_back(d[5] * imgs[d[0]].cols); - // obj.push_back(d[6] * imgs[d[0]].rows); - // obj.push_back(d[2]); - // obj.push_back(d[1]); - // detectResult[imgid].push_back(obj); - // } - //} - //for (int i = 0; i < batchsize; i++) - //{ - // for (int j = 0; j < detectResult[i].size(); j++)//left top right bottom score id - // { - // result[i].obj[j].id = 0; - // result[i].obj[j].left = detectResult[i][j][0]; - // result[i].obj[j].top = detectResult[i][j][1]; - // result[i].obj[j].right = detectResult[i][j][2]; - // result[i].obj[j].bottom = detectResult[i][j][3]; - // result[i].obj[j].confidence = detectResult[i][j][4]; - // result[i].obj[j].index = detectResult[i][j][5] - 1; - // } - // - //} - - return SUCCESS; } \ 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 index 764b1ae..dff315b 100644 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.h +++ b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/VPTProcess.h @@ -11,95 +11,77 @@ #include #include "common.h" +#include "./sort/Sort.h" //tracker + using namespace std; -#define VTTYPECOUNT 10 //֧�ֵ��������� -typedef struct VPTProcess_PARAM +struct TaskTracker +{ + int TaskID; + double ratioWidth; + double ratioHeight; + Sort tracker; + unsigned int lastFrameIndex; +}; + + +class VPTProcess { - int mode; //����ģʽ(DEVICE_GPU / DEVICE_CPU) - int gpuid; //ָ���Կ�id - float threshold; - int max_batch; - int engine; //ָ����������(ENGINE_MCAFFE2 / ENGINE_TENSORRT) - char* preprocess_param; - char* serialize_file; - char* auth_license; - VPTProcess_PARAM() :mode(DEVICE_GPU), gpuid(0), threshold(0.6), max_batch(20) {}; -}VPTProcess_PARAM; +public: + VPTProcess(/* args */); + ~VPTProcess(); + /************************************************************************* + * PURPOSE: 初始化 + * PARAM: + [in] vparam 参数 + * RETURN: handle + * NOTES: + *************************************************************************/ + int init(int gpuid, int max_batch_size); -/************************************************************************* -* FUNCTION: VPT_Init -* PURPOSE: ��ʼ�� -* PARAM: -[in] vparam ���� -* RETURN: handle -* NOTES: -*************************************************************************/ -int VPT_Init(void *&handle, VPTProcess_PARAM vparam); + /************************************************************************* + * PURPOSE: 人车物检测跟踪 + * PARAM: + [in] handle - 处理句柄 + [in] rgb - 图片数据(3通道BGR数据 cv::Mat格式) + [in] width - 图片宽度 + [in] height - 图片高度 + [in] result - 搜索结果,在外部申请足够内存 + [in] deleteObjectID - 删除的轨迹ID号 + [out] traffic - 交通流量结果结构体 内存在外部申请,大小与初始化时流量监测线个数相同,设置为NULL时不返回 + * RETURN: -1:图像错误; 其他:检测到的个数 + * NOTES: + *************************************************************************/ + //startBatch 多batch为了减少显存,串行处理 - add by lm + int process(sy_img * batch_img, int startBatch, int batchsize, vector vec_frameIndex, vector& result, vector>& deleteObjectID, vector>& unUsedResult); -/************************************************************************* -* FUNCTION: VPT_Process -* PURPOSE: �˳�������� -* PARAM: -[in] handle - ������� -[in] rgb - ͼƬ���ݣ�3ͨ��BGR���� cv::Mat��ʽ�� -[in] width - ͼƬ���� -[in] height - ͼƬ�߶� -[in] result - ������������ⲿ�����㹻�ڴ� -[out] traffic - ��ͨ��������ṹ�� �ڴ����ⲿ���룬��С���ʼ��ʱ��������߸�����ͬ������ΪNULLʱ������ -* RETURN: -1��ͼ����� ��������⵽�ĸ��� -* NOTES: -*************************************************************************/ -int VPT_Process(void * handle, unsigned char ** bgr, int batchsize, VPT_Result * result); + /************************************************************************* + * PURPOSE: 资源释放 + * PARAM: + [in] handle - 处理句柄 + * RETURN: NULL + * NOTES: + *************************************************************************/ + void release(); -/************************************************************************* -* FUNCTION: VPT_Process -* PURPOSE: �˳�������� -* PARAM: -[in] handle - ������� -[in] rgb - ͼƬ���ݣ�3ͨ��BGR���� cv::Mat��ʽ�� -[in] width - ͼƬ���� -[in] height - ͼƬ�߶� -[in] result - ������������ⲿ�����㹻�ڴ� -[in] deleteObjectID - ɾ���Ĺ켣ID�� -[out] traffic - ��ͨ��������ṹ�� �ڴ����ⲿ���룬��С���ʼ��ʱ��������߸�����ͬ������ΪNULLʱ������ -* RETURN: -1��ͼ����� ��������⵽�ĸ��� -* NOTES: -*************************************************************************/ -//startBatch ��batchΪ�˼����Դ棬���д��� - add by lm -//int VPT_Process_GPU(void * handle, float * bgr, int width, int height, int startBatch, int batchsize, VPT_Result * result, vector>& deleteObjectID); -int VPT_Process_GPU(void * handle, sy_img * batch_img, int startBatch, int batchsize, vector vec_frameIndex, vector& result, vector>& deleteObjectID, vector>& unUsedResult); -/************************************************************************* -* FUNCTION: VPT_ProcessImage -* PURPOSE: �˳�����-ͼƬ -* PARAM: -[in] handle - ������� -[in] rgb - ͼƬ���ݣ�3ͨ��BGR���� cv::Mat��ʽ�� -[in] width - ͼƬ���� -[in] height - ͼƬ�߶� -[in] result - ������������ⲿ�����㹻�ڴ� -* RETURN: -1��ͼ����� ��������⵽�ĸ��� -* NOTES: -*************************************************************************/ -int VPT_ProcessImage(void * handle, unsigned char ** bgr, int batchsize, VPT_Result * result); -/************************************************************************* -* FUNCTION: VPT_Release -* PURPOSE: ��Դ�ͷ� -* PARAM: -[in] handle - ������� -* RETURN: NULL -* NOTES: -*************************************************************************/ -void VPT_Release(void * handle); + void AddTaskTracker(const int taskID, const double rWidth, const double rHeight); + 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 AddTaskTracker(void * handle, const int taskID, const double rWidth, const double rHeight); -void FinishTaskTracker(void * handle, const int taskID); -void PauseTaskTracker(void * handle, const int taskID); -void RestartTaskTraker(void * handle, const int taskID); -void DrawTracker(void * handle, const int taskID, cv::Mat *img); + void* det_handle {nullptr}; + vector taskTrackers; +}; diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/sort/Sort.h b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/sort/Sort.h index b2f5f7f..8d7d5ec 100644 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/sort/Sort.h +++ b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/sort/Sort.h @@ -9,8 +9,8 @@ #include #include #include -#include "../VPTProcess.h" +#include "../common.h" using namespace std;