diff --git a/algorithm/vid_clothes2/vid_clothes/libvid_clothes.so b/algorithm/vid_clothes2/vid_clothes/libvid_clothes.so new file mode 100644 index 0000000..da46e52 --- /dev/null +++ b/algorithm/vid_clothes2/vid_clothes/libvid_clothes.so diff --git a/algorithm/vid_clothes2/vid_clothes/models/vidClothes0325_310P.om b/algorithm/vid_clothes2/vid_clothes/models/vidClothes0325_310P.om new file mode 100644 index 0000000..393daa8 --- /dev/null +++ b/algorithm/vid_clothes2/vid_clothes/models/vidClothes0325_310P.om diff --git a/algorithm/vid_clothes2/vid_clothes/vid_clothes.h b/algorithm/vid_clothes2/vid_clothes/vid_clothes.h new file mode 100644 index 0000000..3f82bd7 --- /dev/null +++ b/algorithm/vid_clothes2/vid_clothes/vid_clothes.h @@ -0,0 +1,112 @@ +/************************************************************************* +* Version: vid_clothes_recognition_v0.0.0.20220325 +* CopyRight : +* UpdateDate:20220325 +* Content : 司乘人员衣服颜色识别 +*************************************************************************/ +#ifndef VIDCLOTHES_H_ +#define VIDCLOTHES_H_ + +#if _MSC_VER +#ifdef VIDCLOTHES_EXPORTS +#define VIDCLOTHES_API __declspec(dllexport) +#else +#define VIDCLOTHES_API __declspec(dllimport) +#endif +#else +#define VIDCLOTHES_API __attribute__ ((visibility ("default"))) +#endif + +#include "sy_common.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + + +//2.车颜色结果 +#ifndef VIDCLOTHES_RESULT_ +#define VIDCLOTHES_RESULT_ +typedef struct vidclothes_result +{ + float score; + int index; //13类:"棕", "橙", "灰", "白", "粉", "紫", "红", "绿", "蓝", "银", "青", "黄", "黑" +}vidclothes_result; +#endif + + +#ifndef __VIDCLOTHES_PARAM__ +#define __VIDCLOTHES_PARAM__ + typedef struct vidclothes_param + { + int devId; //指定显卡id + char* modelNames; + float thresld; //阈值 + vidclothes_param() :devId(0), thresld(0.6) {}; + }vidclothes_param; +#endif + + /************************************************************************* + * FUNCTION: vidclothes_init + * PURPOSE: 载入模型 + * PARAM: + [in] handle - 句柄 + [in] params - 参数 + * RETURN: 成功(0)或者错误代码 + * NOTES: + *************************************************************************/ + VIDCLOTHES_API int vidclothes_init(void ** handle, vidclothes_param param); + + /************************************************************************* + * FUNCTION: vidclothes_process + * PURPOSE: 车颜色识别 + * PARAM: + [in] handle - 检测句柄 + [in] img_data - 图像数据 + [in] result - 结果 内存在外部申请 + * RETURN: 成功(0) 或 错误代码(< 0) + * NOTES: + *************************************************************************/ + VIDCLOTHES_API int vidclothes_process(void *handle, sy_img img_data, vidclothes_result * result); + + /************************************************************************* + * FUNCTION: vidclothes_batch + * PURPOSE: 车颜色识别 batch + * PARAM: + [in] handle - 检测句柄 + [in] img_data_array - 图像数据 + [in] batch_size - 图像数目 + [in] result - 结果 内存在外部申请 + * RETURN: 成功(0) 或 错误代码(< 0) + * NOTES: + *************************************************************************/ + VIDCLOTHES_API int vidclothes_batch(void *handle, sy_img* img_data_array, int batch_size, vidclothes_result * result); + + + /************************************************************************* + * FUNCTION: vidclothes_release + * PURPOSE: 释放 + * PARAM: + [in] handle - handle + * RETURN: NULL + * NOTES: + *************************************************************************/ + VIDCLOTHES_API void vidclothes_release(void ** handle); + + + /************************************************************************* + * FUNCTION: vidclothes_get_version + * PURPOSE: + * PARAM: NULL + * RETURN: 版本号 + * NOTES: + *************************************************************************/ + VIDCLOTHES_API const char * vidclothes_get_version(); + +#ifdef __cplusplus +}; +#endif + +#endif diff --git a/algorithm/vid_clothes2/vid_clothes_test/Makefile b/algorithm/vid_clothes2/vid_clothes_test/Makefile new file mode 100644 index 0000000..1d63ffd --- /dev/null +++ b/algorithm/vid_clothes2/vid_clothes_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)/../vid_clothes \ + -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)/../vid_clothes \ + +local_shared_libs := \ + ascendcl \ + acl_dvpp \ + opencv_world \ + vid_clothes \ + +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/vid_clothes2/vid_clothes_test/test.cpp b/algorithm/vid_clothes2/vid_clothes_test/test.cpp new file mode 100644 index 0000000..818cfa7 --- /dev/null +++ b/algorithm/vid_clothes2/vid_clothes_test/test.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include "vid_clothes.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 << vidclothes_get_version() << endl; + const char* img_file_path = "../../../data/vid_clothes/"; + string saveimagepath= "../../../data/result/"; + + vidclothes_param param; + param.modelNames = "../vid_clothes/models/vidClothes0325_310P.om"; + param.thresld = 0.0; + 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 = vidclothes_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 = 2; + 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(); + } + + vidclothes_result * results = new vidclothes_result[batchsize]; + double t1,t2; + t1 = msecond(); + int ret = vidclothes_batch(handle, imgs, batchsize, results); + t2 = msecond(); + printf("debug mean process time: %.2f\n", (t2 - t1)/batchsize); + if (SY_SUCCESS != ret) { + printf("vidclothesClassification process failed!"); + return SY_FAILED; + } + + for(int batchIdx = 0;batchIdx