From 0b2e2301a838d5b5436f0cc4baccc3998d8e0d45 Mon Sep 17 00:00:00 2001 From: cmhu <2657262686@qq.com> Date: Wed, 1 Nov 2023 18:33:32 +0800 Subject: [PATCH] 代码优化 --- vehicle_structure_platform.git0708-3080-trt-face/src/FFNvDecoder/FFNvDecoder.cpp | 8 ++++++++ vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.cpp | 1 - vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanCarParsing.cpp | 188 ++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------------------------ vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanCarParsing.h | 16 +++++++++++++--- vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/snapshot_helper.cpp | 22 +++++++++++----------- vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/snapshot_helper.h | 15 +++------------ vehicle_structure_platform.git0708-3080-trt-face/src/test/main.cpp | 2 +- 7 files changed, 62 insertions(+), 190 deletions(-) diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/FFNvDecoder/FFNvDecoder.cpp b/vehicle_structure_platform.git0708-3080-trt-face/src/FFNvDecoder/FFNvDecoder.cpp index c70cfd2..e365cae 100644 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/FFNvDecoder/FFNvDecoder.cpp +++ b/vehicle_structure_platform.git0708-3080-trt-face/src/FFNvDecoder/FFNvDecoder.cpp @@ -53,6 +53,8 @@ FFNvDecoder::FFNvDecoder() FFNvDecoder::~FFNvDecoder() { m_dec_keyframe = false; + + LOG_INFO("~FFNvDecoder"); } bool FFNvDecoder::init(FFDecConfig& cfg) @@ -309,12 +311,18 @@ void FFNvDecoder::decode_thread() void FFNvDecoder::decode_finished(){ if (avctx) { + if (avctx->hw_device_ctx) { + av_buffer_unref(&avctx->hw_device_ctx); + avctx->hw_device_ctx = nullptr; + } avcodec_free_context(&avctx); + avctx = nullptr; } if (fmt_ctx) { avformat_close_input(&fmt_ctx); + fmt_ctx = nullptr; } m_bFinished = true; 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 20e1ab8..9135c83 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 @@ -412,7 +412,6 @@ void CMutliSourceVideoProcess::FinishTask(const int taskID) tasks[i].folderName = nullptr; } - if (tasks[i].folderNameLittle) { delete tasks[i].folderNameLittle; diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanCarParsing.cpp b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanCarParsing.cpp index 53643ce..1012583 100644 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanCarParsing.cpp +++ b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanCarParsing.cpp @@ -45,195 +45,59 @@ const int FIR_INDEX_SIZE = 14; const int SEC_INDEX_SIZE[FIR_INDEX_SIZE] = {4,2,2,2,6,9,4,2,3,3,4,3,2,2 }; -int HumanCarParsing_Init(void *&handle, int gpuid, char* auth_license) + +HumanCarParsing::HumanCarParsing(){ + +} +HumanCarParsing::~HumanCarParsing(){ + LOG_INFO("~HumanCarParsing"); +} + +int HumanCarParsing::init(int gpuid, char* auth_license) { - hcp_param param; - param.mode = DEVICE_GPU; - param.gpuid = gpuid; + hcp_param param; + param.mode = DEVICE_GPU; + param.gpuid = gpuid; param.engine = ENGINE_TENSORRT; - param.max_batch = 20; param.serialize_file = "./serialize_file/HCP"; - param.auth_license = auth_license; + param.max_batch = 20; if (hcp_init(&handle, param) != 0) { - cout << "Init HCP Failed!" << endl; + LOG_ERROR("Init HCP Failed!"); return FAILED; } - + + LOG_INFO("HumanCarParsing init success! gpu_id: {}", gpuid); return SUCCESS; } -#include -int HumanCarParsing_Process(void * handle, sy_img * batch_img, int batch_size, hcp_analysis_result *&result) +int HumanCarParsing::process(sy_img * batch_img, int batch_size, hcp_analysis_result *&result) { - // vector vec_batch_img(batch_size); - // for (int i = 0; i < batch_size; i++) - // { - // if (batch_img[i].data_ == NULL) { - // cout << i << " data null" << endl; - // return -1; - // } - // int data_size = batch_img[i].c_ * batch_img[i].h_ * batch_img[i].w_ ; - // cout << "data size:" << data_size << endl; - // unsigned char *snapshotDataHost = new unsigned char[data_size] {}; - // cudaMemcpy(snapshotDataHost, batch_img[i].data_, data_size * sizeof(unsigned char), cudaMemcpyDeviceToHost); - - // sy_img tmp_img = batch_img[i]; - // tmp_img.data_ = snapshotDataHost; - - // vec_batch_img.push_back(tmp_img); - // } - - sy_img cpu_batch_img[batch_size]; - for (int i = 0; i < batch_size; i++) - { - if (batch_img[i].data_ == NULL) - { - cout << i << " data null" << endl; - LOG_ERROR("") - return FAILED; - } - cpu_batch_img[i].data_ = (unsigned char *)malloc(batch_img[i].w_ *batch_img[i].h_ * batch_img[i].c_ * sizeof(unsigned char)); - cudaMemcpy(cpu_batch_img[i].data_, batch_img[i].data_, batch_img[i].w_ *batch_img[i].h_ * batch_img[i].c_ * sizeof(unsigned char), cudaMemcpyDeviceToHost); - cpu_batch_img[i].w_ = batch_img[i].w_; - cpu_batch_img[i].h_ = batch_img[i].h_; - cpu_batch_img[i].c_ = batch_img[i].c_; - } - - //cout << "begin hcp ProcessBatch" << endl; - hcp_batch(handle, cpu_batch_img, batch_size, result); + LOG_INFO("batch_size: {}", batch_size); + hcp_batch(handle, batch_img, batch_size, result); for (int b = 0; b < batch_size; b++) { hcp_analysis_result &cur_result = result[b]; - for (int i = 0; i < FIR_INDEX_SIZE; i++) { if (cur_result.res_objs[i].res_index >= SEC_INDEX_SIZE[i]) { cur_result.res_objs[i].res_index = 0; - //printf("HumanCarParsing_Process ERROR!!!!!!!!!!!! %d %d %d\n", i, cur_result.res_objs[i].res_index, SEC_INDEX_SIZE[i]); - /*int data_size = batch_img[i].w_ * batch_img[i].h_ * batch_img[i].c_; - unsigned char *snapshotDataHost = new unsigned char[data_size] {}; - cudaMemcpy(snapshotDataHost, batch_img[i].data_, data_size * sizeof(unsigned char), cudaMemcpyDeviceToHost); - cv::Mat newVideoImg(batch_img[i].h_, batch_img[i].w_, CV_8UC3, snapshotDataHost); - cv::imwrite("hcp.jpg", newVideoImg); - delete[] snapshotDataHost;*/ - //printf("HumanCarParsing_Process ERROR!!!!!!!!!!!! %d %d %d\n", i, cur_result.res_objs[i].res_index, SEC_INDEX_SIZE[i]); } } - - free(cpu_batch_img[b].data_) ; } - //cout << "end hcp ProcessBatch" << endl; - // for (int b = 0; b < batch_size; b++) - // { - // ctools_result &cur_result = result[b]; - // int big_class_count = cur_result.obj_count_; - // ctools_obj_result &index_score = cur_result.obj_results_[big_class_count]; - - - // //for (int i = 0; i < cur_result.obj_count_ + 1; i++) - // // { - // // ctools_obj_result &index_score_1 = cur_result.obj_results_[i]; - // // - // // for (int j = 0; j < index_score_1.data_count_; j++) - // // printf("%.2f ", index_score_1.data_[j]); - // // printf("\n"); - // // } - - - // if (index_score.data_count_ != FIR_INDEX_SIZE * 2) - // { - // printf("wrong result.\n"); - // break; - // } - - // for (int i = 0; i < FIR_INDEX_SIZE; i++) - // { - // //int resIndex = result[j][i].index; - // int resIndex = index_score.data_[i * 2]; - //cout << "resIndex" << endl; - // //������ - // switch (i) - // { - // case 0: - // cout << up[resIndex] << " "; - // break; - // case 1: - // cout << up_color[resIndex] << " "; - // break; - // case 2: - // cout << down[resIndex] << " "; - // break; - // case 3: - // cout << down_color[resIndex] << " "; - // break; - // case 4: - // cout << bao[resIndex] << " "; - // break; - // case 5: - // cout << bag_color[resIndex] << " "; - // break; - // case 6: - // cout << head[resIndex] << " "; - // break; - // case 7: - // cout << clothing_text[resIndex] << " "; - // break; - // case 8: - // cout << sex[resIndex] << " "; - // break; - // case 9: - // cout << figure[resIndex] << " "; - // break; - // case 10: - // cout << nationality[resIndex] << " "; - // break; - // case 11: - // cout << age[resIndex] << " "; - // break; - // case 12: - // cout << eye[resIndex] << " "; - // break; - // case 13: - // cout << mouth[resIndex] << " "; - // break; - // case 14: - // cout << weibo[resIndex] << " "; - // break; - // case 15: - // cout << carColor[resIndex] << " "; - // break; - // case 16: - // cout << orient[resIndex] << " "; - // break; - // case 17: - // cout << drivenum[resIndex] << " "; - // break; - // case 18: - // cout << dasan[resIndex] << " "; - // break; - // case 19: - // cout << take[resIndex] << " "; - // break; - // default: - // break; - // } - - // } - // cout << endl; - - // //cudaFree(images[j].data); - // } - return SUCCESS; } -int HumanCarParsing_Release(void *& handle) +int HumanCarParsing::release() { - if(handle) - hcp_release(&handle); + if(handle) { + hcp_release(&handle); + handle = nullptr; + } + + LOG_INFO("HumanCarParsing Release."); return SUCCESS; } diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanCarParsing.h b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanCarParsing.h index b6b8415..e65879b 100644 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanCarParsing.h +++ b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanCarParsing.h @@ -6,7 +6,17 @@ using namespace std; +class HumanCarParsing { +public: + HumanCarParsing(); + ~HumanCarParsing(); + + int init(int gpuid, char* auth_license); + int process(sy_img * batch_img,int batchsize, hcp_analysis_result *&result); + int release(); + +private: + void* handle {nullptr}; +}; + -int HumanCarParsing_Init(void *&handle, int gpuid, char* auth_license); -int HumanCarParsing_Process(void * handle, sy_img * batch_img,int batchsize, hcp_analysis_result *&result); -int HumanCarParsing_Release(void *& handle); diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/snapshot_helper.cpp b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/snapshot_helper.cpp index 25cd04e..f2da446 100644 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/snapshot_helper.cpp +++ b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/snapshot_helper.cpp @@ -2,7 +2,6 @@ #include "opencv2/highgui/highgui.hpp" #include "snapshot_helper.h" #include "HumanParsing.h" -#include "HumanCarParsing.h" #include "VehiclePlate.h" #include "VehicleRecognition.h" #include "VehicleColor.h" @@ -119,7 +118,7 @@ void snapshot_helper::snapshot_helper_init(int gpuid, double gpu_total_memory, c if (hcp_analysis_config == SY_CONFIG_OPEN || hcf_recg_config == SY_CONFIG_OPEN) { - HumanCarParsing_Init(hcp_handle, gpuid, auth_license); + m_human_car_parsing.init(gpuid, auth_license); } if (hf_recg_config == SY_CONFIG_OPEN || hcf_recg_config == SY_CONFIG_OPEN) @@ -175,7 +174,7 @@ void snapshot_helper::snapshot_helper_release() { m_bExit = true; HumanParsing_Release(hp_handle); - HumanCarParsing_Release(hcp_handle); + m_human_car_parsing.release(); VehicleColor_Release(vc_handle); VehiclePlateDetectRecog_Release(vp_handle); VehicleRecognition_Release(vr_handle); @@ -463,6 +462,8 @@ void snapshot_helper::object_attri_analysis() //二次属性分析接口 void snapshot_helper::finish_task_ss_analysis(int task_id, sy_command hp_analysis_config, sy_command hcp_analysis_config, sy_command vehicle_analysis_config, sy_command hf_recg_config, sy_command hcf_recg_config, sy_command vcf_recg_config) //是否开启车辆特征识别); //某一路任务结束了,立马对该路任务进行二次属性分析 { + LOG_INFO("in: {}", task_id); + vector hp_keys; vector hcp_keys; queue vehicle_keys; @@ -613,11 +614,10 @@ void snapshot_helper::finish_task_ss_analysis(int task_id, sy_command hp_analysi // printf() finish_hcp_img[j].set_data(HCP_WIDTH, HCP_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); } - if (hcp_analysis_config == SY_CONFIG_OPEN) - { + + if (hcp_analysis_config == SY_CONFIG_OPEN) { result = new hcp_analysis_result[hcp_batch_count]{}; - //TimeCounting t("HumanCarParsing_Process"); - HumanCarParsing_Process(hcp_handle, finish_hcp_img, hcp_batch_count, result); + m_human_car_parsing.process(finish_hcp_img, hcp_batch_count, result); } if (hcf_recg_cf == SY_CONFIG_OPEN) @@ -832,6 +832,8 @@ void snapshot_helper::finish_task_ss_analysis(int task_id, sy_command hp_analysi } } } + + LOG_INFO("end: {}", task_id); } void snapshot_helper::hp_analysis() @@ -995,11 +997,9 @@ void snapshot_helper::hcp_analysis() ++obj_batch_count; } //printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~count_bike = %d\n", count_bike.size()); - if (hcp_analysis_cf == SY_CONFIG_OPEN) - { + if (hcp_analysis_cf == SY_CONFIG_OPEN) { result = new hcp_analysis_result[obj_batch_count]{}; - //TimeCounting t("HumanCarParsing_Process"); - HumanCarParsing_Process(hcp_handle, batch_hcp, obj_batch_count, result); + m_human_car_parsing.process(batch_hcp, obj_batch_count, result); } if (hcf_recg_cf == SY_CONFIG_OPEN) diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/snapshot_helper.h b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/snapshot_helper.h index 04b74c5..59948a3 100644 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/snapshot_helper.h +++ b/vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/snapshot_helper.h @@ -10,6 +10,7 @@ #include #include #include "ImageSaveCache.h" +#include "HumanCarParsing.h" using namespace std; #define EDGESIZE 4 @@ -173,7 +174,6 @@ private: sy_img * batch_vehicle_vf = nullptr; void * hp_handle = nullptr; - void * hcp_handle = nullptr; void * vc_handle = nullptr; void * vp_handle = nullptr; void * vr_handle = nullptr; @@ -256,15 +256,6 @@ public: void object_attri_analysis(); void snapshot_res_callback(OBJ_KEY obj_key,/* OBJ_VALUE obj_value,*/ void* analysisRes = NULL); - /*void vf_feature(); - void vf_feature(sy_img *batch_vehicle_f, int batch_size, map &mp_head); - void hp_analysis(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis, bool flag); - void hcp_analysis(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis, bool flag); - void vehicle_analysis(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis, bool flag); - void vf_feature(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis); - void hf_features(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis, bool flag); - void vehicle_color_analysis(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis); - void vehicle_plate_dr_analysis(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis); - void vehicle_recg_analysis(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis);*/ - +private: + HumanCarParsing m_human_car_parsing; }; \ No newline at end of file diff --git a/vehicle_structure_platform.git0708-3080-trt-face/src/test/main.cpp b/vehicle_structure_platform.git0708-3080-trt-face/src/test/main.cpp index 89d210e..2b8463f 100644 --- a/vehicle_structure_platform.git0708-3080-trt-face/src/test/main.cpp +++ b/vehicle_structure_platform.git0708-3080-trt-face/src/test/main.cpp @@ -645,7 +645,7 @@ int addTaskCount; std::chrono::time_point beginTime; void videoFinishCallback(void * handle, const int taskID) { - cout << "==============================完成任务ID: " << taskID << " ============================" << endl; + cout << "完成任务ID: " << taskID << "" << endl; // auto itor = cur_task_status_.find(taskID); // if (itor != cur_task_status_.end()) // { -- libgit2 0.21.4