Add person model into RTMPOSE

This commit is contained in:
2026-05-14 19:05:20 +10:00
parent ee8f0d1163
commit 8e4d9a9e5d
5 changed files with 478 additions and 36 deletions
@@ -971,16 +971,19 @@ int VideoDetectorEngine(std::string modelFilePath, std::string videoFilePath, in
printf("Inference Time = %lld ms\n", static_cast<long long int>(elapsed.count()));
if (!detectionResult.empty()) {
// COCO pose skeleton connections
const std::vector<std::pair<int, int>> POSE_SKELETON = {
// ── COCO 17-keypoint skeleton (Ultralytics YOLO Pose format) ──
// Indices: 0=nose, 1=Leye, 2=Reye, 3=Lear, 4=Rear,
// 5=Lshoulder, 6=Rshoulder, 7=Lelbow, 8=Relbow,
// 9=Lwrist, 10=Rwrist, 11=Lhip, 12=Rhip,
// 13=Lknee, 14=Rknee, 15=Lankle, 16=Rankle
const std::vector<std::pair<int, int>> COCO17_SKELETON = {
{0,1}, {0,2}, {1,3}, {2,4}, // Face
{3,5}, {4,6}, // Head-to-shoulder
{5,7}, {7,9}, {6,8}, {8,10}, // Arms
{5,6}, {5,11}, {6,12}, {11,12}, // Body
{11,13}, {13,15}, {12,14}, {14,16} // Legs
};
// Colors for different limb groups
const std::vector<cv::Scalar> LIMB_COLORS = {
const std::vector<cv::Scalar> COCO17_COLORS = {
{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, // Face - blue
{0,255,255}, {0,255,255}, // Head-shoulder - yellow
{0,255,0}, {0,255,0}, {0,165,255}, {0,165,255}, // Arms - green/orange
@@ -988,6 +991,68 @@ int VideoDetectorEngine(std::string modelFilePath, std::string videoFilePath, in
{0,255,0}, {0,255,0}, {0,165,255}, {0,165,255} // Legs - green/orange
};
// ── Halpe 26-keypoint skeleton (MMPose body_2d_keypoint, used by RTMPose) ──
// Layout: 0..16 = COCO 17 (same as above);
// 17=Head, 18=Neck, 19=Hip(mid),
// 20=LBigToe, 21=RBigToe,
// 22=LSmallToe, 23=RSmallToe,
// 24=LHeel, 25=RHeel
const std::vector<std::pair<int, int>> HALPE26_SKELETON = {
{0,1}, {0,2}, {1,3}, {2,4}, // face
{17,18}, // head -> neck
{5,18}, {6,18}, // shoulders -> neck
{18,19}, // neck -> mid hip
{19,11}, {19,12}, // mid hip -> L/R hip
{5,7}, {7,9}, // left arm
{6,8}, {8,10}, // right arm
{11,13}, {13,15}, // left leg
{12,14}, {14,16}, // right leg
{15,20}, {15,22}, {15,24}, // left foot (big-toe / small-toe / heel)
{16,21}, {16,23}, {16,25} // right foot
};
const std::vector<cv::Scalar> HALPE26_COLORS = {
{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, // face - blue
{0,255,255}, // head-neck - yellow
{0,255,255}, {0,255,255}, // shoulders-neck - yellow
{255,255,0}, // neck-midhip - cyan
{255,255,0}, {255,255,0}, // midhip-hips - cyan
{0,255,0}, {0,255,0}, // left arm - green
{0,165,255}, {0,165,255}, // right arm - orange
{0,255,0}, {0,255,0}, // left leg - green
{0,165,255}, {0,165,255}, // right leg - orange
{200,100,255}, {200,100,255}, {200,100,255}, // left foot - light purple
{255,100,100}, {255,100,100}, {255,100,100} // right foot - light pink
};
// ── OpenPose 18-keypoint skeleton (OpenVINO HumanPose, used by ANSPOSE) ──
// DIFFERENT index layout from COCO-17:
// 0=Nose, 1=Neck, 2=RShoulder, 3=RElbow, 4=RWrist,
// 5=LShoulder, 6=LElbow, 7=LWrist,
// 8=RHip, 9=RKnee, 10=RAnkle,
// 11=LHip, 12=LKnee, 13=LAnkle,
// 14=REye, 15=LEye, 16=REar, 17=LEar
// Note: ANSPOSE writes kps as stride-2 (x,y only — no per-kp score),
// so the drawer detects this layout by buffer size and skips
// the confidence threshold for it.
const std::vector<std::pair<int, int>> OPENPOSE18_SKELETON = {
{1,2}, {2,3}, {3,4}, // right arm (neck->shoulder->elbow->wrist)
{1,5}, {5,6}, {6,7}, // left arm
{1,8}, {8,9}, {9,10}, // right leg (neck->hip->knee->ankle)
{1,11}, {11,12}, {12,13}, // left leg
{1,0}, // neck->nose
{0,14}, {14,16}, // right eye->ear
{0,15}, {15,17} // left eye->ear
};
const std::vector<cv::Scalar> OPENPOSE18_COLORS = {
{0,165,255}, {0,165,255}, {0,165,255}, // right arm - orange
{0,255,0}, {0,255,0}, {0,255,0}, // left arm - green
{0,165,255}, {0,165,255}, {0,165,255}, // right leg - orange
{0,255,0}, {0,255,0}, {0,255,0}, // left leg - green
{255,255,0}, // neck-nose - cyan
{255,0,0}, {255,0,0}, // right face - blue
{255,0,0}, {255,0,0} // left face - blue
};
pt.clear();
std::stringstream ss;
ss.clear();
@@ -1103,29 +1168,88 @@ int VideoDetectorEngine(std::string modelFilePath, std::string videoFilePath, in
}
}
// Draw keypoints if available (format: x1;y1;score1;x2;y2;score2;... stride=3)
// Draw keypoints if available. Three on-the-wire formats are
// auto-detected from the buffer length so the same drawer
// handles all pose models without changing the engines:
//
// 51 floats → 17 kps stride-3 → YOLO Pose (COCO-17)
// 78 floats → 26 kps stride-3 → RTMPose (Halpe-26)
// 36 floats → 18 kps stride-2 → OpenPose/ANSPOSE
//
// For stride-3 formats kps[k*3+2] is a per-keypoint confidence
// and we threshold on it. ANSPOSE doesn't emit confidences
// (kps are stride-2), so we use kp.x>0 && kp.y>0 as the
// visibility proxy (ANSPOSE writes 0 / negatives for missing
// keypoints — see ANSPOSE.cpp:292).
if (!kps_str.empty()) {
std::vector<float> kps = ANSCENTER::ANSUtilityHelper::StringToKeypoints(kps_str);
int numKeypoints = (int)kps.size() / 3; // stride of 3: x, y, confidence
const size_t n = kps.size();
// Collect keypoint positions and confidence
const std::vector<std::pair<int, int>>* skeleton = nullptr;
const std::vector<cv::Scalar>* colors = nullptr;
int stride = 3;
int numKeypoints = 0;
bool hasPerKpScore = true;
if (n == 51) { // YOLO Pose: 17 kps × 3
stride = 3;
numKeypoints = 17;
skeleton = &COCO17_SKELETON;
colors = &COCO17_COLORS;
hasPerKpScore = true;
}
else if (n == 78) { // RTMPose: 26 kps × 3
stride = 3;
numKeypoints = 26;
skeleton = &HALPE26_SKELETON;
colors = &HALPE26_COLORS;
hasPerKpScore = true;
}
else if (n == 36) { // OpenPose/ANSPOSE: 18 kps × 2
stride = 2;
numKeypoints = 18;
skeleton = &OPENPOSE18_SKELETON;
colors = &OPENPOSE18_COLORS;
hasPerKpScore = false;
}
else if (n % 3 == 0) { // Unknown stride-3 — draw dots only
stride = 3;
numKeypoints = static_cast<int>(n) / 3;
hasPerKpScore = true;
}
else if (n % 2 == 0) { // Unknown stride-2 — draw dots only
stride = 2;
numKeypoints = static_cast<int>(n) / 2;
hasPerKpScore = false;
}
// Collect keypoint positions and (optional) confidence.
// For stride-2 we treat any kp with x>0 && y>0 as visible.
std::vector<cv::Point2f> keypointPts(numKeypoints);
std::vector<float> keypointConf(numKeypoints, 0.f);
std::vector<float> keypointConf(numKeypoints, 1.f);
for (int k = 0; k < numKeypoints; k++) {
keypointPts[k] = cv::Point2f(kps[k * 3], kps[k * 3 + 1]);
keypointConf[k] = kps[k * 3 + 2];
const float kx = kps[k * stride];
const float ky = kps[k * stride + 1];
keypointPts[k] = cv::Point2f(kx, ky);
if (hasPerKpScore) {
keypointConf[k] = kps[k * stride + 2];
} else {
keypointConf[k] = (kx > 0.f && ky > 0.f) ? 1.f : 0.f;
}
}
const float kpConfThreshold = 0.3f;
// Draw skeleton lines
for (size_t s = 0; s < POSE_SKELETON.size(); s++) {
int idx1 = POSE_SKELETON[s].first;
int idx2 = POSE_SKELETON[s].second;
if (idx1 < numKeypoints && idx2 < numKeypoints &&
keypointConf[idx1] > kpConfThreshold && keypointConf[idx2] > kpConfThreshold) {
cv::Scalar color = (s < LIMB_COLORS.size()) ? LIMB_COLORS[s] : cv::Scalar(200, 200, 200);
cv::line(frame, keypointPts[idx1], keypointPts[idx2], color, 2, cv::LINE_AA);
// Draw skeleton lines (skipped for unknown layouts).
if (skeleton) {
for (size_t s = 0; s < skeleton->size(); s++) {
int idx1 = (*skeleton)[s].first;
int idx2 = (*skeleton)[s].second;
if (idx1 < numKeypoints && idx2 < numKeypoints &&
keypointConf[idx1] > kpConfThreshold && keypointConf[idx2] > kpConfThreshold) {
cv::Scalar color = (s < colors->size()) ? (*colors)[s] : cv::Scalar(200, 200, 200);
cv::line(frame, keypointPts[idx1], keypointPts[idx2], color, 2, cv::LINE_AA);
}
}
}
@@ -1488,7 +1612,6 @@ int YOLO26POSEYolo11Test() {
int modelType = 31; // ONNX YOLO (30) RT YOLO (31)
std::string onnxclmodelFilePath = "E:\\Programs\\DemoAssets\\ANSAIModels\\PoseEstimationModels\\ANSPOSEN.zip";
std::string videoFile = "E:\\Programs\\DemoAssets\\Videos\\train.mp4";
VideoDetectorEngine(onnxclmodelFilePath, videoFile, modelType);// 22 is RT, 21 is onnx
return 0;
}
@@ -1872,14 +1995,23 @@ int FallDetectorTest() {
VideoDetectorEngine(modelFilePath, videoFile, modelType);
return 0;
}
int RTMPOSETest() {
int modelType = 33; // ONNX YOLO (30) RT YOLO (31)
std::string onnxclmodelFilePath = "E:\\Programs\\DemoAssets\\ANSAIModels\\PoseEstimationModels\\ANSRTMPOSE.zip";
std::string videoFile = "E:\\Programs\\DemoAssets\\Videos\\train.mp4";
VideoDetectorEngine(onnxclmodelFilePath, videoFile, modelType);// 22 is RT, 21 is onnx
return 0;
}
int main()
{
FallDetectorTest();
//FallDetectorTest();
//ALPROD_Test();
//FireNSmokeTest();
//ElasticGPUTest();
RTMPOSETest();
//YOLO26SEGYolo11Test();
//YOLO26POSEYolo11Test();
//YOLO26CLYolo11Test();