File size: 7,038 Bytes
f8f9ba8 a20494c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
---
license: apache-2.0
datasets:
- AdityasArsenal/YogaDataSet
pipeline_tag: image-classification
tags:
- pose
- yoga
- human
- mediapipe
language:
- en
---
# Yoga Pose Classification Toolkit
This repository contains a small, script-first pipeline to prepare data, extract pose landmarks with MediaPipe, train machine‑learning pose classifiers, and run a real‑time webcam demo.
The sections below explain what each Python script in the project root does and how to use it on macOS (zsh). For dependencies, see `requirements.txt`.
## Prerequisites
- Python 3.x
- Install Python packages:
```bash
pip install -r requirements.txt
```
Optional but recommended: create and activate a virtual environment before installing.
## Typical end‑to‑end workflow
1) (Optional) Extract raw images from the included Parquet dataset into train/test folders using `extract_images.py`.
2) Run `pose_detection.py` to generate per‑image pose landmark JSON files under `PoseData/label_*`.
3) Train and evaluate a classifier with `ml_pose_classifier.py`. Optionally export to ONNX or TFLite.
4) Run the webcam demo with `realtime_pose_classifier.py` using your saved model.
---
## Script: extract_images.py
Purpose
- Extract images and labels from the provided Parquet files (in `YogaDataSet/data/`) and save them into folders by label for training and testing.
Inputs/Outputs
- Input: `YogaDataSet/data/train-00000-of-00001.parquet`, `YogaDataSet/data/test-00000-of-00001.parquet`
- Output: Images under `TrainData/train/label_*` and/or `TrainData/test/label_*`
Usage
```bash
# Process both train and test (default behavior)
python extract_images.py
# Train only
python extract_images.py --train --output TrainData
# Test only to a custom folder
python extract_images.py --test --output MyOutputDir
```
Notes
- The script creates `label_0`, `label_1`, … subfolders and writes image files with their original extensions.
---
## Script: pose_detection.py
Purpose
- Run MediaPipe Pose on your labeled image folders and save normalized landmark coordinates to JSON files for training.
Preprocessing
- Uses the nose as the head reference point and applies: position = (pos − headPos) × 100, rounded to 2 decimals. This matches the training pipeline.
Inputs/Outputs
- Input: An images root (default `TrainData/train`) organized as `label_*/*.jpg|png|…`
- Output: JSON files under `PoseData/label_*/<image_name>.json`
Usage
```bash
# Process images from default input into PoseData
python pose_detection.py
# Custom input and output
python pose_detection.py --input TrainData/train --output PoseData --batch-size 100
```
Tips
- Supported image extensions: .jpg, .jpeg, .png, .bmp, .tiff
- Requires a working OpenCV + MediaPipe install (see `requirements.txt`).
---
## Script: ml_pose_classifier.py
Purpose
- Train, evaluate, and export pose classifiers from landmark JSONs. Supports Random Forest, SVM, Gradient Boosting, Logistic Regression, and a knowledge‑distilled RF→MLP variant.
Data expectation
- Directory structure like:
- `PoseData/label_0/*.json`
- `PoseData/label_1/*.json`
- …
Common options
- `--data/-d` Pose JSON root (default: `PoseData`)
- `--model/-m` Model type: `random_forest` (default), `svm`, `gradient_boost`, `logistic`, `distilled_rf`
- `--test-size/-t` Test split ratio (default: 0.2)
- `--save-model/-s` Path to save the trained model (`.pkl` via joblib)
- `--load-model/-l` Path to load an existing model
- `--predict/-p` Predict a single JSON file
- `--evaluate/-e` Evaluate a folder of JSON files
- `--export-onnx` Export the trained model to ONNX (tree models or distilled MLP)
- `--export-model-type` Controls which model flavor to export
- `--export-tflite` Export distilled student MLP to TFLite (requires extra deps)
Typical commands
```bash
# 1) Train a Random Forest and save it
python ml_pose_classifier.py \
--data PoseData \
--model random_forest \
--test-size 0.2 \
--save-model models/pose_classifier_random_forest.pkl
# 2) Evaluate a saved model on a held‑out folder (e.g., TestData)
python ml_pose_classifier.py \
--model random_forest \
--load-model models/pose_classifier_random_forest.pkl \
--evaluate TestData
# 3) Export to ONNX (Random Forest or distilled MLP)
python ml_pose_classifier.py \
--model random_forest \
--load-model models/pose_classifier_random_forest.pkl \
--export-onnx models/pose_classifier_random_forest.onnx
# 4) Knowledge distillation: train RF teacher + MLP student
python ml_pose_classifier.py \
--data PoseData \
--model distilled_rf \
--save-model models/pose_classifier_distilled_rf.pkl
# 5) Export the student MLP to TFLite (extra packages required)
python ml_pose_classifier.py \
--model distilled_rf \
--load-model models/pose_classifier_distilled_rf.pkl \
--export-tflite models/pose_classifier_distilled_mlp.tflite
```
Notes
- ONNX export depends on `skl2onnx` and `onnx`. TFLite export additionally needs `onnx-tf` and `tensorflow`.
- Linear classifiers (`svm`, `logistic`) are not supported by Unity Barracuda. Prefer `random_forest` or the distilled MLP for deployment.
---
## Script: realtime_pose_classifier.py
Purpose
- Run live pose classification from your webcam using a previously trained model. Draws the skeleton, highlights the used joints, and overlays prediction + confidence.
Model loading
- If `--model` is not provided, the script auto‑searches common filenames in the project root:
- `pose_classifier_random_forest.pkl`
- `pose_classifier_logistic.pkl`
- `pose_classifier_distilled_rf.pkl`
Usage
```bash
# Auto‑detect a model and open the default camera (0)
python realtime_pose_classifier.py
# Specify a model file and camera index
python realtime_pose_classifier.py \
--model models/pose_classifier_random_forest.pkl \
--camera 0
```
Keyboard controls
- Q: Quit
- L: Toggle landmark keypoints
- C: Toggle pose connections
- R: Reset prediction history (smoothing window)
Notes
- Uses the same preprocessing as training (nose‑relative coordinates ×100, 2‑decimal rounding, StandardScaler).
- For smoother predictions, a small history window is used to compute a stable label and average confidence.
---
## Useful folders and artifacts
- `YogaDataSet/data/` — Parquet files used by `extract_images.py`.
- `TrainData/train|test/label_*/` — Image folders produced by extraction.
- `PoseData/label_*/` — Landmark JSONs generated by `pose_detection.py`.
- `models/` — Example trained/exported models and label mappings.
- `confusion_matrix_*.png` — Saved confusion matrix plots (when enabled in training script).
## Troubleshooting
- MediaPipe install issues on macOS: ensure you’re using a supported Python version and the latest pip; try reinstalling `mediapipe` and `opencv-python`.
- Camera cannot open: try a different `--camera` index, close other apps using the camera, or allow camera permissions for Python in macOS Privacy settings.
- Model not found in real‑time script: pass `--model` with an explicit path to your `.pkl` file.
|