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: For every video in a Google Drive folder, extract body keypoints (COCO-17) for each person per frame on GPU, save per‑video CSVs, and generate annotated videos.

This mirrors the MediaPipe notebook’s structure but uses a CUDA-native pipeline that works reliably on Colab GPUs. This version fixes occasional IndexError when a frame/person returns no keypoints or fewer than 17 keypoints.

1) Runtime & GPU Check

Instruction: Enable GPU in Colab → Runtime → Change runtime type → Hardware accelerator: GPU.

2) Install Dependencies

3) Imports

4) Set Input Folder (Drive) and Output Folder

Edit one line: INPUT_DIR — the Drive folder that contains your videos.
All outputs (CSVs + annotated videos) will be written to OUTPUT_DIR.

5) Choose Pose Model Variant

Pick a model for speed/accuracy trade‑off:

  • yolov8n-pose.pt → nano (fastest)

  • yolov8s-pose.pt → small (balanced) ← default

  • Larger models: yolov8m/l/x-pose.pt (slower, higher accuracy)

6) COCO Keypoint Names (17)

We export COCO‑17 keypoints per person (x, y, conf).

7) Helper — Video Metadata

8) Core Function — Track & Export Keypoints to CSV

  • Uses ByteTrack for stable track_id.

  • Saves annotated video under OUTPUT_DIR/<run_name>/...

  • Saves CSV under OUTPUT_DIR/<run_name>_keypoints.csv

  • The pipeline supports Robust Export (handles missing/partial keypoints) - guarding against frames/people that return zero or partial keypoints by filling None values instead of indexing into an empty array.

9) Batch Run — Process Every Video in the Folder

  • Uses the video filename (without extension) as run_name.

  • Produces one CSV and an annotated video per input file.

10) Preview the First CSV

11) Optional — Merge All CSVs

12) Tips

  • Speed vs. Accuracy: Prefer yolov8n-pose.pt for faster class demos; use yolov8s-pose.pt for better accuracy.

  • Tracking IDs: track_id lets you follow the same person across frames. -1 means no stable ID was assigned.

  • Coordinates: Keypoints are in image pixel space after model preprocessing (letterboxing). For original-space mapping, you’ll need to unletterbox (advanced).

13) Parameter Tuning — What do CONF, IOU, IMG_SIZE do?

  • CONF (confidence threshold): Minimum detection confidence to accept a person before pose is estimated.

    • Higher (e.g., 0.5) → fewer detections, more precise.

    • Lower (e.g., 0.2) → more detections, may include false positives.

  • IOU (Intersection-over-Union for Non-Max Suppression): Overlap threshold for merging/suppressing detections.

    • Higher (e.g., 0.7) keeps more overlapping detections (risk of duplicates).

    • Lower (e.g., 0.4) suppresses overlaps more aggressively.

  • IMG_SIZE (model input size): The size images are resized to before inference.

    • Larger (e.g., 800) → slower, can help small/complex poses.

    • Smaller (e.g., 512) → faster, may miss small details.