Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Authored by Souvik Mandal, Ph.D.

Project Leader & Instructor, Computational Behavioral Sciences, LS100, FAS, Harvard University | Linkedin ID: souvik-mandal-phd


Last updated: 2026-07-03

Open in Colab Open in GitHub Codespaces

Goal: Extract body landmark coordinates (keypoints) for every person in each video frame on GPU and save them to a CSV.
This notebook is designed for undergraduate students in LS100 and mirrors the structure of your previous MediaPipe notebook (setup → verify → run → export), but uses a CUDA-native pipeline so it works reliably on Colab GPUs.


What you’ll get

  • An annotated output video (skeletons drawn).

  • A CSV file with one row per person per frame: frame_idx, timestamp_ms, track_id, 17 COCO keypoints (x, y, confidence) and detection confidence.

What you need to do

  • Provide a path to a video (Drive or uploaded file).

  • Optionally choose a different YOLOv8 pose model variant for accuracy/speed trade-offs.

1) Runtime & GPU Check

Instruction: Make sure your Colab runtime has a GPU enabled.
Menu: Runtime → Change runtime type → Hardware accelerator: GPU → Save.

Then run the cell below to confirm.

2) Install Dependencies

Instruction: Run this once per fresh Colab session. Installs the Ultralytics package (YOLOv8).

3) Imports

Instruction: No edits needed.

4) Set Input/Output Paths

Choose one of the options below:

  • Option A: Mount Google Drive and set INPUT_VIDEO to your file in Drive.

  • Option B: Upload a file from your computer.

Students: Use one option. Comment out the other.

5) Choose Pose Model Variant

Instruction: Pick a model for speed/accuracy:

  • yolov8n-pose.pt → nano (fastest, least accurate)

  • yolov8s-pose.pt → small (good balance)

  • yolov8m/l/x-pose.pt → larger, slower, more accurate

Start with nano or small for classroom demos.

6) COCO Keypoint Mapping (17 points)

We will export 17 COCO keypoints per person:

['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']

Each keypoint will have x, y, conf in the CSV.

7) Helper — Video Metadata

We read FPS (frames per second) to compute timestamp_ms = frame_idx * 1000 / fps.

8) Core Function — Track & Export Keypoints to CSV

This function:

  1. Runs tracking with ByteTrack (stable track IDs per person).

  2. Saves an annotated video to OUTPUT_DIR.

  3. Builds a CSV with columns:

    • frame_idx, timestamp_ms, track_id, det_conf

    • For each keypoint: <name>_x, <name>_y, <name>_conf

Do not modify this function unless instructed.

9) Run on Your Video

Instruction: Set RUN_NAME (used to name outputs) and run.
The annotated video and CSV will appear under OUTPUT_DIR/RUN_NAME/ and OUTPUT_DIR/ respectively.

10) Preview the CSV

Instruction: Display the first 5 rows.

11) Optional — Download Outputs

If you’re on Colab, you can download the CSV and annotated video to your computer.

12) Tips & Troubleshooting

  • Speed vs. Accuracy: Switch to yolov8n-pose.pt for faster processing on large videos or multiple people.

  • Tracking IDs: We use ByteTrack for stable track_id. If you see -1, a track wasn’t assigned for that frame.

  • Coordinates: Keypoints are in pixel coordinates of the processed image (default imgsz=640 maintains aspect by letterboxing in Ultralytics). If you need original-space coordinates, you may need to reverse letterboxing (advanced).

  • Multiple People: One row per person per frame. Use track_id to follow the same person across frames.

  • Memory: Long videos can be large. Consider trimming or using yolov8n-pose.pt.

  • Drive Paths: If paths with spaces cause issues, rename or wrap them in quotes (strings are already quoted in Python).


Assignment Ideas (LS100)

  • Compute joint angles over time (e.g., elbow, knee) from keypoints and visualize trends.

  • Segment activities based on pose dynamics (walking vs. running).

  • Compare left/right symmetry in movement for different subjects.