Blame view

src/PicAnalysis.cpp 13.9 KB
20396d5c   Hu Chunming   添加车头车尾算法
1
2
3
4
5
6
7
8
9
10
11
  #include "PicAnalysis.h"
  #include "./utils/logger.hpp"
  
  
  PicAnalysis::PicAnalysis(/* args */)
  {
      aclInit(nullptr);
  }
  
  PicAnalysis::~PicAnalysis()
  {
4a273a4a   Hu Chunming   添加hcp和hp
12
      release();
20396d5c   Hu Chunming   添加车头车尾算法
13
14
15
      aclFinalize();
  }
  
e8beee4d   Hu Chunming   实现jni接口
16
17
  int PicAnalysis::init(VillageParam param) {
  
e3062370   Hu Chunming   优化日志
18
19
20
21
22
23
24
25
      set_default_logger(LogLevel(param.log_level), "PicAnalysis", param.log_path.c_str(), param.log_mem, param.log_days);
  
      // 输入参数
      LOG_INFO("dev_id: {}", param.dev_id);
      LOG_INFO("sdk_path: {}", param.sdk_path);
      LOG_INFO("log_level: {}", param.log_level);
      LOG_INFO("log_days: {}", param.log_days);
  
e8beee4d   Hu Chunming   实现jni接口
26
      int dev_id = param.dev_id;
20396d5c   Hu Chunming   添加车头车尾算法
27
  
4a273a4a   Hu Chunming   添加hcp和hp
28
29
      int ret = SY_FAILED;
  
581a68a4   Hu Chunming   修正parse_road无返回值导...
30
      ret = m_vehicle_analysis.init(dev_id, param.sdk_path, 16);
9b88b335   Hu Chunming   恢复代码
31
32
33
34
      if(0 != ret){
          return -1;
      }
  
667e7a90   Hu Chunming   雨棚和打电话初步代码
35
36
37
      // head_tail_param ht_param;
      // ht_param.devId = dev_id;
      // ht_param.max_batch = 16;
581a68a4   Hu Chunming   修正parse_road无返回值导...
38
      // ht_param.sdk_path = param.sdk_path;
667e7a90   Hu Chunming   雨棚和打电话初步代码
39
40
41
42
      // ret = m_head_tail_algorithm.init(ht_param);
      // if(0 != ret){
      //     return -1;
      // }
9b88b335   Hu Chunming   恢复代码
43
  
581a68a4   Hu Chunming   修正parse_road无返回值导...
44
      ret = m_clothes_algorithm.init(dev_id, param.sdk_path);
9b88b335   Hu Chunming   恢复代码
45
46
47
48
      if(0 != ret){
          return -1;
      }
  
581a68a4   Hu Chunming   修正parse_road无返回值导...
49
      ret = m_human_algorithm.init(dev_id, param.sdk_path);
9b88b335   Hu Chunming   恢复代码
50
51
52
53
      if(0 != ret){
          return -1;
      }
  
581a68a4   Hu Chunming   修正parse_road无返回值导...
54
      ret = m_human_car_algorithm.init(dev_id, param.sdk_path);
9b88b335   Hu Chunming   恢复代码
55
56
57
58
      if(0 != ret){
          return -1;
      }
  
581a68a4   Hu Chunming   修正parse_road无返回值导...
59
      ret = m_motor_rainshed_algorithm.init(dev_id, param.sdk_path);
9b88b335   Hu Chunming   恢复代码
60
61
62
63
      if(0 != ret){
          return -1;
      }
  
581a68a4   Hu Chunming   修正parse_road无返回值导...
64
      ret = m_motor_phone_algorithm.init(dev_id, param.sdk_path);
9b88b335   Hu Chunming   恢复代码
65
66
67
      if(0 != ret){
          return -1;
      }
2ae58093   Hu Chunming   添加road_seg算法
68
  
581a68a4   Hu Chunming   修正parse_road无返回值导...
69
      ret = m_road_seg_algorithm.init(dev_id, param.sdk_path);
4a273a4a   Hu Chunming   添加hcp和hp
70
71
72
73
      if(0 != ret){
          return -1;
      }
  
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
74
75
76
77
78
      ret = m_crop_util.init(dev_id);
      if(0 != ret){
          return -1;
      }
  
4a273a4a   Hu Chunming   添加hcp和hp
79
80
      ACL_CALL(aclrtCreateContext(&m_ctx, 0), ACL_SUCCESS, SY_FAILED);
  	ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_SUCCESS, SY_FAILED);
20396d5c   Hu Chunming   添加车头车尾算法
81
82
83
84
85
86
87
  	ACL_CALL(aclrtCreateStream(&stream), ACL_SUCCESS, SY_FAILED);
      m_dvpp = new DvppProcess();
  	m_dvpp->InitResource(stream);
  
      return 0;
  }
  
427201b2   Hu Chunming   完善结果返回
88
89
90
  vector<AnalysisResult> PicAnalysis::analysis_file(vector<string> vec_file_path){
  
      vector<AnalysisResult> result;
20396d5c   Hu Chunming   添加车头车尾算法
91
  
e8beee4d   Hu Chunming   实现jni接口
92
93
94
      int ret = aclrtSetCurrentContext(m_ctx);
      if (SY_SUCCESS != ret) {
          printf("aclrtSetCurrentContext failed!");
427201b2   Hu Chunming   完善结果返回
95
          return result;
e8beee4d   Hu Chunming   实现jni接口
96
      }
15756629   Hu Chunming   添加clothes算法
97
  
20396d5c   Hu Chunming   添加车头车尾算法
98
99
100
101
      const int batch_size = vec_file_path.size();
  
      vector<sy_img> vec_img;
  
20396d5c   Hu Chunming   添加车头车尾算法
102
103
104
105
106
107
108
109
110
      // ImageData 内部是智能指针,分析未处理完成前不得释放
      ImageData dvpp_data[batch_size];
      for (size_t i = 0; i < vec_file_path.size(); i++)
      {
          string file_path = vec_file_path[i];
          ImageData src;
          ret = Utils::ReadImageFile(src, file_path); //将二进制图像读入内存,并读取宽高信息
          if(ret != SY_SUCCESS){
              LOG_ERROR("ReadImageFile failed!");
427201b2   Hu Chunming   完善结果返回
111
              return result;
20396d5c   Hu Chunming   添加车头车尾算法
112
113
114
115
116
          }
  
          ret = m_dvpp->CvtJpegToYuv420sp(dvpp_data[i], src); //解码
          if(ret != SY_SUCCESS){
              LOG_ERROR("CvtJpegToYuv420sp failed!");
427201b2   Hu Chunming   完善结果返回
117
              return result;
20396d5c   Hu Chunming   添加车头车尾算法
118
119
120
121
122
123
124
125
126
          }
          
          sy_img img;
          img.w_ = dvpp_data[i].width;
          img.h_ = dvpp_data[i].height;
          img.data_ = dvpp_data[i].data.get();
  
          vec_img.push_back(img);
      }
e8beee4d   Hu Chunming   实现jni接口
127
128
129
130
  
      return analysis_img(vec_img);
  }
  
427201b2   Hu Chunming   完善结果返回
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  vector<AnalysisResult> PicAnalysis::va_result2AnalysisResult(va_result* result, int batchsize) {
  
      vector<AnalysisResult> vec_result;
  
      for (int b = 0; b < batchsize; b++)
      {
          vector<VehicleInfo> vec_info;
          for(int c=0;c<result[b].count;c++)
          {
              vehicle_info result_info = result[b].info[c];
  
              VehicleInfo info;
              info.vehicle_detect_res         = result_info.vehicle_detect_res        ;	 
              info.vehicle_win_detect_res     = result_info.vehicle_win_detect_res    ; 	
              info.vehicle_body_detect_res    = result_info.vehicle_body_detect_res   ;	
              info.vehicle_color_res          = result_info.vehicle_color_res         ;
              info.vehicle_recg_res           = result_info.vehicle_recg_res          ;  
              info.vehicle_plate_det_recg_res = result_info.vehicle_plate_det_recg_res;	 
              info.vehicle_illegal_det_res    = result_info.vehicle_illegal_det_res   ; 	
              info.vehicle_fea_res            = result_info.vehicle_fea_res           ;   	
              info.mta_res                    = result_info.mta_res                   ;   
              info.manned_res	 	            = result_info.manned_res	 	        ;  
              info.type                       = result_info.type                      ;         
              info.vpt_type                   = result_info.vpt_type                  ;
              info.rainshed                   = result_info.rainshed                  ;  
  
              auto pendant_result = result_info.vehicle_pendant_det_res;
              for (size_t j = 0; j < pendant_result.count; j++)
              {
                  auto pendant_det_info = pendant_result.vpd_res[j];
  
                  pendant_info one_pendant_info;
                  one_pendant_info.rect = pendant_det_info.rect;
                  one_pendant_info.index = pendant_det_info.index;
                  one_pendant_info.confidence = pendant_det_info.confidence;
                  one_pendant_info.driver_copilot_info = pendant_det_info.driver_copilot_info;
                  info.vehicle_pendant_det_res.push_back(one_pendant_info);
              }
  
              vec_info.push_back(info);
          }
  
          AnalysisResult one_result;
          one_result.info = vec_info;
          vec_result.push_back(one_result);
      }
  
      return vec_result;
  }
  
  vector<AnalysisResult> PicAnalysis::analysis_img(vector<sy_img> vec_img){
  
      vector<AnalysisResult> vec_result;
e8beee4d   Hu Chunming   实现jni接口
184
185
186
187
188
189
  
      const int batch_size = vec_img.size();
  
      int ret = aclrtSetCurrentContext(m_ctx);
      if (SY_SUCCESS != ret) {
          printf("aclrtSetCurrentContext failed!");
427201b2   Hu Chunming   完善结果返回
190
          return vec_result;
e8beee4d   Hu Chunming   实现jni接口
191
192
      }
  
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
193
      va_result* result = m_vehicle_analysis.detect(vec_img);
427201b2   Hu Chunming   完善结果返回
194
      if (result) {
427201b2   Hu Chunming   完善结果返回
195
196
197
198
          vec_result = va_result2AnalysisResult(result, batch_size);
          m_vehicle_analysis.release_result(result, vec_img.size());
      }
  
81e8a405   Hu Chunming   初步完成SDK
199
      bool bConsistent = false;
06e0182f   Hu Chunming   修复src图片alignWidth...
200
201
      std::vector<RoadInfo> vec_road = m_road_seg_algorithm.detect(vec_img);
      if (vec_road.size() == batch_size && vec_result.size() == batch_size) {
81e8a405   Hu Chunming   初步完成SDK
202
          bConsistent = true;
06e0182f   Hu Chunming   修复src图片alignWidth...
203
204
205
206
207
          for (size_t i = 0; i < batch_size; i++) {
              vec_result[i].vec_line = vec_road[i].vec_line;
              vec_result[i].vec_road = vec_road[i].vec_road;
          }
      }
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
208
  
427201b2   Hu Chunming   完善结果返回
209
      for (int b = 0; b < vec_result.size(); b++)
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
210
      {
427201b2   Hu Chunming   完善结果返回
211
212
          vector<VehicleInfo>& vec_info = vec_result[b].info;
  
81e8a405   Hu Chunming   初步完成SDK
213
214
215
216
217
          RoadInfo road_info;
          if (bConsistent) {
              road_info = vec_road[b];
          }
  
e8beee4d   Hu Chunming   实现jni接口
218
          sy_img img = vec_img[b];
06e0182f   Hu Chunming   修复src图片alignWidth...
219
  
e8beee4d   Hu Chunming   实现jni接口
220
221
222
          ImageData src;
          src.width = img.w_;
          src.height = img.h_;
06e0182f   Hu Chunming   修复src图片alignWidth...
223
224
225
          src.alignWidth = ALIGN_UP16(img.w_);
          src.alignHeight = ALIGN_UP2(img.h_);
          src.size = YUV420SP_SIZE(src.alignWidth, src.alignHeight);
e8beee4d   Hu Chunming   实现jni接口
226
          src.data_naked = img.data_;
427201b2   Hu Chunming   完善结果返回
227
          for(int c=0;c<vec_info.size();c++)
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
228
          {
427201b2   Hu Chunming   完善结果返回
229
230
              VehicleInfo& result_info = vec_info[c];
              int shot_type=result_info.type;
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
231
  
81e8a405   Hu Chunming   初步完成SDK
232
233
              sy_rect vehicle_rect = result_info.vehicle_body_detect_res.rect;
  
5e69f8fc   Hu Chunming   补充字段;
234
235
              if (road_info.vec_direct.size() > 0 && (1 == shot_type || 0 == shot_type)) {
                  result_info.reverse_driving = m_road_seg_algorithm.check_reverse_driving(road_info.vec_direct, vehicle_rect, src.width, src.height, shot_type);
81e8a405   Hu Chunming   初步完成SDK
236
237
              }
  
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
238
239
240
              // 行人
              if (6 == shot_type)
              {
81e8a405   Hu Chunming   初步完成SDK
241
                  ImageData* human_data = m_crop_util.crop(src, vehicle_rect.left_, vehicle_rect.top_, vehicle_rect.left_ + vehicle_rect.width_, vehicle_rect.top_ + vehicle_rect.height_);
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
242
243
244
245
246
247
248
249
250
  
                  sy_img img;
                  img.w_ = human_data->alignWidth;
                  img.h_ = human_data->alignHeight;
                  img.data_ = human_data->data_naked;
  
                  vector<sy_img> vec_human_img;
                  vec_human_img.push_back(img);
  
427201b2   Hu Chunming   完善结果返回
251
252
253
254
                  vector<BodyColorInfo> vec_body_color = m_human_algorithm.detect(vec_human_img);
  
                  result_info.human_upper_color = vec_body_color[0].upper_body_color;
                  result_info.human_lower_color = vec_body_color[0].lower_body_color;
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
255
256
257
  
                  delete human_data;
                  human_data = nullptr;
667e7a90   Hu Chunming   雨棚和打电话初步代码
258
259
260
261
              } else if(1 == shot_type) {
                  // 车尾,判断是否 货车尾部货厢载人
                  if(result_info.vpt_type == 6 || result_info.vpt_type == 7){
                      if(result_info.manned_res.hs_count > 0){
5e69f8fc   Hu Chunming   补充字段;
262
                          result_info.truck_manned = 1;
667e7a90   Hu Chunming   雨棚和打电话初步代码
263
264
265
266
267
268
                      }	
                  }
              } else {
                  if(result_info.vpt_type == 1 || result_info.vpt_type == 2 || result_info.vpt_type == 3){
                      if(result_info.manned_res.hs_count >= 3){
                          // 摩托车、三轮车载人
5e69f8fc   Hu Chunming   补充字段;
269
                          result_info.motor_manned = 1;
667e7a90   Hu Chunming   雨棚和打电话初步代码
270
271
                      }	
                  }
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
272
273
274
              }
  
              // 司乘
427201b2   Hu Chunming   完善结果返回
275
276
              auto& pendant_res = result_info.vehicle_pendant_det_res;
              int vpd_num = pendant_res.size();
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
277
              vector<sy_img> vec_human_img;
427201b2   Hu Chunming   完善结果返回
278
              vector<ImageData*> vec_data;
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
279
280
              for(int p=0; p<vpd_num; p++)
              {
427201b2   Hu Chunming   完善结果返回
281
282
283
                  int index = pendant_res[p].index;
                  if(index == 0){ //driver
                      sy_rect human_rect = pendant_res[p].rect;
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
284
285
286
287
288
289
290
291
                      ImageData* human_data = m_crop_util.crop(src, human_rect.left_, human_rect.top_, human_rect.left_ + human_rect.width_, human_rect.top_ + human_rect.height_);
  
                      sy_img img;
                      img.w_ = human_data->alignWidth;
                      img.h_ = human_data->alignHeight;
                      img.data_ = human_data->data_naked;
  
                      vec_human_img.push_back(img);
427201b2   Hu Chunming   完善结果返回
292
293
  
                      vec_data.push_back(human_data);
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
294
295
296
                  }
              }
  
427201b2   Hu Chunming   完善结果返回
297
              vector<int> vec_color = m_clothes_algorithm.detect(vec_human_img);
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
298
  
427201b2   Hu Chunming   完善结果返回
299
              for(int p=0; p<vec_color.size(); p++)
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
300
              {
427201b2   Hu Chunming   完善结果返回
301
302
303
304
305
306
307
                  int index = pendant_res[p].index;
                  if(index == 0) { // 更新司乘衣着颜色
                      pendant_res[p].iColor = vec_color[p];
  
                      ImageData* human_data = vec_data[p];
                      delete human_data;
                      human_data = nullptr;
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
308
309
310
311
312
313
314
315
316
317
                  }
              }
  
              // 摩托车
              if(shot_type==2)//摩托车
              {
                  //摩托车驾驶人是否戴安全帽
                  // analysis_result.motor_helmeted = result_info.mta_res.motor_driver_helmeted.status;
                  // float score=result_info.mta_res.motor_driver_helmeted.confidence;
  
81e8a405   Hu Chunming   初步完成SDK
318
                  ImageData* motor_data = m_crop_util.crop(src, vehicle_rect.left_, vehicle_rect.top_, vehicle_rect.left_ + vehicle_rect.width_, vehicle_rect.top_ + vehicle_rect.height_);
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
319
320
321
322
323
324
325
326
327
  
                  sy_img img;
                  img.w_ = motor_data->alignWidth;
                  img.h_ = motor_data->alignHeight;
                  img.data_ = motor_data->data_naked;
  
                  vector<sy_img> vec_motor_img;
                  vec_motor_img.push_back(img);
  
6f9dffde   Hu Chunming   返回prob
328
329
330
331
                  vector<MotorRainshedResult> vec_rainshed_result = m_motor_rainshed_algorithm.detect(vec_motor_img);
                  if (vec_rainshed_result.size() > 0) {
                      result_info.rainshed = vec_rainshed_result[0].rainshed;
                      result_info.rainshed_prob = vec_rainshed_result[0].prob;
667e7a90   Hu Chunming   雨棚和打电话初步代码
332
                  }
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
333
  
6f9dffde   Hu Chunming   返回prob
334
335
336
337
                  vector<MotorPhoneResult> vec_phone_result = m_motor_phone_algorithm.detect(vec_motor_img);
                  if (vec_phone_result.size() > 0) {
                      result_info.phoning = vec_phone_result[0].phoning;
                      result_info.phoning_prob = vec_phone_result[0].prob;
81e8a405   Hu Chunming   初步完成SDK
338
339
                  }
  
f258e1c8   Hu Chunming   添加非机动车驾乘信息
340
                  int iColor = -1;
81e8a405   Hu Chunming   初步完成SDK
341
                  std::vector<HumanCarResult> vec_hcp_result = m_human_car_algorithm.detect(vec_motor_img);
5e69f8fc   Hu Chunming   补充字段;
342
                  if (vec_hcp_result.size() > 0 && road_info.vec_direct.size() > 0) {
a1a053f2   Hu Chunming   修正人骑车逻辑错误
343
                      int head_or_tail = vec_hcp_result[0].orient;
5e69f8fc   Hu Chunming   补充字段;
344
345
                      if (head_or_tail == 0 || head_or_tail == 1) {
                          result_info.reverse_driving = m_road_seg_algorithm.check_reverse_driving(road_info.vec_direct, vehicle_rect, src.width, src.height, head_or_tail);
81e8a405   Hu Chunming   初步完成SDK
346
                      }
f258e1c8   Hu Chunming   添加非机动车驾乘信息
347
348
349
350
351
352
353
354
355
356
357
358
359
360
                      iColor = vec_hcp_result[0].up_color;
                  }
  
                  int hs_num = result_info.manned_res.hs_count;
                  for (size_t i = 0; i < hs_num; i++)
                  {
                      pendant_info one_pendant;
                      one_pendant.confidence = result_info.manned_res.hs_rect[i].score;
                      one_pendant.rect = result_info.manned_res.hs_rect[i].rect;
                      one_pendant.index = 0;
                      one_pendant.iColor = iColor;
  
                      result_info.vehicle_pendant_det_res.push_back(one_pendant);
                      
667e7a90   Hu Chunming   雨棚和打电话初步代码
361
362
                  }
  
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
363
364
365
                  delete motor_data;
                  motor_data = nullptr;
              }
81e8a405   Hu Chunming   初步完成SDK
366
367
  
              //压黄实线
581a68a4   Hu Chunming   修正parse_road无返回值导...
368
              result_info.cross_line = m_road_seg_algorithm.check_cross_line(road_info.vec_line, vehicle_rect, src.width, src.height);
81e8a405   Hu Chunming   初步完成SDK
369
              // 压导流线
581a68a4   Hu Chunming   修正parse_road无返回值导...
370
              result_info.cross_diversion_line = m_road_seg_algorithm.check_cross_region(road_info.vec_road, vehicle_rect, src.width, src.height, 3);    //3是导流线区域
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
371
372
          }
      }
20396d5c   Hu Chunming   添加车头车尾算法
373
  
667e7a90   Hu Chunming   雨棚和打电话初步代码
374
375
376
377
378
379
      // vector<HeadTailResult> head_tail_result;
      // ret = m_head_tail_algorithm.detect(vec_img, head_tail_result);
      // if (0 != ret) {
      //     LOG_ERROR("m_head_tail_algorithm failed!");
      //     head_tail_result.clear();
      // }
e8beee4d   Hu Chunming   实现jni接口
380
  
06e0182f   Hu Chunming   修复src图片alignWidth...
381
      LOG_INFO("analysis finished!");
4a273a4a   Hu Chunming   添加hcp和hp
382
  
427201b2   Hu Chunming   完善结果返回
383
      return vec_result;
20396d5c   Hu Chunming   添加车头车尾算法
384
385
386
  }
  
  int PicAnalysis::release() {
4a273a4a   Hu Chunming   添加hcp和hp
387
388
  
      ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_SUCCESS, SY_FAILED);
20396d5c   Hu Chunming   添加车头车尾算法
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
  
      delete m_dvpp;
      m_dvpp = nullptr;
  
      if (stream != nullptr) {
          int ret = aclrtDestroyStream(stream);
          if (ret != ACL_SUCCESS) {
              LOG_ERROR("destroy stream failed");
          }
          stream = nullptr;
      }
  
      aclrtDestroyContext(m_ctx);
  
      return 0;
20396d5c   Hu Chunming   添加车头车尾算法
404
  }