9c03cbe4
Zhao Shuaihua
增加三轮车/货车载人 二轮车超员/...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <algorithm>
#include "./truck_manned_process.h"
#include <cmath>
#include "../decoder/interface/DeviceMemory.hpp"
#include "../common/logger.hpp"
#include "../ai_platform/mvpt_process_assist.h"
namespace ai_engine_module
{
namespace truck_manned_process
{
algorithm_type_t TruckMannedProcess::algor_type_ = algorithm_type_t::TRUCK_MANNED;
|
fa818303
Zhao Shuaihua
增加闯红灯功能(特定场景);优化货...
|
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
int legal_person_motocycle_inarea(const std::vector<input_data_wrap_t>& person_motocycle_data, int left, int top, int right, int bottom) {
int legal_person_motocycle_count = 0;
for (const auto& det_result : person_motocycle_data) {
int x1_intersection = std::max(det_result.box.left, left);
int y1_intersection = std::max(det_result.box.top, top);
int x2_intersection = std::min(det_result.box.right, right);
int y2_intersection = std::min(det_result.box.bottom, bottom);
if (x2_intersection <= x1_intersection || y2_intersection <= y1_intersection) continue; // 没有交集
float area_a = float(det_result.box.right - det_result.box.left) * float(det_result.box.bottom - det_result.box.top);
float area_b = float(right - left) * float(bottom - top);
float area_intersection = float(x2_intersection - x1_intersection) * float(y2_intersection - y1_intersection);
if (area_a > area_b || area_a <= 0) continue; //暂不考虑非机动车目标比货车大的情况
// float iou = area_intersection / (area_a + area_b - area_intersection);
float small_overlap_ratio = area_intersection / area_a; //计算非机动车目标相对于自身的重叠程度
if (small_overlap_ratio > 0.2) legal_person_motocycle_count ++;
}
return legal_person_motocycle_count;
}
|
9c03cbe4
Zhao Shuaihua
增加三轮车/货车载人 二轮车超员/...
|
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
TruckMannedProcess::TruckMannedProcess()
: task_param_manager_(nullptr)
{
}
TruckMannedProcess::~TruckMannedProcess()
{
if (tools_) {
hs_truck_release(&tools_);
tools_ = nullptr;
}
if (m_algorthim_ctx) {
aclrtDestroyContext(m_algorthim_ctx);
}
}
|
15398c62
Hu Chunming
模型路径兼容
|
54
|
bool TruckMannedProcess::init(int gpu_id, string models_dir)
|
9c03cbe4
Zhao Shuaihua
增加三轮车/货车载人 二轮车超员/...
|
55
56
57
|
{
init_ = false;
|
15398c62
Hu Chunming
模型路径兼容
|
58
|
string model_path = models_dir + "/models/hs/hs_truck_310p.om" ;
|
9c03cbe4
Zhao Shuaihua
增加三轮车/货车载人 二轮车超员/...
|
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
141
142
143
144
145
146
147
148
149
150
151
|
LOG_INFO("hs_truck 版本:{} 模型路径:{}", hs_truck_getversion(), model_path);
hs_truck_param param;
char modelNames[100];
strcpy(modelNames, model_path.c_str());
param.modelNames = modelNames;
param.thresld = 0.25;
param.devId = gpu_id;
m_devId = param.devId;
ACL_CALL(aclrtSetDevice(m_devId), ACL_SUCCESS, -1);
ACL_CALL(aclrtCreateContext(&m_algorthim_ctx, m_devId), ACL_SUCCESS, -1);
int status;
if (!(init_ = (0 == (status = hs_truck_init(&tools_, param)))))
LOG_ERROR("Init TruckMannedProcessSdk failed error code is {}", status);
else
if (!task_param_manager_)
task_param_manager_ = task_param_manager::getInstance();
return init_;
}
bool TruckMannedProcess::check_initied()
{
if (!init_)
LOG_ERROR("[%s:%d] call init function please.", __FILE__, __LINE__);
return init_;
}
void TruckMannedProcess::force_release_result(const task_id_t& task_id) {
for (auto iter = id_to_result_.begin(); iter != id_to_result_.end();) {
const auto& key = iter->first;
if (key.task_id == task_id) {
auto& value = iter->second;
if (value.origin_img_desc != nullptr) {
VPCUtil::vpc_pic_desc_release(value.origin_img_desc);
}
if (value.roi_img_desc != nullptr) {
VPCUtil::vpc_pic_desc_release(value.roi_img_desc);
}
iter = id_to_result_.erase(iter);
}
else {
++iter;
}
}
}
std::shared_ptr<results_data_t> TruckMannedProcess::get_result_by_objectid(const id_t& id, bool do_erase)
{
auto it = id_to_result_.find(id);
if (it == id_to_result_.end())
return std::shared_ptr<results_data_t>(nullptr);
std::shared_ptr<results_data_t> res = std::make_shared<results_data_t>(it->second);
if (do_erase)
id_to_result_.erase(id);
return res;
}
bool TruckMannedProcess::update_mstreams(const std::vector<task_id_t>& taskIds, vector<DeviceMemory*> vec_det_input_images, const std::vector<onelevel_det_result> &det_results)
{
if (!check_initied())
return false;
if (det_results.empty())
{
LOG_DEBUG("detection result is empty.");
return false;
}
int n_images = det_results.size(); // or n_stream
unsigned flattened_idx = 0;
std::map<int, int> flattened_idx_to_batch_idx;
/* 1. Crop & keep some interest class. */
auto taskId_iter = taskIds.begin();
std::vector<sy_img> flattened_imgs(0);
std::vector<vpc_img_info> flattened_vpc_imgs(0);
std::vector<input_data_wrap_t> flattened_interest_data(0); //
VPCUtil* pVpcUtil = VPCUtil::getInstance();
for (int n = 0; n < n_images; ++n)
{
int n_interest_obj = 0;
auto& src_img = vec_det_input_images[n];
int src_img_w = src_img->getWidth();
int src_img_h = src_img->getHeight();
auto& boxes_of_one_image = det_results[n].obj;
|
fa818303
Zhao Shuaihua
增加闯红灯功能(特定场景);优化货...
|
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
|
//汇总当前帧的行人/非机动车目标
std::vector<input_data_wrap_t> person_motocycle_data(0);
for (int i = 0; i < det_results[n].obj_count; ++i)
{
auto& box = boxes_of_one_image[i];
if (static_cast<det_class_label_t>(box.index) == det_class_label_t::MOTOCYCLE || static_cast<det_class_label_t>(box.index) == det_class_label_t::BICYCLE ||
static_cast<det_class_label_t>(box.index) == det_class_label_t::HUMAN || static_cast<det_class_label_t>(box.index) == det_class_label_t::TRICYCLE)
{
auto& taskId = *taskId_iter;
input_data_wrap_t data;
int top = std::max(int(box.top), 0); int left = std::max(int(box.left), 0);
int right = std::min(int(box.right), src_img_w); int bottom = std::min(int(box.bottom), src_img_h);
int width = right - left; int height = bottom - top;
if (static_cast<det_class_label_t>(box.index) == det_class_label_t::HUMAN && width >= 0.9*height) //基于载人只能看到半身的假设
continue;
data.box.top = top; data.box.left = left;
data.box.right = right; data.box.bottom = bottom;
data.box.score = box.confidence; data.box.cls = box.index;
data.taskId = taskId; data.objId = box.id;
person_motocycle_data.emplace_back(std::move(data));
}
}
|
9c03cbe4
Zhao Shuaihua
增加三轮车/货车载人 二轮车超员/...
|
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
|
for (int i = 0; i < det_results[n].obj_count; ++i)
{
auto& box = boxes_of_one_image[i];
if (static_cast<det_class_label_t>(box.index) == det_class_label_t::TRUCK)
{
auto& taskId = *taskId_iter;
auto algor_param_wrap = task_param_manager_->get_task_other_param(taskId, this->algor_type_);
if (!algor_param_wrap)
{
LOG_ERROR("{} is nullptr when get algor param from task_param", taskId.c_str());
continue;
}
auto algor_param = ((algor_param_type)algor_param_wrap->algor_param);
input_data_wrap_t data;
int top = std::max(int(box.top - (IMAGE_CROP_EXPAND_RATIO * box.top)), 0);
int left = std::max(int(box.left - (IMAGE_CROP_EXPAND_RATIO * box.left)), 0);
int right = std::min(int(box.right + (IMAGE_CROP_EXPAND_RATIO * box.right)), src_img_w);
int bottom = std::min(int(box.bottom + (IMAGE_CROP_EXPAND_RATIO * box.bottom)), src_img_h);
int width = right - left;
int height = bottom - top;
if ((width < algor_param->obj_min_width || height < algor_param->obj_min_height || box.confidence < algor_param->obj_confidence_threshold) ||
!snapshot_legal_inarea(algor_param_wrap->basic_param->algor_valid_rect, left, top, right, bottom))
continue;
|
fa818303
Zhao Shuaihua
增加闯红灯功能(特定场景);优化货...
|
207
208
209
210
211
|
//统计货车检测框内行人及非机动车的数量
int person_motocycle_inarea = legal_person_motocycle_inarea(person_motocycle_data, left, top, right, bottom);
data.box.top = top; data.box.left = left;
data.box.right = right; data.box.bottom = bottom;
|
9c03cbe4
Zhao Shuaihua
增加三轮车/货车载人 二轮车超员/...
|
212
|
data.box.score = box.confidence;
|
fa818303
Zhao Shuaihua
增加闯红灯功能(特定场景);优化货...
|
213
|
data.taskId = taskId; data.objId = box.id;
|
9c03cbe4
Zhao Shuaihua
增加三轮车/货车载人 二轮车超员/...
|
214
|
data.id = obj_key_t{ box.id, taskId, algorithm_type_t::TRUCK_MANNED };
|
fa818303
Zhao Shuaihua
增加闯红灯功能(特定场景);优化货...
|
215
|
data.person_motocycle_cnt = person_motocycle_inarea;
|
9c03cbe4
Zhao Shuaihua
增加三轮车/货车载人 二轮车超员/...
|
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
// 抠图
video_object_info obj;
strcpy(obj.task_id, taskId.c_str());
obj.object_id = box.id;
obj.left = left; obj.top = top;
obj.right = right; obj.bottom = bottom;
vpc_img_info img_info = pVpcUtil->crop(src_img, obj);
sy_img img;
img.w_ = width;
img.h_ = height;
img.c_ = src_img->getChannel();
if (img_info.pic_desc != nullptr) {
void *outputDataDev = acldvppGetPicDescData(img_info.pic_desc);
img.data_ = reinterpret_cast<unsigned char*>(outputDataDev);
}
else {
LOG_ERROR("Crop image NPU failed wh is [{}, {}] ltrb is [{} {} {} {}]",
src_img_w, src_img_h, data.box.left, data.box.top, data.box.right, data.box.bottom);
continue;
}
flattened_imgs.emplace_back(std::move(img));
flattened_vpc_imgs.emplace_back(std::move(img_info));
flattened_interest_data.emplace_back(std::move(data));
flattened_idx_to_batch_idx[flattened_idx++] = n;
}
}
|
fa818303
Zhao Shuaihua
增加闯红灯功能(特定场景);优化货...
|
247
|
std::vector<input_data_wrap_t>().swap(person_motocycle_data);
|
9c03cbe4
Zhao Shuaihua
增加三轮车/货车载人 二轮车超员/...
|
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
++taskId_iter;
}
int ret = aclrtSetCurrentContext(m_algorthim_ctx);
if (ACL_SUCCESS != ret) {
return false;
}
/* 2. collection result. */
int n_input_image = flattened_imgs.size();
hs_truck_result model_results[n_input_image];
{
int steps = (n_input_image + MAX_BATCH - 1) / MAX_BATCH;
for (int step = 0; step < steps; ++step)
{
int offset = step * MAX_BATCH;
int batch_size = (step == steps - 1) ? n_input_image - offset : MAX_BATCH;
hs_truck_process_batch(tools_, flattened_imgs.data() + offset, batch_size, model_results + offset);
}
}
/* 3. postprocess. */
{
for (int n = 0; n < n_input_image; ++n)
{
auto& det_result = flattened_interest_data[n];
auto& objId = det_result.objId;
if (id_to_result_.find(det_result.id) != id_to_result_.end())
{
VPCUtil::vpc_img_release(flattened_vpc_imgs[n]); //flattened_imgs[n].data_
flattened_imgs[n].data_ = nullptr;
continue;
}
const auto& src_img = vec_det_input_images[flattened_idx_to_batch_idx[n]];
auto algor_param_wrap = task_param_manager_->get_task_other_param(det_result.taskId, this->algor_type_);
if (!algor_param_wrap)
{
LOG_ERROR("{} nullptr when get algor param from task_param", det_result.taskId.c_str());
VPCUtil::vpc_img_release(flattened_vpc_imgs[n]); //flattened_imgs[n].data_
flattened_imgs[n].data_ = nullptr;
continue;
}
auto algor_param = ((algor_param_type)algor_param_wrap->algor_param);
int hs_count = model_results[n].objcount;
obj_key_t obj_key{ det_result.objId, det_result.taskId, algorithm_type_t::TRUCK_MANNED };
auto& e = id_to_mn_[obj_key];
++e.m_frame;
|
fa818303
Zhao Shuaihua
增加闯红灯功能(特定场景);优化货...
|
301
302
|
// if (hs_count >= algor_param->hs_count_threshold)
if (hs_count - det_result.person_motocycle_cnt >= algor_param->hs_count_threshold) //头肩数量去除包含的行人和非机动车数量
|
9c03cbe4
Zhao Shuaihua
增加三轮车/货车载人 二轮车超员/...
|
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
{
if (++e.n_frame == algor_param->n)
{
results_data_t result;
{
result.box = det_result.box;
result.taskId = det_result.taskId;
result.objId = det_result.objId;
// 原图
vpc_img_info src_img_info = VPCUtil::vpc_devMem2vpcImg(src_img);
result.origin_img_desc = src_img_info.pic_desc;
// 抠图
result.roi_img_desc = flattened_vpc_imgs[n].pic_desc;
}
id_to_result_.emplace(obj_key, std::move(result));
goto _continue;
}
}
if (e.m_frame == algor_param->m)
e.reset();
VPCUtil::vpc_img_release(flattened_vpc_imgs[n]); //flattened_imgs[n].data_
_continue:
{
}
}
}
return true;
}
} // namespace truck_manned_process
} // namespace ai_engine_module
|