Rewrite README for the packaged pipeline

Cover the problem, the approach, how to run it and its measured
limitations, and correct what no longer matched the code (the --mode
flag, the .env file, the training directory). Update CLAUDE.md to the
new layout and commands.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-09 12:08:54 +01:00
co-authored by Claude Opus 5.5
parent 7b19d8f3e9
commit 36fae9cdc3
2 changed files with 133 additions and 107 deletions
+31 -32
View File
@@ -4,48 +4,47 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
## Project Overview
A computer vision + LLM pipeline that extracts rowing machine workout data from photos of Concept 2 PM5 displays. Photos go through screen detection, classification, and OCR-via-LLM to produce structured workout metrics.
A computer vision pipeline that extracts rowing machine workout data (time, distance) from photos of Concept 2 PM5 displays: screen detection, classification, then Tesseract OCR with majority voting. A Claude vision reader produces the reference readings the OCR is validated against. See README.md for the approach and measured limitations.
## Pipeline
## Layout
```
photos/ → crop_to_screen.py → screen_classifier.py → extract_rowing_data.py → rowing_results.csv
src/rowing_stats/
cli.py `rowing-stats <command>` dispatcher; imports a command's module lazily
crop.py find_screen / perspective_correct / crop_screen; CropParams holds the tuned defaults
classifier.py feature-based scorer and CNN (torch imported lazily)
ocr.py tesseract subprocess, preprocessing variants, validation report
parsing.py pure vote counting over OCR text (no image code)
claude.py Claude Haiku reader, reply parsing, sanity bounds
tune.py Optuna search over CropParams, scored by the CNN
results.py results CSV rows, pace/calories, reference loading
images.py list_images / read_image / open_image
errors.py RowingStatsError hierarchy
tests/ pytest; Tesseract and the Claude client are faked
photos/ source photos
train/0, train/1 CNN training crops (not rowing / rowing)
rowing_results.csv reference readings from the Claude reader
screen_classifier_model.pth trained CNN weights
```
1. **crop_to_screen.py** — Detects and perspective-corrects the LCD screen region using OpenCV edge detection, contour filtering, and morphological operations. Scores candidates by `edge_density × area × rectangularity`.
2. **screen_classifier.py** — Binary classifier (rowing display vs. not). Two modes: a rule-based feature scorer (no training needed) and a 4-layer CNN with batch norm (requires training on `train/` data).
3. **extract_rowing_data.py** — Sends cropped images to Claude Haiku vision API, extracts time/distance, validates against sanity bounds (pace, distance, duration), computes derived metrics (pace/500m, calories), and reads EXIF date.
4. **optimize_crop.py** — Optuna-based hyperparameter tuner for crop_to_screen.py detection parameters (12 params). Evaluates trials by counting CNN-classified rowing displays.
## Commands
```bash
# Crop screens from photos
python crop_to_screen.py photos/ cropped/
pip install -r requirements-dev.txt && pip install -e .
pytest
# Classify images (feature-based or CNN)
python screen_classifier.py predict --dir cropped/
python screen_classifier.py predict --image path/to/img.jpg --mode cnn
# Train the CNN classifier
python screen_classifier.py train --data-dir train/
# Extract rowing data (requires ANTHROPIC_API_KEY)
python extract_rowing_data.py --dir photos/
python extract_rowing_data.py --image path/to/img.jpg
# Optimize crop parameters
python optimize_crop.py --n-trials 300 --photos-dir photos/
rowing-stats crop photos/ cropped/
rowing-stats classify predict --dir cropped/ [--model cnn]
rowing-stats classify train --data-dir train/
rowing-stats ocr --dir cropped/ --validate rowing_results.csv
rowing-stats claude --dir photos/ --output rowing_results.csv # requires ANTHROPIC_API_KEY
rowing-stats tune --n-trials 300 --photos-dir photos/
```
## Dependencies
No requirements.txt exists. Key packages: `anthropic`, `torch`, `torchvision`, `opencv-python` (cv2), `Pillow`, `numpy`, `optuna`.
## Key Details
- The Claude API call in extract_rowing_data.py uses `claude-haiku-4-5-20251001` for cost efficiency on a structured extraction task.
- Validation bounds in extract_rowing_data.py mirror those from a Go handler (`handle_rowing.go`): distance 100–100k meters, time 30s–2hrs, pace 1:20–2:30 /500m.
- Training data lives in `train/0/` (non-rowing, 80 samples) and `train/1/` (rowing displays, 48 samples).
- The trained CNN model is saved as `screen_classifier_model.pth`.
- `.env` contains API keys — it is gitignored and must not be committed.
- Dependencies are pinned in `requirements*.txt` (core, `-claude`, `-ml`, `-dev`) with matching extras in `pyproject.toml`. `anthropic`, `torch`, `torchvision` and `optuna` are optional and must stay lazily imported.
- Raise a `RowingStatsError` subclass rather than printing or exiting from library code. `SetupError` subclasses mean every file would fail, so CLIs stop on them; anything else is reported per file and the batch continues.
- The crop parameters, preprocessing variants and vote counting are tuned to the sample photos. A refactor should leave `rowing-stats crop` output byte-identical and the per-image lines of `rowing-stats ocr --validate` unchanged.
- The Claude call uses `claude-haiku-4-5-20251001`. Validation bounds in claude.py mirror a Go handler (`handle_rowing.go`): distance 100–100k metres, time 30s–2hrs, pace 1:20–2:30 /500m.
- `.env` is gitignored but nothing loads it; the Claude reader takes its key from the environment.