From d708ccb3e30e89f83f664d6005da98d794857363 Mon Sep 17 00:00:00 2001 From: cmhu <2657262686@qq.com> Date: Fri, 21 Feb 2025 15:47:53 +0800 Subject: [PATCH] 增加日志 --- algorithm/vehicle_analysis/vehicle_analysis.h | 17 ++++++----------- build/jni/Makefile | 4 ++++ src/PicAnalysis.cpp | 15 ++++++++++++++- src/PicAnalysis.h | 5 +++++ src/ai_engine_module/VehicleAnalysis.cpp | 191 ++++++++++++++--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- src/ai_engine_module/VehicleAnalysis.h | 13 +++++++++++-- src/demo/main.cpp | 26 ++++++++++++++++---------- src/utils/DeviceUtil.hpp | 8 +++++--- src/utils/PathUtils.hpp | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 162 insertions(+), 204 deletions(-) create mode 100644 src/utils/PathUtils.hpp diff --git a/algorithm/vehicle_analysis/vehicle_analysis.h b/algorithm/vehicle_analysis/vehicle_analysis.h index 4297155..d027dd2 100644 --- a/algorithm/vehicle_analysis/vehicle_analysis.h +++ b/algorithm/vehicle_analysis/vehicle_analysis.h @@ -18,13 +18,10 @@ #define VEHICLEANALYSIS_API __attribute__ ((visibility ("default"))) #endif -#ifdef __cplusplus -extern "C" -{ -#endif #include "sy_common.h" //通用数据结构体定义 #include "vehicle_result.h" //车辆分析结果结构体定义 +#include #ifndef __VEHICLE_INFO__ @@ -109,7 +106,11 @@ extern "C" float vc_thresld=0.5; //车颜色阈值 char* dbPath; //车型识别车型数据路路径 <当前仅支持英文路径> char* models_Path; //所有模型的路径 <当前仅支持英文路径>//20210615 - + + int log_level{2}; + int log_days{30}; //日志保存周期 + std::string log_path{"logs"}; //日志文件路径 + unsigned long log_mem{64 * 1024 * 1024}; //每个日志最大大小 }va_param; #endif @@ -188,11 +189,5 @@ extern "C" *************************************************************************/ VEHICLEANALYSIS_API float va_compute_similarity_byvafeature(void * handle , float *Afea, float *Bfea, int featuresize); - - -#ifdef __cplusplus -}; -#endif - #endif diff --git a/build/jni/Makefile b/build/jni/Makefile index c60fc5a..c9f0c44 100644 --- a/build/jni/Makefile +++ b/build/jni/Makefile @@ -12,6 +12,7 @@ BIN_ROOT = $(PROJECT_ROOT)/bin ACL_PATH = $(ASCEND_AICPU_PATH)/acllib ALGORITHM_PATH = $(PROJECT_ROOT)/algorithm +SPDLOG_ROOT = $(PROJECT_ROOT)/3rdparty/spdlog-1.9.2/release TARGET = $(PROJECT_ROOT)/bin/libvillage_ascend_arm_jni.so @@ -27,8 +28,10 @@ INCLUDE= -I $(SRC_ROOT)/include \ -I $(THIRDPARTY_ROOT)/opencv_4_1/include \ -I$(PROJECT_ROOT)/src/common \ -I$(PROJECT_ROOT)/src/common/dvpp \ + -I$(PROJECT_ROOT)/src/utils \ -I$(ACL_PATH)/include \ -I$(ALGORITHM_PATH)/vehicle_analysis \ + -I$(SPDLOG_ROOT)/include \ LIBSPATH= -L $(BIN_ROOT) -lvillage_ascend_arm \ @@ -36,6 +39,7 @@ LIBSPATH= -L $(BIN_ROOT) -lvillage_ascend_arm \ -L $(BIN_ROOT) -l:libcurl.so.4 -lssl -lcrypto \ -L $(ACL_PATH)/lib64 -l:libascendcl.so \ -L $(ACL_PATH)/lib64 -l:libacl_dvpp.so \ + -L $(SPDLOG_ROOT)/lib -lspdlog \ SRCS:=$(wildcard $(SRC_ROOT)/*.cpp) diff --git a/src/PicAnalysis.cpp b/src/PicAnalysis.cpp index 0a1e66f..3a7d795 100644 --- a/src/PicAnalysis.cpp +++ b/src/PicAnalysis.cpp @@ -27,11 +27,22 @@ int PicAnalysis::init(VillageParam param) { LOG_INFO("log_days: {}", param.log_days); LOG_INFO("log_path: {}", log_path); + LOG_INFO("编译时间:{} {}", __DATE__, __TIME__); + LOG_INFO("execute dir:{}", helpers::PathUtils::GetCmdDir()); + int dev_id = param.dev_id; int ret = SY_FAILED; - ret = m_vehicle_analysis.init(dev_id, param.sdk_path, 16); + VehicleAnalysisParam vehicle_analysis_param; + vehicle_analysis_param.devId = dev_id; + vehicle_analysis_param.sdk_root = param.sdk_path; + vehicle_analysis_param.max_batch_size = 16; + vehicle_analysis_param.log_level = param.log_level; + vehicle_analysis_param.log_path = param.log_path; + vehicle_analysis_param.log_days = param.log_days; + vehicle_analysis_param.log_mem = param.log_mem; + ret = m_vehicle_analysis.init(vehicle_analysis_param); if(0 != ret){ return -1; } @@ -186,6 +197,8 @@ vector PicAnalysis::analysis_img(vector vec_img){ LOG_INFO("vec_img size:{}", vec_img.size()); + std::lock_guard l(m_single_mtx); + vector vec_result; const int batch_size = vec_img.size(); diff --git a/src/PicAnalysis.h b/src/PicAnalysis.h index 150b125..d42c67f 100644 --- a/src/PicAnalysis.h +++ b/src/PicAnalysis.h @@ -9,9 +9,12 @@ #include "./ai_engine_module/RoadSegAnalysis.h" #include "./utils/CropUtil.h" +#include "./utils/PathUtils.hpp" #include "village_inc.h" +#include + using namespace std; @@ -47,6 +50,8 @@ private: RoadSegAnalysis m_road_seg_algorithm; CropUtil m_crop_util; + + std::mutex m_single_mtx; }; diff --git a/src/ai_engine_module/VehicleAnalysis.cpp b/src/ai_engine_module/VehicleAnalysis.cpp index 668d08d..40757b1 100644 --- a/src/ai_engine_module/VehicleAnalysis.cpp +++ b/src/ai_engine_module/VehicleAnalysis.cpp @@ -9,8 +9,9 @@ VehicleAnalysis::~VehicleAnalysis() { release(); } -int VehicleAnalysis::init(int devId, std::string sdk_root, int max_batch_size) { - +int VehicleAnalysis::init(VehicleAnalysisParam tParam) { + + va_param param; param.vehicle_detect_config= SY_CONFIG_OPEN; //1.开启车检测 SY_CONFIG_CLOSE SY_CONFIG_OPEN param.vehicle_recg_config= SY_CONFIG_OPEN; //4.开启车型识别 param.vehicle_recg_supplement_config= SY_CONFIG_OPEN; //4.开启车型识别补充识别 @@ -22,7 +23,7 @@ int VehicleAnalysis::init(int devId, std::string sdk_root, int max_batch_size) { param.vehicle_color_config= SY_CONFIG_OPEN; //3.开启车颜色识别 param.vehicle_feature_config= SY_CONFIG_OPEN; //8.开启车辆特征提取 - param.gpuid=devId; + param.gpuid=tParam.devId; //车检测参数 //param.vehicle_det_param.process_min_l = 720;// 720; @@ -52,12 +53,17 @@ int VehicleAnalysis::init(int devId, std::string sdk_root, int max_batch_size) { //车颜色阈值 param.vc_thresld = 0.5; - string dbPath = sdk_root + "/models/vehicle_analysis/db/vr_h0725x210605_r191230.db"; - string models_path = sdk_root + "/models/vehicle_analysis/"; + string dbPath = tParam.sdk_root + "/models/vehicle_analysis/db/vr_h0725x210605_r191230.db"; + string models_path = tParam.sdk_root + "/models/vehicle_analysis/"; //车型参数 param.dbPath= (char*)dbPath.data(); param.models_Path= (char*)models_path.data(); //所有模型的地址 + param.log_level = tParam.log_level; + param.log_path = tParam.log_path; + param.log_days = tParam.log_days; + param.log_mem = tParam.log_mem; + LOG_INFO("va_init start"); // 内部有 ctx int ret = va_init(&m_handle, param); @@ -83,180 +89,13 @@ va_result * VehicleAnalysis::detect(vector vec_img) { result[b].info[c].vehicle_pendant_det_res.vpd_res=new v_pendant_d_info[300]; } } - - int ret = va_batch(m_handle, vec_img.data(), batch_size, result); - vector vec_result; + LOG_INFO("va_batch start"); - for (int b = 0; b < batch_size; b++) - { - for(int c=0;c0) - { - float vehicle_body_detect_res_score = result_info.vehicle_body_detect_res.score; - int x1=result_info.vehicle_body_detect_res.rect.left_; - int y1=result_info.vehicle_body_detect_res.rect.top_; - int x2=result_info.vehicle_body_detect_res.rect.left_+result_info.vehicle_body_detect_res.rect.width_; - int y2=result_info.vehicle_body_detect_res.rect.top_+result_info.vehicle_body_detect_res.rect.height_; - std::cout << " vehicle_body_detect_res_score:" <0) - // { - // float vehicle_detect_res_score = result_info.vehicle_detect_res.score; - // int x1=result_info.vehicle_detect_res.rect.left_; - // int y1=result_info.vehicle_detect_res.rect.top_; - // int x2=result_info.vehicle_detect_res.rect.left_+result_info.vehicle_detect_res.rect.width_; - // int y2=result_info.vehicle_detect_res.rect.top_+result_info.vehicle_detect_res.rect.height_; - // std::cout << " vehicle_detect_res_score:" <0) - // { - // float vehicle_win_detect_res_score = result_info.vehicle_win_detect_res.score; - // int x1=result_info.vehicle_win_detect_res.rect.left_; - // int y1=result_info.vehicle_win_detect_res.rect.top_; - // int x2=result_info.vehicle_win_detect_res.rect.left_+result_info.vehicle_win_detect_res.rect.width_; - // int y2=result_info.vehicle_win_detect_res.rect.top_+result_info.vehicle_win_detect_res.rect.height_; - // std::cout << " vehicle_win_detect_res_score:" < name_score_thre) - { - analysis_result.vehicle_type = vehicle_type_; - } - } else { - analysis_result.vehicle_type = str_vehicle_type; - } - std::cout << "vehicle_type_: " << analysis_result.vehicle_type << std::endl; - - //5.VP车牌检测识别 - if(param.vehicle_plate_det_recg_config==SY_CONFIG_OPEN && (shot_type==0 || shot_type==1)) - { - vplate_results plate_result = result_info.vehicle_plate_det_recg_res; - std::cout << " car plate info:"<< endl; - int special_type=plate_result.special_type;//常规车牌、临时车牌、低速车牌。0-common,1-temporary_license_plate,2-low_speed_license_plate. - std::cout << " special_type:" << special_type<< std::endl; - - float detect_score=plate_result.detect_score; - if(detect_score>0.3) - { - //车牌识别结果 - std::string plate_recg=""; - std::string character_prob; - //if(num_score>0.99)//车牌识别建议置信度阈值0.99 - { - - for (int m = 0; m < PLATENUM; m++) - { - plate_recg=plate_recg + plate_result.recg[m].character; - character_prob += std::string(plate_result.recg[m].character) + "-" + std::to_string(plate_result.recg[m].maxprob) + ","; - } - } - - analysis_result.plate_det_score = plate_result.detect_score; - analysis_result.plate_rect = plate_result.rect;//车牌检测坐标 - analysis_result.plate_type = plate_result.type; //车牌类型:0-单排蓝色 1-单排黄色 2-单排白色 3-单排黑色 4-双排黄色 5-双排白色 6-新能源黄绿色 7-新能源白绿色 - analysis_result.plate_num = plate_recg; - analysis_result.character_prob = character_prob; - analysis_result.plate_num_score = plate_result.num_score;//识别置信度 - // analysis_result.special_type; //常规车牌、临时车牌、低速车牌。0-common,1-temporary_license_plate,2-low_speed_license_plate. - } - - } - - //6.车属性结果: - if(param.vehicle_pendant_det_config==SY_CONFIG_OPEN && (shot_type==0|| shot_type==4)) - { - std::cout << " vehicle_pendant_det_res info:"<< endl; - int vpd_num = result_info.vehicle_pendant_det_res.count; - //std::cout << vpd_num<< std::endl; - std::cout << " vpd_num:"<= 3){ - analysis_result.motor_manned = 1; - } - } - } - } - //delete result // for(int b=0;b vec_img) { // { // if(result[b].info[c].vehicle_pendant_det_res.vpd_res!=NULL) // delete[] result[b].info[c].vehicle_pendant_det_res.vpd_res; - // } // if(result[b].info!=NULL){ // delete[] result[b].info; // } - // } // if(result!=NULL){ diff --git a/src/ai_engine_module/VehicleAnalysis.h b/src/ai_engine_module/VehicleAnalysis.h index 2d1a98e..0c0b972 100644 --- a/src/ai_engine_module/VehicleAnalysis.h +++ b/src/ai_engine_module/VehicleAnalysis.h @@ -11,6 +11,16 @@ struct DriverInfo { int driver_color; }; +struct VehicleAnalysisParam { + int devId; + std::string sdk_root; + int max_batch_size; + int log_level{2}; + int log_days{30}; //日志保存周期 + std::string log_path{"logs"}; //日志文件路径 + unsigned long log_mem{64 * 1024 * 1024}; //每个日志最大大小 +}; + struct VehicleAnalysisResult{ int shot_type; sy_rect vehicle_rect; @@ -38,7 +48,7 @@ public: VehicleAnalysis(/* args */); ~VehicleAnalysis(); - int init(int devId, std::string sdk_root, int max_batch_size); + int init(VehicleAnalysisParam tParam); va_result* detect(vector vec_img); @@ -49,7 +59,6 @@ private: private: void* m_handle{nullptr}; - va_param param; float m_threshold; int m_max_batchsize{1}; diff --git a/src/demo/main.cpp b/src/demo/main.cpp index 674d0a0..f34e2a5 100644 --- a/src/demo/main.cpp +++ b/src/demo/main.cpp @@ -72,37 +72,43 @@ int main() { vector vec_path; - GetFileNames("/home/share/data/villiage_test/test", vec_path); + GetFileNames("/home/share/data/villiage_test/test3", vec_path); - vec_path = GetFilesFromFile("./files1.log"); + // vec_path = GetFilesFromFile("./files1.log"); // { // string path = "./img/test_road2.jpg"; // vec_path.push_back(path); // } - int last_recoder = 0; + size_t last_recoder = 0; int index = 0; + int max_value = vec_path.size(); while (true) { - for (size_t i = 0; i < vec_path.size(); i++) + // for (size_t i = 0; i < vec_path.size(); i++) { + int first_index = rand() % max_value; + int second_index = rand() % max_value; + vector vec_file_name; - vec_file_name.push_back(vec_path[i]); - vec_file_name.push_back(vec_path[i]); + vec_file_name.push_back(vec_path[first_index]); + vec_file_name.push_back(vec_path[second_index]); std::vector result = village_pic_analysis_file(vaHandle, vec_file_name); - show_result(result, vec_path); + // show_result(result, vec_path); - int cur_recoder = GetDeviceMem(0); + size_t cur_recoder = GetDeviceMem(0); if (cur_recoder > last_recoder) { - LOG_INFO("memesize cur: {} last:{} file:{}", cur_recoder, last_recoder, vec_path[i]); + // LOG_INFO("memesize cur: {} last:{} file:{} file:{}", cur_recoder, last_recoder, vec_path[first_index], vec_path[second_index]); + LOG_INFO("memesize cur: {} last:{} ", cur_recoder, last_recoder); } last_recoder = cur_recoder; } - LOG_INFO("第{}次完成:{}", index, vec_path.size()); + // LOG_INFO("第{}次完成:{}", index, vec_path.size()); + LOG_INFO("第{}次完成", index); index++; } diff --git a/src/utils/DeviceUtil.hpp b/src/utils/DeviceUtil.hpp index caa65bd..7b662fa 100644 --- a/src/utils/DeviceUtil.hpp +++ b/src/utils/DeviceUtil.hpp @@ -8,7 +8,7 @@ #include "logger.hpp" -int GetDeviceMem(int dev_id) { +size_t GetDeviceMem(int dev_id) { aclrtSetDevice(dev_id); size_t free = 0; size_t total = 0; @@ -18,9 +18,11 @@ int GetDeviceMem(int dev_id) { LOG_ERROR("aclrtGetMemInfo error!"); return -1; } - LOG_INFO("device {} memory usage/total:{}/{}", dev_id, total - free, total); - return total - free; + size_t usage = (total - free)/(1024*1024); + LOG_INFO("device {} memory usage/total:{}/{}", dev_id, usage, total/(1024*1024)); + + return usage; } diff --git a/src/utils/PathUtils.hpp b/src/utils/PathUtils.hpp new file mode 100644 index 0000000..40369cd --- /dev/null +++ b/src/utils/PathUtils.hpp @@ -0,0 +1,87 @@ +#ifndef _PATH_UTILS_H_ +#define _PATH_UTILS_H_ + +#include + +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + +#include + +#elif defined(linux) || defined(__linux) + +#include +#include +#include +#include +#include + +#endif // WINDOWS + +namespace helpers +{ + namespace PathUtils { + + #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + static HMODULE GetSelfModuleHandle() + { + MEMORY_BASIC_INFORMATION mbi; + return ( + (::VirtualQuery(GetSelfModuleHandle, &mbi, sizeof(mbi)) != 0) + ? (HMODULE)mbi.AllocationBase : NULL + ); + } + + static std::string GetCurrentProgramDir() + { + std::string strCurrentPath = ""; + char curDirector[260] = { 0 }; + GetModuleFileName(GetSelfModuleHandle(), curDirector, 260); + strCurrentPath = curDirector; + + size_t nameStart = strCurrentPath.rfind("\\"); + strCurrentPath = strCurrentPath.substr(0, nameStart + 1); + return strCurrentPath; + } + + #elif defined(linux) || defined(__linux) + static std::string GetCurrentProgramDir() + { + std::string strCurrentPath = ""; + char szCurWorkPath[256]; + memset(szCurWorkPath, '\0', 256); + int nRet = readlink("/proc/self/exe", szCurWorkPath, 256); + if (nRet > 256 || nRet < 0) + { + return strCurrentPath; + } + + for (int i = nRet; i > 0; i--) + { + if (szCurWorkPath[i] == '/' || szCurWorkPath[i] == '\\') + { + szCurWorkPath[i] = '\0'; + break; + } + } + + strCurrentPath = szCurWorkPath; + + return strCurrentPath; + } + + static std::string GetCmdDir() { + char cwd[PATH_MAX]; + if (getcwd(cwd, sizeof(cwd)) != nullptr) { + std::cout << "当前工作目录是: " << cwd << std::endl; + } else { + std::cerr << "错误: 无法获取当前工作目录。" << std::endl; + } + return cwd; + } + #endif + + } + +} + +#endif // !_PATH_UTILS_H_ -- libgit2 0.21.4