Commit 52516fb93472b0e1a6cf6d87ad3426f7db96c7f0
1 parent
3f4b245b
添加授权代码;
代码优化
Showing
4 changed files
with
197 additions
and
102 deletions
src/PicAnalysis.cpp
1 | 1 | #include "PicAnalysis.h" |
2 | 2 | #include "./utils/logger.hpp" |
3 | 3 | |
4 | +#define AUTHORIZATION | |
5 | +#define productSN "C7C1FF4EDEF1452F8BBEC8B36EAECEC0" //linux 农村一体机图片版 产品序列号 | |
4 | 6 | |
5 | 7 | PicAnalysis::PicAnalysis(/* args */) |
6 | 8 | { |
... | ... | @@ -34,6 +36,18 @@ int PicAnalysis::init(VillageParam param) { |
34 | 36 | |
35 | 37 | int ret = SY_FAILED; |
36 | 38 | |
39 | +#ifndef AUTHORIZATION | |
40 | + if (!CheckTime()) { //时间限制 | |
41 | + LOG_ERROR("CheckTime failed."); | |
42 | + return -106 ; | |
43 | + } | |
44 | +#else | |
45 | + if (!CheckLabel(dev_id)) { //机器授权 | |
46 | + LOG_ERROR("CheckLabel failed."); | |
47 | + return -106 ; | |
48 | + } | |
49 | +#endif | |
50 | + | |
37 | 51 | VehicleAnalysisParam vehicle_analysis_param; |
38 | 52 | vehicle_analysis_param.devId = dev_id; |
39 | 53 | vehicle_analysis_param.sdk_root = param.sdk_path; |
... | ... | @@ -139,64 +153,30 @@ vector<AnalysisResult> PicAnalysis::analysis_file(vector<string> vec_file_path){ |
139 | 153 | return analysis_img(vec_img); |
140 | 154 | } |
141 | 155 | |
142 | -vector<AnalysisResult> PicAnalysis::va_result2AnalysisResult(va_result* result, int batchsize) { | |
143 | - | |
144 | - vector<AnalysisResult> vec_result; | |
156 | +vector<AnalysisResult> PicAnalysis::analysis_img(vector<sy_img> vec_img){ | |
145 | 157 | |
146 | - for (int b = 0; b < batchsize; b++) | |
147 | - { | |
148 | - vector<VehicleInfo> vec_info; | |
149 | - for(int c=0;c<result[b].count;c++) | |
150 | - { | |
151 | - vehicle_info result_info = result[b].info[c]; | |
152 | - | |
153 | - VehicleInfo info; | |
154 | - info.vehicle_detect_res = result_info.vehicle_detect_res ; | |
155 | - info.vehicle_win_detect_res = result_info.vehicle_win_detect_res ; | |
156 | - info.vehicle_body_detect_res = result_info.vehicle_body_detect_res ; | |
157 | - info.vehicle_color_res = result_info.vehicle_color_res ; | |
158 | - info.vehicle_recg_res = result_info.vehicle_recg_res ; | |
159 | - info.vehicle_plate_det_recg_res = result_info.vehicle_plate_det_recg_res; | |
160 | - info.vehicle_illegal_det_res = result_info.vehicle_illegal_det_res ; | |
161 | - info.vehicle_fea_res = result_info.vehicle_fea_res ; | |
162 | - info.mta_res = result_info.mta_res ; | |
163 | - info.manned_res = result_info.manned_res ; | |
164 | - info.type = result_info.type ; | |
165 | - info.vpt_type = result_info.vpt_type ; | |
166 | - | |
167 | - | |
168 | - auto pendant_result = result_info.vehicle_pendant_det_res; | |
169 | - for (size_t j = 0; j < pendant_result.count; j++) | |
170 | - { | |
171 | - auto pendant_det_info = pendant_result.vpd_res[j]; | |
172 | - | |
173 | - pendant_info one_pendant_info; | |
174 | - one_pendant_info.rect = pendant_det_info.rect; | |
175 | - one_pendant_info.index = pendant_det_info.index; | |
176 | - one_pendant_info.confidence = pendant_det_info.confidence; | |
177 | - one_pendant_info.driver_copilot_info = pendant_det_info.driver_copilot_info; | |
178 | - info.vehicle_pendant_det_res.push_back(one_pendant_info); | |
179 | - } | |
158 | + std::lock_guard<std::mutex> l(m_single_mtx); | |
180 | 159 | |
181 | - vec_info.push_back(info); | |
182 | - } | |
160 | + vector<AnalysisResult> vec_result; | |
183 | 161 | |
184 | - AnalysisResult one_result; | |
185 | - one_result.info = vec_info; | |
186 | - vec_result.push_back(one_result); | |
162 | +#ifndef AUTHORIZATION | |
163 | + if (!CheckTime()) { //时间限制 | |
164 | + LOG_FATAL("authority failed!"); | |
165 | + return vec_result; | |
187 | 166 | } |
188 | - | |
189 | - return vec_result; | |
190 | -} | |
191 | - | |
192 | -vector<AnalysisResult> PicAnalysis::analysis_img(vector<sy_img> vec_img){ | |
167 | +#else | |
168 | + if (check_label == -1) { //机器授权 | |
169 | + LOG_FATAL("authority failed!"); | |
170 | + return vec_result; | |
171 | + } | |
172 | + if (!UpdateLabel()) { | |
173 | + LOG_FATAL("authority failed!"); | |
174 | + return vec_result; | |
175 | + } | |
176 | +#endif | |
193 | 177 | |
194 | 178 | LOG_INFO("vec_img size:{}", vec_img.size()); |
195 | 179 | |
196 | - std::lock_guard<std::mutex> l(m_single_mtx); | |
197 | - | |
198 | - vector<AnalysisResult> vec_result; | |
199 | - | |
200 | 180 | const int batch_size = vec_img.size(); |
201 | 181 | |
202 | 182 | int ret = aclrtSetCurrentContext(m_ctx); |
... | ... | @@ -205,11 +185,7 @@ vector<AnalysisResult> PicAnalysis::analysis_img(vector<sy_img> vec_img){ |
205 | 185 | return vec_result; |
206 | 186 | } |
207 | 187 | |
208 | - va_result* result = m_vehicle_analysis.detect(vec_img); | |
209 | - if (result) { | |
210 | - vec_result = va_result2AnalysisResult(result, batch_size); | |
211 | - m_vehicle_analysis.release_result(result, vec_img.size()); | |
212 | - } | |
188 | + vec_result = m_vehicle_analysis.detect(vec_img); | |
213 | 189 | |
214 | 190 | LOG_INFO("vec_result size:{}", vec_result.size()); |
215 | 191 | |
... | ... | @@ -426,4 +402,105 @@ int PicAnalysis::release() { |
426 | 402 | aclrtDestroyContext(m_ctx); |
427 | 403 | |
428 | 404 | return 0; |
405 | +} | |
406 | + | |
407 | +bool PicAnalysis::CheckLabel(int devId) { | |
408 | + const char *token_password = "village_atlas_arm_2023"; | |
409 | + std::string guid = std::string("village_atlas_arm")+std::to_string(devId); | |
410 | + atlas_licence_param lic_param; | |
411 | + memset(&lic_param, 0, sizeof(atlas_licence_param)); | |
412 | + sprintf(lic_param.product_id, "%s", productSN); | |
413 | + sprintf(lic_param.guid, "%s",guid.c_str()); | |
414 | + sprintf(lic_param.token_pwd, "%s", token_password); | |
415 | + lic_param.channel_num = 1; | |
416 | + atlas_licence_token param_token; | |
417 | + memset(¶m_token, 0, sizeof(atlas_licence_token)); | |
418 | + | |
419 | + int result = atlas_licence_connect(&(skt_handle), lic_param, ¶m_token); | |
420 | + //printf("result:%d,code:%d,msg:%s \n", result, param_token.code, param_token.msg); | |
421 | + //printf("token:%s\n", param_token.token); | |
422 | + std::string recv_token = std::string(param_token.token); | |
423 | + | |
424 | + //atlas_licence_check_param check_param; | |
425 | + memset(&(check_param), 0, sizeof(atlas_licence_check_param)); | |
426 | + sprintf(check_param.token_pwd, "%s", token_password); | |
427 | + sprintf(check_param.time, "%s", "2023-01-10 20:00:00"); | |
428 | + sprintf(check_param.token, "%s", recv_token .c_str()); | |
429 | + check_param.consume = 2; | |
430 | + | |
431 | + //授权check------------ | |
432 | + check_label = -1;//初始值 | |
433 | + std::cout << "sy_licence_check start." << std::endl; | |
434 | + atlas_licence_check_result check_result; | |
435 | + memset(&check_result, 0, sizeof(atlas_licence_check_result)); | |
436 | + check_result.code = -1; | |
437 | + | |
438 | + int res = atlas_licence_check(skt_handle, check_param, &check_result); | |
439 | + if(res!=0) { | |
440 | + std::cout << "sy_licence_check failed." << std::endl; | |
441 | + return false; | |
442 | + } | |
443 | + //std::cout << "code:" << check_result.code << ",msg:" << check_result.msg << std::endl; | |
444 | + if(check_result.code!=0) { | |
445 | + std::cout << "code:" << check_result.code << ",msg:" << check_result.msg << std::endl; | |
446 | + return false; | |
447 | + } | |
448 | + std::cout << "sy_licence_check end." << std::endl; | |
449 | + | |
450 | + check_label = 0;//授权成功 | |
451 | + return true; | |
452 | +} | |
453 | + | |
454 | +bool PicAnalysis::UpdateLabel() { | |
455 | + //获取系统时间,每个月1号check一次授权 | |
456 | + struct tm* info; | |
457 | + int nYear, nMonth, nDay; | |
458 | + time_t raw; | |
459 | + time(&raw); | |
460 | + info = localtime(&raw); | |
461 | + nYear = info->tm_year + 1900; | |
462 | + nMonth = info->tm_mon + 1; | |
463 | + nDay = info->tm_mday; | |
464 | + if(nDay==1) { | |
465 | + if(check_label ==0) { | |
466 | + LOG_INFO("atlas_licence_check start."); | |
467 | + atlas_licence_check_result check_result; | |
468 | + memset(&check_result, 0, sizeof(atlas_licence_check_result)); | |
469 | + check_result.code = -1; | |
470 | + | |
471 | + int res = atlas_licence_check(skt_handle, check_param, &check_result); | |
472 | + if(res!=0) { | |
473 | + LOG_FATAL("sy_licence_check failed."); | |
474 | + return false; | |
475 | + } | |
476 | + if(check_result.code!=0) { | |
477 | + check_label = -1; | |
478 | + LOG_FATAL("atlas_licence_check code:{}, msg:{}",check_result.code, check_result.msg); | |
479 | + return false; | |
480 | + } | |
481 | + LOG_INFO("atlas_licence_check end."); | |
482 | + check_label =1; | |
483 | + } | |
484 | + } | |
485 | + else { | |
486 | + check_label =0; | |
487 | + } | |
488 | + return true; | |
489 | +} | |
490 | + | |
491 | +bool PicAnalysis::CheckTime() { | |
492 | + struct tm* info; | |
493 | + int nYear, nMonth, nDay; | |
494 | + time_t raw; | |
495 | + time(&raw); | |
496 | + info = localtime(&raw); | |
497 | + nYear = info->tm_year + 1900; | |
498 | + nMonth = info->tm_mon + 1; | |
499 | + nDay = info->tm_mday; | |
500 | + | |
501 | + if (nYear == 2025 && (nMonth <= 11 || (nMonth <= 12 && nDay <= 31))){ | |
502 | + return true; | |
503 | + } else { | |
504 | + return false; | |
505 | + } | |
429 | 506 | } |
430 | 507 | \ No newline at end of file | ... | ... |
src/PicAnalysis.h
... | ... | @@ -12,6 +12,7 @@ |
12 | 12 | #include "./utils/PathUtils.hpp" |
13 | 13 | |
14 | 14 | #include "village_inc.h" |
15 | +#include "atlas_licence.h" | |
15 | 16 | |
16 | 17 | #include <mutex> |
17 | 18 | |
... | ... | @@ -33,7 +34,9 @@ public: |
33 | 34 | private: |
34 | 35 | int release(); |
35 | 36 | |
36 | - vector<AnalysisResult> va_result2AnalysisResult(va_result* result, int batchsize); | |
37 | + bool CheckTime(); | |
38 | + bool CheckLabel(int devId); | |
39 | + bool UpdateLabel(); | |
37 | 40 | |
38 | 41 | private: |
39 | 42 | aclrtContext m_ctx{nullptr}; |
... | ... | @@ -52,6 +55,10 @@ private: |
52 | 55 | CropUtil m_crop_util; |
53 | 56 | |
54 | 57 | std::mutex m_single_mtx; |
58 | + | |
59 | + void *skt_handle {nullptr};//授权 | |
60 | + atlas_licence_check_param check_param;//授权 | |
61 | + int check_label {-1};//授权 -1=未授权 0=授权 1=授权check日已经check成功(每月一次check) | |
55 | 62 | }; |
56 | 63 | |
57 | 64 | ... | ... |
src/ai_engine_module/VehicleAnalysis.cpp
... | ... | @@ -72,45 +72,24 @@ int VehicleAnalysis::init(VehicleAnalysisParam tParam) { |
72 | 72 | return 0; |
73 | 73 | } |
74 | 74 | |
75 | -va_result * VehicleAnalysis::detect(vector<sy_img> vec_img) { | |
75 | +vector<AnalysisResult> VehicleAnalysis::detect(vector<sy_img> vec_img) { | |
76 | 76 | |
77 | 77 | int batch_size = vec_img.size(); |
78 | 78 | |
79 | 79 | va_result *result=new va_result[batch_size]; |
80 | - for(int b=0;b<batch_size;b++) | |
81 | - { | |
82 | - result[b].count=0; | |
83 | - result[b].info=new vehicle_info[100]; | |
84 | - for(int c=0; c<100; c++) | |
85 | - { | |
86 | - result[b].info[c].vehicle_pendant_det_res.vpd_res=new v_pendant_d_info[300]; | |
87 | - } | |
88 | - } | |
89 | 80 | |
90 | 81 | LOG_INFO("va_batch start"); |
91 | 82 | |
92 | 83 | int ret = va_batch(m_handle, vec_img.data(), batch_size, result); |
93 | 84 | |
85 | + auto vec_result = va_result2AnalysisResult(result, batch_size); | |
86 | + | |
87 | + delete[] result; | |
88 | + result = nullptr; | |
89 | + | |
94 | 90 | LOG_INFO("va_batch end:{}", ret); |
95 | 91 | |
96 | - //delete result | |
97 | - // for(int b=0;b<batch_size;b++) | |
98 | - // { | |
99 | - // for(int c=0; c<100; c++) | |
100 | - // { | |
101 | - // if(result[b].info[c].vehicle_pendant_det_res.vpd_res!=NULL) | |
102 | - // delete[] result[b].info[c].vehicle_pendant_det_res.vpd_res; | |
103 | - // } | |
104 | - // if(result[b].info!=NULL){ | |
105 | - // delete[] result[b].info; | |
106 | - // } | |
107 | - // } | |
108 | - | |
109 | - // if(result!=NULL){ | |
110 | - // delete[] result; | |
111 | - // } | |
112 | - | |
113 | - return result; | |
92 | + return vec_result; | |
114 | 93 | } |
115 | 94 | |
116 | 95 | void VehicleAnalysis::release(){ |
... | ... | @@ -119,22 +98,52 @@ void VehicleAnalysis::release(){ |
119 | 98 | } |
120 | 99 | } |
121 | 100 | |
122 | -void VehicleAnalysis::release_result(va_result* result, int batch_size){ | |
123 | - for(int b=0;b<batch_size;b++) | |
101 | +vector<AnalysisResult> VehicleAnalysis::va_result2AnalysisResult(va_result* result, int batchsize) { | |
102 | + | |
103 | + vector<AnalysisResult> vec_result; | |
104 | + | |
105 | + for (int b = 0; b < batchsize; b++) | |
124 | 106 | { |
125 | - for(int c=0; c<100; c++) | |
107 | + vector<VehicleInfo> vec_info; | |
108 | + for(int c=0;c<result[b].count;c++) | |
126 | 109 | { |
127 | - if(result[b].info[c].vehicle_pendant_det_res.vpd_res!=NULL) | |
128 | - delete[] result[b].info[c].vehicle_pendant_det_res.vpd_res; | |
129 | - | |
130 | - } | |
131 | - if(result[b].info!=NULL){ | |
132 | - delete[] result[b].info; | |
110 | + vehicle_info result_info = result[b].info[c]; | |
111 | + | |
112 | + VehicleInfo info; | |
113 | + info.vehicle_detect_res = result_info.vehicle_detect_res ; | |
114 | + info.vehicle_win_detect_res = result_info.vehicle_win_detect_res ; | |
115 | + info.vehicle_body_detect_res = result_info.vehicle_body_detect_res ; | |
116 | + info.vehicle_color_res = result_info.vehicle_color_res ; | |
117 | + info.vehicle_recg_res = result_info.vehicle_recg_res ; | |
118 | + info.vehicle_plate_det_recg_res = result_info.vehicle_plate_det_recg_res; | |
119 | + info.vehicle_illegal_det_res = result_info.vehicle_illegal_det_res ; | |
120 | + info.vehicle_fea_res = result_info.vehicle_fea_res ; | |
121 | + info.mta_res = result_info.mta_res ; | |
122 | + info.manned_res = result_info.manned_res ; | |
123 | + info.type = result_info.type ; | |
124 | + info.vpt_type = result_info.vpt_type ; | |
125 | + | |
126 | + | |
127 | + auto pendant_result = result_info.vehicle_pendant_det_res; | |
128 | + for (size_t j = 0; j < pendant_result.count; j++) | |
129 | + { | |
130 | + auto pendant_det_info = pendant_result.vpd_res[j]; | |
131 | + | |
132 | + pendant_info one_pendant_info; | |
133 | + one_pendant_info.rect = pendant_det_info.rect; | |
134 | + one_pendant_info.index = pendant_det_info.index; | |
135 | + one_pendant_info.confidence = pendant_det_info.confidence; | |
136 | + one_pendant_info.driver_copilot_info = pendant_det_info.driver_copilot_info; | |
137 | + info.vehicle_pendant_det_res.push_back(one_pendant_info); | |
138 | + } | |
139 | + | |
140 | + vec_info.push_back(info); | |
133 | 141 | } |
134 | - | |
135 | - } | |
136 | 142 | |
137 | - if(result!=NULL){ | |
138 | - delete[] result; | |
143 | + AnalysisResult one_result; | |
144 | + one_result.info = vec_info; | |
145 | + vec_result.push_back(one_result); | |
139 | 146 | } |
140 | -} | |
147 | + | |
148 | + return vec_result; | |
149 | +} | |
141 | 150 | \ No newline at end of file | ... | ... |
src/ai_engine_module/VehicleAnalysis.h
... | ... | @@ -5,6 +5,8 @@ |
5 | 5 | |
6 | 6 | #include "vehicle_analysis.h" |
7 | 7 | |
8 | +#include "../village_inc.h" | |
9 | + | |
8 | 10 | struct DriverInfo { |
9 | 11 | sy_rect driver_rect; |
10 | 12 | float driver_prob; |
... | ... | @@ -46,13 +48,13 @@ public: |
46 | 48 | |
47 | 49 | int init(VehicleAnalysisParam tParam); |
48 | 50 | |
49 | - va_result* detect(vector<sy_img> vec_img); | |
50 | - | |
51 | - void release_result(va_result*, int); | |
51 | + vector<AnalysisResult> detect(vector<sy_img> vec_img); | |
52 | 52 | |
53 | 53 | private: |
54 | 54 | void release(); |
55 | 55 | |
56 | + vector<AnalysisResult> va_result2AnalysisResult(va_result* result, int batchsize); | |
57 | + | |
56 | 58 | private: |
57 | 59 | void* m_handle{nullptr}; |
58 | 60 | ... | ... |