Sub topic 1
What a pose is: keypoints, skeletons and the COCO layout
Prerequisites
This chapter assumes you can read basic Python and NumPy. It also assumes you know what a convolutional neural network does in image classification, and what an object detector's bounding box is. Later chapters rely on training loss and evaluation metrics, but this one only needs a general idea of them.
From pixels to a pose
A pose estimation model takes an image of a person and returns a small set of named points on the body, such as the left elbow or the right ankle. The model doesn't return a mask or a class label. Its output is a list of coordinates, and those coordinates describe the body's configuration in the image. That configuration is the pose.
Three terms come up throughout the tutorial:
- A keypoint is one named body location, stored as an (x, y) position in image pixels. It usually comes with a confidence score or a visibility flag.
- A skeleton is a fixed list of pairs of keypoints, called limbs or edges, that are connected. It adds structure to the points but does not add new numbers.
- A pose is the set of keypoint coordinates for one person, in a fixed order.
A pose is a small, structured output. One person in the COCO layout needs only 17 points, which is 34 coordinates, or 51 numbers if you include a per-point score. This is far smaller than a segmentation mask, and it is much easier to use in later code. You can measure joint angles, count repetitions of an exercise, or compare two people's postures directly from these numbers.
The COCO 17-keypoint layout
Many pretrained models, including the ones used in later chapters, follow the COCO keypoint layout. COCO is a large image dataset whose person annotations contain 17 keypoints. The order is fixed, and every tool that uses COCO relies on it. If you swap two indices, left and right get confused, or the skeleton is drawn wrongly.
| Index | Name | Index | Name |
|---|---|---|---|
| 0 | nose | 9 | left wrist |
| 1 | left eye | 10 | right wrist |
| 2 | right eye | 11 | left hip |
| 3 | left ear | 12 | right hip |
| 4 | right ear | 13 | left knee |
| 5 | left shoulder | 14 | right knee |
| 6 | right shoulder | 15 | left ankle |
| 7 | left elbow | 16 | right ankle |
| 8 | right elbow |
The layout has two features worth noting.
First, the points alternate left and right. Apart from the nose at index 0, odd indices are the person's left side and even indices are the person's right side. "Left" means the person's own left, not the left of the image. A person facing the camera has their left elbow on the right side of the picture. This is the origin of many labelling and debugging mistakes, which a later chapter on failure modes returns to.
Second, the layout is coarse. There are five points on the head but only one each for the shoulder, elbow, wrist, hip, knee and ankle on each side. There are no points for fingers, toes, the spine, or the centre of the chest. COCO also has no neck point. Other layouts exist, such as whole-body layouts with hands and face, and 3D datasets that use 15 to 25 points. The idea in this chapter carries over to all of them, but the names and the indices change. Always check which layout a model uses before you read its output.
Skeletons: connecting the points
The skeleton is the list of index pairs that you draw as lines. The COCO convention uses 19 limbs. Here is the list in Python, with a function that draws it.
import numpy as np
KEYPOINT_NAMES = [
"nose", "left_eye", "right_eye", "left_ear", "right_ear",
"left_shoulder", "right_shoulder", "left_elbow", "right_elbow",
"left_wrist", "right_wrist", "left_hip", "right_hip",
"left_knee", "right_knee", "left_ankle", "right_ankle",
]
# Pairs of indices into KEYPOINT_NAMES (0-based).
SKELETON = [
(15, 13), (13, 11), (16, 14), (14, 12), (11, 12), # legs and hips
(5, 11), (6, 12), (5, 6), # torso
(5, 7), (6, 8), (7, 9), (8, 10), # arms
(1, 2), (0, 1), (0, 2), (1, 3), (2, 4), # face
(3, 5), (4, 6), # ears to shoulders
]
def limb_lengths(keypoints):
"""keypoints: array of shape (17, 2). Returns a dict of limb name -> pixel length."""
lengths = {}
for a, b in SKELETON:
name = f"{KEYPOINT_NAMES[a]}-{KEYPOINT_NAMES[b]}"
lengths[name] = float(np.linalg.norm(keypoints[a] - keypoints[b]))
return lengths
if __name__ == "__main__":
pose = np.zeros((17, 2))
pose[5] = (100, 100) # left shoulder
pose[7] = (100, 160) # left elbow
print(limb_lengths(pose)["left_shoulder-left_elbow"]) # 60.0The skeleton is only a drawing convention in most models. The network predicts keypoints, and the skeleton is added afterwards for display. A few methods, covered in the bottom-up chapter, use the limbs in the model itself to decide which points belong to which person.
The graph can be shown as a diagram. The torso forms a closed loop of shoulders and hips. Each arm and each leg is a chain that hangs from it.
Coordinates and what they mean
A keypoint is an (x, y) pair in the pixel grid of the image. The origin (0, 0) is the top-left corner. The x value grows to the right and the y value grows downwards. This is the opposite of the vertical direction in a maths plot, so a head has a smaller y than a foot.
Models often work on a resized or cropped version of the image. Their raw outputs are in the coordinates of that resized input, and you must map them back to the original image before you draw or measure anything. Heatmap models add another factor: their output grid is smaller than the input by a stride. The decoding chapter returns to this. For now, remember that "the coordinates" always refer to a specific frame of reference, and a mix-up of frames is the most common reason a skeleton looks shifted or scaled.
Some pipelines store keypoints as normalised values between 0 and 1, divided by the image width and height. Others store pixels. Check the range of the numbers when you first see a new model's output. A set of values all below 1.0 on a 640-pixel image is almost certainly normalised.
Confidence and visibility
Every keypoint has more than a position. Two kinds of extra information appear in practice, and they should not be mixed up.
- Visibility is part of the ground-truth annotation that a human labeller supplies. COCO uses three flags: 0 means the point is not labelled (it is outside the image or cannot be placed), 1 means it is labelled but occluded, and 2 means it is labelled and visible. The next chapter covers these flags in detail.
- Confidence is a number a model outputs at prediction time, usually between 0 and 1. It expresses how strongly the model believes the point is where it says.
A model returns a coordinate for every keypoint, even for a point that is hidden. A hidden wrist still gets an (x, y). The model infers it from context, such as the position of the elbow and the arm's direction. This is useful, but it means you cannot treat a returned coordinate as proof that the joint can be seen. Use the confidence score to decide what to trust.
import numpy as np
def filter_keypoints(xy, conf, threshold=0.5):
"""Replace low-confidence keypoints with NaN so later code can skip them.
xy: (K, 2) array of coordinates. conf: (K,) array of confidences.
"""
out = np.asarray(xy, dtype=float).copy()
out[np.asarray(conf) < threshold] = np.nan
return out
xy = np.array([[120.0, 80.0], [130.0, 140.0], [90.0, 150.0]])
conf = np.array([0.92, 0.35, 0.80])
print(filter_keypoints(xy, conf))The second row prints as nan nan. Using NaN instead of zero is deliberate: a zero would look like a real point at the top-left corner, and it would distort any average or distance you computed from it.
A threshold of 0.5 is a common starting point, but it is not a rule. The right value depends on the model and the use. A fitness application may prefer to drop uncertain points, while a research visualisation may prefer to show everything.
Why the representation matters
Choosing 17 points is a design decision with consequences.
- Resolution of detail. With a single point per wrist, you can see where a hand is, but not whether it is open or closed. A model trained on COCO cannot tell you a finger position.
- Rotations are missing. A point has no orientation. An elbow at a given position tells you nothing about how the forearm is twisted. Systems that need twist, such as animation, use body models with joint rotations, which are a different representation.
- Depth is missing. A 2D pose has no z coordinate. Two different 3D postures can project to the same 2D points. The last chapter looks at lifting 2D poses to 3D.
- Scale depends on distance. The same pose appears in pixels as large or small depending on how far the person is from the camera. When you compare poses, normalise by a body length, such as the distance between the shoulders and hips, or by the bounding box.
import numpy as np
def normalise_pose(xy):
"""Centre on the hip midpoint and scale by the torso length (COCO indices)."""
xy = np.asarray(xy, dtype=float)
hip_mid = (xy[11] + xy[12]) / 2
shoulder_mid = (xy[5] + xy[6]) / 2
torso = np.linalg.norm(shoulder_mid - hip_mid)
return (xy - hip_mid) / (torso + 1e-9)After this transformation, two people of different sizes at different distances can be compared point by point. The torso length is a better scale than image height because it does not change when the person bends or sits.
Common mistakes
- Reading the wrong layout. A model with a different keypoint count or order will give wrong names if you apply the COCO table. Check the number of keypoints first.
- Confusing the person's left with the image's left. Always use the person's side when labelling or flipping.
- Flipping images without swapping labels. A horizontal flip turns a left wrist into a right-side image position. The labels for left and right must be swapped as well. This matters for augmentation, which a later chapter covers.
- Treating every returned point as visible. Filter by confidence before you calculate angles or distances.
- Mixing coordinate frames. Keep track of whether values are in input, crop, or original-image pixels, or normalised.
Check your understanding
Exercise
A model returns a (17, 2) array for a person facing the camera. The point at index 7 is at x = 400, and the point at index 8 is at x = 250. Which elbow is on the left of the image, and which is the person's left elbow? Also, what does it mean for the result if the confidence for index 8 is 0.1?
Show answer
Index 7 is the left elbow and index 8 is the right elbow, by the COCO order. The smaller x value, 250, belongs to index 8, so the person's right elbow is on the left of the image. The person's left elbow (index 7, x = 400) is on the right of the image, because the person faces the camera. A confidence of 0.1 for index 8 means the model has very little belief in that position. The coordinate is probably a guess based on context, for example because the arm is hidden, so you should discard it or mark it as NaN before you compute any angle or distance.
Exercise
Using the COCO order, write the indices of the three points you need to measure the angle at the person's right elbow, and say why two people of different heights can be compared more fairly after normalising by torso length.
Show answer
The right shoulder is index 6, the right elbow is index 8 and the right wrist is index 10, so the angle at point 8 is formed by 6, 8 and 10. Normalising by torso length removes the effect of the person's size in pixels and their distance from the camera. After centring on the hips and dividing by the torso length, both people have a torso of length 1, so differences in the numbers reflect differences in posture and not in scale.