head-tracking/README.md
This implements head tracking with AirPods by gathering sensor data over l2cap, processing orientation and acceleration values, and detecting head gestures. The codebase is split into the following components:
Connect your airpods and change the mac address in plot.py to your airpods mac address. Then run the following command to start the program.
python plot.py
Alternatively, you can directly run the gestures.py to just detect gestures.
python gestures.py
Connection and Data Collection
The project uses a custom ConnectionManager (imported in multiple files) to connect via Bluetooth to AirPods. Once connected, sensor packets are received in raw hex format. An AirPodsTracker class (in plot.py) handles the start/stop of tracking, logging of raw data, and parsing of packets into useful fields.
Orientation Calculation and Visualization
The HeadOrientation class (in head_orientation.py) is responsible for:
o1_neutral = np.mean(samples[:, 0])pitch = (o2_norm + o3_norm) / 2 / 32000 * 180
yaw = (o2_norm - o3_norm) / 2 / 32000 * 180
Live Plotting and Interactive Commands
The code offers both terminal-based plotting and graphical plotting via matplotlib. The AirPodsTracker manages live plotting by maintaining a buffer of recent packets. When in terminal mode, the code uses libraries like asciichartpy and drawille to render charts; in graphical mode, it creates live-updating plots.
Gesture Detection
The GestureDetector class (in gestures.py) processes the head tracking data to detect nodding ("Yes") or head shaking ("No"):
Smoothing:
Raw horizontal and vertical sensor data undergo moving-average smoothing using small fixed-size buffers. This reduces noise and provides a steadier signal.
Peak and Trough Detection:
The code monitors small sections (e.g. the last 4 values) to compute variance and dynamically determine thresholds for direction changes. When a significant reversal (e.g. from increasing to decreasing) is detected that surpasses the dynamic threshold value (derived partly from a fixed threshold and variance), a peak or trough is recorded.
Rhythm Consistency:
Time intervals between detected peaks are captured. The consistency of these intervals (by comparing them to their mean and computing relative variance) is used to evaluate whether the movement is rhythmic—a trait of intentional gestures.
Confidence Calculation:
Multiple factors are considered:
A weighted sum of these factors forms a confidence score which, if above a predefined threshold (e.g. 0.7), confirms a detected gesture.