Sub topic 13
Running a Pretrained Pose Model
Assume you know basic Python, NumPy, convolutional neural networks, bounding boxes, and training metrics. This chapter uses Ultralytics YOLO to run a pretrained single-stage pose model. The model detects people and predicts the keypoints for each person in one call.
Install and run the model
A pretrained model has already learned from labelled pose images. You do not need to train it before testing an image. Install the package in your environment:
pip install ultralyticsThe following program loads a small pose model and reads the first detected person:
from ultralytics import YOLO
model = YOLO("yolov8n-pose.pt")
results = model("person.jpg")
keypoints = results[0].keypoints
if keypoints is not None and len(keypoints.xy) > 0:
xy = keypoints.xy[0].cpu().numpy()
confidence = keypoints.conf[0].cpu().numpy()
print("coordinates:", xy.shape)
print("confidence:", confidence.shape)
print("right shoulder:", xy[6])
print("right shoulder confidence:", confidence[6])
else:
print("No person with pose keypoints was detected.")The model downloads its weights the first time it runs. The image path can point to a local file or another supported image source.
For the standard COCO pose layout, xy has shape (17, 2). Each row contains an (x, y) pixel coordinate. The confidence array has shape (17,), with one score per keypoint. Coordinates start at the upper-left corner of the image. The x value increases to the right, and the y value increases downward.
Ultralytics uses this COCO order:
| Index | Keypoint |
|---|---|
| 0 | nose |
| 5 | left shoulder |
| 6 | right shoulder |
| 9 | left wrist |
| 10 | right wrist |
| 11 | left hip |
| 12 | right hip |
| 15 | left ankle |
| 16 | right ankle |
The full result can contain several people. The expression keypoints.xy[0] selects the first detected person. For production code, loop over all detected people and use the associated person confidence and bounding box.
Understand the result pipeline
A single-stage pose model predicts person boxes and keypoints together. This differs from a top-down pipeline, which first detects people, crops each box, and then runs a separate pose model on each crop.
A high confidence does not guarantee a correct point. The model may be confident about a point that belongs to another person, especially in a crowd. Check the image, the person box, and nearby keypoints when debugging. Also remember that a keypoint confidence measures the model's belief in that point, not physical certainty or measurement accuracy.
Exercise
Exercise
Why does keypoints.xy[0] contain only one person's pose, and how would you access the second person's pose?
Show answer
The first index selects the first detected person. Use keypoints.xy[1] and keypoints.conf[1] for the second person after checking that at least two people were detected.