From d7bafd6794e7caa1a693a52606a404db1c2b4850 Mon Sep 17 00:00:00 2001 From: fiss <2657262686@qq.com> Date: Thu, 18 May 2023 17:34:09 +0800 Subject: [PATCH] 添加最优快照截取模式 --- .gitignore | 2 ++ src/ai_platform/MultiSourceProcess.cpp | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- src/ai_platform/MultiSourceProcess.h | 4 ++++ src/ai_platform/header.h | 2 +- src/ai_platform/mvpt_process_assist.cpp | 10 ---------- src/ai_platform/mvpt_process_assist.h | 35 +---------------------------------- src/decoder/dvpp/CircularQueue.hpp | 2 +- src/decoder/dvpp/DvppDec.cpp | 6 ++++-- src/decoder/dvpp/FFReceiver.cpp | 17 +++++++++-------- src/demo/demo.cpp | 12 +++++++----- src/reprocessing_module/save_snapshot_reprocessing.h | 1 + src/reprocessing_module/snapshot_reprocessing.cpp | 221 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/reprocessing_module/snapshot_reprocessing.h | 58 ++++++++++++++++++++++++++++++++++++++++++---------------- src/util/crop_process.cpp | 416 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- src/util/crop_process.h | 88 ---------------------------------------------------------------------------------------- src/util/vpc_util.cpp | 39 ++++++++++++++++++--------------------- src/util/vpc_util.h | 6 +++--- 17 files changed, 431 insertions(+), 615 deletions(-) delete mode 100644 src/util/crop_process.cpp delete mode 100644 src/util/crop_process.h diff --git a/.gitignore b/.gitignore index ecac760..74cd035 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ bin/vpt_proj bin/logs/* +bin/res/* +bin/vpt_proj diff --git a/src/ai_platform/MultiSourceProcess.cpp b/src/ai_platform/MultiSourceProcess.cpp index 242528b..26dd06a 100644 --- a/src/ai_platform/MultiSourceProcess.cpp +++ b/src/ai_platform/MultiSourceProcess.cpp @@ -9,7 +9,6 @@ #include "../decoder/interface/DecoderManager.h" #include "../decoder/interface/utiltools.hpp" -#include "../util/crop_process.h" #include "../helpers/time_helper.hpp" #include "../helpers/os_helper.hpp" #include "../helpers/gen_json.hpp" @@ -18,10 +17,21 @@ #include "macro_definition.h" #include "SourceSingleton.hpp" -#define VEHICLE_MULTI_BOXES +// #define VEHICLE_MULTI_BOXES using namespace std; +map index_to_algo_type = {{0, algorithm_type_t::HUMAN_SNAPSHOT}, + {1, algorithm_type_t::NONMOTOR_VEHICLE_SNAPSHOT}, + {2, algorithm_type_t::NONMOTOR_VEHICLE_SNAPSHOT}, + {3, algorithm_type_t::NONMOTOR_VEHICLE_SNAPSHOT}, + {4, algorithm_type_t::VEHICLE_SNAPSHOT}, + {5, algorithm_type_t::VEHICLE_SNAPSHOT}, + {6, algorithm_type_t::VEHICLE_SNAPSHOT}, + {7, algorithm_type_t::VEHICLE_SNAPSHOT}, + {8, algorithm_type_t::VEHICLE_SNAPSHOT}, + {9, algorithm_type_t::FACE_SNAPSHOT}}; + struct decode_cbk_userdata{ string task_id; @@ -30,7 +40,6 @@ struct decode_cbk_userdata{ }; - /** * 注意: gpuFrame 在解码器设置的显卡上,后续操作要十分注意这一点,尤其是多线程情况 * */ @@ -62,7 +71,7 @@ CMultiSourceProcess::CMultiSourceProcess(){ } CMultiSourceProcess::~CMultiSourceProcess(){ - vpc_util.vpc_crop_release(); + vpc_util.release(); } int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam){ @@ -90,10 +99,10 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam){ } m_task_param_manager = task_param_manager::getInstance(); - m_snapshot_reprocessing = snapshot_reprocessing::getInstance(); + m_snapshot_reprocessing = new snapshot_reprocessing(m_devId); m_save_snapshot_reprocessing = new save_snapshot_reprocessing(m_devId); - vpc_util.vpc_crop_init(m_devId); + vpc_util.init(m_devId); m_pAlgorthimThread = new thread([](void* arg) { CMultiSourceProcess* process = (CMultiSourceProcess*)arg ; @@ -391,7 +400,7 @@ int CMultiSourceProcess::algorthim_process_thread(){ continue; } - ACL_CALL(aclrtSetCurrentContext(ctx), ACL_ERROR_NONE, 1); + aclrtSetCurrentContext(ctx); algorthim_vpt(vec_gpuMem); for(int i=0;i < vec_gpuMem.size(); i++){ @@ -406,6 +415,8 @@ int CMultiSourceProcess::algorthim_process_thread(){ } + aclrtDestroyContext(ctx); + LOG_INFO("algorthim_process_thread exit."); return 0; @@ -445,7 +456,10 @@ int CMultiSourceProcess::algorthim_vpt(vector vec_gpuMem){ #ifndef VEHICLE_MULTI_BOXES /* 快照优选(内部可实现不同的快照优选策略) */ - m_snapshot_reprocessing->update_bestsnapshot(vpt_interest_task_id, vpt_interest_imgs.data(), vptResult, deleteObjectID); + m_snapshot_reprocessing->update_bestsnapshot(vec_vptMem, vptResult, deleteObjectID); + + /* for snapshot algorithm. 轨迹结束目标 做最后的结果返回(当前返回算法结果+快照保存路径)*/ + vehicle_snapshot(vpt_interest_task_id, deleteObjectID); #else algorithm_vehicle_relult(vec_vptMem, vptResult, deleteObjectID); @@ -486,7 +500,7 @@ int CMultiSourceProcess::algorithm_vehicle_relult(vector vec_devM saveInfo.img_info = vpc_util.vpc_devMem2vpcImg(result.memPtr); m_save_snapshot_reprocessing->reprocessing_process_wo_locus_async(saveInfo); - vector vec_obj_info_list = vpc_util.vpc_crop_batch(result.memPtr, result.objs); + vector vec_obj_info_list = vpc_util.crop_batch(result.memPtr, result.objs); if(vec_obj_info_list.size() != result.objs.size()){ LOG_ERROR("vpc_crop size error !"); vpc_util.vpc_imgList_release(vec_obj_info_list); @@ -560,4 +574,99 @@ void CMultiSourceProcess::send_locus_finished_msg(vector& vpt_interest_t } } } +} + +// for snapshot algorithm. 轨迹结束目标 做最后的结果返回(当前返回算法结果+快照保存路径) +void CMultiSourceProcess::vehicle_snapshot(vector& vpt_interest_task_id, vector> deleteObjectID) { + auto task_iter = vpt_interest_task_id.begin(); + + for (int i = 0; i < deleteObjectID.size(); i++, ++task_iter) // loop taskId. + { + for (int &j : deleteObjectID[i]) // loop algor type. + { + OBJ_KEY obj_key = {*task_iter, j}; + endframe_obj_process(obj_key, algorithm_type_t::PLACEHOLDER); + } + } +} + +/* 轨迹结束帧需要做的算法模块 */ +/* 轨迹结束帧需要做的算法模块 */ +int CMultiSourceProcess::endframe_obj_process(const OBJ_KEY &obj_key, algorithm_type_t algor_type) { + auto task_param_ptr = m_task_param_manager->get_task_algor_param(obj_key.video_id); + auto task_other_param_ptr = m_task_param_manager->get_task_other_param(obj_key.video_id); + + // 该路任务开启了抓拍功能 开始抓拍保存;若未开启抓拍,清空显存资源 + if ((task_param_ptr->human_algors.find(algorithm_type_t::HUMAN_SNAPSHOT) != task_param_ptr->human_algors.end() || + task_param_ptr->vehicle_algors.find(algorithm_type_t::VEHICLE_SNAPSHOT) != task_param_ptr->vehicle_algors.end() || + task_param_ptr->nonmotor_vehicle_algors.find(algorithm_type_t::NONMOTOR_VEHICLE_SNAPSHOT) != task_param_ptr->nonmotor_vehicle_algors.end())) { + + vehicle_locus_finished(obj_key); + + } +} + +void CMultiSourceProcess::vehicle_locus_finished(const OBJ_KEY obj_key){ + map _total_snapshot_info = m_snapshot_reprocessing->get_total_snapshot_info(); + + if (_total_snapshot_info.find(obj_key) == _total_snapshot_info.end()) { + return; + } + + const OBJ_VALUE obj_value = _total_snapshot_info[obj_key]; + auto task_other_param = m_task_param_manager->get_task_other_params()[obj_key.video_id]; + + auto algor_type = index_to_algo_type[obj_value.index.index]; + + auto algor_param = (algor_config_param_snapshot *)task_other_param[algor_type]->algor_param; + if (obj_value.confidence <= algor_param->threshold) { + LOG_DEBUG("Snapshot conf filter ({} vs {})", obj_value.confidence, algor_param->threshold); + // 删除记录,同时释放显存 + m_snapshot_reprocessing->release_finished_locus_snapshot(obj_key.video_id, obj_key.obj_id, true); + return; + } + + const algor_basic_config_param_t *cur_param = task_other_param[algor_type]->basic_param; + + // 原图 + std::string cur_timestamp_ms = std::to_string(helpers::timer::get_cur_time_ms()); + std::string fpath_origin = std::string(cur_param->result_folder) + helpers::os::sep + obj_key.video_id + "_" + + std::to_string(obj_key.obj_id) + "_" + cur_timestamp_ms + ".jpg"; + + ImgSaveInfo origin_save_info; + origin_save_info.file_path = fpath_origin; + origin_save_info.img_info = obj_value.snapShot; + origin_save_info.obj_rect = obj_value.obj_pos; + m_save_snapshot_reprocessing->reprocessing_process_wo_locus_async(origin_save_info); + + { + // 抠图 + string json_str = ""; + std::string cur_timestamp_ms = std::to_string(helpers::timer::get_timestamp()); + string object_file_name = std::string(cur_param->result_folder_little) + helpers::os::sep + obj_key.video_id + "_" + + std::to_string(obj_key.obj_id) + "_" + cur_timestamp_ms + ".jpg"; + +#ifdef POST_USE_RABBITMQ + video_object_snapshot new_obj_ss_info; + new_obj_ss_info.analysisRes = nullptr; + new_obj_ss_info.object_id = obj_key.obj_id; + new_obj_ss_info.obj_info.set_data(obj_value.index.index, obj_value.confidence, obj_value.obj_pos.left_, + obj_value.obj_pos.top_, obj_value.obj_pos.left_ + obj_value.obj_pos.width_, + obj_value.obj_pos.top_ + obj_value.obj_pos.height_); + strcpy(new_obj_ss_info.task_id, obj_key.video_id.c_str()); + strcpy(new_obj_ss_info.video_image_path, fpath_origin.c_str()); + strcpy(new_obj_ss_info.snapshot_image_path, object_file_name.c_str()); + json_str = helpers::gen_json::gen_snapshot_json(algor_type, new_obj_ss_info); +#endif + + ImgSaveInfo obj_save_info; + obj_save_info.file_path = object_file_name; + obj_save_info.img_info = obj_value.snapShotLittle; + obj_save_info.json_str = json_str; + m_save_snapshot_reprocessing->reprocessing_process_wo_locus_async(obj_save_info); + } + + // 仅删除记录,显存会在保存线程中删除 + m_snapshot_reprocessing->release_finished_locus_snapshot(obj_key.video_id, obj_key.obj_id, false); + } \ No newline at end of file diff --git a/src/ai_platform/MultiSourceProcess.h b/src/ai_platform/MultiSourceProcess.h index 846f95c..fb4ed39 100644 --- a/src/ai_platform/MultiSourceProcess.h +++ b/src/ai_platform/MultiSourceProcess.h @@ -54,6 +54,10 @@ private: bool finish_task(const string taskID, const bool delete_snapshot); int algorithm_vehicle_relult(vector vec_devMem, vector& vptResult, vector>& delete_object_id); void send_locus_finished_msg(vector& vpt_interest_task_id, vector> deleteObjectID); + void vehicle_snapshot(vector& vpt_interest_task_id, vector> deleteObjectID); + void vehicle_locus_finished(const OBJ_KEY obj_key); + + int endframe_obj_process(const OBJ_KEY &obj_key, algorithm_type_t algor_type); private: int m_devId; diff --git a/src/ai_platform/header.h b/src/ai_platform/header.h index 111a59a..4b414a5 100644 --- a/src/ai_platform/header.h +++ b/src/ai_platform/header.h @@ -231,7 +231,7 @@ typedef struct video_object_snapshot { #ifndef __VIDEO_OBJECT_INFO__ #define __VIDEO_OBJECT_INFO__ typedef struct video_object_info { - int task_id; //该物体属于的任务ID号 + char task_id[128]; //该物体属于的任务ID号 int task_frame_count; //该物体当前出现的帧号 int object_id; //该物体的ID号 int left; //该物体位置的左坐标 diff --git a/src/ai_platform/mvpt_process_assist.cpp b/src/ai_platform/mvpt_process_assist.cpp index f8e188d..008a173 100644 --- a/src/ai_platform/mvpt_process_assist.cpp +++ b/src/ai_platform/mvpt_process_assist.cpp @@ -183,16 +183,6 @@ void CreateResultFolder(char* resultFolder, const char* jointFolder) } } - -bool best_snapshot_judge_algor(const OBJ_KEY& obj_key, const OBJ_VALUE& obj_value, int left, int top, int width, int height, int image_width, int image_height) -{ - return snapshot_legal_pos(obj_value.flags, left, top, left + width, top + height, image_width, image_height) - && snapshot_legal_minarea(obj_value.index.index, width, height) - && snapshot_legal_inarea(width, height) - && snapshot_legal_area(obj_value.max_area, obj_value.last_area, left, top, left + width, top + height); - return true; -} - bool snapshot_judge_algor(int index, int width, int height) { return snapshot_legal_minarea(index, width, height) //判断最小面积 diff --git a/src/ai_platform/mvpt_process_assist.h b/src/ai_platform/mvpt_process_assist.h index 9cec1b7..27309d3 100644 --- a/src/ai_platform/mvpt_process_assist.h +++ b/src/ai_platform/mvpt_process_assist.h @@ -22,39 +22,6 @@ using namespace std; -typedef struct DxGPUFrame -{ - void * frame; - unsigned int size; - unsigned int width; - unsigned int height; - unsigned long long timestamp; -}DxGPUFrame; - -struct OBJ_VALUE { - bool finishTracker; //轨迹结束可以保存了 - int frameCount; - bool isupdate; - int lost; - DxGPUFrame snapShot; - DxGPUFrame snapShotLittle; - sy_rect obj_pos; - double confidence; // 置信度 - float last_area; - float max_area; - OBJ_INDEX index; - - bitset< EDGESIZE > flags; //标志位,标记快照框应该如何判断,left:0 top:1 right:2 bottom:3 - sy_point landmark_point[25];//人脸关键点位置信息 - sy_rect position; //检测实际目标框 - //-added by zsh 220719 人脸姿态角- - float roll = 0.0; - float yaw = 0.0; - float pitch = 0.0; - //------------------------------- - -}; - int CreateDir(char *pszDir); void CreateResultFolder(char* resultFolder, const char* jointFolder); @@ -68,7 +35,7 @@ void CreateResultFolder(char* resultFolder, const char* jointFolder); bool snapshot_legal_inarea(int width, int height); bool snapshot_legal_inarea(sy_rect algor_area, int left, int top, int right, int bottom); bool snapshot_legal_minarea(int index, int width, int height); -bool snapshot_algor_open_config(const OBJ_KEY& obj_key); +// bool snapshot_algor_open_config(const OBJ_KEY& obj_key); bool snapshot_legal_pos(bitset flags, int left, int top, int right, int bottom, int image_width, int image_height); bool snapshot_legal_area(int max_area, int last_area, int left, int top, int right, int bottom); bool snapshot_legal_pose(float last_roll, float last_yaw, float last_pitch, float roll, float yaw, float pitch); // added by zsh 220719 判断人脸姿态角 diff --git a/src/decoder/dvpp/CircularQueue.hpp b/src/decoder/dvpp/CircularQueue.hpp index 368291c..330c6a8 100644 --- a/src/decoder/dvpp/CircularQueue.hpp +++ b/src/decoder/dvpp/CircularQueue.hpp @@ -77,7 +77,7 @@ template void CircularQueue::addTail() { std::lock_guard l(m_mutex); - rear = (rear + 1) % max_size;//队尾指针加1 + rear = (rear + 1) % max_size;//队尾指针加1 } //循环队列的出队 diff --git a/src/decoder/dvpp/DvppDec.cpp b/src/decoder/dvpp/DvppDec.cpp index 5092e81..21b8e53 100644 --- a/src/decoder/dvpp/DvppDec.cpp +++ b/src/decoder/dvpp/DvppDec.cpp @@ -446,8 +446,10 @@ int DvppDec::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_count CHECK_NOT_RETURN(acldvppDestroyPicDesc(output_pic_desc), "acldvppDestroyPicDesc failed"); } - acldvppFree(vdecOutputBuf); - vdecOutputBuf = nullptr; + if (vdecOutputBuf){ + acldvppFree(vdecOutputBuf); + vdecOutputBuf = nullptr; + } return 1; } diff --git a/src/decoder/dvpp/FFReceiver.cpp b/src/decoder/dvpp/FFReceiver.cpp index 26685a6..3bbbffe 100644 --- a/src/decoder/dvpp/FFReceiver.cpp +++ b/src/decoder/dvpp/FFReceiver.cpp @@ -160,12 +160,10 @@ void FFReceiver::read_thread(){ int frame_count = 0; int ret = -1; - while (m_bRunning) - { - if (!m_bReal) - { - if (m_bPause) - { + while (m_bRunning){ + + if (!m_bReal){ + if (m_bPause){ std::this_thread::sleep_for(std::chrono::milliseconds(3)); continue; } @@ -178,8 +176,8 @@ void FFReceiver::read_thread(){ } int result = av_read_frame(fmt_ctx, pkt); - if (result == AVERROR_EOF || result < 0) - { + if (result == AVERROR_EOF || result < 0){ + av_packet_unref(pkt); LOG_ERROR("[{}]- Failed to read frame!", m_dec_name); break; } @@ -210,6 +208,9 @@ void FFReceiver::read_thread(){ frame_count++; } + } else { + // 音频等其他分量的情形 + av_packet_unref(pkt); } } diff --git a/src/demo/demo.cpp b/src/demo/demo.cpp index 7cd2fec..4399460 100644 --- a/src/demo/demo.cpp +++ b/src/demo/demo.cpp @@ -478,8 +478,8 @@ static long long get_cur_time(){ string createTask(void *handle, std::vector algor_vec, int gi){ task_param tparam; // tparam.ipc_url = "rtsp://admin:ad123456@192.168.60.108:554/cam/realmonitor?channel=1&subtype=0"; - // tparam.ipc_url = "/home/huchunming/data/caishenkezhan.mp4"; - tparam.ipc_url = "/home/cmhu/data/Street.uvf"; + tparam.ipc_url = "/home/cmhu/data/bayue.mp4"; + // tparam.ipc_url = "/home/cmhu/data/Street.uvf"; // tparam.ipc_url = "rtsp://122.97.218.170:8604/openUrl/V5nXRHa?params=eyJwcm90b2NhbCI6InJ0c3AiLCJjbGllbnRUeXBlIjoib3Blbl9hcGkiLCJleHByaWVUaW1lIjotMSwicHJvdG9jb2wiOiJydHNwIiwiZXhwaXJlVGltZSI6MzAwLCJlbmFibGVNR0MiOnRydWUsImV4cGFuZCI6InN0YW5kYXJkPXJ0c3Amc3RyZWFtZm9ybT1ydHAiLCJhIjoiMTBjZjM4N2JjY2Y5NDg3YzhjNWYzNjE2M2ViMWUyNTJ8MXwwfDEiLCJ0IjoxfQ=="; tparam.algor_counts = algor_vec.size(); tparam.dec_type = 2; @@ -525,8 +525,8 @@ void test_gpu(int gpuID){ algorithm_type_t::VEHICLE_SNAPSHOT, algorithm_type_t::NONMOTOR_VEHICLE_SNAPSHOT, algorithm_type_t::VIDEO_TIMING_SNAPSHOT}; string task_id = createTask(handle, algor_vec, 0 + gpuID * 10); - string task_id1 = createTask(handle, algor_vec, 1 + gpuID * 10); - string task_id2 = createTask(handle, algor_vec, 2 + gpuID * 10); + // string task_id1 = createTask(handle, algor_vec, 1 + gpuID * 10); + // string task_id2 = createTask(handle, algor_vec, 2 + gpuID * 10); // finish_task(handle, (char*)task_id.data(), 0); @@ -549,7 +549,9 @@ int main(int argc, char *argv[]) { int gpuID = atoi(argv[4]); test_gpu(0); - test_gpu(1); + // test_gpu(1); + // test_gpu(2); + // test_gpu(3); while (getchar() != 'q'); diff --git a/src/reprocessing_module/save_snapshot_reprocessing.h b/src/reprocessing_module/save_snapshot_reprocessing.h index 7147dce..1dc1dd6 100644 --- a/src/reprocessing_module/save_snapshot_reprocessing.h +++ b/src/reprocessing_module/save_snapshot_reprocessing.h @@ -31,6 +31,7 @@ struct ImgSaveInfo{ string file_path {""}; vpc_img_info img_info; string json_str {""}; + sy_rect obj_rect; }; class save_snapshot_reprocessing diff --git a/src/reprocessing_module/snapshot_reprocessing.cpp b/src/reprocessing_module/snapshot_reprocessing.cpp index 3272423..5588c5c 100644 --- a/src/reprocessing_module/snapshot_reprocessing.cpp +++ b/src/reprocessing_module/snapshot_reprocessing.cpp @@ -5,13 +5,20 @@ #include "../decoder/interface/DeviceMemory.hpp" -snapshot_reprocessing::snapshot_reprocessing() +snapshot_reprocessing::snapshot_reprocessing(int devId) { m_task_param_manager = task_param_manager::getInstance(); algor_index_table["human"] = { (int)det_class_label_t::HUMAN }; algor_index_table["nonmotor_vehicle"] = { (int)det_class_label_t::BICYCLE, (int)det_class_label_t::MOTOCYCLE, (int)det_class_label_t::TRICYCLE }; algor_index_table["vehicle"] = { (int)det_class_label_t::SMALL_CAR, (int)det_class_label_t::LARGE_CAR, (int)det_class_label_t::TRUCK, (int)det_class_label_t::TRACTOR, (int)det_class_label_t::MEDIUM_BUS }; + + vpcUtil.init(devId); +} + +snapshot_reprocessing::~snapshot_reprocessing() +{ + vpcUtil.release(); } static void box_expansion(video_object_info& obj_info, float expand_ratio, int frame_width, int frame_height){ @@ -170,4 +177,216 @@ void snapshot_reprocessing::filter_vehicle(vector vec_devMem, vec _onelevel_det_result[task_idx].obj_count = effective_count; task_idx++; } +} + +/* 获取人车物目标快照图 */ +void snapshot_reprocessing::update_bestsnapshot(vector vec_devMem, vector &ol_det_result, vector>& delete_object_id){ + map && algor_config_param = m_task_param_manager->get_task_algor_params(); + map> && algor_param = m_task_param_manager->get_task_other_params(); + + for (size_t i = 0; i < vec_devMem.size(); i++){ + DeviceMemory* memPtr = vec_devMem[i]; + + onelevel_det_result det_result = ol_det_result[i]; + + if (0 == det_result.obj_count){ + ++i; + continue; + } + + string task_id = memPtr->getId(); + int frame_height = memPtr->getHeight(); + int frame_width = memPtr->getWidth(); + + vector vec_obj_info; + for (int c = 0; c < det_result.obj_count; c++){ + + det_objinfo obj_info = det_result.obj[c]; + OBJ_KEY new_obj = { task_id, obj_info.id }; + + int index = 0; + + /* 投票确定目标index */ + if (total_snapshot_info.find(new_obj) == total_snapshot_info.end()){ + index = obj_info.index; + } else { + index = total_snapshot_info[new_obj].index.index; + } + + int cur_real_width = (obj_info.right - obj_info.left); + int cur_real_height = (obj_info.bottom - obj_info.top); + int cur_real_index = obj_info.index; + + int expansion_width = cur_real_width * EXPANSION_PROPORTION; + int expansion_height = cur_real_height * EXPANSION_PROPORTION; + // DEBUG----------------------------------------------------------------- + // 0-行人 1-自行车 2-摩托车 3-三轮车 4-小型车 5-大车 6-卡车 7-拖拉机 8-中巴 + if(index ==0 || index ==1 || index ==2 || index ==3) { //行人和非机动车外扩指定像素即可 + expansion_width = 10; + expansion_height = 10; + } + + /* 若该目标第一次出现 */ + if (total_snapshot_info.find(new_obj) == total_snapshot_info.end()) + { + /* manager insert new object. */ + /* 判断目标合法 */ + algorithm_type_t algor_type; + if (index ==0) algor_type = algorithm_type_t::HUMAN_SNAPSHOT; + if (index ==1 || index ==2 || index ==3) algor_type = algorithm_type_t::NONMOTOR_VEHICLE_SNAPSHOT; + if (index ==4 || index ==5 || index ==6 || index ==7 || index ==8) algor_type = algorithm_type_t::VEHICLE_SNAPSHOT; + if (!(algor_config_param.count(task_id) && algor_param.count(task_id) && algor_param[task_id].count(algor_type))) + continue; + task_param_manager::algo_param_type_t_* cur_task_params = algor_param[task_id][algor_type]; + if (!snapshot_legal_inarea(cur_task_params->basic_param->algor_valid_rect,obj_info.left, obj_info.top, obj_info.right, obj_info.bottom)){ + continue; + } + + if(!snapshot_legal_minarea(index, cur_real_width, cur_real_height)){ + continue; + } + + /* 存入当前抠图目标参数 flags用于判断目标从画面什么位置出现 方便之后排除出画面边缘的快照图 */ + total_snapshot_info[new_obj].index.count++; + total_snapshot_info[new_obj].index.index = cur_real_index; + total_snapshot_info[new_obj].confidence = obj_info.confidence; + total_snapshot_info[new_obj].flags[0] = obj_info.left < minDistance[0] + SCALE_OUT ? 0 : 1; //left + total_snapshot_info[new_obj].flags[1] = obj_info.top < minDistance[1] + SCALE_OUT ? 0 : 1; //top + total_snapshot_info[new_obj].flags[2] = obj_info.right > frame_width - minDistance[2] - SCALE_OUT ? 0 : 1; //right + total_snapshot_info[new_obj].flags[3] = obj_info.bottom > frame_height - minDistance[3] - SCALE_OUT ? 0 : 1; //bottom + + int cur_left = max(obj_info.left - 10, 0); + int cur_top = max(obj_info.top - 10, 0); + int cur_right = min(obj_info.right + 10, frame_width - 1); + int cur_bottom = min(obj_info.bottom + 10, frame_height - 1); + total_snapshot_info[new_obj].obj_pos = { cur_left, cur_top, cur_right - cur_left, cur_bottom - cur_top }; //debug by zsh 推出的坐标外扩10像素 + total_snapshot_info[new_obj].last_area = total_snapshot_info[new_obj].max_area = (cur_right - cur_left) * (cur_bottom - cur_top); + + video_object_info info; + info.left = max(obj_info.left - expansion_width, 0); + info.top = max(obj_info.top - expansion_height, 0); + info.right = min(obj_info.right + expansion_width, frame_width - 1); + info.bottom = min(obj_info.bottom + expansion_height, frame_height - 1); + strcpy(info.task_id, task_id.c_str()); + info.object_id = obj_info.id; + info.confidence = obj_info.confidence; + info.index = index; + + vec_obj_info.push_back(info); + } else { + total_snapshot_info[new_obj].last_area = (obj_info.right - obj_info.left) * (obj_info.bottom - obj_info.top); + + algorithm_type_t algor_type; + if (index ==0) algor_type = algorithm_type_t::HUMAN_SNAPSHOT; + if (index ==1 || index ==2 || index ==3) algor_type = algorithm_type_t::NONMOTOR_VEHICLE_SNAPSHOT; + if (index ==4 || index ==5 || index ==6 || index ==7 || index ==8) algor_type = algorithm_type_t::VEHICLE_SNAPSHOT; + if (!(algor_config_param.count(task_id) && algor_param.count(task_id) && algor_param[task_id].count(algor_type))) + continue; + task_param_manager::algo_param_type_t_* cur_task_params = algor_param[task_id][algor_type]; + if (!snapshot_legal_inarea(cur_task_params->basic_param->algor_valid_rect,obj_info.left, obj_info.top, obj_info.right, obj_info.bottom)) + continue; + //--------------------------------------------------------------- + if (!best_snapshot_judge_algor(new_obj, total_snapshot_info[new_obj], obj_info.left, obj_info.top, + cur_real_width, cur_real_height, frame_width, frame_height)) + { + continue; + } + /* 若更优于之前的快照 做快照的更新 */ + if (total_snapshot_info[new_obj].index.count == 0) + { + total_snapshot_info[new_obj].index.count++; + total_snapshot_info[new_obj].index.index = cur_real_index; + } + else + { + if (total_snapshot_info[new_obj].index.index == cur_real_index) + total_snapshot_info[new_obj].index.count++; + else + total_snapshot_info[new_obj].index.count--; + } + + int cur_left = max(obj_info.left - 10, 0); + int cur_top = max(obj_info.top - 10, 0); + int cur_right = min(obj_info.right + 10, frame_width - 1); + int cur_bottom = min(obj_info.bottom + 10, frame_height - 1); + total_snapshot_info[new_obj].obj_pos = { cur_left, cur_top, cur_right - cur_left, cur_bottom - cur_top }; //debug by zsh 推出的坐标外扩10像素 + total_snapshot_info[new_obj].last_area = total_snapshot_info[new_obj].max_area = (cur_right - cur_left) * (cur_bottom - cur_top); + + video_object_info info; + info.left = max(obj_info.left - expansion_width, 0); + info.top = max(obj_info.top - expansion_height, 0); + info.right = min(obj_info.right + expansion_width, frame_width - 1); + info.bottom = min(obj_info.bottom + expansion_height, frame_height - 1); + strcpy(info.task_id, task_id.c_str()); + info.object_id = obj_info.id; + info.confidence = obj_info.confidence; + info.index = index; + + vec_obj_info.push_back(info); + } + } + + vector imgList = vpcUtil.crop_batch(memPtr, vec_obj_info); + + if(imgList.size() > 0){ + for (size_t i = 0; i < imgList.size(); i++) { + vpc_img_info obj_info = imgList[i]; + OBJ_KEY objKey = { obj_info.task_id, obj_info.object_id }; + vpcUtil.vpc_img_release(total_snapshot_info[objKey].snapShot); + total_snapshot_info[objKey].snapShot = vpcUtil.vpc_devMem2vpcImg(memPtr); + vpcUtil.vpc_img_release(total_snapshot_info[objKey].snapShotLittle); + total_snapshot_info[objKey].snapShotLittle = obj_info; + } + } + } +} + +map snapshot_reprocessing::get_total_snapshot_info(){ + return total_snapshot_info; +} + +/* 快照判定辅助函数 */ +bool snapshot_reprocessing::best_snapshot_judge_algor(const OBJ_KEY& obj_key, const OBJ_VALUE& obj_value, int left, int top, int width, int height, int image_width, int image_height) +{ + return snapshot_legal_pos(obj_value.flags, left, top, left + width, top + height, image_width, image_height) + && snapshot_legal_minarea(obj_value.index.index, width, height) + && snapshot_legal_inarea(width, height) + && snapshot_legal_area(obj_value.max_area, obj_value.last_area, left, top, left + width, top + height); + return true; +} + +/* 删除指定快照 清空资源占用(使用场景:任务结束删除该路任务所有缓存快照;目标轨迹结束,分析保存完,删除该目标快照缓存)*/ +void snapshot_reprocessing::release_finished_locus_snapshot(const string taskid, const int obj_id, bool bRelease) +{ + LOG_DEBUG("[info] task_id {} delete obj_id {}", taskid, obj_id); //221026 + if (obj_id != -1) + { + OBJ_KEY cur_key = { taskid , obj_id }; + auto it = total_snapshot_info.find(cur_key); + if (it == total_snapshot_info.end()){ + return; + } + + if (bRelease){ + OBJ_VALUE ss = total_snapshot_info[cur_key]; + + vpcUtil.vpc_img_release(ss.snapShot); + vpcUtil.vpc_img_release(ss.snapShotLittle); + } + + total_snapshot_info.erase(cur_key); + return; + } + + for(auto ss = total_snapshot_info.begin(); ss != total_snapshot_info.end(); ss++) + { + if (strcmp(ss->first.video_id.c_str(), taskid.c_str()) == 0) + { + if (bRelease){ + vpcUtil.vpc_img_release(ss->second.snapShot); + vpcUtil.vpc_img_release(ss->second.snapShotLittle); + } + total_snapshot_info.erase(ss); + } + } } \ No newline at end of file diff --git a/src/reprocessing_module/snapshot_reprocessing.h b/src/reprocessing_module/snapshot_reprocessing.h index 6e8ad66..bb2c1ed 100644 --- a/src/reprocessing_module/snapshot_reprocessing.h +++ b/src/reprocessing_module/snapshot_reprocessing.h @@ -9,22 +9,19 @@ #include "../ai_platform/det_obj_header.h" #include -#include #include #include #include #include "../ai_platform/task_param_manager.h" #include "../ai_platform/header.h" +#include "../util/vpc_util.h" // #define EXPANSION_PROPORTION 0.1 #define EXPANSION_PROPORTION 0.25 #define FACE_EXPANSION_PROPORTION 0.5 #define IMG_CHANNELS 3 -// using namespace std; -using std::vector; -using std::map; -using std::set; +using namespace std; class DeviceMemory; @@ -35,27 +32,56 @@ typedef struct multi_obj_data_t{ string task_id; } multi_obj_data_t; + +struct OBJ_VALUE { + bool finishTracker; //轨迹结束可以保存了 + int frameCount; + bool isupdate; + int lost; + vpc_img_info snapShot; + vpc_img_info snapShotLittle; + sy_rect obj_pos; + double confidence; // 置信度 + float last_area; + float max_area; + OBJ_INDEX index; + + bitset< EDGESIZE > flags; //标志位,标记快照框应该如何判断,left:0 top:1 right:2 bottom:3 + sy_point landmark_point[25];//人脸关键点位置信息 + sy_rect position; //检测实际目标框 + //-added by zsh 220719 人脸姿态角- + float roll = 0.0; + float yaw = 0.0; + float pitch = 0.0; + //------------------------------- + +}; + class snapshot_reprocessing { public: //更新最优快照 - static snapshot_reprocessing* getInstance() - { - static snapshot_reprocessing snapshot_reprocessing_instance; - return &snapshot_reprocessing_instance; - } + snapshot_reprocessing(int devId); + ~snapshot_reprocessing(); vector get_vehicle_snapshot(vector vec_devMem, vector& ol_det_result, int skip_frame); + void screen_effective_snapshot(const vector &taskid_inplay, vector &_onelevel_det_result); + void filter_vehicle(vector vec_devMem, vector &_onelevel_det_result); + + void update_bestsnapshot(vector vec_devMem, vector &ol_det_result, vector>& delete_object_id); + map get_total_snapshot_info(); + + void release_finished_locus_snapshot(const string taskid, const int obj_id, bool bRelease); + +private: + bool best_snapshot_judge_algor(const OBJ_KEY& obj_key, const OBJ_VALUE& obj_value, int left, int top, int width, int height, int image_width, int image_height); + private: - snapshot_reprocessing(); + map total_snapshot_info; map> algor_index_table; task_param_manager *m_task_param_manager; -public: - - void screen_effective_snapshot(const vector &taskid_inplay, vector &_onelevel_det_result); - void filter_vehicle(vector vec_devMem, vector &_onelevel_det_result); - + VPCUtil vpcUtil; }; \ No newline at end of file diff --git a/src/util/crop_process.cpp b/src/util/crop_process.cpp deleted file mode 100644 index d68b0fa..0000000 --- a/src/util/crop_process.cpp +++ /dev/null @@ -1,416 +0,0 @@ -/** -* @file dvpp_process.cpp -* -* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -*/ -#include "crop_process.h" -#include -#include -#include -#include -#include -#include "utils.h" -#include "acl/acl_base.h" -#include "acl/acl_rt.h" -#include "acl/acl_op.h" -#include "acl/acl_mdl.h" -#include "acl/ops/acl_dvpp.h" -#include "acl/ops/acl_cblas.h" -#include "../decoder/interface/DeviceMemory.hpp" - -using namespace std; - -namespace { - uint32_t AlignSize(uint32_t origSize, uint32_t alignment) - { - if (alignment == 0) { - return 0; - } - uint32_t alignmentH = alignment - 1; - return (origSize + alignmentH) / alignment * alignment; - } -} - -DvppCropProcess::DvppCropProcess() - : dvppChannelDesc_(nullptr), inputBatchPicDesc_(nullptr), - outputBatchPicDesc_(nullptr), inputBatchSize_(0), outputBatchSize_(0) -{ -} - -DvppCropProcess::~DvppCropProcess() -{ - DestroyBatchCropResource(); -} - -Result DvppCropProcess::InitProcess() -{ - // create vpc channel description - dvppChannelDesc_ = acldvppCreateChannelDesc(); - if (dvppChannelDesc_ == nullptr) { - ERROR_LOG("acldvppCreateChannelDesc failed"); - return FAILED; - } - - // create vpc channel - aclError aclRet = acldvppCreateChannel(dvppChannelDesc_); - if (aclRet != ACL_SUCCESS) { - ERROR_LOG("acldvppCreateChannel failed, errorCode = %d", static_cast(aclRet)); - return FAILED; - } - - // create dvpp resize config - resizeConfig_ = acldvppCreateResizeConfig(); - if (resizeConfig_ == nullptr) { - ERROR_LOG("acldvppCreateResizeConfig failed"); - return FAILED; - } - - INFO_LOG("dvpp init resource success"); - return SUCCESS; -} - -Result DvppCropProcess::InitBatchCropInputDesc(DeviceMemory *devMem) -{ - inputBatchPicDesc_ = acldvppCreateBatchPicDesc(inputBatchSize_); - if (inputBatchPicDesc_ == nullptr) { - ERROR_LOG("InitBatchCropInputDesc inBatchPicDesc failed"); - return FAILED; - } - - acldvppBatchPicDesc *inputBatchPicDesc_ = acldvppCreateBatchPicDesc(1); - acldvppPicDesc *vpcInputDesc_ = acldvppGetPicDesc(inputBatchPicDesc_, 0); - acldvppSetPicDescData(vpcInputDesc_, devMem->getMem()); - acldvppSetPicDescFormat(vpcInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - acldvppSetPicDescWidth(vpcInputDesc_, devMem->getWidth()); - acldvppSetPicDescHeight(vpcInputDesc_, devMem->getHeight()); - acldvppSetPicDescWidthStride(vpcInputDesc_, devMem->getWidthStride()); - acldvppSetPicDescHeightStride(vpcInputDesc_, devMem->getHeightStride()); - acldvppSetPicDescSize(vpcInputDesc_, devMem->getSize()); - - // if (inputBatchSize_ > batchInput_.size()) { - // inputBatchSize_ = batchInput_.size(); - // } - // uint32_t inputWidth = devMem->getWidth(); - // for (uint32_t i = 0; i < inputBatchSize_; i++) { - // vecInPtr_.push_back(inputBufferDev); - // acldvppPicDesc *vpcInputDesc = acldvppGetPicDesc(inputBatchPicDesc_, i); - // (void)acldvppSetPicDescData(vpcInputDesc, devMem->getMem()); - // (void)acldvppSetPicDescFormat(vpcInputDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - // (void)acldvppSetPicDescWidth(vpcInputDesc, batchInput_[i].inputWidth); - // (void)acldvppSetPicDescHeight(vpcInputDesc, batchInput_[i].inputHeight); - // (void)acldvppSetPicDescWidthStride(vpcInputDesc, inputWidthStride_); - // (void)acldvppSetPicDescHeightStride(vpcInputDesc, inputHeightStride_); - // (void)acldvppSetPicDescSize(vpcInputDesc, inputBufferSize); - // INFO_LOG("set inputDesc success."); - // } - return SUCCESS; -} - -Result DvppCropProcess::InitBatchCropOutputDesc(vector objs) -{ - const uint32_t widthAlignment = 16; - const uint32_t heightAlignment = 16; - const uint32_t sizeAlignment = 3; - const uint32_t sizeNum = 2; - - outputBatchPicDesc_ = acldvppCreateBatchPicDesc(outputBatchSize_); - if (outputBatchPicDesc_ == nullptr) { - ERROR_LOG("acldvppCreatePicDesc outBatchPicDesc failed"); - return FAILED; - } - acldvppPicDesc *vpcOutputDesc = nullptr; - for (uint32_t i = 0; i < outputBatchSize_; i++) { - video_object_info obj = objs[i]; - - int outputWidth_ = obj.right - obj.left; - int outputHeight_ = obj.bottom - obj.top; - int modelInputLeft = obj.left; - int modelInputTop = obj.top; - - uint32_t outputWidthStride = AlignSize(outputWidth_, widthAlignment); - uint32_t outputHeightStride = AlignSize(outputHeight_, heightAlignment); - uint32_t outputBufferSize = outputWidthStride * outputHeightStride * sizeAlignment / sizeNum; - - void *vpcBatchOutputBufferDev = nullptr; - auto ret = acldvppMalloc(&vpcBatchOutputBufferDev, outputBufferSize); - if (ret != ACL_SUCCESS) { - ERROR_LOG("acldvppMalloc failed, size = %u, errorCode = %d.", - outputBufferSize, static_cast(ret)); - return FAILED; - } - vecOutPtr_.push_back(vpcBatchOutputBufferDev); - vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i); - (void)acldvppSetPicDescData(vpcOutputDesc, vpcBatchOutputBufferDev); - (void)acldvppSetPicDescFormat(vpcOutputDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - (void)acldvppSetPicDescWidth(vpcOutputDesc, outputWidth_); - (void)acldvppSetPicDescHeight(vpcOutputDesc, outputHeight_); - (void)acldvppSetPicDescWidthStride(vpcOutputDesc, outputWidthStride); - (void)acldvppSetPicDescHeightStride(vpcOutputDesc, outputHeightStride); - (void)acldvppSetPicDescSize(vpcOutputDesc, outputBufferSize); - } - return SUCCESS; -} - -static Result dvpp_jpege_save(char* pcData , uint32_t dataLen, string out_file_name) -{ - FILE* fd = nullptr; - fd = fopen(out_file_name.c_str(), "wb"); - if (fd == nullptr) { - printf("open output file err\n"); - return FAILED; - } - - fwrite(pcData, dataLen, 1, fd); - fflush(fd); - - fclose(fd); - return SUCCESS; -} - -static void dvpp_jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_name){ - - // 2.运行管理资源申请(依次申请Device、Context、Stream) - aclrtContext context_; - aclrtStream stream_; - aclrtSetDevice(0); - aclrtCreateContext(&context_, 0); - aclrtCreateStream(&stream_); - - // 3.创建图片数据处理通道时的通道描述信息,dvppChannelDesc_是acldvppChannelDesc类型 - acldvppChannelDesc *dvppChannelDesc_ = acldvppCreateChannelDesc(); - - // 4.创建图片数据处理的通道 - aclError aclRet = acldvppCreateChannel(dvppChannelDesc_); - - // 7. 创建图片编码配置数据,设置编码质量 - // 编码质量范围[0, 100],其中level 0编码质量与level 100差不多,而在[1, 100]内数值越小输出图片质量越差。 - acldvppJpegeConfig *jpegeConfig_ = acldvppCreateJpegeConfig(); - acldvppSetJpegeConfigLevel(jpegeConfig_, 100); - - // 8. 申请输出内存,申请Device内存encodeOutBufferDev_,存放编码后的输出数据 - uint32_t outBufferSize= 0; - int ret = acldvppJpegPredictEncSize(encodeInputDesc_, jpegeConfig_, &outBufferSize); - void *encodeOutBufferDev_ = nullptr; - ret = acldvppMalloc(&encodeOutBufferDev_, outBufferSize); - - // 9. 执行异步编码,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成 - aclRet = acldvppJpegEncodeAsync(dvppChannelDesc_, encodeInputDesc_, encodeOutBufferDev_, - &outBufferSize, jpegeConfig_, stream_); - aclRet = aclrtSynchronizeStream(stream_); - - // 该模式下,由于处理结果在Device侧,因此需要调用内存复制接口传输结果数据后,再释放Device侧内存 - // 申请Host内存outputHostBuffer - void* outputHostBuffer = malloc(outBufferSize); - // 通过aclrtMemcpy接口将Device的处理结果数据传输到Host - aclRet = aclrtMemcpy(outputHostBuffer, outBufferSize, encodeOutBufferDev_, outBufferSize, ACL_MEMCPY_DEVICE_TO_HOST); - // 释放掉输入输出的device内存 - (void)acldvppFree(encodeOutBufferDev_); - // 数据使用完成后,释放内存 - dvpp_jpege_save((char*)outputHostBuffer, outBufferSize, out_file_name); - free(outputHostBuffer); - - acldvppDestroyChannel(dvppChannelDesc_); - (void)acldvppDestroyChannelDesc(dvppChannelDesc_); - dvppChannelDesc_ = nullptr; - - // 11. 释放运行管理资源(依次释放Stream、Context、Device) - aclrtDestroyStream(stream_); - aclrtDestroyContext(context_); - aclrtResetDevice(0); -} - -Result DvppCropProcess::ProcessBatchCrop(DeviceMemory *devMem, vector objs) -{ - inputBatchSize_ = 1; - outputBatchSize_ = objs.size(); - - INFO_LOG("ProcessBatchCrop start."); - const uint32_t oddNum = 1; - - std::unique_ptr - cropArea(new(std::nothrow) acldvppRoiConfig *[outputBatchSize_ * sizeof(acldvppRoiConfig *)]); - - for (uint32_t i = 0; i < outputBatchSize_; i++) { - video_object_info obj = objs[i]; - cropArea[i] = acldvppCreateRoiConfig(obj.left, obj.right, obj.top, obj.bottom); - if (cropArea[i] == nullptr) { - ERROR_LOG("acldvppCreateRoiConfig cropArea_ failed"); - return FAILED; - } - } - Result ret = InitBatchCropInputDesc(devMem); - if (ret != SUCCESS) { - ERROR_LOG("InitBatchCropInputDesc failed"); - return FAILED; - } - - ret = InitBatchCropOutputDesc(objs); - if (ret != SUCCESS) { - ERROR_LOG("InitBatchCropOutputDesc failed"); - return FAILED; - } - - // calculate total number of crop image - uint32_t totalNum = 0; - std::unique_ptr roiNums(new (std::nothrow) uint32_t[inputBatchSize_]); - if (roiNums != nullptr){ - for (int i = 0; i < inputBatchSize_; i++) { - // crop number of images from one source image is outputBatchSize_ / inputBatchSize_ - roiNums[i] = outputBatchSize_ / inputBatchSize_; - totalNum += roiNums[i]; - } - } - // crop number of images from last source image is - // outputBatchSize_ / inputBatchSize_ + outputBatchSize_ % inputBatchSize_ - if (outputBatchSize_ % inputBatchSize_ != 0) { - roiNums[inputBatchSize_ - 1] = (outputBatchSize_ - totalNum) + roiNums[inputBatchSize_ - 1]; - } - - aclError aclRet = acldvppSetResizeConfigInterpolation(resizeConfig_, 0); - aclRet = acldvppVpcBatchCropResizeAsync(dvppChannelDesc_, inputBatchPicDesc_, - roiNums.get(), inputBatchSize_, - outputBatchPicDesc_, cropArea.get(), resizeConfig_, stream_); - if (aclRet != ACL_SUCCESS) { - ERROR_LOG("acldvppVpcBatchCropAsync failed, errorCode = %d", static_cast(aclRet)); - return FAILED; - } - - aclRet = aclrtSynchronizeStream(stream_); - if (aclRet != ACL_SUCCESS) { - ERROR_LOG("crop aclrtSynchronizeStream failed, errorCode = %d", static_cast(aclRet)); - return FAILED; - } - - for (uint32_t i = 0; i < outputBatchSize_; i++) { - acldvppPicDesc *vpcOutputDesc = acldvppGetPicDesc(outputBatchPicDesc_, i); - string file_name = "output"; - file_name = file_name + to_string(i) + ".jpg"; - dvpp_jpeg_encode(vpcOutputDesc, file_name); - } - - for (uint32_t i = 0; i < outputBatchSize_; i++) { - if (cropArea[i] != nullptr) { - (void)acldvppDestroyRoiConfig(cropArea[i]); - cropArea[i] = nullptr; - } - } - INFO_LOG("ProcessBatchCrop success."); - return SUCCESS; -} - -void DvppCropProcess::DestroyBatchCropResource() -{ - INFO_LOG("DestroyBatchCropResource start."); - - if (inputBatchPicDesc_ != nullptr) { - (void)acldvppDestroyBatchPicDesc(inputBatchPicDesc_); - inputBatchPicDesc_ = nullptr; - } - for (auto ptr : vecOutPtr_) { - if (ptr != nullptr) { - (void)acldvppFree(ptr); - } - } - vecOutPtr_.clear(); - if (outputBatchPicDesc_ != nullptr) { - (void)acldvppDestroyBatchPicDesc(outputBatchPicDesc_); - outputBatchPicDesc_ = nullptr; - } - if (dvppChannelDesc_ != nullptr) { - aclError aclRet = acldvppDestroyChannel(dvppChannelDesc_); - if (aclRet != ACL_SUCCESS) { - ERROR_LOG("acldvppDestroyChannel failed, errorCode = %d", static_cast(aclRet)); - } - (void)acldvppDestroyChannelDesc(dvppChannelDesc_); - dvppChannelDesc_ = nullptr; - } - - if (resizeConfig_ != nullptr) { - (void)acldvppDestroyResizeConfig(resizeConfig_); - resizeConfig_ = nullptr; - } - - INFO_LOG("DestroyBatchCropResource end."); - return; -} - -Result DvppCropProcess::init(int deviceId) -{ - deviceId_ = deviceId; - - aclError ret; - // ret = aclInit(NULL); - // if (ret != ACL_SUCCESS) { - // ERROR_LOG("acl init failed, errorCode = %d", static_cast(ret)); - // return FAILED; - // } - // INFO_LOG("acl init success"); - - // set device - ret = aclrtSetDevice(deviceId_); - if (ret != ACL_SUCCESS) { - ERROR_LOG("acl set device %d failed, errorCode = %d", deviceId_, static_cast(ret)); - return FAILED; - } - INFO_LOG("set device %d success", deviceId_); - - // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); - if (ret != ACL_SUCCESS) { - ERROR_LOG("acl create context failed, deviceId = %d, errorCode = %d", - deviceId_, static_cast(ret)); - return FAILED; - } - INFO_LOG("create context success"); - - // create stream - ret = aclrtCreateStream(&stream_); - if (ret != ACL_SUCCESS) { - ERROR_LOG("acl create stream failed, deviceId = %d, errorCode = %d", - deviceId_, static_cast(ret)); - return FAILED; - } - INFO_LOG("create stream success"); - - InitProcess(); - - return SUCCESS; -} - -void DvppCropProcess::DestroyResource() -{ - aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); - if (ret != ACL_SUCCESS) { - ERROR_LOG("destroy stream failed, errorCode = %d", static_cast(ret)); - } - stream_ = nullptr; - } - INFO_LOG("end to destroy stream"); - - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); - if (ret != ACL_SUCCESS) { - ERROR_LOG("destroy context failed, errorCode = %d", static_cast(ret)); - } - context_ = nullptr; - } - INFO_LOG("end to destroy context"); - - ret = aclrtResetDevice(deviceId_); - if (ret != ACL_SUCCESS) { - ERROR_LOG("reset device %d failed, errorCode = %d", deviceId_, static_cast(ret)); - } - INFO_LOG("end to reset device %d", deviceId_); - - ret = aclFinalize(); - if (ret != ACL_SUCCESS) { - ERROR_LOG("finalize acl failed, errorCode = %d", static_cast(ret)); - } - INFO_LOG("end to finalize acl"); -} diff --git a/src/util/crop_process.h b/src/util/crop_process.h deleted file mode 100644 index 54c5dae..0000000 --- a/src/util/crop_process.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef ___CROP_PROCESS_H__ -#define ___CROP_PROCESS_H__ - -#include -#include "acl/ops/acl_dvpp.h" -#include "../ai_platform/header.h" - -using namespace std; - -class DeviceMemory; - -typedef enum Result { - SUCCESS = 0, - FAILED = 1 -} Result; - -class DvppCropProcess { -public: - /** - * @brief Constructor - * @param [in] stream: stream - */ - DvppCropProcess(); - - /** - * @brief Destructor - */ - ~DvppCropProcess(); - -public: - /** - * @brief Process Batch Crop - * @return result - */ - Result ProcessBatchCrop(DeviceMemory *devMem, vector objs); - - /** - * @brief init reousce - * @return result - */ - Result init(int deviceId); - - -private: - /** - * @brief InitBatchCropInputDesc - * @return Result - */ - Result InitBatchCropInputDesc(DeviceMemory *devMem); - - /** - * @brief InitBatchCropOutputDesc - * @return Result - */ - Result InitBatchCropOutputDesc(vector objs); - - /** - * @brief DestroyBatchCropResource - */ - void DestroyBatchCropResource(); - - - void DestroyResource(); - - /** - * @brief init reousce - * @return result - */ - Result InitProcess(); - -private: - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; - - acldvppChannelDesc *dvppChannelDesc_; - acldvppResizeConfig *resizeConfig_; - - std::vector vecOutPtr_; - - acldvppBatchPicDesc *inputBatchPicDesc_; // vpc input desc - acldvppBatchPicDesc *outputBatchPicDesc_; // vpc output desc - - uint32_t inputBatchSize_; - uint32_t outputBatchSize_; -}; - -#endif // ___CROP_PROCESS_H__ \ No newline at end of file diff --git a/src/util/vpc_util.cpp b/src/util/vpc_util.cpp index a659d70..2ff6789 100644 --- a/src/util/vpc_util.cpp +++ b/src/util/vpc_util.cpp @@ -16,14 +16,10 @@ #include "vpc_util.h" #include "JpegUtil.h" #include "../decoder/interface/DeviceMemory.hpp" +#include "../common/logger.hpp" -#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) -#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args) -#define ERROR_LOG(fmt, args...) fprintf(stderr, "[ERROR] " fmt "\n", ##args) - - -void VPCUtil::vpc_crop_release() +void VPCUtil::release() { aclError ret; // ret = aclrtSetDevice(deviceId_); @@ -35,26 +31,26 @@ void VPCUtil::vpc_crop_release() if (stream_ != nullptr) { ret = aclrtDestroyStream(stream_); if (ret != ACL_SUCCESS) { - ERROR_LOG("destroy stream failed"); + LOG_ERROR("destroy stream failed"); } stream_ = nullptr; } - INFO_LOG("end to destroy stream"); + LOG_INFO("end to destroy stream"); if (context_ != nullptr) { ret = aclrtDestroyContext(context_); if (ret != ACL_SUCCESS) { - ERROR_LOG("destroy context failed"); + LOG_ERROR("destroy context failed"); } context_ = nullptr; } - INFO_LOG("end to destroy context"); + LOG_INFO("end to destroy context"); ret = aclrtResetDevice(deviceId_); if (ret != ACL_SUCCESS) { - ERROR_LOG("reset device failed"); + LOG_ERROR("reset device failed"); } - INFO_LOG("end to reset device is %d", deviceId_); + LOG_INFO("end to reset device is %d", deviceId_); } @@ -109,7 +105,7 @@ int VPCUtil::vpc_crop(acldvppPicDesc *input_pic_desc, video_object_info obj) return 0; } -int VPCUtil::vpc_crop_init(int32_t devId){ +int VPCUtil::init(int32_t devId){ deviceId_ = devId; aclError ret; @@ -140,10 +136,15 @@ static void check_coordinate(uint32_t& cropLeftOffset, uint32_t& cropRightOffset } -vector VPCUtil::vpc_crop_batch(DeviceMemory *devMem, vector objs){ +vector VPCUtil::crop_batch(DeviceMemory *devMem, vector objs){ vector vec_img_info; + const uint32_t outputBatchSize_ = objs.size(); + if(outputBatchSize_ <= 0){ + return vec_img_info; + } + /* 1.ACL initialization */ // aclInit(nullptr); aclError ret; @@ -161,18 +162,14 @@ vector VPCUtil::vpc_crop_batch(DeviceMemory *devMem, vectorgetHeightStride()); acldvppSetPicDescSize(vpcInputDesc_, devMem->getSize()); - const uint32_t outputBatchSize_ = objs.size(); // 输出 acldvppBatchPicDesc *outputBatchPicDesc_ = acldvppCreateBatchPicDesc(outputBatchSize_); if (outputBatchPicDesc_ == nullptr) { - ERROR_LOG("acldvppCreatePicDesc outBatchPicDesc failed"); + LOG_ERROR("acldvppCreatePicDesc outBatchPicDesc failed"); return vec_img_info; } vector vecOutPtr_; acldvppPicDesc *vpcOutputDesc = nullptr; - // acldvppRoiConfig cropAreas[outputBatchSize_]; - // std::unique_ptr - // cropAreas(new(std::nothrow) acldvppRoiConfig *[outputBatchSize_ * sizeof(acldvppRoiConfig *)]); acldvppRoiConfig *cropAreas[outputBatchSize_]; for (uint32_t i = 0; i < outputBatchSize_; i++) { video_object_info obj = objs[i]; @@ -199,7 +196,7 @@ vector VPCUtil::vpc_crop_batch(DeviceMemory *devMem, vector(ret)); + LOG_ERROR("acldvppMalloc failed, size = %u, errorCode = %d.", vpcOutBufferSize_, static_cast(ret)); // 释放之前成功的部分 再退出 for(int i = 0; i < vecOutPtr_.size(); i++){ if (vecOutPtr_[i] != nullptr){ @@ -304,7 +301,7 @@ vpc_img_info VPCUtil::vpc_devMem2vpcImg(DeviceMemory *devMem){ void *devBuffer = nullptr; auto ret = acldvppMalloc(&devBuffer, nBufferSize); if (ret != ACL_SUCCESS) { - ERROR_LOG("acldvppMalloc failed, size = %u, errorCode = %d.", nBufferSize, static_cast(ret)); + LOG_ERROR("acldvppMalloc failed, size = %u, errorCode = %d.", nBufferSize, static_cast(ret)); // 这里应释放之前成功的部分 再退出 return img_info; } diff --git a/src/util/vpc_util.h b/src/util/vpc_util.h index f0199d0..9ec96cb 100644 --- a/src/util/vpc_util.h +++ b/src/util/vpc_util.h @@ -27,15 +27,15 @@ class VPCUtil { public: int vpc_crop(acldvppPicDesc *input_pic_desc, video_object_info obj); - int vpc_crop_init(int32_t devId); + int init(int32_t devId); - vector vpc_crop_batch(DeviceMemory *devMem, vector objs); + vector crop_batch(DeviceMemory *devMem, vector objs); void vpc_img_release(vpc_img_info ); void vpc_imgList_release(vector& ); - void vpc_crop_release(); + void release(); vpc_img_info vpc_devMem2vpcImg(DeviceMemory *devMem); -- libgit2 0.21.4