Sub topic 14
Using the Output: Joint Angles and Smoothing
This chapter assumes basic Python and NumPy, familiarity with convolutional neural networks, and an understanding of pose-model confidence scores. The model has already produced keypoints as pixel coordinates and a confidence value for each keypoint.
From Keypoints to Joint Angles
A keypoint is a point such as a shoulder, elbow, or wrist. A joint angle uses three points. For an elbow, use the shoulder, elbow, and wrist. The angle is measured at the middle point:
\theta = \cos^{-1}\left(\frac{u \cdot v}{\|u\|\|v\|}\right)Here, u points from the elbow to the shoulder, and v points from the elbow to the wrist. The result is between 0 and 180 degrees.
import numpy as np
def joint_angle(a, b, c):
"""Return the angle at b for three (x, y) points."""
a = np.asarray(a, dtype=float)
b = np.asarray(b, dtype=float)
c = np.asarray(c, dtype=float)
first = a - b
second = c - b
denominator = np.linalg.norm(first) * np.linalg.norm(second)
if denominator < 1e-9:
return np.nan
cosine = np.dot(first, second) / denominator
return float(np.degrees(np.arccos(np.clip(cosine, -1.0, 1.0))))
shoulder = (100, 120)
elbow = (140, 170)
wrist = (190, 150)
print(f"{joint_angle(shoulder, elbow, wrist):.1f} degrees")The calculation uses image coordinates, but the angle does not depend on the image scale. Resizing the whole image preserves the angle. Perspective can still change the apparent angle because a camera records a three-dimensional pose in two dimensions.
Always check keypoint confidence before calculating an angle. A badly estimated wrist can create a large, false movement. You can reject the result when any of the three required confidence values is below a chosen threshold.
Why Pose Measurements Jitter
A pose model processes each frame independently. Small changes in pixels can move the predicted peak by one or more heatmap cells. Detector box changes, motion blur, and occlusion add more variation. The resulting angle may jump even when the person is still.
A simple temporal smoother combines the current measurement with the previous smoothed value:
s_t = \alpha x_t + (1-\alpha)s_{t-1}A larger alpha follows motion more quickly but removes less noise. A smaller alpha produces a steadier signal but adds more delay.
def exponential_smooth(values, alpha=0.35):
"""Smooth a one-dimensional sequence, preserving missing values."""
smoothed = []
previous = None
for value in values:
value = float(value)
if np.isnan(value):
smoothed.append(np.nan)
continue
previous = value if previous is None else alpha * value + (1 - alpha) * previous
smoothed.append(previous)
return np.array(smoothed)
angles = np.array([92, 88, 95, 90, 91], dtype=float)
print(exponential_smooth(angles))Use the same frame rate when tuning alpha. At a higher frame rate, the same value may feel more responsive or more sluggish depending on the motion. For real-time interaction, measure both delay and noise instead of choosing a value by appearance alone.
A Practical Post-Processing Pipeline
A robust application should retain the raw coordinates, confidences, raw angle, and smoothed angle. This record makes failures easier to diagnose and prevents a smoother from hiding a persistent left-right swap or an occluded limb.
Exercise
Why can a smaller smoothing factor make a pose-controlled interface feel unresponsive?
Show answer
It gives more weight to older measurements, so the output changes slowly and lags behind the person's actual movement.