Commit f171c20a79db5509e973484f142bdbdfad2ad28e
1 parent
4a273a4a
添加moter_rainshed 和 motor_phone
Showing
17 changed files
with
1068 additions
and
36 deletions
bin/models/motor_phone/motor_phone1127_310p.om
0 → 100755
No preview for this file type
bin/models/rainshed/motor_rainshed_231123_310p.om
0 → 100755
No preview for this file type
build/src/Makefile
... | ... | @@ -18,6 +18,7 @@ ALGORITHM_PATH = $(PROJ_ALL_PATH)/algorithm |
18 | 18 | |
19 | 19 | INCLUDES = -I$(PROJ_ALL_PATH)/src/common \ |
20 | 20 | -I$(PROJ_ALL_PATH)/src/common/dvpp \ |
21 | + -I$(PROJ_ALL_PATH)/src/common/cnn \ | |
21 | 22 | -I$(PROJ_ALL_PATH)/src/common/cnn_cls \ |
22 | 23 | -I$(PROJ_ALL_PATH)/src/common/dvppx \ |
23 | 24 | -I$(PROJ_ALL_PATH)/src/common/model_process \ |
... | ... | @@ -64,6 +65,7 @@ SRCS := $(wildcard $(CUR_PROJ_PATH)/*.cpp) \ |
64 | 65 | $(wildcard $(CUR_PROJ_PATH)/common/*.cpp) \ |
65 | 66 | $(wildcard $(CUR_PROJ_PATH)/common/dvpp/*.cpp) \ |
66 | 67 | $(wildcard $(CUR_PROJ_PATH)/common/cnn_cls/*.cpp) \ |
68 | + $(wildcard $(CUR_PROJ_PATH)/common/cnn/*.cpp) \ | |
67 | 69 | $(wildcard $(CUR_PROJ_PATH)/common/dvppx/*.cpp) \ |
68 | 70 | $(wildcard $(CUR_PROJ_PATH)/common/model_process/*.cpp) \ |
69 | 71 | |
... | ... | @@ -87,6 +89,9 @@ $(TARGET):$(OBJS) |
87 | 89 | %.o:$(CUR_PROJ_PATH)/common/dvpp/%.cpp |
88 | 90 | $(XX) $(CXXFLAGS) -c $< |
89 | 91 | |
92 | +%.o:$(CUR_PROJ_PATH)/common/cnn/%.cpp | |
93 | + $(XX) $(CXXFLAGS) -c $< | |
94 | + | |
90 | 95 | %.o:$(CUR_PROJ_PATH)/common/cnn_cls/%.cpp |
91 | 96 | $(XX) $(CXXFLAGS) -c $< |
92 | 97 | ... | ... |
src/PicAnalysis.cpp
... | ... | @@ -17,30 +17,40 @@ int PicAnalysis::init(int dev_id) { |
17 | 17 | |
18 | 18 | int ret = SY_FAILED; |
19 | 19 | |
20 | - ret = m_vehicle_analysis.init(dev_id, 16); | |
21 | - if(0 != ret){ | |
22 | - return -1; | |
23 | - } | |
24 | - | |
25 | - head_tail_param ht_param; | |
26 | - ht_param.devId = dev_id; | |
27 | - ht_param.max_batch = 16; | |
28 | - ret = m_head_tail_algorithm.init(ht_param); | |
29 | - if(0 != ret){ | |
30 | - return -1; | |
31 | - } | |
32 | - | |
33 | - ret = m_clothes_algorithm.init(dev_id); | |
34 | - if(0 != ret){ | |
35 | - return -1; | |
36 | - } | |
37 | - | |
38 | - ret = m_human_algorithm.init(dev_id); | |
39 | - if(0 != ret){ | |
40 | - return -1; | |
41 | - } | |
42 | - | |
43 | - ret = m_human_car_algorithm.init(dev_id); | |
20 | + // ret = m_vehicle_analysis.init(dev_id, 16); | |
21 | + // if(0 != ret){ | |
22 | + // return -1; | |
23 | + // } | |
24 | + | |
25 | + // head_tail_param ht_param; | |
26 | + // ht_param.devId = dev_id; | |
27 | + // ht_param.max_batch = 16; | |
28 | + // ret = m_head_tail_algorithm.init(ht_param); | |
29 | + // if(0 != ret){ | |
30 | + // return -1; | |
31 | + // } | |
32 | + | |
33 | + // ret = m_clothes_algorithm.init(dev_id); | |
34 | + // if(0 != ret){ | |
35 | + // return -1; | |
36 | + // } | |
37 | + | |
38 | + // ret = m_human_algorithm.init(dev_id); | |
39 | + // if(0 != ret){ | |
40 | + // return -1; | |
41 | + // } | |
42 | + | |
43 | + // ret = m_human_car_algorithm.init(dev_id); | |
44 | + // if(0 != ret){ | |
45 | + // return -1; | |
46 | + // } | |
47 | + | |
48 | + // ret = m_motor_rainshed_algorithm.init(dev_id); | |
49 | + // if(0 != ret){ | |
50 | + // return -1; | |
51 | + // } | |
52 | + | |
53 | + ret = m_motor_phone_algorithm.init(dev_id); | |
44 | 54 | if(0 != ret){ |
45 | 55 | return -1; |
46 | 56 | } |
... | ... | @@ -90,20 +100,24 @@ int PicAnalysis::analysis_sync(vector<string> vec_file_path){ |
90 | 100 | vec_img.push_back(img); |
91 | 101 | } |
92 | 102 | |
93 | - m_vehicle_analysis.detect(vec_img); | |
103 | + // m_vehicle_analysis.detect(vec_img); | |
94 | 104 | |
95 | - vector<HeadTailResult> head_tail_result; | |
96 | - ret = m_head_tail_algorithm.detect(vec_img, head_tail_result); | |
97 | - if (0 != ret) { | |
98 | - LOG_ERROR("m_head_tail_algorithm failed!"); | |
99 | - head_tail_result.clear(); | |
100 | - } | |
105 | + // vector<HeadTailResult> head_tail_result; | |
106 | + // ret = m_head_tail_algorithm.detect(vec_img, head_tail_result); | |
107 | + // if (0 != ret) { | |
108 | + // LOG_ERROR("m_head_tail_algorithm failed!"); | |
109 | + // head_tail_result.clear(); | |
110 | + // } | |
111 | + | |
112 | + // m_clothes_algorithm.detect(vec_img); | |
113 | + | |
114 | + // m_human_algorithm.detect(vec_img); | |
101 | 115 | |
102 | - m_clothes_algorithm.detect(vec_img); | |
116 | + // m_human_car_algorithm.detect(vec_img); | |
103 | 117 | |
104 | - m_human_algorithm.detect(vec_img); | |
118 | + // m_motor_rainshed_algorithm.detect(vec_img); | |
105 | 119 | |
106 | - m_human_car_algorithm.detect(vec_img); | |
120 | + m_motor_phone_algorithm.detect(vec_img); | |
107 | 121 | |
108 | 122 | LOG_INFO("analysis_sync finished!"); |
109 | 123 | ... | ... |
src/PicAnalysis.h
... | ... | @@ -4,6 +4,8 @@ |
4 | 4 | #include "./ai_engine_module/VidClothes.h" |
5 | 5 | #include "./ai_engine_module/HumanAnalysis.h" |
6 | 6 | #include "./ai_engine_module/HumanCarAnalysis.h" |
7 | +#include "./ai_engine_module/MotorRainshedAnalysis.h" | |
8 | +#include "./ai_engine_module/MotorPhoneAnalysis.h" | |
7 | 9 | |
8 | 10 | using namespace std; |
9 | 11 | |
... | ... | @@ -31,6 +33,8 @@ private: |
31 | 33 | VidClothes m_clothes_algorithm; |
32 | 34 | HumanAnalysis m_human_algorithm; |
33 | 35 | HumanCarAnalysis m_human_car_algorithm; |
36 | + MotorRainshedAnalysis m_motor_rainshed_algorithm; | |
37 | + MotorPhoneAnalysis m_motor_phone_algorithm; | |
34 | 38 | }; |
35 | 39 | |
36 | 40 | ... | ... |
src/ai_engine_module/HumanAnalysis.cpp
... | ... | @@ -41,7 +41,7 @@ int HumanAnalysis::detect(vector<sy_img> vec_img){ |
41 | 41 | { |
42 | 42 | ret = hp_batch(m_handle, vec_img.data(), batchsize, results); |
43 | 43 | if (SY_SUCCESS != ret) { |
44 | - printf("vidclothesClassification process failed!"); | |
44 | + printf("hp_batch failed!"); | |
45 | 45 | break; |
46 | 46 | } |
47 | 47 | ... | ... |
src/ai_engine_module/HumanCarAnalysis.cpp
... | ... | @@ -41,7 +41,7 @@ int HumanCarAnalysis::detect(vector<sy_img> vec_img){ |
41 | 41 | { |
42 | 42 | ret = hcp_batch(m_handle, vec_img.data(), batchsize, results); |
43 | 43 | if (SY_SUCCESS != ret) { |
44 | - printf("vidclothesClassification process failed!"); | |
44 | + printf("hcp_batch failed!"); | |
45 | 45 | break; |
46 | 46 | } |
47 | 47 | ... | ... |
src/ai_engine_module/MotorPhoneAnalysis.cpp
0 → 100644
1 | +#include "MotorPhoneAnalysis.h" | |
2 | +#include "motor_phone_det.h" | |
3 | + | |
4 | +MotorPhoneAnalysis::MotorPhoneAnalysis(/* args */) | |
5 | +{ | |
6 | +} | |
7 | + | |
8 | +MotorPhoneAnalysis::~MotorPhoneAnalysis() | |
9 | +{ | |
10 | + release(); | |
11 | +} | |
12 | + | |
13 | +int MotorPhoneAnalysis::init(int devId){ | |
14 | + ACL_CALL(aclrtCreateContext(&ctx, devId), SY_SUCCESS, SY_FAILED); | |
15 | + | |
16 | + motor_phone_param param; | |
17 | + param.modelNames = "./models/motor_phone/motor_phone1127_310p.om"; | |
18 | + param.thresld = 0.25; | |
19 | + param.devId = devId; | |
20 | + | |
21 | + cout << "motor_phone_init start " << endl; | |
22 | + int ret = motor_phone_init(&m_handle, param); | |
23 | + if (ret != 0) { | |
24 | + return -1; | |
25 | + } | |
26 | + | |
27 | + cout << "motor_phone_init success " << endl; | |
28 | + | |
29 | + return SY_SUCCESS; | |
30 | +} | |
31 | + | |
32 | +int MotorPhoneAnalysis::detect(vector<sy_img> vec_img){ | |
33 | + | |
34 | + ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED); | |
35 | + | |
36 | + const int batchsize = vec_img.size(); | |
37 | + motor_phone_result * results = new motor_phone_result[batchsize]; | |
38 | + | |
39 | + int ret = SY_FAILED; | |
40 | + | |
41 | + do | |
42 | + { | |
43 | + ret = motor_phone_process_batch(m_handle, vec_img.data(), batchsize, results); | |
44 | + if (SY_SUCCESS != ret) { | |
45 | + printf("motor_phone_process_batch failed!"); | |
46 | + break; | |
47 | + } | |
48 | + | |
49 | + for(int batchIdx = 0; batchIdx < batchsize; batchIdx ++){ | |
50 | + printf("debug det num:%d\n",results[batchIdx].objcount); | |
51 | + for (int i = 0; i < results[batchIdx].objcount; i++) { | |
52 | + printf(" %d:%.2f \n", results[batchIdx].objinfo[i].index,results[batchIdx].objinfo[i].confidence); | |
53 | + } | |
54 | + } | |
55 | + } while (0); | |
56 | + | |
57 | + if (results) { | |
58 | + delete [] results; | |
59 | + } | |
60 | + | |
61 | + return ret; | |
62 | +} | |
63 | + | |
64 | +int MotorPhoneAnalysis::release() { | |
65 | + | |
66 | + ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED); | |
67 | + | |
68 | + if (m_handle) { | |
69 | + motor_phone_release(&m_handle); | |
70 | + } | |
71 | + | |
72 | + if(ctx){ | |
73 | + aclrtDestroyContext(ctx); | |
74 | + ctx = nullptr; | |
75 | + } | |
76 | + | |
77 | + return SY_SUCCESS; | |
78 | +} | |
0 | 79 | \ No newline at end of file | ... | ... |
src/ai_engine_module/MotorPhoneAnalysis.h
0 → 100644
1 | +#include "include.h" | |
2 | + | |
3 | +class MotorPhoneAnalysis | |
4 | +{ | |
5 | +public: | |
6 | + MotorPhoneAnalysis(/* args */); | |
7 | + ~MotorPhoneAnalysis(); | |
8 | + | |
9 | + int init(int devId); | |
10 | + | |
11 | + int detect(vector<sy_img> vec_img); | |
12 | + | |
13 | +private: | |
14 | + int release(); | |
15 | + | |
16 | +private: | |
17 | + void* m_handle{nullptr}; | |
18 | + aclrtContext ctx{nullptr}; | |
19 | +}; | |
20 | + | ... | ... |
src/ai_engine_module/MotorRainshedAnalysis.cpp
0 → 100644
1 | +#include "MotorRainshedAnalysis.h" | |
2 | +#include "motor_rainshed_cls.h" | |
3 | + | |
4 | +MotorRainshedAnalysis::MotorRainshedAnalysis(/* args */) | |
5 | +{ | |
6 | +} | |
7 | + | |
8 | +MotorRainshedAnalysis::~MotorRainshedAnalysis() | |
9 | +{ | |
10 | + release(); | |
11 | +} | |
12 | + | |
13 | +int MotorRainshedAnalysis::init(int devId){ | |
14 | + ACL_CALL(aclrtCreateContext(&ctx, devId), SY_SUCCESS, SY_FAILED); | |
15 | + | |
16 | + mrc_param param; | |
17 | + param.modelNames = "./models/rainshed/motor_rainshed_231123_310p.om"; | |
18 | + param.thresld = 0.0; | |
19 | + param.devId = devId; | |
20 | + | |
21 | + cout << "mrc_init start " << endl; | |
22 | + int ret = mrc_init(&m_handle, param); | |
23 | + if (ret != 0) { | |
24 | + return -1; | |
25 | + } | |
26 | + | |
27 | + cout << "mrc_init success " << endl; | |
28 | + | |
29 | + return SY_SUCCESS; | |
30 | +} | |
31 | + | |
32 | +int MotorRainshedAnalysis::detect(vector<sy_img> vec_img){ | |
33 | + | |
34 | + ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED); | |
35 | + | |
36 | + const int batchsize = vec_img.size(); | |
37 | + mrc_result * results = new mrc_result[batchsize]; | |
38 | + | |
39 | + int ret = SY_FAILED; | |
40 | + | |
41 | + do | |
42 | + { | |
43 | + ret = mrc_batch(m_handle, vec_img.data(), batchsize, results); | |
44 | + if (SY_SUCCESS != ret) { | |
45 | + printf("mrc_batch failed!"); | |
46 | + break; | |
47 | + } | |
48 | + | |
49 | + for(int batchIdx = 0;batchIdx<batchsize;batchIdx++){ | |
50 | + printf("index:%d,confidence:%f\n",results[batchIdx].index,results[batchIdx].score); | |
51 | + } | |
52 | + } while (0); | |
53 | + | |
54 | + if (results) { | |
55 | + delete [] results; | |
56 | + } | |
57 | + | |
58 | + return ret; | |
59 | +} | |
60 | + | |
61 | +int MotorRainshedAnalysis::release() { | |
62 | + | |
63 | + ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED); | |
64 | + | |
65 | + if (m_handle) { | |
66 | + mrc_release(&m_handle); | |
67 | + } | |
68 | + | |
69 | + if(ctx){ | |
70 | + aclrtDestroyContext(ctx); | |
71 | + ctx = nullptr; | |
72 | + } | |
73 | + | |
74 | + return SY_SUCCESS; | |
75 | +} | |
0 | 76 | \ No newline at end of file | ... | ... |
src/ai_engine_module/MotorRainshedAnalysis.h
0 → 100644
1 | +#include "include.h" | |
2 | + | |
3 | +class MotorRainshedAnalysis | |
4 | +{ | |
5 | +public: | |
6 | + MotorRainshedAnalysis(/* args */); | |
7 | + ~MotorRainshedAnalysis(); | |
8 | + | |
9 | + int init(int devId); | |
10 | + | |
11 | + int detect(vector<sy_img> vec_img); | |
12 | + | |
13 | +private: | |
14 | + int release(); | |
15 | + | |
16 | +private: | |
17 | + void* m_handle{nullptr}; | |
18 | + aclrtContext ctx{nullptr}; | |
19 | +}; | |
20 | + | ... | ... |
src/ai_engine_module/motor_phone_det.cpp
0 → 100755
1 | +#include "motor_phone_det.h" | |
2 | +#include "sy_errorinfo.h" | |
3 | +#include "cnn_extractor.h" | |
4 | +#include "dvpp_processx.h" | |
5 | +#include <time.h> | |
6 | +#include <sys/time.h> | |
7 | +#include "stream_data.h" | |
8 | +#include <algorithm> | |
9 | +#include <map> | |
10 | +#include <cstring> | |
11 | + | |
12 | + | |
13 | +using namespace atlas_utils; | |
14 | +using namespace std; | |
15 | + | |
16 | +struct Resource { | |
17 | + aclrtContext ctx; | |
18 | + aclrtStream stream; | |
19 | +}; | |
20 | + | |
21 | +typedef struct Tools { | |
22 | + Resource src; | |
23 | + CNNExtract* phoneDetMotor; | |
24 | + DvppProcessx* dvpp; | |
25 | +}Tools; | |
26 | + | |
27 | + | |
28 | +int motor_phone_init(void **handle, motor_phone_param param){ | |
29 | + | |
30 | + int ret = SY_SUCCESS; | |
31 | + Tools* tools = new Tools; | |
32 | + // init resource | |
33 | + // ACL_CALL(aclInit(nullptr), ACL_SUCCESS, SY_FAILED); | |
34 | + // ACL_CALL(aclrtSetDevice(param.devId), ACL_SUCCESS, SY_FAILED); | |
35 | + // ACL_CALL(aclrtCreateContext(&tools->src.ctx, param.devId), ACL_SUCCESS, SY_FAILED); | |
36 | + ACL_CALL(aclrtCreateStream(&tools->src.stream), ACL_SUCCESS, SY_FAILED); | |
37 | + | |
38 | + // head_shoulder detection init | |
39 | + tools->phoneDetMotor = new CNNExtract(); | |
40 | + tools->phoneDetMotor->config.confThr = param.thresld; | |
41 | + | |
42 | + ret = tools->phoneDetMotor->Init(param.modelNames); | |
43 | + if (ret != SY_SUCCESS) { | |
44 | + delete tools->phoneDetMotor; | |
45 | + tools->phoneDetMotor = nullptr; | |
46 | + return SY_FAILED; | |
47 | + } | |
48 | + | |
49 | + tools->dvpp = new DvppProcessx(); | |
50 | + tools->dvpp->InitResource(tools->src.stream); | |
51 | + | |
52 | + *handle = tools; | |
53 | + | |
54 | + return SY_SUCCESS; | |
55 | +} | |
56 | + | |
57 | + | |
58 | +//======================= yolo v8 postprocess ============================// | |
59 | +float iou(float *lbox, float *rbox) { | |
60 | + float interBox[] = { | |
61 | + (std::max)(lbox[0] - lbox[2] / 2.f , rbox[0] - rbox[2] / 2.f), //left | |
62 | + (std::min)(lbox[0] + lbox[2] / 2.f , rbox[0] + rbox[2] / 2.f), //right | |
63 | + (std::max)(lbox[1] - lbox[3] / 2.f , rbox[1] - rbox[3] / 2.f), //top | |
64 | + (std::min)(lbox[1] + lbox[3] / 2.f , rbox[1] + rbox[3] / 2.f), //bottom | |
65 | + }; | |
66 | + | |
67 | + if (interBox[2] > interBox[3] || interBox[0] > interBox[1]) | |
68 | + return 0.0f; | |
69 | + | |
70 | + float interBoxS = (interBox[1] - interBox[0])*(interBox[3] - interBox[2]); | |
71 | + return interBoxS / (lbox[2] * lbox[3] + rbox[2] * rbox[3] - interBoxS); | |
72 | +} | |
73 | + | |
74 | +bool cmp(const vector<float>& a, const vector<float>& b) { | |
75 | + return a[4] > b[4]; | |
76 | +} | |
77 | + | |
78 | +//wh20230330 | |
79 | +//yolov5的数据结构是4个bbox + 检测置信度+ 多cls | |
80 | +//yolov8的数据结构是4个bbox + 多cls ,选多cls中最大值作为检测置信度 | |
81 | +// void nms_yolov8(std::vector<vector<float>>& res, float *output, int outnum, int CLS_NUM, float conf_thresh, float nms_thresh = 0.5) | |
82 | +void nms_yolov8(std::vector<vector<float>>& res, std::vector<float> &output, int outnum, int CLS_NUM, float conf_thresh, float nms_thresh = 0.5) | |
83 | +{ | |
84 | + //printf("nms_yolov8:outnum=%d,CLS_NUM=%d,conf_thresh=%f,nms_thresh=%f \n",outnum,CLS_NUM,conf_thresh,nms_thresh); | |
85 | + int det_size = 6;// sizeof(Yolo::Detection) / sizeof(float); | |
86 | + std::map<float, std::vector<vector<float>>> m; | |
87 | + for (int i = 0; i < outnum; i++) | |
88 | + { | |
89 | + //vector<float> cls_prob(CLS_NUM - 5); | |
90 | + vector<float> cls_prob(CLS_NUM - 4);//wh多cls的值 | |
91 | + //memcpy(cls_prob.data(), &output[CLS_NUM * i + 5], (CLS_NUM - 5) * sizeof(float)); | |
92 | + memcpy(cls_prob.data(), &output[CLS_NUM * i + 4], (CLS_NUM - 4) * sizeof(float)); | |
93 | + auto maxPosition = max_element(cls_prob.begin(), cls_prob.end());//wh多cls的最大值 | |
94 | + | |
95 | + //if (output[CLS_NUM * i + 4] <= conf_thresh) continue; | |
96 | + if (*maxPosition <= conf_thresh ) continue; | |
97 | + //if(1) | |
98 | + //{ | |
99 | + // printf("nms_yolov8:output:["); | |
100 | + // for(int kk=0;kk<CLS_NUM;kk++) | |
101 | + // { | |
102 | + // printf("%f ",output[CLS_NUM * i + kk]); | |
103 | + // } | |
104 | + // printf(" ]\n"); | |
105 | + //} | |
106 | + | |
107 | + vector<float> det(det_size); | |
108 | + //Yolo::Detection det; | |
109 | + //memcpy(&det[0], &output[CLS_NUM * i], (det_size - 1) * sizeof(float)); | |
110 | + memcpy(&det[0], &output[CLS_NUM * i], (det_size - 2) * sizeof(float)); | |
111 | + //det[4] = det[4]*(*maxPosition); | |
112 | + det[4] = (*maxPosition); | |
113 | + det[5] = maxPosition - cls_prob.begin(); | |
114 | + //printf("nms_yolov8 det =%f-%f-%f-%f %f %f \n",det[0],det[1],det[2],det[3],det[4],det[5]); | |
115 | + | |
116 | + if (m.count(det[5]) == 0) | |
117 | + m.emplace(det[5], std::vector<vector<float>>()); | |
118 | + m[det[5]].push_back(det); | |
119 | + | |
120 | + /*det.conf = det.conf*(*maxPosition); | |
121 | + det.class_id = maxPosition - cls_prob.begin(); | |
122 | + if (m.count(det.class_id) == 0) | |
123 | + m.emplace(det.class_id, std::vector<Yolo::Detection>()); | |
124 | + m[det.class_id].push_back(det);*/ | |
125 | + } | |
126 | + | |
127 | + for (auto it = m.begin(); it != m.end(); it++) { | |
128 | + //std::cout << it->second[0].class_id << " --- " << std::endl; | |
129 | + auto& dets = it->second; | |
130 | + std::sort(dets.begin(), dets.end(), cmp); | |
131 | + | |
132 | + for (size_t m = 0; m < dets.size(); ++m) { | |
133 | + auto& item = dets[m]; | |
134 | + | |
135 | + vector<float> det_temp(det_size); | |
136 | + //Yolo::Detection det_temp; | |
137 | + /*det_temp.conf = item.conf; | |
138 | + det_temp.class_id = item.class_id; | |
139 | + det_temp.bbox[0] = item.bbox[0] - item.bbox[2] / 2; | |
140 | + det_temp.bbox[1] = item.bbox[1] - item.bbox[3] / 2; | |
141 | + det_temp.bbox[2] = item.bbox[2]; | |
142 | + det_temp.bbox[3] = item.bbox[3];*/ | |
143 | + | |
144 | + det_temp[4] = item[4]; | |
145 | + det_temp[5] = item[5]; | |
146 | + det_temp[0] = item[0] - item[2] / 2; | |
147 | + det_temp[1] = item[1] - item[3] / 2; | |
148 | + det_temp[2] = item[2]; | |
149 | + det_temp[3] = item[3]; | |
150 | + | |
151 | + res.push_back(det_temp); | |
152 | + | |
153 | + for (size_t n = m + 1; n < dets.size(); ++n) { | |
154 | + if (iou(&item[0], &dets[n][0]) > nms_thresh) { | |
155 | + dets.erase(dets.begin() + n); | |
156 | + --n; | |
157 | + } | |
158 | + } | |
159 | + } | |
160 | + } | |
161 | +} | |
162 | + | |
163 | +int motor_phone_process_batch(void * handle, sy_img *image_data_array, int batchsize, motor_phone_result *result){ | |
164 | + Tools* tools = (Tools*) handle; | |
165 | + | |
166 | + int inputW = tools->phoneDetMotor->GetInputWidth(); | |
167 | + int inputH = tools->phoneDetMotor->GetInputHeight(); | |
168 | + | |
169 | + //printf("debug inputw:%d,inputh:%d\n",inputW,inputH); | |
170 | + | |
171 | + for (int b = 0; b < batchsize; b++) { | |
172 | + if (image_data_array[b].data_ == NULL || image_data_array[b].w_ == 0 || image_data_array[b].h_ == 0) { | |
173 | + ERROR_LOG(" Headshoulder get null input ptr!"); | |
174 | + return SY_FAILED; | |
175 | + } | |
176 | + | |
177 | + ImageData resizeImg, src; | |
178 | + // Utils::CopysyImageDataToDvpp(src, image_data_array[b]); | |
179 | + ACL_CALL(Utils::CopysyImageDataToDvppV2(src, image_data_array[b]), SY_SUCCESS, SY_FAILED); | |
180 | + ACL_CALL(tools->dvpp->CropAndPadding(resizeImg, src, inputW, inputH), SY_SUCCESS, SY_FAILED); | |
181 | + // forward | |
182 | + // double t1, t2; | |
183 | + // t1 = msecond(); | |
184 | + int ret = tools->phoneDetMotor->Inference(resizeImg); | |
185 | + if (ret != SY_SUCCESS) { | |
186 | + return SY_MODEL_FORWARD_ERROR; | |
187 | + } | |
188 | + // t2 = msecond(); | |
189 | + // printf("debug infer time: %.2f\n", t2 - t1); | |
190 | + | |
191 | + vector<float> detRes; | |
192 | + ret = tools->phoneDetMotor->PostProcess(detRes); | |
193 | + if (ret != SY_SUCCESS) { | |
194 | + return SY_MODEL_GETRESULT_ERROR; | |
195 | + } | |
196 | + | |
197 | + int img_w = image_data_array[b].w_; | |
198 | + int img_h = image_data_array[b].h_; | |
199 | + int INPUT_W = 224;//模型输入 | |
200 | + int INPUT_H = 224; | |
201 | + // int max_obj_data_count = 10;//4+多类置信度(这里是6类) 0912/1013 | |
202 | + int max_obj_data_count = 9;//4+多类置信度(这里是5类) 1127模型 | |
203 | + | |
204 | + // printf("datacount:%d\n",detRes.size()); | |
205 | + int num_det = detRes.size() / max_obj_data_count; | |
206 | + | |
207 | + int w, h, x, y; | |
208 | + float r_w = (float)INPUT_W / (img_w*1.0); | |
209 | + float r_h = (float)INPUT_H / (img_h*1.0); | |
210 | + float ratio = r_w < r_h ? r_w : r_h; | |
211 | + if (r_h > r_w) { | |
212 | + w = INPUT_W; | |
213 | + h = r_w * img_h; | |
214 | + x = 0; | |
215 | + y = (INPUT_H - h) / 2; | |
216 | + } | |
217 | + else { | |
218 | + w = r_h * img_w; | |
219 | + h = INPUT_H; | |
220 | + x = (INPUT_W - w) / 2; | |
221 | + y = 0; | |
222 | + } | |
223 | + | |
224 | + | |
225 | + vector<vector<float>> nms_res; | |
226 | + //printf("%d %d %d %d %d %d\n", img_w, img_h, w, h, x, y); | |
227 | + nms_yolov8(nms_res, detRes, num_det, max_obj_data_count, tools->phoneDetMotor->config.confThr, 0.7); | |
228 | + | |
229 | + int obj_count = 0; | |
230 | + result[b].objcount = 0; | |
231 | + for (int i = 0; i < nms_res.size(); i++) | |
232 | + { | |
233 | + //class_id, score, x1, y1, x2, y2 | |
234 | + int index = nms_res[i][5]; | |
235 | + | |
236 | + float detect_score = nms_res[i][4]; | |
237 | + if(detect_score > tools->phoneDetMotor->config.confThr) | |
238 | + { | |
239 | + | |
240 | + int x1 = (nms_res[i][0] - x) / ratio; | |
241 | + int y1 = (nms_res[i][1] - y) / ratio; | |
242 | + int x2 = nms_res[i][2] / ratio + x1; | |
243 | + int y2 = nms_res[i][3] / ratio + y1; | |
244 | + | |
245 | + //边界判断 | |
246 | + if(x1<0)x1=0; | |
247 | + if(y1<0)y1=0; | |
248 | + if(x2>=img_w) x2 = img_w-1; | |
249 | + if(y2>=img_h) y2 = img_h-1; | |
250 | + | |
251 | + result[b].objinfo[obj_count].left = x1; | |
252 | + result[b].objinfo[obj_count].top = y1; | |
253 | + result[b].objinfo[obj_count].right = x2; | |
254 | + result[b].objinfo[obj_count].bottom = y2; | |
255 | + | |
256 | + result[b].objinfo[obj_count].confidence = detect_score; | |
257 | + result[b].objinfo[obj_count].index = index; | |
258 | + | |
259 | + obj_count++; | |
260 | + } | |
261 | + } | |
262 | + result[b].objcount =obj_count; | |
263 | + | |
264 | + vector<vector<float>>().swap(nms_res); | |
265 | + vector<float>().swap(detRes); | |
266 | + | |
267 | + | |
268 | + | |
269 | + // result[i].objcount = detRes.size() > MAX_OBJ_COUNT ? MAX_OBJ_COUNT : detRes.size(); | |
270 | + // int objIdx = 0; | |
271 | + // for (auto& det : detRes) { | |
272 | + // if (objIdx >= MAX_OBJ_COUNT) continue; | |
273 | + // result[i].objinfo[objIdx].left = det[2]; | |
274 | + // result[i].objinfo[objIdx].top = det[3]; | |
275 | + // result[i].objinfo[objIdx].right = det[4]; | |
276 | + // result[i].objinfo[objIdx].bottom = det[5]; | |
277 | + // result[i].objinfo[objIdx].confidence = det[1]; | |
278 | + // objIdx++; | |
279 | + | |
280 | + // } | |
281 | + | |
282 | + | |
283 | + } | |
284 | + | |
285 | + return SY_SUCCESS; | |
286 | +} | |
287 | + | |
288 | +void motor_phone_release(void **handle) { | |
289 | + Tools* tools = (Tools*) handle; | |
290 | + if (tools) { | |
291 | + if (tools->phoneDetMotor) { | |
292 | + //delete tools->phoneDetMotor; | |
293 | + tools->phoneDetMotor = nullptr; | |
294 | + } | |
295 | + if (tools->dvpp) { | |
296 | + //delete tools->dvpp; | |
297 | + tools->dvpp = nullptr; | |
298 | + } | |
299 | + // aclFinalize(); | |
300 | + //delete tools; | |
301 | + tools = NULL; | |
302 | + } | |
303 | +} | |
304 | + | |
305 | +const char * motor_phone_getversion() { | |
306 | + return "motor_phone_vdec_arm_v310p_0.0.2.20231127_without_timelimit"; | |
307 | +} | ... | ... |
src/ai_engine_module/motor_phone_det.h
0 → 100755
1 | +/******************************************************************************************* | |
2 | +* Version: motor_phone_det_x64_v0.0.1 | |
3 | +* CopyRight: 中科视语(北京)科技有限公司 | |
4 | +* UpdateDate: 20230911 | |
5 | +* Content:二轮车玩手机检测 | |
6 | +********************************************************************************************/ | |
7 | +#ifndef MOTOR_PHONE_DET_H_ | |
8 | +#define MOTOR_PHONE_DET_H_ | |
9 | + | |
10 | +#ifdef _MSC_VER | |
11 | +#ifdef MOTOR_PHONE_DET_EXPORTS | |
12 | +#define MOTOR_PHONE_DET_API __declspec(dllexport) | |
13 | +#else | |
14 | +#define MOTOR_PHONE_DET_API __declspec(dllimport) | |
15 | +#endif | |
16 | +#else | |
17 | +#define MOTOR_PHONE_DET_API __attribute__ ((visibility ("default"))) | |
18 | +#endif | |
19 | + | |
20 | +#include "sy_common.h" | |
21 | + | |
22 | +#define MAX_OBJ_COUNT 1000 | |
23 | + | |
24 | +#ifdef __cplusplus | |
25 | +extern "C" | |
26 | +{ | |
27 | +#endif | |
28 | + | |
29 | + typedef struct motor_phone_info //结果结构体 | |
30 | + { | |
31 | + int left; | |
32 | + int top; | |
33 | + int right; | |
34 | + int bottom; | |
35 | + int index; | |
36 | + double confidence; // 置信度 | |
37 | + }motor_phone_info; | |
38 | + | |
39 | + typedef struct motor_phone_result | |
40 | + { | |
41 | + motor_phone_info objinfo[MAX_OBJ_COUNT]; | |
42 | + int objcount; | |
43 | + }; | |
44 | + | |
45 | + typedef struct motor_phone_param | |
46 | + { | |
47 | + //int mode; //运行模式 GPU_MODE 或者 CPU_MODE | |
48 | + int devId; //运行卡号 GPU模式下有效 | |
49 | + char* modelNames; | |
50 | + float thresld; //检测阈值 默认为0.3 | |
51 | + | |
52 | + //int engine; //指定运行引擎(ENGINE_MCAFFE2 / ENGINE_TENSORRT) | |
53 | + //int max_batch; //ָ指定trt最大batch数 | |
54 | + //char* trt_serialize_file; | |
55 | + //motor_phone_param() :mode(DEVICE_GPU), gpuid(0), thresld(0.4), engine(ENGINE_MCAFFE2), max_batch(10){}; | |
56 | + }motor_phone_param; | |
57 | + | |
58 | + /************************************************************************* | |
59 | + * FUNCTION: hst_init | |
60 | + * PURPOSE: 初始化 | |
61 | + * PARAM: | |
62 | + [in] handle -处理句柄 | |
63 | + [in] param -初始化参数 | |
64 | + * RETURN: handle | |
65 | + * NOTES:成功(0)或者错误代码(<0) | |
66 | + *************************************************************************/ | |
67 | + MOTOR_PHONE_DET_API int motor_phone_init(void **handle, motor_phone_param param); | |
68 | + MOTOR_PHONE_DET_API int motor_phone_process_batch(void * handle, sy_img *image_data_array, int batchsize, motor_phone_result *result); | |
69 | + | |
70 | + /************************************************************************* | |
71 | + * FUNCTION: hst_release | |
72 | + * PURPOSE: 资源释放 | |
73 | + * PARAM: | |
74 | + [in] handle - 处理句柄 | |
75 | + * RETURN: NULL | |
76 | + * NOTES: | |
77 | + *************************************************************************/ | |
78 | + MOTOR_PHONE_DET_API void motor_phone_release(void **handle); | |
79 | + | |
80 | + | |
81 | + /************************************************************************* | |
82 | + * FUNCTION: hst_process_gpu | |
83 | + * PURPOSE: | |
84 | + * PARAM: | |
85 | + [in] handle - 处理句柄 | |
86 | + [in] rgb - 图片数据(3通道BGR数据 cv::Mat格式) | |
87 | + [in] width - 图片宽度 | |
88 | + [in] height - 图片高度 | |
89 | + [in] result - 搜索结果,在外部申请足够内存 | |
90 | + * RETURN: -1:图像错误; 其他:检测到的个数 | |
91 | + * NOTES: | |
92 | + *************************************************************************/ | |
93 | + MOTOR_PHONE_DET_API int motor_phone_process(void * handle, sy_img image, motor_phone_result *result); | |
94 | + | |
95 | + /************************************************************************* | |
96 | + * FUNCTION: hst_getversion | |
97 | + * PURPOSE: 释放 | |
98 | + * PARAM: NULL | |
99 | + * RETURN: 版本号 | |
100 | + * NOTES: | |
101 | + *************************************************************************/ | |
102 | + MOTOR_PHONE_DET_API const char * motor_phone_getversion(); | |
103 | + | |
104 | +#ifdef __cplusplus | |
105 | +}; | |
106 | +#endif | |
107 | + | |
108 | +#endif | ... | ... |
src/ai_engine_module/motor_rainshed_cls.cpp
0 → 100755
1 | +#include "motor_rainshed_cls.h" | |
2 | +#include "sy_errorinfo.h" | |
3 | +#include "cnn_cls.h" | |
4 | +#include "dvpp_processx.h" | |
5 | +#include <time.h> | |
6 | +#include <sys/time.h> | |
7 | +#include "stream_data.h" | |
8 | + | |
9 | + | |
10 | +using namespace atlas_utils; | |
11 | +using namespace std; | |
12 | + | |
13 | +struct Resource { | |
14 | + aclrtContext ctx; | |
15 | + aclrtStream stream; | |
16 | +}; | |
17 | + | |
18 | +typedef struct Tools { | |
19 | + Resource src; | |
20 | + CnnCls* mRainCls; | |
21 | + DvppProcessx* dvpp; | |
22 | +}Tools; | |
23 | + | |
24 | +int mrc_init(void** handle, mrc_param param) { | |
25 | + | |
26 | + int ret = SY_SUCCESS; | |
27 | + Tools* tools = new Tools; | |
28 | + // init resource | |
29 | + // ACL_CALL(aclInit(nullptr), ACL_SUCCESS, SY_FAILED); | |
30 | + // ACL_CALL(aclrtSetDevice(param.devId), ACL_SUCCESS, SY_FAILED); | |
31 | + // ACL_CALL(aclrtCreateContext(&tools->src.ctx, param.devId), ACL_SUCCESS, SY_FAILED); | |
32 | + ACL_CALL(aclrtCreateStream(&tools->src.stream), ACL_SUCCESS, SY_FAILED); | |
33 | + | |
34 | + // motor rainshed classfication init | |
35 | + tools->mRainCls = new CnnCls(); | |
36 | + tools->mRainCls->config.confThr = param.thresld; | |
37 | + ret = tools->mRainCls->Init(param.modelNames); | |
38 | + if (ret != SY_SUCCESS) { | |
39 | + delete tools->mRainCls; | |
40 | + tools->mRainCls = nullptr; | |
41 | + return SY_FAILED; | |
42 | + } | |
43 | + | |
44 | + tools->dvpp = new DvppProcessx(); | |
45 | + tools->dvpp->InitResource(tools->src.stream); | |
46 | + | |
47 | + *handle = tools; | |
48 | + | |
49 | + return SY_SUCCESS; | |
50 | +} | |
51 | + | |
52 | + | |
53 | +// double msecond() { | |
54 | +// struct timeval tv; | |
55 | +// gettimeofday(&tv, 0); | |
56 | +// return (tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0); | |
57 | +// } | |
58 | + | |
59 | +int mrc_batch(void * handle, sy_img *img_data_array, int batch_size, mrc_result *result) { | |
60 | + Tools* tools = (Tools*) handle; | |
61 | + | |
62 | + int inputW = tools->mRainCls->GetInputWidth(); | |
63 | + int inputH = tools->mRainCls->GetInputHeight(); | |
64 | + | |
65 | + // printf("debug inputw:%d,inputh:%d\n",inputW,inputH); | |
66 | + | |
67 | + for (int i = 0; i < batch_size; i++) { | |
68 | + if (img_data_array[i].data_ == NULL || img_data_array[i].w_ == 0 || img_data_array[i].h_ == 0) { | |
69 | + ERROR_LOG("mRainCls get null input ptr!"); | |
70 | + return SY_FAILED; | |
71 | + } | |
72 | + ImageData resizeImg, src; | |
73 | + //debug======================================================================== | |
74 | + // src.width = img_data_array[i].w_; | |
75 | + // src.height = img_data_array[i].h_; | |
76 | + // src.alignWidth = ((src.width + 127) & ~(127)); | |
77 | + // src.alignHeight = ((src.height + 15) & ~(15)); | |
78 | + // src.size = src.alignWidth * src.alignHeight * 1.5; | |
79 | + // src.data.reset((uint8_t*)img_data_array[i].data_, [](uint8_t* p) { acldvppFree((void *)p); }); | |
80 | + //debug end==================================================================== | |
81 | + // Utils::CopysyImageDataToDvpp(src, img_data_array[i]); | |
82 | + ACL_CALL(Utils::CopysyImageDataToDvppV2(src, img_data_array[i]), SY_SUCCESS, SY_FAILED); | |
83 | + ACL_CALL(tools->dvpp->CropAndPaste(resizeImg, src, inputW, inputH), SY_SUCCESS, SY_FAILED); | |
84 | + // forward | |
85 | + //double t1, t2; | |
86 | + //t1 = msecond(); | |
87 | + int ret = tools->mRainCls->Inference(resizeImg); | |
88 | + if (ret != SY_SUCCESS) { | |
89 | + return SY_MODEL_FORWARD_ERROR; | |
90 | + } | |
91 | + //t2 = msecond(); | |
92 | + //printf("debug infer time: %.2f\n", t2 - t1); | |
93 | + | |
94 | + vector<float> mRainClsRes; | |
95 | + ret = tools->mRainCls->PostProcess(mRainClsRes); | |
96 | + if (ret != SY_SUCCESS) { | |
97 | + return SY_MODEL_GETRESULT_ERROR; | |
98 | + } | |
99 | + result[i].index = mRainClsRes[0]; | |
100 | + result[i].score = mRainClsRes[1]; | |
101 | + // printf("debug index:%d,confidence:%f\n",result[i].index,result[i].score); | |
102 | + // for (auto res : mRainClsRes) { | |
103 | + // printf("debug info: %f\n",res); | |
104 | + // } | |
105 | + | |
106 | + } | |
107 | + | |
108 | + return SY_SUCCESS; | |
109 | +} | |
110 | + | |
111 | +void mrc_release(void **handle) { | |
112 | + Tools* tools = (Tools*) handle; | |
113 | + // printf("debug line:%d\n",__LINE__); | |
114 | + if (tools) { | |
115 | + if (tools->mRainCls) { | |
116 | + // delete tools->mRainCls; | |
117 | + tools->mRainCls = nullptr; | |
118 | + } | |
119 | + // printf("debug line:%d\n",__LINE__); | |
120 | + if (tools->dvpp) { | |
121 | + // delete tools->dvpp; | |
122 | + tools->dvpp = nullptr; | |
123 | + } | |
124 | + //printf("debug line:%d\n",__LINE__); | |
125 | + // aclFinalize(); | |
126 | + aclrtDestroyStream(&tools->src.stream); | |
127 | + // delete tools; | |
128 | + tools = NULL; | |
129 | + //printf("debug line:%d\n",__LINE__); | |
130 | + } | |
131 | +} | |
132 | + | |
133 | +const char * mrc_get_version() { | |
134 | + return "motorrc_arm_vdec_310p_v0.0.1.20230921_without_timelimit"; | |
135 | +} | |
0 | 136 | \ No newline at end of file | ... | ... |
src/ai_engine_module/motor_rainshed_cls.h
0 → 100755
1 | +/************************************************************************* | |
2 | +* Version: motor_rainshed_cls_v0.0.0.20230921 | |
3 | +* CopyRight : 中国科学院自动化所模式识别实验室图像视频组 | |
4 | +* UpdateDate:20230921 | |
5 | +* Content : 二轮车是否加装雨棚 | |
6 | +*************************************************************************/ | |
7 | +#ifndef MOTORRAINCLS_H_ | |
8 | +#define MOTORRAINCLS_H_ | |
9 | + | |
10 | +#if _MSC_VER | |
11 | +#ifdef MOTORRAINCLS_EXPORTS | |
12 | +#define MOTORRAINCLS_API __declspec(dllexport) | |
13 | +#else | |
14 | +#define MOTORRAINCLS_API __declspec(dllimport) | |
15 | +#endif | |
16 | +#else | |
17 | +#define MOTORRAINCLS_API __attribute__ ((visibility ("default"))) | |
18 | +#endif | |
19 | + | |
20 | +#include "sy_common.h" | |
21 | + | |
22 | + | |
23 | + | |
24 | +#ifndef MAX_BATCH_SIZE | |
25 | +#define MAX_BATCH_SIZE 16 | |
26 | +#endif | |
27 | + | |
28 | + | |
29 | +#ifdef __cplusplus | |
30 | +extern "C" | |
31 | +{ | |
32 | +#endif | |
33 | + | |
34 | + | |
35 | + | |
36 | +//分类结果 | |
37 | +#ifndef MRCRESULT_ | |
38 | +#define MRCRESULT_ | |
39 | +typedef struct mrc_result | |
40 | +{ | |
41 | + float score; | |
42 | + int index; | |
43 | +}mrc_result; | |
44 | +#endif | |
45 | + | |
46 | + | |
47 | +#ifndef __MRCPARAM__ | |
48 | +#define __MRCPARAM__ | |
49 | + typedef struct mrc_param | |
50 | + { | |
51 | + //int mode; //运行模式(DEVICE_GPU / DEVICE_CPU) | |
52 | + //mrc_param() :mode(DEVICE_GPU), gpuid(0) {}; | |
53 | + int devId; //ָ指定显卡id | |
54 | + char* modelNames; | |
55 | + float thresld; //阈值 | |
56 | + }mrc_param; | |
57 | +#endif | |
58 | + | |
59 | + /************************************************************************* | |
60 | + * FUNCTION: mrc_init | |
61 | + * PURPOSE: 载入模型 | |
62 | + * PARAM: | |
63 | + [in] handle - 句柄 | |
64 | + [in] params - 参数 | |
65 | + * RETURN: 成功(0)或者错误代码 | |
66 | + * NOTES: | |
67 | + *************************************************************************/ | |
68 | + MOTORRAINCLS_API int mrc_init(void ** handle, mrc_param param); | |
69 | + | |
70 | + /************************************************************************* | |
71 | + * FUNCTION: mrc_process | |
72 | + * PURPOSE: 二轮车加装雨棚分类 | |
73 | + * PARAM: | |
74 | + [in] handle - 检测句柄 | |
75 | + [in] img_data - 图像数据 | |
76 | + [in] result - 结果 内存在外部申请 | |
77 | + * RETURN: 成功(0) 或 错误代码(< 0) | |
78 | + * NOTES: | |
79 | + *************************************************************************/ | |
80 | + MOTORRAINCLS_API int mrc_process(void *handle, sy_img img_data, mrc_result * result); | |
81 | + | |
82 | + /************************************************************************* | |
83 | + * FUNCTION: mrc_batch | |
84 | + * PURPOSE: 二轮车加装雨棚分类 batch | |
85 | + * PARAM: | |
86 | + [in] handle - 检测句柄 | |
87 | + [in] img_data_array - 图像数据 | |
88 | + [in] batch_size - 图像数目 | |
89 | + [in] result - 结果 内存在外部申请 | |
90 | + * RETURN: 成功(0) 或 错误代码(< 0) | |
91 | + * NOTES: | |
92 | + *************************************************************************/ | |
93 | + MOTORRAINCLS_API int mrc_batch(void *handle, sy_img* img_data_array, int batch_size, mrc_result * result); | |
94 | + | |
95 | + | |
96 | + /************************************************************************* | |
97 | + * FUNCTION: mrc_release | |
98 | + * PURPOSE: 释放 | |
99 | + * PARAM: | |
100 | + [in] handle - handle | |
101 | + * RETURN: NULL | |
102 | + * NOTES: | |
103 | + *************************************************************************/ | |
104 | + MOTORRAINCLS_API void mrc_release(void ** handle); | |
105 | + | |
106 | + | |
107 | + /************************************************************************* | |
108 | + * FUNCTION: mrc_get_version | |
109 | + * PURPOSE: | |
110 | + * PARAM: NULL | |
111 | + * RETURN: 版本号 | |
112 | + * NOTES: | |
113 | + *************************************************************************/ | |
114 | + MOTORRAINCLS_API const char * mrc_get_version(); | |
115 | + | |
116 | +#ifdef __cplusplus | |
117 | +}; | |
118 | +#endif | |
119 | + | |
120 | +#endif | ... | ... |
src/common/cnn/cnn_extractor.cpp
0 → 100755
1 | +#include "cnn_extractor.h" | |
2 | +#include <iostream> | |
3 | +#include "acl/acl.h" | |
4 | +#include "model_process.h" | |
5 | +#include "sy_errorinfo.h" | |
6 | +#include <time.h> | |
7 | +#include <sys/time.h> | |
8 | +#include <algorithm> | |
9 | + | |
10 | +using namespace std; | |
11 | + | |
12 | +namespace atlas_utils { | |
13 | + | |
14 | +int CNNExtract::Init(const char* modelPath) { | |
15 | + ACL_CALL(aclrtGetRunMode(&runMode_), SY_SUCCESS, SY_FAILED);//获取当前昇腾AI软件栈的运行模式,根据不同的运行模式,后续的接口调用方式不同 | |
16 | + ACL_CALL(model_.LoadModelFromFileWithMem(modelPath), SY_SUCCESS, SY_FAILED);//从文件加载离线模型数据,由用户自行管理模型运行的内存 | |
17 | + ACL_CALL(model_.CreateDesc(), SY_SUCCESS, SY_FAILED);//获取模型的描述信息 | |
18 | + ACL_CALL(model_.CreateOutput(outDims_), SY_SUCCESS, SY_FAILED); | |
19 | + ACL_CALL(model_.GetInputDims(inDims_), SY_SUCCESS, SY_FAILED); | |
20 | + modelHeight_ = inDims_[0][1]; | |
21 | + modelWidth_ = inDims_[0][2]; | |
22 | + | |
23 | + return SY_SUCCESS; | |
24 | +} | |
25 | + | |
26 | + | |
27 | +int CNNExtract::Inference(ImageData& input) { | |
28 | + model_.CreateInput(input.data.get(), input.size); | |
29 | + ACL_CALL(model_.Execute(), SY_SUCCESS, SY_FAILED); | |
30 | + model_.DestroyInput(); //需调用CreateInput的销毁类接口DestroyInput!!! | |
31 | + return SY_SUCCESS; | |
32 | +} | |
33 | + | |
34 | +int CNNExtract::GetInputWidth() { | |
35 | + return modelWidth_; | |
36 | +} | |
37 | + | |
38 | +int CNNExtract::GetInputHeight() { | |
39 | + return modelHeight_; | |
40 | +} | |
41 | + | |
42 | +int CNNExtract::PostProcess(vector<float>& results) { | |
43 | + aclmdlDataset* modelOutput = model_.GetModelOutputData(); | |
44 | + int outDatasetNum = aclmdlGetDatasetNumBuffers(modelOutput); | |
45 | + for (int i = 0; i < outDatasetNum; i++) { | |
46 | + aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(modelOutput, i); | |
47 | + if (dataBuffer == nullptr) { | |
48 | + return SY_FAILED; | |
49 | + } | |
50 | + uint32_t dataBufferSize = aclGetDataBufferSize(dataBuffer); | |
51 | + void* data = aclGetDataBufferAddr(dataBuffer); | |
52 | + if (data == nullptr) { | |
53 | + return SY_FAILED; | |
54 | + } | |
55 | + | |
56 | + int length = dataBufferSize/sizeof(float); | |
57 | + float outInfo[length]; | |
58 | + | |
59 | + if (runMode_ == ACL_HOST) { | |
60 | + ACL_CALL(aclrtMemcpy(outInfo, sizeof(outInfo), data, sizeof(outInfo), ACL_MEMCPY_DEVICE_TO_HOST), | |
61 | + ACL_SUCCESS, SY_FAILED); | |
62 | + } else { | |
63 | + ACL_CALL(aclrtMemcpy(outInfo, sizeof(outInfo), data, sizeof(outInfo), ACL_MEMCPY_DEVICE_TO_DEVICE),ACL_SUCCESS, SY_FAILED); | |
64 | + //return SY_FAILED; | |
65 | + } | |
66 | + | |
67 | + // //归一化 | |
68 | + // float sum = 0.0; | |
69 | + // for(int j=0;j<length;j++){ | |
70 | + // sum = sum + outInfo[j]*outInfo[j]; | |
71 | + // } | |
72 | + // sum = sqrt(sum) + 1e-6; | |
73 | + // for(int j=0;j<length;j++){ | |
74 | + // results.emplace_back(outInfo[j]/sum); | |
75 | + // } | |
76 | + | |
77 | + | |
78 | + for(uint32_t b = 0; b < length; b++) { | |
79 | + results.emplace_back(outInfo[b]); | |
80 | + } | |
81 | + | |
82 | + } | |
83 | + | |
84 | + | |
85 | + return SY_SUCCESS; | |
86 | +} | |
87 | + | |
88 | +void CNNExtract::Release() { | |
89 | + model_.Unload(); | |
90 | + model_.DestroyDesc(); | |
91 | + model_.DestroyOutput(); | |
92 | +} | |
93 | + | |
94 | +} | ... | ... |
src/common/cnn/cnn_extractor.h
0 → 100755
1 | +#ifndef _CNN_EXTRACT_H_ | |
2 | +#define _CNN_EXTRACT_H_ | |
3 | + | |
4 | +#include <memory> | |
5 | +#include "utils.h" | |
6 | +#include "acl/acl.h" | |
7 | +#include "model_process.h" | |
8 | + | |
9 | +using namespace std; | |
10 | + | |
11 | +namespace atlas_utils { | |
12 | + | |
13 | + | |
14 | +#ifndef DATA_TYPE_SIZE | |
15 | +#define DATA_TYPE_SIZE 4 | |
16 | +#endif | |
17 | + | |
18 | +class CNNExtract { | |
19 | +public: | |
20 | + struct ConfigParams { | |
21 | + float confThr = 0.0; | |
22 | + }; | |
23 | + | |
24 | + CNNExtract() {} | |
25 | + ~CNNExtract() { | |
26 | + Release(); | |
27 | + } | |
28 | + | |
29 | + int Init(const char* modelPath); | |
30 | + int Inference(ImageData& input); | |
31 | + int PostProcess(vector<float>& result); | |
32 | + | |
33 | + int GetInputWidth(); | |
34 | + int GetInputHeight(); | |
35 | + | |
36 | + ConfigParams config; | |
37 | + | |
38 | +private: | |
39 | + void Release(); | |
40 | + | |
41 | + vector<vector<int>> outDims_; | |
42 | + vector<vector<int>> inDims_; | |
43 | + ModelProcess model_; | |
44 | + uint32_t modelWidth_; | |
45 | + uint32_t modelHeight_; | |
46 | + aclrtRunMode runMode_; | |
47 | + | |
48 | +}; | |
49 | + | |
50 | +} | |
51 | + | |
52 | +#endif | ... | ... |