Blame view

src/ai_engine_module/VPTProcess.cpp 7.03 KB
09c2d08c   Hu Chunming   arm交付版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include "VPTProcess.h"
  #include "../common/logger.hpp"
  #include "../ai_platform/task_param_manager.h"
  
  #include <stdlib.h>
  #include <time.h>
  #include <fstream>
  
  #include "vpt.h"
  #include "../ai_platform/macro_definition.h"
  #include "../ai_platform/det_obj_header.h"
  
  #include "opencv2/opencv.hpp"
  
  #include "../util/vpc_util.h"
  
  VPTProcess::VPTProcess(){
      m_max_batchsize = 16;
  }
  
  VPTProcess::~VPTProcess(){
      release();
  }
  
  /* 算法初始化 */
  int VPTProcess::init(VPTProcess_PARAM vparam){
  
      string model_path = vparam.model_dir + "/models/vpt230323_310p.om" ;
  
      LOG_INFO("vpt 版本:{}  模型路径:{}", vpt_get_version(), model_path);
  
      vpt_param param;
      char modelNames[100];
      strcpy(modelNames, model_path.c_str());
      param.modelNames = modelNames;
      param.threshold = vparam.threshold;
      param.devId = vparam.gpuid;
      param.isTrk = false;
  
      m_devId = param.devId;
02e5e637   Zhao Shuaihua   增加行人/非机动车占机动车道(50...
41
42
      ACL_CALL(aclrtSetDevice(m_devId), ACL_SUCCESS, -1);
      ACL_CALL(aclrtCreateContext(&m_algorthim_ctx, m_devId), ACL_SUCCESS, -1);
09c2d08c   Hu Chunming   arm交付版
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  
      int ret = vpt_init(&m_det_handle, param);
      if(ret != 0){
          LOG_DEBUG("vpt init error.");
          return -1;
      }
  
      return 0;
  }
  
  /* 算法计算 */
  int VPTProcess::process_gpu(sy_img * batch_img, vector<string>& tasklist,
  					vector<onelevel_det_result>& result, vector<vector<int>>& deleteObjectID, vector<vector<onelevel_det_result>>& unUsedResult)
  {
  	int batchsize = tasklist.size();
  
  	if (result.empty())
  		result.resize(batchsize);
  
  	/* 结果结构体初始化 */
  	vpt_result *vpt_det_result = new vpt_result[batchsize];
  	for (int b = 0; b < batchsize; b++){
  		vpt_det_result[b].obj_count_ = 0;
  		vpt_det_result[b].obj_results_ = new vpt_obj_result[MAX_DET_COUNT];
  	}
  
      do{
          /* 路数太多时 按照最大batchsize数 拆批次运行 */
          int cur_batch_size = m_max_batchsize;
          int cycleTimes = batchsize / cur_batch_size + (batchsize % cur_batch_size == 0 ? 0 : 1);
  
          for (int c = 0; c < cycleTimes; c++){
  
              int real_batchsize = c == cycleTimes - 1 ? (batchsize - cur_batch_size*c) : cur_batch_size;
              int startbatch = c*cur_batch_size;
  
              vpt_result *real_res = vpt_det_result + startbatch;
  
02e5e637   Zhao Shuaihua   增加行人/非机动车占机动车道(50...
81
              // aclrtSetDevice(m_devId);
09c2d08c   Hu Chunming   arm交付版
82
              int ret = aclrtSetCurrentContext(m_algorthim_ctx);
02e5e637   Zhao Shuaihua   增加行人/非机动车占机动车道(50...
83
              if(ACL_SUCCESS != ret){
09c2d08c   Hu Chunming   arm交付版
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
                  break;
              }
              ret = vpt_batch(m_det_handle, batch_img + startbatch, real_batchsize, real_res);
              if(ret != 0){
                  break;
              }
          }
  
          vector <vector< vector <float>>> detectResult(batchsize);  // sort
  
          /* 将检测的结果放进数组 转换为跟踪的输入需要(若为人脸 则检测结果可能跟多,比如需要带上ldmk点) */
          // filter by threshold.
          for (int b = 0; b < batchsize; b++)
          {
              vpt_result cur_result = vpt_det_result[b];
  
              for (int c = 0; c < cur_result.obj_count_ && c < MAX_OBJ_COUNT; c++)
              {
                  float x1 = vpt_det_result[b].obj_results_[c].obj_rect.left_;
                  float y1 = vpt_det_result[b].obj_results_[c].obj_rect.top_;
                  float x2 = vpt_det_result[b].obj_results_[c].obj_rect.left_ + vpt_det_result[b].obj_results_[c].obj_rect.width_;
                  float y2 = vpt_det_result[b].obj_results_[c].obj_rect.top_ + vpt_det_result[b].obj_results_[c].obj_rect.height_;
  
                  float class_id = vpt_det_result[b].obj_results_[c].obj_index;
                  float score = vpt_det_result[b].obj_results_[c].obj_score;
  
                  if (score >= THRESHOLD)
                  {
                      vector <float> obj;
                      obj.push_back(x1);
                      obj.push_back(y1);
                      obj.push_back(x2);
                      obj.push_back(y2);
                      obj.push_back(score);
                      obj.push_back(class_id);
                      detectResult[b].push_back(obj);
                  }
              }
          }
  
          bool isUseDet = true;
          for (size_t detectIndex = 0; detectIndex < batchsize; detectIndex++) {
              string task_id = tasklist[detectIndex];
  
              if (! taskTrackers[task_id].tracker.GetState())
                  continue;
  
              Sort &cur_sort = taskTrackers[task_id].tracker;
              isUseDet = true;
              
              const float maxLen = std::sqrt(batch_img[detectIndex].w_ * batch_img[detectIndex].w_ + batch_img[detectIndex].h_ * batch_img[detectIndex].h_); //-modified by zsh 220719
              /* FusionInterval是跳帧参数,以十类人车物为例,一般跳5帧,所以第一帧检测,后续四帧纯跟踪 */
              for (int j = 0; j < taskTrackers[task_id].tracker.FusionInterval; j++)
              {
                  /* 跟踪:第一帧 带检测框信息的跟踪,取结果返回 */
                  if (j == 0)
                  {
83f4facf   Zhao Shuaihua   目标消失时增加一次推送;修复算法框...
141
142
                      // int objCount = cur_sort.update_v2(isUseDet, /*save lk = */false, /*center_dist = */true, maxLen, detectResult[detectIndex], result[detectIndex].obj, deleteObjectID[detectIndex]);
                      int objCount = cur_sort.update_v3(isUseDet, /*save lk = */false, /*center_dist = */false, maxLen, detectResult[detectIndex], result[detectIndex].obj, deleteObjectID[detectIndex]);
09c2d08c   Hu Chunming   arm交付版
143
144
145
146
147
148
149
150
151
152
                      result[detectIndex].obj_count = objCount;
                      result[detectIndex].task_id = task_id;
  
                      vector<vector<float>>().swap(detectResult[detectIndex]);
                      detectResult[detectIndex].clear();
                      isUseDet = false;
                  } else  /* 跟踪:后四帧 纯粹跟踪 纯跟踪结果不返回 */
                  {
                      onelevel_det_result un_result;
                      //un_result.obj_count = cur_sort.update(isUseDet, false, detectResult[detectIndex], un_result.obj, deleteObjectID[detectIndex]);
83f4facf   Zhao Shuaihua   目标消失时增加一次推送;修复算法框...
153
154
                      // un_result.obj_count = cur_sort.update_v2(isUseDet, false, true, maxLen, detectResult[detectIndex], un_result.obj, deleteObjectID[detectIndex]);
                      un_result.obj_count = cur_sort.update_v3(isUseDet, false, false, maxLen, detectResult[detectIndex], un_result.obj, deleteObjectID[detectIndex]);
09c2d08c   Hu Chunming   arm交付版
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
                  }
              }
          }
  
          vector <vector< vector <float>>>().swap(detectResult);  // free memory.
      } while (0);
      
  	if(vpt_det_result){
  		for (int b = 0; b < batchsize; b++){
  			delete[] vpt_det_result[b].obj_results_;
  		}
  		delete[] vpt_det_result;
  	}
  
  	return 0;
  }
  
  
  /* 算法句柄 资源释放 */
  void VPTProcess::release(){
      if (m_det_handle){
          vpt_release(&m_det_handle);
          m_det_handle = NULL;
      }
      if(m_algorthim_ctx){
          aclrtDestroyContext(m_algorthim_ctx);
      }
  }
  
  // 221117byzsh
  void VPTProcess::addTaskTracker(const string taskID, double rWidth, double rHeight, int skip_frame)
  {
  	TaskTracker t;
  	t.TaskID = taskID;
  	t.ratioWidth = rWidth;
  	t.ratioHeight = rHeight;
  	t.tracker.FusionInterval = skip_frame;
  
  	taskTrackers[taskID] = t;
  }
  
  /* 任务结束跟踪器 */
  bool VPTProcess::finishTaskTracker(const string taskID)
  {
      taskTrackers.erase(taskID);
  	return true;
  }