// 第一帧处理 if (isfirstframe path == 1) { rockxhandlandmarkt handlandmark; //…
- User
// 第一帧处理 if (is_first_frame || path == 1) { rockx_hand_landmark_t hand_landmark; // 移到循环内部,并去掉memset操作 rockx_keypoints_array_t hand_array; // 移到循环内部,并去掉memset操作
// hand detection ret = rockx_hand_detect(hand_det_handle, &input_image, &hand_array); if (ret != ROCKX_RET_SUCCESS) { printf("rockx_hand_detect error %d\n", ret); continue; } for (int i = 0; i < hand_array.count; i++) { if (hand_array.keypoints[i].box_score > 0.7f && hand_array.keypoints[i].count > 0) // 添加关键点数量检查 { int left = hand_array.keypoints[i].box.left; int top = hand_array.keypoints[i].box.top; int right = hand_array.keypoints[i].box.right; int bottom = hand_array.keypoints[i].box.bottom; inbox.count = hand_array.keypoints[i].count; for (int j = 0; j < hand_array.keypoints[i].count; j++) { inbox.points[j].x = hand_array.keypoints[i].points[j].x; inbox.points[j].y = hand_array.keypoints[i].points[j].y; inbox.points[j].z = hand_array.keypoints[i].points[j].z; cv::Scalar black_color(0, 0, 0); // 黑色 cv::circle(frame, cv::Point(static_cast<int>(inbox.points[j].x), static_cast<int>(inbox.points[j].y)), 5, black_color, -1); // 绘制黑色圆圈 } inbox.box = hand_array.keypoints[i].box; cv::Scalar black_color(0, 0, 0); // 黑色 cv::rectangle(frame, cv::Point(inbox.box.left, inbox.box.top), cv::Point(inbox.box.right, inbox.box.bottom), black_color, 2); // 绘制黑色框 // 调用手部关键点检测函数 ret = rockx_hand_landmark(hand_landmark_handle, &input_image, &inbox, &hand_landmark); if (ret != ROCKX_RET_SUCCESS) { printf("rockx_hand_landmark error %d\n", ret); return -1; } std::cout << "hand_landmark.score1: " << hand_landmark.score << std::endl; all_hand_landmarks_map[i] = hand_landmark; // 使用索引 i 存储手部关键点信息 if (hand_landmark.score > 0.5f) { cv::Scalar blue_color(255, 0, 0); // 蓝色 for (int j = 0; j < inbox.count; j++) { int mapped_index = mapping_indices[j]; float x_diff = hand_landmark.landmarks[mapped_index].x - inbox.points[j].x; float y_diff = hand_landmark.landmarks[mapped_index].y - inbox.points[j].y; offset_matrix(j, mapped_index) = x_diff; offset_matrix(j + 7, mapped_index) = y_diff; } for (int j = 0; j < hand_landmark.landmarks_count; j++) { float x_raw = hand_landmark.landmarks[j].x; float y_raw = hand_landmark.landmarks[j].y; cv::circle(frame, cv::Point(static_cast<int>(x_raw), static_cast<int>(y_raw)), 5, blue_color, -1); } } all_offset_matrices[i] = offset_matrix; // 存储对应的 offset_matrix } } is_first_frame = false; path = 2; } else { int index = 0; for (auto it = all_hand_landmarks_map.begin(); it != all_hand_landmarks_map.end(); ++it) { new_inbox.count = 7; // 设置关键点数量为与上一帧相同 int hand_index = it->first; // 获取当前手部的索引 float min_x = std::numeric_limits<float>::max(); float min_y = std::numeric_limits<float>::max(); float max_x = std::numeric_limits<float>::min(); float max_y = std::numeric_limits<float>::min(); std::cout << "min_x: " << min_x << std::endl; std::cout << "min_y: " << min_y << std::endl; for (int j = 0; j < new_inbox.count; j++) { int mapped_index = mapping_indices[j]; new_inbox.points[j].x = it->second.landmarks[mapped_index].x - all_offset_matrices[hand_index](j, mapped_index); new_inbox.points[j].y = it->second.landmarks[mapped_index].y - all_offset_matrices[hand_index](j + 7, mapped_index); min_x = std::min(min_x, new_inbox.points[j].x); min_y = std::min(min_y, new_inbox.points[j].y); max_x = std::max(max_x, new_inbox.points[j].x); max_y = std::max(max_y, new_inbox.points[j].y); } new_inbox.box.left = static_cast<int>(min_x); new_inbox.box.top = static_cast<int>(min_y); new_inbox.box.right = static_cast<int>(max_x); new_inbox.box.bottom = static_cast<int>(max_y); std::cout << "new_inbox.box.left: " << new_inbox.box.left << std::endl; std::cout << "new_inbox.box.top: " << new_inbox.box.top << std::endl; std::cout << "new_inbox.box.right: " << new_inbox.box.right << std::endl; std::cout << "new_inbox.box.bottom: " << new_inbox.box.bottom << std::endl; cv::Scalar green_color(0, 255, 0); // 绿色 cv::rectangle(frame, cv::Point(new_inbox.box.left, new_inbox.box.top), cv::Point(new_inbox.box.right, new_inbox.box.bottom), green_color, 2); for (int j = 0; j < new_inbox.count; j++) { float x_raw = new_inbox.points[j].x; float y_raw = new_inbox.points[j].y; cv::circle(frame, cv::Point(static_cast<int>(x_raw), static_cast<int>(y_raw)), 5, green_color, -1); } rockx_hand_landmark_t hand_landmark_temp; memset(&hand_landmark_temp, 0, sizeof(rockx_hand_landmark_t)); // 创建局部变量 ret = rockx_hand_landmark(hand_landmark_handle, &input_image, &new_inbox, &hand_landmark_temp); // 使用局部变量 if (ret != ROCKX_RET_SUCCESS) { printf("rockx_hand_landmark error %d\n", ret); return -1; } for (int j = 0; j < new_inbox.count; j++) { int mapped_index = mapping_indices[j]; offset_matrix(j, mapped_index) = hand_landmark_temp.landmarks[mapped_index].x - new_inbox.points[j].x; offset_matrix(j + 7, mapped_index) = hand_landmark_temp.landmarks[mapped_index].y - new_inbox.points[j].y; } all_offset_matrices[hand_index] = offset_matrix; if (hand_landmark_temp.score > 0.5f) // 使用局部变量的score { cv::Scalar red_color(0, 0, 255); // 红色 for (int j = 0; j < hand_landmark_temp.landmarks_count; j++) // 使用局部变量的landmarks_count { float x_raw = hand_landmark_temp.landmarks[j].x; // 使用局部变量的landmarks float y_raw = hand_landmark_temp.landmarks[j].y; cv::circle(frame, cv::Point(static_cast<int>(x_raw), static_cast<int>(y_raw)), 5, red_color, -1); } } std::cout << "hand_landmark_temp.score3: " << hand_landmark_temp.score << std::endl; all_hand_landmarks_map[index] = hand_landmark_temp; // 更新map的值 if (it->second.score < 0.5f) { is_first_frame = true; } index++; } }以上代码有什么问题 - Assistant
该段代码中存在一些问题:
-
在第一帧处理时,进行了手部检测(rockx_hand_detect)和手部关键点检测(rockx_hand_landmark)的操作,但在第二帧及之后的处理中没有进行手部检测的操作,只进行了手部关键点检测,这可能导致在后续的处理中无法正确识别和追踪手部。
-
在第一帧处理时,手部检测(rockx_hand_detect)的结果中,对于关键点数量大于0的手部才进行后续操作,但在第二帧及之后的处理中没有对关键点数量进行检查,可能导致在处理过程中使用到了无效或不完整的关键点数据。
-
在第一帧处理时,将所有手部关键点信息都存储在了一个map中,但在第二帧及之后的处理中,通过遍历map来处理每个手部的关键点信息,这可能导致在多个手部同时存在的情况下顺序不一致,无法正确匹配每个手部的关键点信息。
-
在第二帧及之后的处理中,对于每个手部,使用一个局部变量(hand_landmark_temp)来存储手部关键点检测的结果,但在使用局部变量之前没有进行初始化操作,可能导致使用了未经初始化的内存。
-
在第二帧及之后的处理中,对于每个手部,更新了对应手部的offset_matrix和手部关键点信息,并将其存储在了all_offset_matrices和all_hand_landmarks_map中,但并没有对所有的hand_landmark进行更新操作,可能导致在后续的处理中无法正确使用更新后的数据。
-
- User
根据上述问题进行修改
- Assistant