4bcfa5f5
Hu Chunming
代码优化
|
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
// 人脸检测初始化
fd_param sdk_param;
char model_path_yolov5s[100];
strcpy(model_path_yolov5s, (models_dir + "/models/face_detect/face_det_yolov5s_310p.om").c_str());
sdk_param.det_modelNames = model_path_yolov5s;
char model_path_ldmk[100];
strcpy(model_path_ldmk, (models_dir + "/models/face_detect/face_ldmk_310p.om").c_str());
sdk_param.ldmk_modelNames = model_path_ldmk;
char model_path_pose[100];
strcpy(model_path_pose, (models_dir + "/models/face_detect/face_pose_310p.om").c_str());
sdk_param.pose_modelNames = model_path_pose;
char model_path_score[100];
strcpy(model_path_score, (models_dir + "/models/face_detect/face_score_310p.om").c_str());
sdk_param.score_modelNames = model_path_score;
char model_path_fuzzy[100];
strcpy(model_path_fuzzy, (models_dir + "/models/face_detect/face_fuzzy_310p.om").c_str());
sdk_param.fuzzy_modelNames = model_path_fuzzy;
char model_path_occlusion[100];
strcpy(model_path_occlusion, (models_dir + "/models/face_detect/face_occlusion_310p.om").c_str());
sdk_param.occlusion_modelNames = model_path_occlusion;
sdk_param.thresld = 0.6;
sdk_param.devId = m_devId;
sdk_param.auth_license = "sy_tongtu_aiplatform_sdk_2023";
sdk_param.facial_fea_point_config = SY_CONFIG_OPEN; //是否启动关键点检测
sdk_param.pose_config = SY_CONFIG_OPEN; //是否启动姿态角
sdk_param.quality_config = SY_CONFIG_OPEN; //是否启动质量检测
sdk_param.score_config = SY_CONFIG_OPEN; //是否启动人脸置信度 //SY_CONFIG_OPEN SY_CONFIG_CLOSE
sdk_param.max_result_count = 50;
return fd_init(&handle, sdk_param);
|
8072fc32
Hu Chunming
代码同步初步完成,人脸检测模型还不兼容
|
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
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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
|
}
int face_det_ai_engine::ai_engine_process_batch(std::vector<std::string> &task_ids, sy_img *image_data_array, std::vector<onelevel_det_result> &result , std::vector<std::vector<int>> &deleteObjectID){
map<string, map<algo_type, task_param_manager::algo_param_type_t_*>> && algor_param = task_param_manager_->get_task_other_params();
const int total_batchsize = task_ids.size();
if (total_batchsize <= 0){
return 0;
}
aclrtSetDevice(m_devId);
int ret = aclrtSetCurrentContext(m_algorthim_ctx);
if(ACL_ERROR_NONE != ret){
return 0;
}
fd_result *fd_result_ = new fd_result[total_batchsize];
for (int i = 0; i < total_batchsize; ++i)
fd_result_[i].info = new fd_info[50];
do{
int stride = m_max_batchsize;
int steps = (total_batchsize + stride - 1) / stride;
bool bError = false;
for (int c = 0; c < steps; ++c) {
int offset = c * m_max_batchsize;
const int batchsize = (c == steps - 1) ? (total_batchsize - offset) : stride;
int ret = fd_detect_batch(handle, image_data_array + offset, SY_FORMAT_BGR888, batchsize, fd_result_ + offset);
if(ret < 0){
LOG_ERROR(" fd_detect_batch error!!! image_size: {} model_batch_size: {}, step: [{}/{}] offset: {} batchsize: {}", total_batchsize, m_max_batchsize, c, steps, offset, batchsize);
bError = true;
break;
}
}
if(bError){
break;
}
// 属性检测使用人脸检测的原图,不需要切图
int cur_index = 0;
int img_index = 0;
vector <vector< vector <float>>> detectResult(total_batchsize); // sort
auto task_id_iter = task_ids.cbegin();
for (int c = 0; c < total_batchsize; ++c)
{
string task_id = *task_id_iter;
task_id_iter++;
task_param_manager::algo_param_type_t_* cur_task_params = algor_param[task_id][algorithm_type_t::FACE_SNAPSHOT];
if (cur_task_params->basic_param == nullptr) {
continue;
}
auto adapt_param = cur_task_params->basic_param->adapt_param;
if (adapt_param == nullptr){
continue;
}
for (int i = 0; i < fd_result_[c].count; ++i)
{
auto& obj_c = fd_result_[c].info[i];
sy_point center;
center.x_ = obj_c.face_position.left_ + obj_c.face_position.width_ / 2 ;
center.y_ = obj_c.face_position.top_ + obj_c.face_position.height_ / 2;
if (!common::isInPolygon(adapt_param->points, adapt_param->points_count, center) || obj_c.face_pos_score < ((algor_config_param_snapshot *)cur_task_params->algor_param)->threshold) {
continue;
}
vector <float> obj;
obj.push_back(fd_result_[c].info[i].face_position.left_);
obj.push_back(fd_result_[c].info[i].face_position.top_);
obj.push_back(fd_result_[c].info[i].face_position.left_ + fd_result_[c].info[i].face_position.width_); //right
obj.push_back(fd_result_[c].info[i].face_position.top_ + fd_result_[c].info[i].face_position.height_); //bottom
obj.push_back(fd_result_[c].info[i].score);
obj.push_back(1); //统一index值为1
//存入关键点信息
for(int j = 0; j < FACIALFEAPOINTSIZE; ++j)
{
obj.push_back(fd_result_[c].info[i].facial_fea_point[j].x_);
obj.push_back(fd_result_[c].info[i].facial_fea_point[j].y_);
}
//-added by zsh 添加姿态角信息------------------------------
obj.push_back(fd_result_[c].info[i].roll);
obj.push_back(fd_result_[c].info[i].yaw);
obj.push_back(fd_result_[c].info[i].pitch);
// cout << fabs(fd_result_[c].info[i].roll) << " " << fabs(fd_result_[c].info[i].yaw) << " " << fabs(fd_result_[c].info[i].pitch) << endl;
//--------------------------------------------------------
detectResult[c].push_back(obj);
#if 0
if (fd_result_[img_index].count > 1)
{
//选择居中且靠上的人脸作为唯一的结果
float min_dis = numeric_limits<float>::max();
int min_index = 0;
float person_center_x = (float)(cur_persondet_result[c]->obj[i].right - cur_persondet_result[c]->obj[i].left) / 2.0;
float person_center_y = (float)(cur_persondet_result[c]->obj[i].bottom - cur_persondet_result[c]->obj[i].top) / 6.0;
for (int j = 0; j < fd_result_[img_index].count; ++j)
{
float cx = (float)fd_result_[img_index].info[j].face_position.left_ + (float)(fd_result_[img_index].info[j].face_position.width_) / 2.0;
float cy = (float)fd_result_[img_index].info[j].face_position.top_ + (float)(fd_result_[img_index].info[j].face_position.height_) / 2.0;
float dis = (person_center_x - cx) * (person_center_x - cx) + (person_center_y - cy) * (person_center_y - cy);
if (dis < min_dis)
{
min_dis = dis;
min_index = j;
}
}
//姿态角控制
if (fabs(fd_result_[img_index].info[min_index].roll) < pose_thresld[c] && fabs(fd_result_[img_index].info[min_index].yaw) < pose_thresld[c] && fabs(fd_result_[img_index].info[min_index].pitch) < pose_thresld[c])
{
cur_res.count = 1;
cur_res.info = new fd_info[1];
memcpy(&cur_res.info[0], &fd_result_[img_index].info[min_index], sizeof(fd_info));
}
else
{
cur_res.info = new fd_info[1];
cur_res.count = 0;
}
}
else if (fd_result_[img_index].count == 1 && fabs(fd_result_[img_index].info[0].roll) < pose_thresld[c] && fabs(fd_result_[img_index].info[0].yaw) < pose_thresld[c] && fabs(fd_result_[img_index].info[0].pitch) < pose_thresld[c]) //姿态角控制
{
cur_res.count = 1;
cur_res.info = new fd_info[1];
memcpy(&cur_res.info[0], &fd_result_[img_index].info[0], sizeof(fd_info));
}
else
{
cur_res.info = new fd_info[1];
cur_res.count = 0;
}
_fd_result[vec_ids[c]].push_back(cur_res);
for (int j = 0; j < cur_res.count; ++j)
{
++cur_index;
}
#endif
}
}
//跟踪
for (size_t real_index = 0; real_index < total_batchsize; real_index++) {
string task_id = task_ids[real_index];
bool isUseDet = true;
vector<int> delete_ids;
const float maxLen = std::sqrt(image_data_array[real_index].w_ * image_data_array[real_index].w_ + image_data_array[real_index].h_ * image_data_array[real_index].h_); //-modified by zsh 220719
for (int j = 0; j < task_trackers[task_id].fusion_interval; ++j) {
if (j == 0) {
int objCount = task_trackers[task_id].tracker.update_v2(isUseDet, /*save lk = */true, /*center_dist = */true, maxLen, detectResult[real_index], result[real_index].obj, deleteObjectID[real_index]);
result[real_index].obj_count = objCount;
vector<vector<float>>().swap(detectResult[real_index]);
detectResult[real_index].clear();
isUseDet = false;
} else {
onelevel_det_result unresult;
unresult.obj_count = task_trackers[task_id].tracker.update_v2(isUseDet, true, true, maxLen, detectResult[real_index], unresult.obj, deleteObjectID[real_index]);
}
}
++real_index;
}
vector <vector< vector <float>>>().swap(detectResult); // free memory.
ret = total_batchsize;
}while(0);
if (fd_result_) {
for (int i = 0; i < total_batchsize; ++i) {
delete[] fd_result_[i].info;
fd_result_[i].info = nullptr;
}
delete[] fd_result_;
fd_result_ = nullptr;
}
return ret;
}
void face_det_ai_engine::clear()
{
for (auto it = _fd_result.begin(); it != _fd_result.end();)
{
for (auto &fd : it->second)
{
delete[] fd.info;
fd.info = nullptr;
}
_fd_result.erase(it++);
}
}
void face_det_ai_engine::add_tracker(std::string task_id, int fusion_interval){
LOG_INFO("face: tracker add task {}", task_id.c_str());
task_tracker t;
t.task_id = task_id;
t.tracker.FusionInterval = fusion_interval; // 221117byzsh
t.fusion_interval = fusion_interval;
task_trackers.insert(std::make_pair(task_id, t));
}
void face_det_ai_engine::finish_task(std::string task_id)
{
LOG_INFO("face: tracker finish task {}", task_id.c_str());
auto iter = task_trackers.find(task_id);
if (iter != task_trackers.end())
{
task_trackers.erase(task_id);
}
}
|