Blame view

src/PicAnalysis.cpp 8.29 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
18
  int PicAnalysis::init(VillageParam param) {
  
      int dev_id = param.dev_id;
20396d5c   Hu Chunming   添加车头车尾算法
19
  
4a273a4a   Hu Chunming   添加hcp和hp
20
21
      int ret = SY_FAILED;
  
9b88b335   Hu Chunming   恢复代码
22
23
24
25
26
      ret = m_vehicle_analysis.init(dev_id, 16);
      if(0 != ret){
          return -1;
      }
  
667e7a90   Hu Chunming   雨棚和打电话初步代码
27
28
29
30
31
32
33
      // head_tail_param ht_param;
      // ht_param.devId = dev_id;
      // ht_param.max_batch = 16;
      // ret = m_head_tail_algorithm.init(ht_param);
      // if(0 != ret){
      //     return -1;
      // }
9b88b335   Hu Chunming   恢复代码
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  
      ret = m_clothes_algorithm.init(dev_id);
      if(0 != ret){
          return -1;
      }
  
      ret = m_human_algorithm.init(dev_id);
      if(0 != ret){
          return -1;
      }
  
      ret = m_human_car_algorithm.init(dev_id);
      if(0 != ret){
          return -1;
      }
  
      ret = m_motor_rainshed_algorithm.init(dev_id);
      if(0 != ret){
          return -1;
      }
  
      ret = m_motor_phone_algorithm.init(dev_id);
      if(0 != ret){
          return -1;
      }
2ae58093   Hu Chunming   添加road_seg算法
59
60
  
      ret = m_road_seg_algorithm.init(dev_id);
4a273a4a   Hu Chunming   添加hcp和hp
61
62
63
64
      if(0 != ret){
          return -1;
      }
  
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
65
66
67
68
69
      ret = m_crop_util.init(dev_id);
      if(0 != ret){
          return -1;
      }
  
4a273a4a   Hu Chunming   添加hcp和hp
70
71
      ACL_CALL(aclrtCreateContext(&m_ctx, 0), ACL_SUCCESS, SY_FAILED);
  	ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_SUCCESS, SY_FAILED);
20396d5c   Hu Chunming   添加车头车尾算法
72
73
74
75
76
77
78
  	ACL_CALL(aclrtCreateStream(&stream), ACL_SUCCESS, SY_FAILED);
      m_dvpp = new DvppProcess();
  	m_dvpp->InitResource(stream);
  
      return 0;
  }
  
e8beee4d   Hu Chunming   实现jni接口
79
  va_result* PicAnalysis::analysis_file(vector<string> vec_file_path){
20396d5c   Hu Chunming   添加车头车尾算法
80
  
e8beee4d   Hu Chunming   实现jni接口
81
82
83
84
85
      int ret = aclrtSetCurrentContext(m_ctx);
      if (SY_SUCCESS != ret) {
          printf("aclrtSetCurrentContext failed!");
          return nullptr;
      }
15756629   Hu Chunming   添加clothes算法
86
  
20396d5c   Hu Chunming   添加车头车尾算法
87
88
89
90
      const int batch_size = vec_file_path.size();
  
      vector<sy_img> vec_img;
  
20396d5c   Hu Chunming   添加车头车尾算法
91
92
93
94
95
96
97
98
99
      // 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!");
e8beee4d   Hu Chunming   实现jni接口
100
              return nullptr;
20396d5c   Hu Chunming   添加车头车尾算法
101
102
103
104
105
          }
  
          ret = m_dvpp->CvtJpegToYuv420sp(dvpp_data[i], src); //解码
          if(ret != SY_SUCCESS){
              LOG_ERROR("CvtJpegToYuv420sp failed!");
e8beee4d   Hu Chunming   实现jni接口
106
              return nullptr;
20396d5c   Hu Chunming   添加车头车尾算法
107
108
109
110
111
112
113
114
115
          }
          
          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接口
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  
      return analysis_img(vec_img);
  }
  
  va_result* PicAnalysis::analysis_img(vector<sy_img> vec_img){
  
      const int batch_size = vec_img.size();
  
      int ret = aclrtSetCurrentContext(m_ctx);
      if (SY_SUCCESS != ret) {
          printf("aclrtSetCurrentContext failed!");
          return nullptr;
      }
  
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
130
131
132
133
134
135
      va_result* result = m_vehicle_analysis.detect(vec_img);
  
      m_road_seg_algorithm.detect(vec_img);
  
      for (int b = 0; b < batch_size; b++)
      {
e8beee4d   Hu Chunming   实现jni接口
136
137
138
139
140
          sy_img img = vec_img[b];
          ImageData src;
          src.width = img.w_;
          src.height = img.h_;
          src.data_naked = img.data_;
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
141
142
          for(int c=0;c<result[b].count;c++)
          {
667e7a90   Hu Chunming   雨棚和打电话初步代码
143
              vehicle_info& result_info = result[b].info[c];
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
              int shot_type=result[b].info[c].type;
  
              // 行人
              if (6 == shot_type)
              {
                  sy_rect human_rect = result_info.vehicle_body_detect_res.rect;
                  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;
  
                  vector<sy_img> vec_human_img;
                  vec_human_img.push_back(img);
  
                  human_analysis(vec_human_img);
  
                  delete human_data;
                  human_data = nullptr;
667e7a90   Hu Chunming   雨棚和打电话初步代码
164
165
166
167
168
169
170
171
172
173
174
175
176
              } else if(1 == shot_type) {
                  // 车尾,判断是否 货车尾部货厢载人
                  if(result_info.vpt_type == 6 || result_info.vpt_type == 7){
                      if(result_info.manned_res.hs_count > 0){
                          // 货车载人
                      }	
                  }
              } 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){
                          // 摩托车、三轮车载人
                      }	
                  }
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
              }
  
              // 司乘
              int vpd_num = result_info.vehicle_pendant_det_res.count;
              vector<sy_img> vec_human_img;
              for(int p=0; p<vpd_num; p++)
              {
                  int index = result_info.vehicle_pendant_det_res.vpd_res[p].index;
                  if(index == 0){
                      sy_rect human_rect = result_info.vehicle_pendant_det_res.vpd_res[p].rect;
                      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);
                  }
              }
  
              m_clothes_algorithm.detect(vec_human_img);
  
              for(int p=0; p<vpd_num; p++)
              {
                  int index = result_info.vehicle_pendant_det_res.vpd_res[p].index;
                  if(index == 0){
                      sy_rect human_rect = result_info.vehicle_pendant_det_res.vpd_res[p].rect;
                  }
              }
  
              // 摩托车
              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;
  
                  sy_rect motor_rect = result_info.vehicle_body_detect_res.rect;
                  ImageData* motor_data = m_crop_util.crop(src, motor_rect.left_, motor_rect.top_, motor_rect.left_ + motor_rect.width_, motor_rect.top_ + motor_rect.height_);
  
                  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);
  
667e7a90   Hu Chunming   雨棚和打电话初步代码
226
227
228
229
230
                  vector<int> vec_rainshed_result = m_motor_rainshed_algorithm.detect(vec_motor_img);
                  if (vec_rainshed_result.size() > 0)
                  {
                      result_info.rainshed = vec_rainshed_result[0];
                  }
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
231
232
                  
  
667e7a90   Hu Chunming   雨棚和打电话初步代码
233
234
235
236
237
238
                  vector<int> vec_phone_result = m_motor_phone_algorithm.detect(vec_motor_img);
                  if (vec_phone_result.size() > 0)
                  {
                      int phoning = vec_phone_result[0];
                  }
  
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
239
240
241
242
243
244
                  delete motor_data;
                  motor_data = nullptr;
              }
              
          }
      }
20396d5c   Hu Chunming   添加车头车尾算法
245
  
667e7a90   Hu Chunming   雨棚和打电话初步代码
246
247
248
249
250
251
      // 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接口
252
253
254
255
  
      // if (reult) {
      //     m_vehicle_analysis.release_result(reult, vec_img.size());
      // }
20396d5c   Hu Chunming   添加车头车尾算法
256
      
4a273a4a   Hu Chunming   添加hcp和hp
257
258
      LOG_INFO("analysis_sync finished!");
  
e8beee4d   Hu Chunming   实现jni接口
259
      return result;
20396d5c   Hu Chunming   添加车头车尾算法
260
261
262
  }
  
  int PicAnalysis::release() {
4a273a4a   Hu Chunming   添加hcp和hp
263
264
  
      ACL_CALL(aclrtSetCurrentContext(m_ctx), ACL_SUCCESS, SY_FAILED);
20396d5c   Hu Chunming   添加车头车尾算法
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  
      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;
b3012672   Hu Chunming   天啊及hp,road_seg子sd...
280
281
282
283
284
285
286
287
  }
  
  int PicAnalysis::human_analysis(vector<sy_img> vec_img) {
      vector<BodyColorInfo> vec_body_color = m_human_algorithm.detect(vec_img);
  }
  
  int PicAnalysis::check_motor_retrograde_motion(vector<sy_img> vec_motor_img) {
      m_human_car_algorithm.detect(vec_motor_img);
20396d5c   Hu Chunming   添加车头车尾算法
288
  }