Commit f3d83919345b591f3756cbe9f876a88fb5bb15b9

Authored by Hu Chunming
1 parent 20396d5c

添加clothes算法

algorithm/vid_clothes2/vid_clothes/libvid_clothes.so 0 → 100644
No preview for this file type
algorithm/vid_clothes2/vid_clothes/models/vidClothes0325_310P.om 0 → 100644
No preview for this file type
algorithm/vid_clothes2/vid_clothes/vid_clothes.h 0 → 100644
  1 +/*************************************************************************
  2 +* Version: vid_clothes_recognition_v0.0.0.20220325
  3 +* CopyRight :
  4 +* UpdateDate:20220325
  5 +* Content : 司乘人员衣服颜色识别
  6 +*************************************************************************/
  7 +#ifndef VIDCLOTHES_H_
  8 +#define VIDCLOTHES_H_
  9 +
  10 +#if _MSC_VER
  11 +#ifdef VIDCLOTHES_EXPORTS
  12 +#define VIDCLOTHES_API __declspec(dllexport)
  13 +#else
  14 +#define VIDCLOTHES_API __declspec(dllimport)
  15 +#endif
  16 +#else
  17 +#define VIDCLOTHES_API __attribute__ ((visibility ("default")))
  18 +#endif
  19 +
  20 +#include "sy_common.h"
  21 +
  22 +#ifdef __cplusplus
  23 +extern "C"
  24 +{
  25 +#endif
  26 +
  27 +
  28 +
  29 +//2.车颜色结果
  30 +#ifndef VIDCLOTHES_RESULT_
  31 +#define VIDCLOTHES_RESULT_
  32 +typedef struct vidclothes_result
  33 +{
  34 + float score;
  35 + int index; //13类:"棕", "橙", "灰", "白", "粉", "紫", "红", "绿", "蓝", "银", "青", "黄", "黑"
  36 +}vidclothes_result;
  37 +#endif
  38 +
  39 +
  40 +#ifndef __VIDCLOTHES_PARAM__
  41 +#define __VIDCLOTHES_PARAM__
  42 + typedef struct vidclothes_param
  43 + {
  44 + int devId; //指定显卡id
  45 + char* modelNames;
  46 + float thresld; //阈值
  47 + vidclothes_param() :devId(0), thresld(0.6) {};
  48 + }vidclothes_param;
  49 +#endif
  50 +
  51 + /*************************************************************************
  52 + * FUNCTION: vidclothes_init
  53 + * PURPOSE: 载入模型
  54 + * PARAM:
  55 + [in] handle - 句柄
  56 + [in] params - 参数
  57 + * RETURN: 成功(0)或者错误代码
  58 + * NOTES:
  59 + *************************************************************************/
  60 + VIDCLOTHES_API int vidclothes_init(void ** handle, vidclothes_param param);
  61 +
  62 + /*************************************************************************
  63 + * FUNCTION: vidclothes_process
  64 + * PURPOSE: 车颜色识别
  65 + * PARAM:
  66 + [in] handle - 检测句柄
  67 + [in] img_data - 图像数据
  68 + [in] result - 结果 内存在外部申请
  69 + * RETURN: 成功(0) 或 错误代码(< 0)
  70 + * NOTES:
  71 + *************************************************************************/
  72 + VIDCLOTHES_API int vidclothes_process(void *handle, sy_img img_data, vidclothes_result * result);
  73 +
  74 + /*************************************************************************
  75 + * FUNCTION: vidclothes_batch
  76 + * PURPOSE: 车颜色识别 batch
  77 + * PARAM:
  78 + [in] handle - 检测句柄
  79 + [in] img_data_array - 图像数据
  80 + [in] batch_size - 图像数目
  81 + [in] result - 结果 内存在外部申请
  82 + * RETURN: 成功(0) 或 错误代码(< 0)
  83 + * NOTES:
  84 + *************************************************************************/
  85 + VIDCLOTHES_API int vidclothes_batch(void *handle, sy_img* img_data_array, int batch_size, vidclothes_result * result);
  86 +
  87 +
  88 + /*************************************************************************
  89 + * FUNCTION: vidclothes_release
  90 + * PURPOSE: 释放
  91 + * PARAM:
  92 + [in] handle - handle
  93 + * RETURN: NULL
  94 + * NOTES:
  95 + *************************************************************************/
  96 + VIDCLOTHES_API void vidclothes_release(void ** handle);
  97 +
  98 +
  99 + /*************************************************************************
  100 + * FUNCTION: vidclothes_get_version
  101 + * PURPOSE:
  102 + * PARAM: NULL
  103 + * RETURN: 版本号
  104 + * NOTES:
  105 + *************************************************************************/
  106 + VIDCLOTHES_API const char * vidclothes_get_version();
  107 +
  108 +#ifdef __cplusplus
  109 +};
  110 +#endif
  111 +
  112 +#endif
... ...
algorithm/vid_clothes2/vid_clothes_test/Makefile 0 → 100644
  1 +CC = gcc
  2 +XX = c++
  3 +
  4 +TARGET = start
  5 +
  6 +PROJ_ALL_PATH = $(PWD)/../..
  7 +CUR_PROJ_PATH = $(PWD)
  8 +OPENCV_PATH = $(PROJ_ALL_PATH)/third_party/opencv_4_1
  9 +ACL_PATH = $(ASCEND_AICPU_PATH)/acllib
  10 +
  11 +INCLUDES = -I$(PROJ_ALL_PATH)/common \
  12 + -I$(ACL_PATH)/include \
  13 + -I$(PROJ_ALL_PATH)/common/dvpp \
  14 + -I$(CUR_PROJ_PATH)/../vid_clothes \
  15 + -I$(OPENCV_PATH)/include \
  16 + -I$(OPENCV_PATH)/include/opencv2 \
  17 +
  18 +CFLAGS = -O2 -std=c++11 $(INCLUDES) -DENABLE_DVPP_INTERFACE -D_GLIBCXX_USE_CXX11_ABI=0
  19 +
  20 +local_shared_libs_dirs := \
  21 + $(ACL_PATH)/lib64 \
  22 + $(OPENCV_PATH)/lib \
  23 + $(CUR_PROJ_PATH)/../vid_clothes \
  24 +
  25 +local_shared_libs := \
  26 + ascendcl \
  27 + acl_dvpp \
  28 + opencv_world \
  29 + vid_clothes \
  30 +
  31 +SHARED_LIBRARIES := $(foreach shared_lib, $(local_shared_libs), -l$(shared_lib))
  32 +#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))
  33 +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))
  34 +
  35 +SRCS := $(wildcard $(CUR_PROJ_PATH)/*.cpp)
  36 +SRCS += $(wildcard $(PROJ_ALL_PATH)/common/dvpp/*.cpp)
  37 +
  38 +DIRS := $(notdir $(SRCS))
  39 +OBJS := $(patsubst %cpp, %o, $(DIRS))
  40 +
  41 +all: $(TARGET)
  42 +
  43 +$(TARGET):$(OBJS)
  44 + $(XX) $(CFLAGS) -o $@ $^ $(SHARED_LIBRARIES_DIRS) $(SHARED_LIBRARIES)
  45 +%.o:$(CUR_PROJ_PATH)/%.cpp
  46 + $(XX) $(CFLAGS) -c $<
  47 +%.o:$(PROJ_ALL_PATH)/common/dvpp/%.cpp
  48 + $(XX) $(CFLAGS) -c $<
  49 +
  50 +clean:
  51 + @rm -f $(TARGET)
  52 + @rm -f $(OBJS)
... ...
algorithm/vid_clothes2/vid_clothes_test/test.cpp 0 → 100644
  1 +#include <iostream>
  2 +#include <sstream>
  3 +#include <fstream>
  4 +#include "vid_clothes.h"
  5 +#include "opencv2/opencv.hpp"
  6 +#include "opencv2/imgcodecs/legacy/constants_c.h"
  7 +#include "opencv2/imgproc/types_c.h"
  8 +#include "time.h"
  9 +#include "sys/time.h"
  10 +#include "sy_errorinfo.h"
  11 +#include "utils.h"
  12 +#include "dvpp_process.h"
  13 +
  14 +using namespace std;
  15 +using namespace cv;
  16 +#include <chrono>
  17 +#include <dirent.h>
  18 +
  19 +double msecond() {
  20 + struct timeval tv;
  21 + gettimeofday(&tv, 0);
  22 + return (tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0);
  23 +}
  24 +void getAllNm(std::string pthNm, std::vector<std::string>& fileList)
  25 +{
  26 + DIR *dir;
  27 + struct dirent *ptr;
  28 + dir = opendir(pthNm.c_str()); ///open the dir
  29 + int filenum = 0;
  30 + while((ptr = readdir(dir)) != NULL) ///read the list of this dir
  31 + {
  32 + // char* to string
  33 + std::string curNm = ptr->d_name;
  34 + if(curNm != "." && curNm != "..")
  35 + {
  36 + filenum++;
  37 + fileList.push_back(curNm);
  38 + //printf("file %d name: %s\n", filenum, curNm.c_str());
  39 + }
  40 + }
  41 + closedir(dir);
  42 +}
  43 +
  44 +int main() {
  45 + cout << vidclothes_get_version() << endl;
  46 + const char* img_file_path = "../../../data/vid_clothes/";
  47 + string saveimagepath= "../../../data/result/";
  48 +
  49 + vidclothes_param param;
  50 + param.modelNames = "../vid_clothes/models/vidClothes0325_310P.om";
  51 + param.thresld = 0.0;
  52 + param.devId = 0;
  53 +
  54 + void* handle = nullptr;
  55 + cout << "init start " << endl;
  56 + ACL_CALL(aclInit(nullptr), ACL_SUCCESS, SY_FAILED);
  57 + ACL_CALL(aclrtSetDevice(param.devId), ACL_SUCCESS, SY_FAILED);
  58 + aclrtContext ctx;
  59 + ACL_CALL(aclrtCreateContext(&ctx, param.devId), ACL_SUCCESS, SY_FAILED);
  60 + aclrtStream stream = nullptr;
  61 + ACL_CALL(aclrtCreateStream(&stream), ACL_SUCCESS, SY_FAILED);
  62 + DvppProcess* dvpp = new DvppProcess();
  63 + dvpp->InitResource(stream);
  64 + int ret = vidclothes_init(&handle, param);
  65 + if (ret == 0) {
  66 + cout << "init success " << endl;
  67 +
  68 +
  69 + std::vector<std::string> fileList;
  70 +
  71 + getAllNm(img_file_path,fileList);
  72 + if (fileList.empty()) throw std::logic_error("No suitable images were found");
  73 +
  74 + for (auto & file : fileList) {
  75 + string filename = img_file_path + file;
  76 + cout << "img path: " << filename << endl;
  77 + const int batchsize = 2;
  78 + sy_img imgs[batchsize];
  79 + ImageData src[batchsize], dvpp_data[batchsize];
  80 + for (int b = 0; b < batchsize; b++) {
  81 + Utils::ReadImageFile(src[b], filename); //将二进制图像读入内存,并读取宽高信息
  82 + ACL_CALL(dvpp->CvtJpegToYuv420sp(dvpp_data[b], src[b]), SY_SUCCESS, SY_FAILED); //解码
  83 + imgs[b].w_ = dvpp_data[b].width;
  84 + imgs[b].h_ = dvpp_data[b].height;
  85 + imgs[b].data_ = dvpp_data[b].data.get();
  86 + }
  87 +
  88 + vidclothes_result * results = new vidclothes_result[batchsize];
  89 + double t1,t2;
  90 + t1 = msecond();
  91 + int ret = vidclothes_batch(handle, imgs, batchsize, results);
  92 + t2 = msecond();
  93 + printf("debug mean process time: %.2f\n", (t2 - t1)/batchsize);
  94 + if (SY_SUCCESS != ret) {
  95 + printf("vidclothesClassification process failed!");
  96 + return SY_FAILED;
  97 + }
  98 +
  99 + for(int batchIdx = 0;batchIdx<batchsize;batchIdx++) {
  100 + printf("index:%d,confidence:%f\n",results[batchIdx].index,results[batchIdx].score);
  101 + }
  102 +
  103 + // vidclothes_result single_results[1];
  104 + // vidclothes_process(handle, imgs[0], single_results);
  105 + // printf("index:%d,confidence:%f\n",single_results[0].index,single_results[0].score);
  106 +
  107 + if (results) {
  108 + delete [] results;
  109 + }
  110 + }
  111 +
  112 + }
  113 +
  114 + if (dvpp) {
  115 + delete dvpp;
  116 + dvpp = nullptr;
  117 + }
  118 + vidclothes_release(&handle);
  119 + aclrtDestroyContext(ctx);
  120 + aclrtResetDevice(param.devId);
  121 + aclFinalize();
  122 + return 0;
  123 +}
  124 +
... ...
bin/libvid_clothes.so 0 → 100644
No preview for this file type
bin/models/vid_clothes/vidClothes0325_310P.om 0 → 100644
No preview for this file type
build/src/Makefile
... ... @@ -27,6 +27,7 @@ INCLUDES = -I$(PROJ_ALL_PATH)/src/common \
27 27 -I$(ACL_PATH)/include \
28 28 -I$(SPDLOG_ROOT)/include \
29 29 -I$(ALGORITHM_PATH)/vehicle_analysis \
  30 + -I$(ALGORITHM_PATH)/vid_clothes2/vid_clothes \
30 31 -I$(CUR_PROJ_PATH)/ai_engine_module \
31 32  
32 33 # CXXFLAGS = -O0 -std=c++11 $(INCLUDES) -DENABLE_DVPP_INTERFACE -D_GLIBCXX_USE_CXX11_ABI=0
... ... @@ -40,10 +41,11 @@ local_shared_libs_dirs := \
40 41  
41 42 local_shared_libs := \
42 43 opencv_world \
43   - vehicle_analysis \
44 44 ascendcl \
45 45 acl_dvpp \
46 46 spdlog \
  47 + vehicle_analysis \
  48 + vid_clothes \
47 49  
48 50 SHARED_LIBRARIES := $(foreach shared_lib, $(local_shared_libs), -l$(shared_lib))
49 51 #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))
... ...