diff --git a/algorithm/hcp2/hcp/human_car_parsing.h b/algorithm/hcp2/hcp/human_car_parsing.h new file mode 100644 index 0000000..0ad48b4 --- /dev/null +++ b/algorithm/hcp2/hcp/human_car_parsing.h @@ -0,0 +1,131 @@ +/******************************************************************************************* +* Version: human_car_parsing_v0.0.0 +* CopyRight: 中科院自动化研究所模式识别实验室图像视频组 +* UpdateDate: 20190402 +* Content: 人骑车语义分析 +********************************************************************************************/ + +#ifndef HUMANCARPARSING_H_ +#define HUMANCARPARSING_H_ + +#ifdef _MSC_VER +#ifdef HUMANCARPARSING_EXPORTS +#define HUMANCARPARSING_API __declspec(dllexport) +#else +#define HUMANCARPARSING_API __declspec(dllimport) +#endif +#else +#define HUMANCARPARSING_API __attribute__ ((visibility ("default"))) +#endif + +#include "sy_common.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +#ifndef __CLASSIFY_OBJ_RESULT__ +#define __CLASSIFY_OBJ_RESULT__ + typedef struct classify_obj_res //分类结果结构体 + { + int res_index; //分类结果 + float res_prob; //分类结构体 + classify_obj_res() : res_index(0), res_prob(0) {}; + } classify_obj_res; +#endif + +#ifndef __HCF_FEA_RES__ +#define __HCF_FEA_RES__ +const int HCF_FEATURE_SIZE = 1280; +#endif + +#ifndef __HCP_RESULT__ +#define __HCP_RESULT__ +const int HCP_ATTRI_INDEX_SIZE = 14; +typedef struct hcp_analysis_result +{ + classify_obj_res res_objs[HCP_ATTRI_INDEX_SIZE]; //分类结果 + float feature[HCF_FEATURE_SIZE]; //人骑车特征 +} hcp_analysis_result; +#endif + +#ifndef __HCP_PARAM__ +#define __HCP_PARAM__ + typedef struct hcp_param + { + int devId; //ָ指定显卡id + char* modelNames; + // int mode; //运行模式(DEVICE_GPU / DEVICE_CPU) + // int gpuid; //ָ指定显卡id + // int engine; //ָ����������(ENGINE_MCAFFE2 / ENGINE_TENSORRT) + // int max_batch; //ָ��trt���batch�� + // char* serialize_file; + // hcp_param() :mode(DEVICE_GPU), gpuid(0) {}; + }hcp_param; +#endif + + /************************************************************************* + * FUNCTION: hcp_init + * PURPOSE: 初始化 + * PARAM: + [out] tools - 句柄 + [in] param - 初始化参数 + * RETURN: + [out] int - 初始化是否成功(SUCCEEDED表示成功,FAILED表示失败) + * NOTES: + *************************************************************************/ + HUMANCARPARSING_API int hcp_init(void ** handle, hcp_param param); + + /************************************************************************* + * FUNCTION: hcp_process + * PURPOSE: 人骑车属性检测 + * PARAM: + [in] tools - 句柄 + [in] img_data - 检测图像数据 + [in] result - 检测结果 + * RETURN: + [out] int - 检测是否成功(SUCCEEDED表示成功,FAILED表示失败) + * NOTES: + *************************************************************************/ + // HUMANCARPARSING_API int hcp_process(void * handle, sy_img img_data, hcp_analysis_result * result); + + /************************************************************************* + * FUNCTION: hcp_batch + * PURPOSE: 人骑车属性检测 + * PARAM: + [in] tools - 句柄 + [in] img_data_array - 检测图像数据 + [in] batch_size - 检测图像数目 + [in] result - 检测结果 + * RETURN: + [out] int - 检测是否成功(SUCCEEDED表示成功,FAILED表示失败) + * NOTES: + *************************************************************************/ + HUMANCARPARSING_API int hcp_batch(void * handle, sy_img * img_data_array, int batch_size, hcp_analysis_result * result); + + + /************************************************************************* + * FUNCTION: hcp_release + * PURPOSE: 资源释放 + * PARAM: + [in] tools - 处理句柄 + * RETURN: NULL + * NOTES: + *************************************************************************/ + HUMANCARPARSING_API void hcp_release(void ** handle); + + /************************************************************************* + * FUNCTION: hcp_get_version + * PURPOSE: + * PARAM: NULL + * RETURN: 版本号 + * NOTES: + *************************************************************************/ + HUMANCARPARSING_API const char * hcp_get_version(); + +#ifdef __cplusplus +}; +#endif + +#endif diff --git a/algorithm/hcp2/hcp/libhcp.so b/algorithm/hcp2/hcp/libhcp.so new file mode 100644 index 0000000..089b850 --- /dev/null +++ b/algorithm/hcp2/hcp/libhcp.so diff --git a/algorithm/hcp2/hcp/models/hcp211008_310p.om b/algorithm/hcp2/hcp/models/hcp211008_310p.om new file mode 100644 index 0000000..854c5fb --- /dev/null +++ b/algorithm/hcp2/hcp/models/hcp211008_310p.om diff --git a/algorithm/hcp2/hcp_test/Makefile b/algorithm/hcp2/hcp_test/Makefile new file mode 100644 index 0000000..726959c --- /dev/null +++ b/algorithm/hcp2/hcp_test/Makefile @@ -0,0 +1,52 @@ +CC = gcc +XX = c++ + +TARGET = start + +PROJ_ALL_PATH = $(PWD)/../.. +CUR_PROJ_PATH = $(PWD) +OPENCV_PATH = $(PROJ_ALL_PATH)/third_party/opencv_4_1 +ACL_PATH = $(ASCEND_AICPU_PATH)/acllib + +INCLUDES = -I$(PROJ_ALL_PATH)/common \ + -I$(ACL_PATH)/include \ + -I$(PROJ_ALL_PATH)/common/dvpp \ + -I$(CUR_PROJ_PATH)/../hcp \ + -I$(OPENCV_PATH)/include \ + -I$(OPENCV_PATH)/include/opencv2 \ + +CFLAGS = -O2 -std=c++11 $(INCLUDES) -DENABLE_DVPP_INTERFACE -D_GLIBCXX_USE_CXX11_ABI=0 + +local_shared_libs_dirs := \ + $(ACL_PATH)/lib64 \ + $(OPENCV_PATH)/lib \ + $(CUR_PROJ_PATH)/../hcp \ + +local_shared_libs := \ + ascendcl \ + acl_dvpp \ + opencv_world \ + hcp \ + +SHARED_LIBRARIES := $(foreach shared_lib, $(local_shared_libs), -l$(shared_lib)) +#SHARED_LIBRARIES_DIRS := $(foreach shared_lib_dir, $(local_shared_libs_dirs), -L$(shared_lib_dir) -Wl,-z,relro,-z,now,-z,noexecstack,-rpath-link,$(shared_lib_dir)) +SHARED_LIBRARIES_DIRS := $(foreach shared_lib_dir, $(local_shared_libs_dirs), -L$(shared_lib_dir) -Wl,-z,relro,-z,now,-z,noexecstack,-rpath,$(shared_lib_dir)) + +SRCS := $(wildcard $(CUR_PROJ_PATH)/*.cpp) +SRCS += $(wildcard $(PROJ_ALL_PATH)/common/dvpp/*.cpp) + +DIRS := $(notdir $(SRCS)) +OBJS := $(patsubst %cpp, %o, $(DIRS)) + +all: $(TARGET) + +$(TARGET):$(OBJS) + $(XX) $(CFLAGS) -o $@ $^ $(SHARED_LIBRARIES_DIRS) $(SHARED_LIBRARIES) +%.o:$(CUR_PROJ_PATH)/%.cpp + $(XX) $(CFLAGS) -c $< +%.o:$(PROJ_ALL_PATH)/common/dvpp/%.cpp + $(XX) $(CFLAGS) -c $< + +clean: + @rm -f $(TARGET) + @rm -f $(OBJS) diff --git a/algorithm/hcp2/hcp_test/test.cpp b/algorithm/hcp2/hcp_test/test.cpp new file mode 100644 index 0000000..96fb0ce --- /dev/null +++ b/algorithm/hcp2/hcp_test/test.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +#include "human_car_parsing.h" +#include "opencv2/opencv.hpp" +#include "opencv2/imgcodecs/legacy/constants_c.h" +#include "opencv2/imgproc/types_c.h" +#include "time.h" +#include "sys/time.h" +#include "sy_errorinfo.h" +#include "utils.h" +#include "dvpp_process.h" + +using namespace std; +using namespace cv; +#include +#include + + +double msecond() { + struct timeval tv; + gettimeofday(&tv, 0); + return (tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0); +} +void getAllNm(std::string pthNm, std::vector& fileList) +{ + DIR *dir; + struct dirent *ptr; + dir = opendir(pthNm.c_str()); ///open the dir + int filenum = 0; + while((ptr = readdir(dir)) != NULL) ///read the list of this dir + { + // char* to string + std::string curNm = ptr->d_name; + if(curNm != "." && curNm != "..") + { + filenum++; + fileList.push_back(curNm); + //printf("file %d name: %s\n", filenum, curNm.c_str()); + } + } + closedir(dir); +} + + +int main() { + cout << hcp_get_version() << endl; + const char* img_file_path = "../../../data/hcp/"; + string saveimagepath= "../../../data/result/"; + + hcp_param param; + param.modelNames = "../hcp/models/hcp211008_310p.om"; + // param.modelNames = "hcp0629x.om"; + param.devId = 0; + + void* handle = nullptr; + cout << "init start " << endl; + ACL_CALL(aclInit(nullptr), ACL_SUCCESS, SY_FAILED); + ACL_CALL(aclrtSetDevice(param.devId), ACL_SUCCESS, SY_FAILED); + aclrtContext ctx; + ACL_CALL(aclrtCreateContext(&ctx, param.devId), ACL_SUCCESS, SY_FAILED); + aclrtStream stream = nullptr; + ACL_CALL(aclrtCreateStream(&stream), ACL_SUCCESS, SY_FAILED); + DvppProcess* dvpp = new DvppProcess(); + dvpp->InitResource(stream); + int ret = hcp_init(&handle, param); + if (ret == 0) { + cout << "init success " << endl; + + std::vector fileList; + + getAllNm(img_file_path,fileList); + if (fileList.empty()) throw std::logic_error("No suitable images were found"); + + for (auto & file : fileList) { + string filename = img_file_path + file; + cout << "img path: " << filename << endl; + + const int batchsize = 1; + + Mat cvImg = imread(filename.c_str()); + sy_img imgs[batchsize]; + + ImageData src[batchsize], dvpp_data[batchsize]; + for (int b = 0; b < batchsize; b++) { + Utils::ReadImageFile(src[b], filename); //将二进制图像读入内存,并读取宽高信息 + ACL_CALL(dvpp->CvtJpegToYuv420sp(dvpp_data[b], src[b]), SY_SUCCESS, SY_FAILED); //解码 + imgs[b].w_ = dvpp_data[b].width; + imgs[b].h_ = dvpp_data[b].height; + imgs[b].data_ = dvpp_data[b].data.get(); + } + + hcp_analysis_result * results = new hcp_analysis_result[batchsize]; + int ret = hcp_batch(handle, imgs, batchsize, results); + if (SY_SUCCESS != ret) { + printf("HumanCarParse process failed!"); + return SY_FAILED; + } + + for(int batchIdx = 0;batchIdx +#include +#include +#include "human_parsing.h" +#include "opencv2/opencv.hpp" +#include "opencv2/imgcodecs/legacy/constants_c.h" +#include "opencv2/imgproc/types_c.h" +#include "time.h" +#include "sys/time.h" +#include "sy_errorinfo.h" +#include "utils.h" +#include "dvpp_process.h" + +using namespace std; +using namespace cv; +#include +#include + +double msecond() { + struct timeval tv; + gettimeofday(&tv, 0); + return (tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0); +} + + +void getAllNm(std::string pthNm, std::vector& fileList) +{ + DIR *dir; + struct dirent *ptr; + dir = opendir(pthNm.c_str()); ///open the dir + int filenum = 0; + while((ptr = readdir(dir)) != NULL) ///read the list of this dir + { + // char* to string + std::string curNm = ptr->d_name; + if(curNm != "." && curNm != "..") + { + filenum++; + fileList.push_back(curNm); + //printf("file %d name: %s\n", filenum, curNm.c_str()); + } + } + closedir(dir); +} + +int main() { + cout << hp_get_version() << endl; + const char* img_file_path = "../../../data/hp/"; + string saveimagepath= "../../../data/result/"; + + hp_param param; + param.modelNames = "../hp/models/hp220908_310p.om"; + param.devId = 0; + + void* handle = nullptr; + cout << "init start " << endl; + ACL_CALL(aclInit(nullptr), ACL_SUCCESS, SY_FAILED); + ACL_CALL(aclrtSetDevice(param.devId), ACL_SUCCESS, SY_FAILED); + aclrtContext ctx; + ACL_CALL(aclrtCreateContext(&ctx, param.devId), ACL_SUCCESS, SY_FAILED); + aclrtStream stream = nullptr; + ACL_CALL(aclrtCreateStream(&stream), ACL_SUCCESS, SY_FAILED); + DvppProcess* dvpp = new DvppProcess(); + dvpp->InitResource(stream); + int ret = hp_init(&handle, param); + if (ret == 0) { + cout << "init success " << endl; + + std::vector fileList; + + getAllNm(img_file_path,fileList); + if (fileList.empty()) throw std::logic_error("No suitable images were found"); + + for (auto & file : fileList) { + string filename = img_file_path + file; + cout << "img path: " << filename << endl; + + const int batchsize = 1; + + Mat cvImg = imread(filename.c_str()); + sy_img imgs[batchsize]; + + ImageData src[batchsize], dvpp_data[batchsize]; + for (int b = 0; b < batchsize; b++) { + Utils::ReadImageFile(src[b], filename); //将二进制图像读入内存,并读取宽高信息 + ACL_CALL(dvpp->CvtJpegToYuv420sp(dvpp_data[b], src[b]), SY_SUCCESS, SY_FAILED); //解码 + imgs[b].w_ = dvpp_data[b].width; + imgs[b].h_ = dvpp_data[b].height; + imgs[b].data_ = dvpp_data[b].data.get(); + } + + hp_analysis_res * results = new hp_analysis_res[batchsize]; + int ret = hp_batch(handle, imgs, batchsize, results); + if (SY_SUCCESS != ret) { + printf("HumanCarParse process failed!"); + return SY_FAILED; + } + + for(int batchIdx = 0;batchIdxInitResource(stream); @@ -43,7 +56,7 @@ int PicAnalysis::init(int dev_id) { int PicAnalysis::analysis_sync(vector vec_file_path){ - ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_ERROR_NONE, SY_FAILED); + ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_SUCCESS, SY_FAILED); const int batch_size = vec_file_path.size(); @@ -87,13 +100,19 @@ int PicAnalysis::analysis_sync(vector vec_file_path){ } m_clothes_algorithm.detect(vec_img); + + m_human_algorithm.detect(vec_img); + + m_human_car_algorithm.detect(vec_img); - + LOG_INFO("analysis_sync finished!"); + return 0; } int PicAnalysis::release() { - ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_ERROR_NONE, SY_FAILED); + + ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_SUCCESS, SY_FAILED); delete m_dvpp; m_dvpp = nullptr; diff --git a/src/PicAnalysis.h b/src/PicAnalysis.h index a85155d..fa71368 100644 --- a/src/PicAnalysis.h +++ b/src/PicAnalysis.h @@ -2,6 +2,8 @@ #include "./ai_engine_module/VehicleAnalysis.h" #include "./ai_engine_module/VehicleHeadTail.h" #include "./ai_engine_module/VidClothes.h" +#include "./ai_engine_module/HumanAnalysis.h" +#include "./ai_engine_module/HumanCarAnalysis.h" using namespace std; @@ -27,6 +29,8 @@ private: VehicleAnalysis m_vehicle_analysis; VehicleHeadTail m_head_tail_algorithm; VidClothes m_clothes_algorithm; + HumanAnalysis m_human_algorithm; + HumanCarAnalysis m_human_car_algorithm; }; diff --git a/src/ai_engine_module/HumanAnalysis.cpp b/src/ai_engine_module/HumanAnalysis.cpp new file mode 100644 index 0000000..3504163 --- /dev/null +++ b/src/ai_engine_module/HumanAnalysis.cpp @@ -0,0 +1,76 @@ +#include "HumanAnalysis.h" + +HumanAnalysis::HumanAnalysis(/* args */) +{ +} + +HumanAnalysis::~HumanAnalysis() +{ + release(); +} + +int HumanAnalysis::init(int devId){ + ACL_CALL(aclrtCreateContext(&ctx, devId), SY_SUCCESS, SY_FAILED); + ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED); + + hp_param param; + param.modelNames = "./models/hp/hp220908_310p.om"; + param.devId = devId; + + cout << "hp_init start " << endl; + int ret = hp_init(&m_handle, param); + if (ret != 0) { + return -1; + } + + cout << "hp_init success " << endl; + + return SY_SUCCESS; +} + +int HumanAnalysis::detect(vector vec_img){ + + ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED); + + const int batchsize = vec_img.size(); + hp_analysis_res * results = new hp_analysis_res[batchsize]; + + int ret = SY_FAILED; + + do + { + ret = hp_batch(m_handle, vec_img.data(), batchsize, results); + if (SY_SUCCESS != ret) { + printf("vidclothesClassification process failed!"); + break; + } + + for(int batchIdx = 0;batchIdx vec_img); + +private: + int release(); + +private: + void* m_handle{nullptr}; + aclrtContext ctx{nullptr}; +}; + diff --git a/src/ai_engine_module/HumanCarAnalysis.cpp b/src/ai_engine_module/HumanCarAnalysis.cpp new file mode 100644 index 0000000..2be2cbf --- /dev/null +++ b/src/ai_engine_module/HumanCarAnalysis.cpp @@ -0,0 +1,76 @@ +#include "HumanCarAnalysis.h" + +HumanCarAnalysis::HumanCarAnalysis(/* args */) +{ +} + +HumanCarAnalysis::~HumanCarAnalysis() +{ + release(); +} + +int HumanCarAnalysis::init(int devId){ + ACL_CALL(aclrtCreateContext(&ctx, devId), SY_SUCCESS, SY_FAILED); + ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED); + + hcp_param param; + param.modelNames = "./models/hcp/hcp211008_310p.om"; + param.devId = devId; + + cout << "hcp_init start " << endl; + int ret = hcp_init(&m_handle, param); + if (ret != 0) { + return -1; + } + + cout << "hcp_init success " << endl; + + return SY_SUCCESS; +} + +int HumanCarAnalysis::detect(vector vec_img){ + + ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED); + + const int batchsize = vec_img.size(); + hcp_analysis_result * results = new hcp_analysis_result[batchsize]; + + int ret = SY_FAILED; + + do + { + ret = hcp_batch(m_handle, vec_img.data(), batchsize, results); + if (SY_SUCCESS != ret) { + printf("vidclothesClassification process failed!"); + break; + } + + for(int batchIdx = 0;batchIdx vec_img); + +private: + int release(); + +private: + void* m_handle{nullptr}; + aclrtContext ctx{nullptr}; +}; + diff --git a/src/ai_engine_module/VidClothes.cpp b/src/ai_engine_module/VidClothes.cpp index fcfbb65..bfacf93 100644 --- a/src/ai_engine_module/VidClothes.cpp +++ b/src/ai_engine_module/VidClothes.cpp @@ -15,9 +15,9 @@ int VidClothes::init(int devId){ vidclothes_param param; param.modelNames = "./models/vid_clothes/vidClothes0325_310P.om"; param.thresld = 0.0; - param.devId = 0; + param.devId = devId; - cout << "vidclothes_init start " << endl; + cout << "vidclothes_init start " << endl; int ret = vidclothes_init(&m_handle, param); if (ret != 0) { return -1; @@ -57,13 +57,18 @@ int VidClothes::detect(vector vec_img){ return ret; } -void VidClothes::release() { - if(ctx){ - aclrtSetCurrentContext(ctx); - aclrtDestroyContext(ctx); - } +int VidClothes::release() { + + ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED); if (m_handle) { vidclothes_release(&m_handle); } + + if(ctx){ + aclrtDestroyContext(ctx); + ctx = nullptr; + } + + return SY_SUCCESS; } \ No newline at end of file diff --git a/src/ai_engine_module/VidClothes.h b/src/ai_engine_module/VidClothes.h index 9aa0c2c..641d125 100644 --- a/src/ai_engine_module/VidClothes.h +++ b/src/ai_engine_module/VidClothes.h @@ -3,8 +3,6 @@ class VidClothes { -private: - /* data */ public: VidClothes(/* args */); ~VidClothes(); @@ -14,7 +12,7 @@ public: int detect(vector vec_img); private: - void release(); + int release(); private: void* m_handle{nullptr}; diff --git a/src/main.cpp b/src/main.cpp index 0f65a7e..3aa6c5b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,12 +9,12 @@ int main() { vector vec_path; for (size_t i = 0; i < 10; i++) { - vec_path.push_back("./test_head_tail.jpg"); + vec_path.push_back("./test_hv.jpg"); } pic_analysis.analysis_sync(vec_path); - pic_analysis.release(); + while (getchar() != 'q'); return 0; } \ No newline at end of file