Commit bab9a6c39958899dd220bf7372901307a11cc808

Authored by Hu Chunming
1 parent 5e907174

代码优化,改动较大,可能有逻辑被修改

vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.cpp
... ... @@ -167,7 +167,6 @@ int CMutliSourceVideoProcess::FinishProcessThread()
167 167 ProcessThread.join(); //waiting thread finish
168 168  
169 169 VPT_Release(VPT_Handle);
170   - m_face_det_module->face_det_module_release();
171 170  
172 171 m_snaphot_helper.snapshot_helper_release();
173 172  
... ... @@ -262,14 +261,6 @@ int CMutliSourceVideoProcess::InitAlgorthim(mvpt_param vptParam, VIDEO_OBJECT_IN
262 261 if (0 != ret)
263 262 return ret;
264 263  
265   - m_face_det_module = nullptr;
266   - if (vptParam.face_det_config == SY_CONFIG_OPEN)
267   - {
268   - m_face_det_module = new face_det_module();
269   - printf("begin init face det\n");
270   - m_face_det_module->face_det_module_init(vptParam.gpuid, vptParam.auth_license);
271   - }
272   -
273 264 viewTaskID = -1;
274 265 TaskinPlay = 0;
275 266 TotalTask = 0;
... ... @@ -1014,46 +1005,47 @@ void CMutliSourceVideoProcess::algorthim_process()
1014 1005 iter = TaskinPlayID.begin();
1015 1006 for (int i = 0; i < curPlayTaskCount; i++)
1016 1007 {
1017   - tasks[*iter].taskFrameCount = tasks[*iter].task_algorithm_data.timestamp;
  1008 + Task task = tasks[*iter];
  1009 + task.taskFrameCount = task.task_algorithm_data.timestamp;
1018 1010 //若该路任务当前帧未检测到目标,返回ID为-1的目标表明未检测到目标
1019 1011 if (VPTResult[i].objCount == 0)
1020 1012 {
1021   - callTaskObjInfoCallbackFunc(0, nullptr, tasks[*iter].taskFrameCount, *iter);
  1013 + callTaskObjInfoCallbackFunc(0, nullptr, task.taskFrameCount, *iter);
1022 1014 }
1023 1015  
1024 1016 //实时查看模块,若存在实时查看,把当前视频画面cp回内存
1025 1017 bool view = false;
1026   - int frameHeight = tasks[*iter].task_algorithm_data.height;
1027   - int frameWidth = tasks[*iter].task_algorithm_data.width;
  1018 + int frameHeight = task.task_algorithm_data.height;
  1019 + int frameWidth = task.task_algorithm_data.width;
1028 1020  
1029 1021 if (*iter == viewTaskID)
1030 1022 {
1031   - cudaMemcpy(tasks[*iter].frameImage.data, tasks[*iter].task_algorithm_data.frame, 3 * tasks[*iter].task_algorithm_data.width * tasks[*iter].task_algorithm_data.height * sizeof(unsigned char), cudaMemcpyDeviceToHost);
  1023 + cudaMemcpy(task.frameImage.data, task.task_algorithm_data.frame, 3 * task.task_algorithm_data.width * task.task_algorithm_data.height * sizeof(unsigned char), cudaMemcpyDeviceToHost);
1032 1024 view = true;
1033 1025 }
1034 1026  
1035 1027 //跟踪帧也需要返回跟踪的结果
1036   - if (tasks[*iter].taskLastFrameCount > 0)
  1028 + if (task.taskLastFrameCount > 0)
1037 1029 {
1038 1030 vector<VPT_Result> OneUnUsedResult = unUsedResult[i];
1039 1031 if (OneUnUsedResult.size() == 0)
1040 1032 {
1041   - callTaskObjInfoCallbackFunc(0, nullptr, tasks[*iter].taskLastFrameCount + 1, *iter);
  1033 + callTaskObjInfoCallbackFunc(0, nullptr, task.taskLastFrameCount + 1, *iter);
1042 1034 }
1043 1035 for (int k = 0; k < OneUnUsedResult.size(); ++k)
1044 1036 {
1045 1037 if (OneUnUsedResult[k].objCount == 0)
1046 1038 {
1047   - callTaskObjInfoCallbackFunc(0, nullptr, tasks[*iter].taskLastFrameCount + k + 1, *iter);
  1039 + callTaskObjInfoCallbackFunc(0, nullptr, task.taskLastFrameCount + k + 1, *iter);
1048 1040 }
1049 1041 else
1050 1042 {
1051 1043 //cout << "OneUnUsedResult.size = " << OneUnUsedResult.size() << " k=" << k << " OneUnUsedResult[k].objCount = " << OneUnUsedResult[k].objCount << endl;
1052   - callTaskObjInfoCallbackFunc(OneUnUsedResult[k].objCount, OneUnUsedResult[k].obj, tasks[*iter].taskLastFrameCount + k + 1, *iter);
  1044 + callTaskObjInfoCallbackFunc(OneUnUsedResult[k].objCount, OneUnUsedResult[k].obj, task.taskLastFrameCount + k + 1, *iter);
1053 1045 }
1054 1046 }
1055 1047 }
1056   - tasks[*iter].taskLastFrameCount = tasks[*iter].taskFrameCount;
  1048 + task.taskLastFrameCount = task.taskFrameCount;
1057 1049  
1058 1050 unsigned char* snapshot_image_data[MAX_OBJ_COUNT]{};// = new unsigned char*[VPTResult[i].objCount];
1059 1051 int snapshot_left[MAX_OBJ_COUNT]{};// = new int[VPTResult[i].objCount];
... ... @@ -1067,7 +1059,7 @@ void CMutliSourceVideoProcess::algorthim_process()
1067 1059 vector<int> human_idx; //用于记录快照数组中那些是人脸
1068 1060 vector<OBJ_KEY> human_obj_keys;
1069 1061  
1070   - callTaskObjInfoCallbackFunc(VPTResult[i].objCount, VPTResult[i].obj, tasks[*iter].taskFrameCount, *iter);
  1062 + callTaskObjInfoCallbackFunc(VPTResult[i].objCount, VPTResult[i].obj, task.taskFrameCount, *iter);
1071 1063 for (int c = 0; c < VPTResult[i].objCount; c++)
1072 1064 {
1073 1065 OBJ_KEY newObj = { (*iter), VPTResult[i].obj[c].id };
... ... @@ -1085,19 +1077,19 @@ void CMutliSourceVideoProcess::algorthim_process()
1085 1077 int p1 = VPTResult[i].obj[c].left - 10 > 0 ? VPTResult[i].obj[c].left - 10 : 0;
1086 1078 int p2 = VPTResult[i].obj[c].top - 15 > 0 ? VPTResult[i].obj[c].top - 15 : 0;
1087 1079  
1088   - cv::rectangle(tasks[*iter].frameImage, Rect(VPTResult[i].obj[c].left, VPTResult[i].obj[c].top,
  1080 + cv::rectangle(task.frameImage, Rect(VPTResult[i].obj[c].left, VPTResult[i].obj[c].top,
1089 1081 VPTResult[i].obj[c].right - VPTResult[i].obj[c].left,
1090 1082 VPTResult[i].obj[c].bottom - VPTResult[i].obj[c].top), Scalar(158, 52, 254), 3, 1, 0);
1091 1083 #ifdef _MSC_VER
1092 1084 string resss = "" + to_string(index) + " " + ObjTypes[index];
1093   - putTextZH(tasks[*iter].frameImage, resss.c_str(), { p1, p2 }, Scalar(20, 255, 20), 14, "Arial");
  1085 + putTextZH(task.frameImage, resss.c_str(), { p1, p2 }, Scalar(20, 255, 20), 14, "Arial");
1094 1086 #else
1095 1087 string resss = "" + to_string(VPTResult[i].obj[c].id) + " " + ObjTypesEnglish[VPTResult[i].obj[c].index];
1096   - cv::putText(tasks[*iter].frameImage, resss.c_str(), cv::Point(p1, p2), cv::FONT_HERSHEY_COMPLEX, 2, Scalar(20, 255, 20), 2, 8, 0);
  1088 + cv::putText(task.frameImage, resss.c_str(), cv::Point(p1, p2), cv::FONT_HERSHEY_COMPLEX, 2, Scalar(20, 255, 20), 2, 8, 0);
1097 1089 #endif
1098 1090 }
1099 1091  
1100   - CropInfo crop_info = m_snaphot_helper.cacheSnapShotInfo(newObj, VPTResult[i].obj[c], tasks[*iter]);
  1092 + CropInfo crop_info = m_snaphot_helper.cacheSnapShotInfo(newObj, VPTResult[i].obj[c], task);
1101 1093 if(crop_info.bCrop){
1102 1094 snapshot_image_data[copy_obj_count] = crop_info.snapshot_image_data;
1103 1095 snapshot_left[copy_obj_count] = crop_info.snapshot_left;
... ... @@ -1122,7 +1114,7 @@ void CMutliSourceVideoProcess::algorthim_process()
1122 1114 {
1123 1115 cudaSetDevice(mgpuid);
1124 1116 cuCtxPushCurrent(context);
1125   - PartMemResizeBatch((unsigned char*)tasks[*iter].task_algorithm_data.frame, frameWidth, frameHeight,
  1117 + PartMemResizeBatch((unsigned char*)task.task_algorithm_data.frame, frameWidth, frameHeight,
1126 1118 snapshot_image_data, copy_obj_count, snapshot_left, snapshot_top, snapshot_right, snapshot_bottom, snapshot_dst_width, snapshot_dst_height, 0, 0, 0, 1, 1, 1);
1127 1119 cuCtxPopCurrent(&context);
1128 1120  
... ... @@ -1131,9 +1123,8 @@ void CMutliSourceVideoProcess::algorthim_process()
1131 1123 {
1132 1124 //需要做人脸检测
1133 1125 int human_count = human_idx.size();
1134   - sy_img *human_img = new sy_img[human_count];
1135   -
1136   - sy_point* ori_points = new sy_point[human_count];
  1126 + sy_img human_img[human_count];
  1127 + sy_point ori_points[human_count];
1137 1128 for (int idx = 0; idx < human_count; idx++)
1138 1129 {
1139 1130 int ii = human_idx[idx];
... ... @@ -1142,167 +1133,16 @@ void CMutliSourceVideoProcess::algorthim_process()
1142 1133 ori_points[idx].y_ = (snapshot_bottom[ii] - snapshot_top[ii]);
1143 1134 }
1144 1135  
1145   - fd_result *face_det_result = new fd_result[human_count];
1146   - for (int fd_i = 0; fd_i < human_count; fd_i++)
1147   - {
1148   - face_det_result[fd_i].info = new fd_info[10]; //内存由外部申请
1149   - }
1150   -
1151   - if (m_face_det_module->face_det_module_process(human_img, human_count, face_det_result, ori_points) == SUCCESS)
1152   - {
1153   - //printf("finish face_det_module_process: %d\n", human_count);
1154   - for (int idx = 0; idx < human_count; idx++)
1155   - {
1156   - OBJ_KEY cur_obj_key = human_obj_keys[idx];
1157   - fd_result &cur_det_res = face_det_result[idx];
1158   - int face_count = cur_det_res.count;
1159   - int face_idx = 0;
1160   - int ii = human_idx[idx];
1161   -
1162   - if (face_count == 0) continue;
1163   -
1164   - if (face_count > 1) //检测到多余一个人脸 选最佳
1165   - {
1166   - sy_point center_human = { human_img[idx].w_/2, human_img [idx].h_/2};
1167   - float min_distance = INT_MAX;
1168   -
1169   - for (int c = 0; c < face_count; c++)
1170   - {
1171   - sy_point face_center = { cur_det_res.info[c].face_position.width_/2, cur_det_res.info[c].face_position.height_/2};
1172   - float distance = fabs(center_human.x_ - face_center.x_) + fabs(center_human.y_ - face_center.y_);
1173   -
1174   - if (distance < min_distance)
1175   - {
1176   - min_distance = distance;
1177   - face_idx = c;
1178   - }
1179   - }
1180   - }
1181   -
1182   - fd_info& cur_det_info = cur_det_res.info[face_idx];
1183   - if (m_snaphot_helper.snapShotInfo[cur_obj_key].snapShotFace.frame == nullptr)
1184   - {
1185   - sy_rect face_ori_rect = { (int)(snapshot_left[ii] + cur_det_info.face_position.left_ ),
1186   - (int)(snapshot_top[ii] + cur_det_info.face_position.top_),
1187   - (int)(cur_det_info.face_position.width_), (int)(cur_det_info.face_position.height_) };
1188   -
1189   - int new_left = max(0, face_ori_rect.left_ - face_ori_rect.width_);
1190   - int new_top = max(0, face_ori_rect.top_ - face_ori_rect.height_);
1191   - int new_right = min((int)tasks[*iter].task_algorithm_data.width - 1, (face_ori_rect.left_ + 2 * face_ori_rect.width_));
1192   - int new_bottom = min((int)tasks[*iter].task_algorithm_data.height - 1, (face_ori_rect.top_ + 2 * face_ori_rect.height_));
1193   - int new_width = new_right - new_left;
1194   - int new_height = new_bottom - new_top;
1195   -
1196   - sy_rect face_expand_rect = { new_left, new_top, new_width, new_height };
1197   -
1198   - int face_img_length = 3 * face_expand_rect.width_ * face_expand_rect.height_;
1199   -
1200   - cudaError_t cudaStatus = cudaMalloc((void**)&m_snaphot_helper.snapShotInfo[cur_obj_key].snapShotFace.frame, face_img_length * sizeof(unsigned char));
1201   - if (cudaStatus != cudaSuccess) {
1202   - fprintf(stderr, "here cudaMalloc frame[0] failed! error: %s\n", cudaGetErrorString(cudaStatus));
1203   - break;
1204   - }
1205   -
1206   - m_snaphot_helper.snapShotInfo[cur_obj_key].snapShotFace.width = face_expand_rect.width_;
1207   - m_snaphot_helper.snapShotInfo[cur_obj_key].snapShotFace.height = face_expand_rect.height_;
1208   - memcpy((void*)&m_snaphot_helper.snapShotInfo[cur_obj_key].face_info, (void*)&cur_det_info, sizeof(fd_info));
1209   -
1210   - //矫正坐标 从行人抠图检测结果 -> 人脸外扩抠图坐标
1211   - fd_info& tmp_info = m_snaphot_helper.snapShotInfo[cur_obj_key].face_info;
1212   -
1213   - for (int p = 0; p < FACIALFEAPOINTSIZE; p++)
1214   - {
1215   - tmp_info.facial_fea_point[p].x_ =
1216   - tmp_info.facial_fea_point[p].x_ - tmp_info.face_position.left_ + (face_ori_rect.left_ - face_expand_rect.left_);
1217   - tmp_info.facial_fea_point[p].y_ =
1218   - tmp_info.facial_fea_point[p].y_ - tmp_info.face_position.top_ + (face_ori_rect.top_ - face_expand_rect.top_);
1219   - }
1220   -
1221   - m_snaphot_helper.snapShotInfo[cur_obj_key].face_info.face_position =
1222   - { (face_ori_rect.left_ - face_expand_rect.left_), (face_ori_rect.top_ - face_expand_rect.top_), face_ori_rect.width_, face_ori_rect.height_ };
1223   -
1224   -
1225   - cudacommon::CropImgGpu((unsigned char*)tasks[*iter].task_algorithm_data.frame, tasks[*iter].task_algorithm_data.width, tasks[*iter].task_algorithm_data.height,
1226   - (unsigned char*)m_snaphot_helper.snapShotInfo[cur_obj_key].snapShotFace.frame, face_expand_rect.left_, face_expand_rect.top_, face_expand_rect.width_, face_expand_rect.height_);
1227   -
1228   - //show_gpu_img_func(m_snaphot_helper.snapShotInfo[cur_obj_key].snapShotFace);
1229   - }
1230   - else
1231   - {
1232   - sy_rect face_ori_rect = { (int)(snapshot_left[ii] + cur_det_info.face_position.left_),
1233   - (int)(snapshot_top[ii] + cur_det_info.face_position.top_),
1234   - (int)(cur_det_info.face_position.width_), (int)(cur_det_info.face_position.height_) };
1235   -
1236   - //更新人脸快照条件:① 角度满足条件 ② 面积比之前人脸面积大
1237   - if (validAngle(cur_det_res.info[face_idx].roll, cur_det_res.info[face_idx].yaw, cur_det_res.info[face_idx].pitch, 15.0, 20.0)
1238   - && betterArea(face_ori_rect, m_snaphot_helper.snapShotInfo[cur_obj_key].face_info.face_position))
1239   - {
1240   - int new_left = max(0, face_ori_rect.left_ - face_ori_rect.width_);
1241   - int new_top = max(0, face_ori_rect.top_ - face_ori_rect.height_);
1242   - int new_right = min((int)tasks[*iter].task_algorithm_data.width - 1, (face_ori_rect.left_ + 2 * face_ori_rect.width_));
1243   - int new_bottom = min((int)tasks[*iter].task_algorithm_data.height - 1, (face_ori_rect.top_ + 2 * face_ori_rect.height_));
1244   - int new_width = new_right - new_left;
1245   - int new_height = new_bottom - new_top;
1246   -
1247   - sy_rect face_expand_rect = { new_left, new_top, new_width, new_height };
1248   -
1249   - //更新快照
1250   - int face_img_length = 3 * face_expand_rect.width_ * face_expand_rect.height_;
1251   -
1252   - cudaFree(m_snaphot_helper.snapShotInfo[cur_obj_key].snapShotFace.frame);
1253   - cudaError_t cudaStatus = cudaMalloc((void**)&m_snaphot_helper.snapShotInfo[cur_obj_key].snapShotFace.frame, face_img_length * sizeof(unsigned char));
1254   - if (cudaStatus != cudaSuccess) {
1255   - fprintf(stderr, "here cudaMalloc frame[0] failed! error: %s\n", cudaGetErrorString(cudaStatus));
1256   - break;
1257   - }
1258   -
1259   - m_snaphot_helper.snapShotInfo[cur_obj_key].snapShotFace.width = face_expand_rect.width_;
1260   - m_snaphot_helper.snapShotInfo[cur_obj_key].snapShotFace.height = face_expand_rect.height_;
1261   - memcpy((void*)&m_snaphot_helper.snapShotInfo[cur_obj_key].face_info, (void*)&cur_det_info, sizeof(fd_info));
1262   -
1263   - //矫正坐标 从行人抠图检测结果 -> 人脸外扩抠图坐标
1264   - fd_info& tmp_info = m_snaphot_helper.snapShotInfo[cur_obj_key].face_info;
1265   -
1266   - for (int p = 0; p < FACIALFEAPOINTSIZE; p++)
1267   - {
1268   - tmp_info.facial_fea_point[p].x_ =
1269   - tmp_info.facial_fea_point[p].x_ - tmp_info.face_position.left_ + (face_ori_rect.left_ - face_expand_rect.left_);
1270   - tmp_info.facial_fea_point[p].y_ =
1271   - tmp_info.facial_fea_point[p].y_ - tmp_info.face_position.top_ + (face_ori_rect.top_ - face_expand_rect.top_);
1272   - }
1273   -
1274   - m_snaphot_helper.snapShotInfo[cur_obj_key].face_info.face_position =
1275   - {(face_ori_rect.left_ - face_expand_rect.left_), (face_ori_rect.top_ - face_expand_rect.top_), face_ori_rect.width_, face_ori_rect.height_};
1276   -
1277   - cudacommon::CropImgGpu((unsigned char*)tasks[*iter].task_algorithm_data.frame, tasks[*iter].task_algorithm_data.width, tasks[*iter].task_algorithm_data.height,
1278   - (unsigned char*)m_snaphot_helper.snapShotInfo[cur_obj_key].snapShotFace.frame, face_expand_rect.left_, face_expand_rect.top_, face_expand_rect.width_, face_expand_rect.height_);
1279   -
1280   - //show_gpu_image_withfdinfo_(m_snaphot_helper.snapShotInfo[cur_obj_key].snapShotFace, cur_det_info);
1281   -
1282   - //show_gpu_image_withfdinfo_(m_snaphot_helper.snapShotInfo[cur_obj_key].snapShotFace, m_snaphot_helper.snapShotInfo[cur_obj_key].face_info);
1283   - }
1284   - }
1285   - }
1286   - }
1287   -
1288   - if (face_det_result)
1289   - {
1290   - for (int fd_i = 0; fd_i < human_count; fd_i++)
1291   - delete[] face_det_result[fd_i].info;
1292   - delete face_det_result;
1293   - }
1294   -
1295   - if (human_img) delete[] human_img;
1296   - if (ori_points) delete[] ori_points;
  1136 + m_snaphot_helper.cacheFaceSnapshotInfo(human_img, human_count, ori_points, human_idx, human_obj_keys, snapshot_left, snapshot_top, task);
1297 1137 }
1298 1138 }
1299 1139  
1300 1140 //实时查看 绘制目标轨迹 回调函数返回
1301 1141 if (view)
1302 1142 {
1303   - DrawTracker(VPT_Handle, *iter, &tasks[*iter].frameImage);
1304   - if (tasks[*iter].taskRealTimeCallbackFunc != nullptr)
1305   - tasks[*iter].taskRealTimeCallbackFunc(tasks[*iter].frameImage.data, tasks[*iter].frameImage.rows, tasks[*iter].frameImage.cols);
  1143 + DrawTracker(VPT_Handle, *iter, &task.frameImage);
  1144 + if (task.taskRealTimeCallbackFunc != nullptr)
  1145 + task.taskRealTimeCallbackFunc(task.frameImage.data, task.frameImage.rows, task.frameImage.cols);
1306 1146 }
1307 1147 // tasks[*iter].taskFrameCount += skip_frame_;
1308 1148 iter++;
... ...
vehicle_structure_platform.git0708-3080-trt-face/src/VPT/MutliSourceVideoProcess.h
... ... @@ -18,7 +18,6 @@
18 18  
19 19 #include "mvpt_process_assist.h"
20 20 #include "snapshot_helper.h"
21   -#include "FaceDetModule.h"
22 21  
23 22 #include <atomic>
24 23 //#include "DxLogInterface.h"
... ... @@ -156,7 +155,6 @@ private:
156 155 public: /*��������Ӧ����public�� �������̺߳����л��õ����µ����� ÿ����дһ��get����̫������*/
157 156 //fstream fout[THREAD_COUNT]; //����������
158 157 void * VPT_Handle;
159   - face_det_module* m_face_det_module;
160 158 int section_batch_size;
161 159 int licence_status;
162 160 int thrd_status;
... ...
vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanCarParsing.cpp
... ... @@ -71,7 +71,7 @@ int HumanCarParsing::init(int gpuid, char* auth_license)
71 71 return SUCCESS;
72 72 }
73 73  
74   -int HumanCarParsing::process(sy_img * batch_img, int batch_size, hcp_analysis_result *&result)
  74 +int HumanCarParsing::process(sy_img * batch_img, int batch_size, hcp_analysis_result *result)
75 75 {
76 76 LOG_DEBUG("batch_size: {}", batch_size);
77 77 hcp_batch(handle, batch_img, batch_size, result);
... ...
vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanCarParsing.h
... ... @@ -12,7 +12,7 @@ public:
12 12 ~HumanCarParsing();
13 13  
14 14 int init(int gpuid, char* auth_license);
15   - int process(sy_img * batch_img,int batchsize, hcp_analysis_result *&result);
  15 + int process(sy_img * batch_img,int batchsize, hcp_analysis_result *result);
16 16 int release();
17 17  
18 18 private:
... ...
vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanFea.cpp
... ... @@ -36,7 +36,7 @@ int HumanFea::init(int gpuid, char* auth_license)
36 36 return SUCCESS;
37 37 }
38 38  
39   -int HumanFea::process(sy_img * batch_img, int batch_size, human_fea_result*& result)
  39 +int HumanFea::process(sy_img * batch_img, int batch_size, human_fea_result* result)
40 40 {
41 41 for (int i = 0; i < batch_size; i++) {
42 42 if (batch_img[i].data_ == NULL) {
... ...
vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/HumanFea.h
... ... @@ -9,7 +9,7 @@ public:
9 9 ~HumanFea();
10 10  
11 11 int init(int gpuid, char* auth_license);
12   - int process(sy_img * batch_img, int batch_size, human_fea_result*& result);
  12 + int process(sy_img * batch_img, int batch_size, human_fea_result* result);
13 13 int release();
14 14  
15 15 private:
... ...
vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/snapshot_helper.cpp
... ... @@ -140,6 +140,9 @@ void snapshot_helper::snapshot_helper_init(int gpuid, double gpu_total_memory, c
140 140 m_vehicle_feature.init_vf(dbpath_utf8, gpuid, auth_license);
141 141 }
142 142  
  143 + if(face_detect_cf == SY_CONFIG_OPEN) {
  144 + m_face_det_module.face_det_module_init(gpuid, auth_license);
  145 + }
143 146  
144 147 OBJ_BATCH_COUNT = 20;
145 148 if (gpu_total_memory < 9000) //8G显存,小内存方案
... ... @@ -153,7 +156,6 @@ void snapshot_helper::snapshot_helper_init(int gpuid, double gpu_total_memory, c
153 156 v_analysis = V_ANALYSIS_TYPE::VC_ANALYSIS;
154 157  
155 158 batch_hp = new sy_img[OBJ_BATCH_COUNT]{};
156   - batch_hcp = new sy_img[OBJ_BATCH_COUNT]{};
157 159 batch_vehicle = new sy_img[OBJ_BATCH_COUNT_VEHICLE]{};
158 160 batch_vehicle_vf = new sy_img[OBJ_BATCH_COUNT_VEHICLE]{};
159 161  
... ... @@ -178,17 +180,14 @@ void snapshot_helper::snapshot_helper_release()
178 180 m_vehicle_feature.release_vpd();
179 181 m_vehicle_feature.release_vf();
180 182 m_human_fea.release();
  183 + m_face_det_module.face_det_module_release();
181 184  
182 185 if (batch_hp != NULL)
183 186 {
184 187 delete[] batch_hp;
185 188 batch_hp = NULL;
186 189 }
187   - if (batch_hcp != NULL)
188   - {
189   - delete[] batch_hcp;
190   - batch_hcp = NULL;
191   - }
  190 +
192 191 if (batch_vehicle != NULL)
193 192 {
194 193 delete[] batch_vehicle;
... ... @@ -585,21 +584,20 @@ void snapshot_helper::finish_task_ss_analysis(int task_id) //是
585 584  
586 585 if (!hcp_keys.empty())
587 586 {
  587 + LOG_DEBUG("hcp_keys size: {}", hcp_keys.size());
  588 +
588 589 const int obj_batch_count = OBJ_BATCH_COUNT / OBJ_SCALE;
589 590 const int hcp_batch_size = hcp_keys.size();
590 591 int hcp_batch_count = obj_batch_count;
591 592  
592 593 for (int i = 0; i <= (hcp_batch_size / obj_batch_count); ++i)
593 594 {
594   - hcp_analysis_result *result = nullptr;
595   - human_fea_result * result_f = nullptr;
596 595 if (i == (hcp_batch_size / obj_batch_count))
597 596 hcp_batch_count = hcp_batch_size % obj_batch_count;
598 597  
599 598 if (hcp_batch_count == 0) continue;
600 599  
601   - sy_img* finish_hcp_img = new sy_img[hcp_batch_count]{};
602   -
  600 + sy_img finish_hcp_img[hcp_batch_count];
603 601 for (int j = 0; j < hcp_batch_count; j++)
604 602 {
605 603 OBJ_KEY cur_obj_key = hcp_keys[j];
... ... @@ -607,13 +605,13 @@ void snapshot_helper::finish_task_ss_analysis(int task_id) //是
607 605 finish_hcp_img[j].set_data(HCP_WIDTH, HCP_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame);
608 606 }
609 607  
  608 + hcp_analysis_result result[hcp_batch_count];
610 609 if (hcp_analysis_cf == SY_CONFIG_OPEN) {
611   - result = new hcp_analysis_result[hcp_batch_count]{};
612 610 m_human_car_parsing.process(finish_hcp_img, hcp_batch_count, result);
613 611 }
614 612  
  613 + human_fea_result result_f[hcp_batch_count];
615 614 if (hcf_recg_cf == SY_CONFIG_OPEN) {
616   - result_f = new human_fea_result[hcp_batch_count]{};
617 615 m_human_fea.process(finish_hcp_img, hcp_batch_count, result_f);
618 616 }
619 617 int resIndex = 0;
... ... @@ -665,22 +663,6 @@ void snapshot_helper::finish_task_ss_analysis(int task_id) //是
665 663 }
666 664  
667 665 hcp_keys.erase(hcp_keys.begin(), hcp_keys.begin() + hcp_batch_count);
668   -
669   - if (result != nullptr)
670   - {
671   - delete[] result;
672   - result = nullptr;
673   - }
674   - if (result_f != nullptr)
675   - {
676   - delete[] result_f;
677   - result_f = nullptr;
678   - }
679   - if (finish_hcp_img != NULL)
680   - {
681   - delete[] finish_hcp_img;
682   - finish_hcp_img = NULL;
683   - }
684 666 }
685 667 }
686 668  
... ... @@ -952,8 +934,8 @@ void snapshot_helper::hcp_analysis()
952 934 human_fea_result * result_f = nullptr;
953 935  
954 936 int cur_batchsize = index == 0 ? per_batchsize : OBJ_BATCH_COUNT - per_batchsize;
955   - for (int i = 0; i < cur_batchsize/*OBJ_BATCH_COUNT / OBJ_SCALE*/; i++)
956   - //for (int i = 0; i < OBJ_BATCH_COUNT/ OBJ_SCALE; i++)
  937 + sy_img batch_hcp[cur_batchsize];
  938 + for (int i = 0; i < cur_batchsize; i++)
957 939 {
958 940 OBJ_KEY cur_obj_key = count_bike.front();
959 941 count_bike.pop();
... ... @@ -2132,6 +2114,7 @@ CropInfo snapshot_helper::cacheSnapShotInfo(OBJ_KEY newObj, VPT_ObjInfo obj, Tas
2132 2114 {
2133 2115 cur_width = HCP_WIDTH;
2134 2116 cur_height = HCP_HEIGHT;
  2117 + LOG_DEBUG("task_id:{} obj_id:{} index = 0、1", newObj.videoID, newObj.objID);
2135 2118 }
2136 2119 else if (8 == snapShotInfo[newObj].index.index || (snapShotInfo[newObj].index.index >= 4 && snapShotInfo[newObj].index.index <= 6))
2137 2120 {
... ... @@ -2319,4 +2302,150 @@ int snapshot_helper::getIndexByKey(OBJ_KEY newObj) {
2319 2302 }
2320 2303  
2321 2304 return -1;
  2305 +}
  2306 +
  2307 +void snapshot_helper::cacheFaceSnapshotInfo(sy_img *human_img, int human_count, sy_point* ori_points, vector<int> human_idx, vector<OBJ_KEY> human_obj_keys, int snapshot_left[], int snapshot_top[], Task task) {
  2308 + fd_result *face_det_result = new fd_result[human_count];
  2309 + for (int fd_i = 0; fd_i < human_count; fd_i++)
  2310 + {
  2311 + face_det_result[fd_i].info = new fd_info[10]; //内存由外部申请
  2312 + }
  2313 +
  2314 + int ret = m_face_det_module.face_det_module_process(human_img, human_count, face_det_result, ori_points);
  2315 + if (SUCCESS == ret)
  2316 + {
  2317 + for (int idx = 0; idx < human_count; idx++)
  2318 + {
  2319 + OBJ_KEY cur_obj_key = human_obj_keys[idx];
  2320 + fd_result &cur_det_res = face_det_result[idx];
  2321 + int face_count = cur_det_res.count;
  2322 + int face_idx = 0;
  2323 + int ii = human_idx[idx];
  2324 +
  2325 + if (face_count == 0) continue;
  2326 +
  2327 + if (face_count > 1) //检测到多余一个人脸 选最佳
  2328 + {
  2329 + sy_point center_human = { human_img[idx].w_/2, human_img [idx].h_/2};
  2330 + float min_distance = INT_MAX;
  2331 +
  2332 + for (int c = 0; c < face_count; c++)
  2333 + {
  2334 + sy_point face_center = { cur_det_res.info[c].face_position.width_/2, cur_det_res.info[c].face_position.height_/2};
  2335 + float distance = fabs(center_human.x_ - face_center.x_) + fabs(center_human.y_ - face_center.y_);
  2336 +
  2337 + if (distance < min_distance)
  2338 + {
  2339 + min_distance = distance;
  2340 + face_idx = c;
  2341 + }
  2342 + }
  2343 + }
  2344 +
  2345 + fd_info& cur_det_info = cur_det_res.info[face_idx];
  2346 + if (snapShotInfo[cur_obj_key].snapShotFace.frame == nullptr)
  2347 + {
  2348 + sy_rect face_ori_rect = { (int)(snapshot_left[ii] + cur_det_info.face_position.left_ ),
  2349 + (int)(snapshot_top[ii] + cur_det_info.face_position.top_),
  2350 + (int)(cur_det_info.face_position.width_), (int)(cur_det_info.face_position.height_) };
  2351 +
  2352 + int new_left = max(0, face_ori_rect.left_ - face_ori_rect.width_);
  2353 + int new_top = max(0, face_ori_rect.top_ - face_ori_rect.height_);
  2354 + int new_right = min((int)task.task_algorithm_data.width - 1, (face_ori_rect.left_ + 2 * face_ori_rect.width_));
  2355 + int new_bottom = min((int)task.task_algorithm_data.height - 1, (face_ori_rect.top_ + 2 * face_ori_rect.height_));
  2356 + int new_width = new_right - new_left;
  2357 + int new_height = new_bottom - new_top;
  2358 +
  2359 + sy_rect face_expand_rect = { new_left, new_top, new_width, new_height };
  2360 +
  2361 + int face_img_length = 3 * face_expand_rect.width_ * face_expand_rect.height_;
  2362 +
  2363 + cudaError_t cudaStatus = cudaMalloc((void**)&snapShotInfo[cur_obj_key].snapShotFace.frame, face_img_length * sizeof(unsigned char));
  2364 + if (cudaStatus != cudaSuccess) {
  2365 + fprintf(stderr, "here cudaMalloc frame[0] failed! error: %s\n", cudaGetErrorString(cudaStatus));
  2366 + break;
  2367 + }
  2368 +
  2369 + snapShotInfo[cur_obj_key].snapShotFace.width = face_expand_rect.width_;
  2370 + snapShotInfo[cur_obj_key].snapShotFace.height = face_expand_rect.height_;
  2371 + memcpy((void*)&snapShotInfo[cur_obj_key].face_info, (void*)&cur_det_info, sizeof(fd_info));
  2372 +
  2373 + //矫正坐标 从行人抠图检测结果 -> 人脸外扩抠图坐标
  2374 + fd_info& tmp_info = snapShotInfo[cur_obj_key].face_info;
  2375 +
  2376 + for (int p = 0; p < FACIALFEAPOINTSIZE; p++)
  2377 + {
  2378 + tmp_info.facial_fea_point[p].x_ =
  2379 + tmp_info.facial_fea_point[p].x_ - tmp_info.face_position.left_ + (face_ori_rect.left_ - face_expand_rect.left_);
  2380 + tmp_info.facial_fea_point[p].y_ =
  2381 + tmp_info.facial_fea_point[p].y_ - tmp_info.face_position.top_ + (face_ori_rect.top_ - face_expand_rect.top_);
  2382 + }
  2383 +
  2384 + snapShotInfo[cur_obj_key].face_info.face_position =
  2385 + { (face_ori_rect.left_ - face_expand_rect.left_), (face_ori_rect.top_ - face_expand_rect.top_), face_ori_rect.width_, face_ori_rect.height_ };
  2386 +
  2387 +
  2388 + cudacommon::CropImgGpu((unsigned char*)task.task_algorithm_data.frame, task.task_algorithm_data.width, task.task_algorithm_data.height,
  2389 + (unsigned char*)snapShotInfo[cur_obj_key].snapShotFace.frame, face_expand_rect.left_, face_expand_rect.top_, face_expand_rect.width_, face_expand_rect.height_);
  2390 + }
  2391 + else
  2392 + {
  2393 + sy_rect face_ori_rect = { (int)(snapshot_left[ii] + cur_det_info.face_position.left_),
  2394 + (int)(snapshot_top[ii] + cur_det_info.face_position.top_),
  2395 + (int)(cur_det_info.face_position.width_), (int)(cur_det_info.face_position.height_) };
  2396 +
  2397 + //更新人脸快照条件:① 角度满足条件 ② 面积比之前人脸面积大
  2398 + if (validAngle(cur_det_res.info[face_idx].roll, cur_det_res.info[face_idx].yaw, cur_det_res.info[face_idx].pitch, 15.0, 20.0)
  2399 + && betterArea(face_ori_rect, snapShotInfo[cur_obj_key].face_info.face_position))
  2400 + {
  2401 + int new_left = max(0, face_ori_rect.left_ - face_ori_rect.width_);
  2402 + int new_top = max(0, face_ori_rect.top_ - face_ori_rect.height_);
  2403 + int new_right = min((int)task.task_algorithm_data.width - 1, (face_ori_rect.left_ + 2 * face_ori_rect.width_));
  2404 + int new_bottom = min((int)task.task_algorithm_data.height - 1, (face_ori_rect.top_ + 2 * face_ori_rect.height_));
  2405 + int new_width = new_right - new_left;
  2406 + int new_height = new_bottom - new_top;
  2407 +
  2408 + sy_rect face_expand_rect = { new_left, new_top, new_width, new_height };
  2409 +
  2410 + //更新快照
  2411 + int face_img_length = 3 * face_expand_rect.width_ * face_expand_rect.height_;
  2412 +
  2413 + cudaFree(snapShotInfo[cur_obj_key].snapShotFace.frame);
  2414 + cudaError_t cudaStatus = cudaMalloc((void**)&snapShotInfo[cur_obj_key].snapShotFace.frame, face_img_length * sizeof(unsigned char));
  2415 + if (cudaStatus != cudaSuccess) {
  2416 + fprintf(stderr, "here cudaMalloc frame[0] failed! error: %s\n", cudaGetErrorString(cudaStatus));
  2417 + break;
  2418 + }
  2419 +
  2420 + snapShotInfo[cur_obj_key].snapShotFace.width = face_expand_rect.width_;
  2421 + snapShotInfo[cur_obj_key].snapShotFace.height = face_expand_rect.height_;
  2422 + memcpy((void*)&snapShotInfo[cur_obj_key].face_info, (void*)&cur_det_info, sizeof(fd_info));
  2423 +
  2424 + //矫正坐标 从行人抠图检测结果 -> 人脸外扩抠图坐标
  2425 + fd_info& tmp_info = snapShotInfo[cur_obj_key].face_info;
  2426 +
  2427 + for (int p = 0; p < FACIALFEAPOINTSIZE; p++)
  2428 + {
  2429 + tmp_info.facial_fea_point[p].x_ =
  2430 + tmp_info.facial_fea_point[p].x_ - tmp_info.face_position.left_ + (face_ori_rect.left_ - face_expand_rect.left_);
  2431 + tmp_info.facial_fea_point[p].y_ =
  2432 + tmp_info.facial_fea_point[p].y_ - tmp_info.face_position.top_ + (face_ori_rect.top_ - face_expand_rect.top_);
  2433 + }
  2434 +
  2435 + snapShotInfo[cur_obj_key].face_info.face_position =
  2436 + {(face_ori_rect.left_ - face_expand_rect.left_), (face_ori_rect.top_ - face_expand_rect.top_), face_ori_rect.width_, face_ori_rect.height_};
  2437 +
  2438 + cudacommon::CropImgGpu((unsigned char*)task.task_algorithm_data.frame, task.task_algorithm_data.width, task.task_algorithm_data.height,
  2439 + (unsigned char*)snapShotInfo[cur_obj_key].snapShotFace.frame, face_expand_rect.left_, face_expand_rect.top_, face_expand_rect.width_, face_expand_rect.height_);
  2440 + }
  2441 + }
  2442 + }
  2443 + }
  2444 +
  2445 + if (face_det_result)
  2446 + {
  2447 + for (int fd_i = 0; fd_i < human_count; fd_i++)
  2448 + delete[] face_det_result[fd_i].info;
  2449 + delete face_det_result;
  2450 + }
2322 2451 }
2323 2452 \ No newline at end of file
... ...
vehicle_structure_platform.git0708-3080-trt-face/src/VPT/snapshot_analysis/snapshot_helper.h
... ... @@ -18,6 +18,7 @@
18 18 #include "VehicleColor.h"
19 19 #include "VehicleRearRecg.h"
20 20 #include "VehicleRecognition.h"
  21 +#include "FaceDetModule.h"
21 22  
22 23 using namespace std;
23 24  
... ... @@ -195,6 +196,8 @@ public:
195 196  
196 197 CropInfo cacheSnapShotInfo(OBJ_KEY newObj, VPT_ObjInfo obj, Task task);
197 198  
  199 + void cacheFaceSnapshotInfo(sy_img *human_img, int human_count, sy_point* ori_points, vector<int> human_idx, vector<OBJ_KEY> human_obj_keys, int snapshot_left[], int snapshot_top[], Task task);
  200 +
198 201 int getIndexByKey(OBJ_KEY newObj);
199 202  
200 203 private:
... ... @@ -216,6 +219,7 @@ private:
216 219 VehiclePlate m_vehicle_plate;
217 220 VehicleRearRecg m_vehicle_rear_recog;
218 221 VehicleRecognition m_vehicle_recognition;
  222 + face_det_module m_face_det_module;
219 223  
220 224 queue<OBJ_KEY> count_person;
221 225 queue<OBJ_KEY> count_bike;
... ... @@ -226,7 +230,6 @@ private:
226 230 vector<vehicle_result> vehicle_result_v;
227 231  
228 232 sy_img * batch_hp = nullptr;
229   - sy_img * batch_hcp = nullptr;
230 233 sy_img * batch_vehicle = nullptr;
231 234 sy_img * batch_vehicle_vf = nullptr;
232 235  
... ...