CHALKESAHIL.AI · VIBE CODING · DAY 2

Ballspeed: the exact prompt

Upload a video of a bowler and get the ball speed in km/h. Built with Claude Code in 54 minutes, from the one prompt below.

Try the live app

It runs on a free server, so the first load can take up to a minute.

The prompt

Paste it into Claude Code in an empty folder. It plans first and waits for your go-ahead before writing code.

<pasted_content id="3afc">
I want to build a cricket ball speed estimator: a web app where I upload a video of a bowler
bowling, and it estimates the ball's speed in km/h. Please plan first, then build it in phases.
Don't write code until I approve the plan.

## Architecture
Phone video (ideally 240 fps slow-mo, tripod) → React frontend (upload + display)
→ FastAPI backend → Python CV pipeline:
  1. Frame extraction (FFmpeg + OpenCV)
  2. Ball detection
  3. Ball tracking (Kalman filter)
  4. Release frame detection (MediaPipe Pose, bowler's wrist)
  5. Calibration (pixel → metre homography using the crease/stumps)
  6. Speed calculation (NumPy/SciPy trajectory fit)
→ JSON result back to the frontend (speed + per-frame ball positions for an overlay)

## Stack
- Monorepo: /backend (Python 3.11, FastAPI, uv for deps) and /frontend (React + TypeScript + Vite)
- Backend libs: opencv-python, numpy, scipy, filterpy, mediapipe, ultralytics (YOLO, optional at first)
- FFmpeg/ffprobe via subprocess
- No database for now. Store uploads in a temp dir and clean them up.

## Phases (build and verify each before moving on)

### Phase 1: Video ingest
- Upload endpoint accepting mp4/mov.
- Use ffprobe to get the real fps, frame count and duration. Phones often record variable
  frame rate (VFR). Detect this and either re-encode to constant fps or use per-frame
  timestamps (pts) for timing. Never assume nominal fps.
- Return metadata and a preview frame.

### Phase 2: Calibration + manual mode (the MVP; must work without any ML)
- Frontend shows a frame. The user clicks 4 known points (crease/stump corners) to compute
  a homography with cv2.findHomography. Pitch facts: 20.12 m stump to stump, popping crease
  1.22 m in front of the stumps, pitch width 3.05 m between return creases.
- Frontend frame scrubber (step frame by frame). User marks the release frame and the frame
  where the ball reaches the batter's stumps.
- "Frame-count mode" speed: distance ≈ 18.9 m (release near the popping crease to the far
  stumps) / time between marked frames using real timestamps.
- Show the error bound: ±1 frame of timing error expressed in km/h.

### Phase 3: Automatic ball detection + tracking
- Start with classical CV: background subtraction (MOG2) plus contour filtering by area,
  circularity and colour, restricted to an ROI around the pitch.
- Kalman filter (constant velocity or constant acceleration) to smooth the track and bridge
  missed frames. Reject outliers.
- Design a detector interface so I can swap in a fine-tuned YOLO model later
  (ultralytics, weights path in config). Don't train anything yet.

### Phase 4: Automatic release detection
- MediaPipe Pose on the bowler. The release frame is roughly where the tracked ball separates
  from the bowling-hand wrist keypoint.
- Always allow manual override from the UI.

### Phase 5: Speed calculation
- Project ball positions to the pitch plane via the homography. Note in code comments that
  this is an approximation, since the ball is in the air and not on the ground plane.
- Use only pre-bounce frames. Fit position vs time and estimate speed near release
  (radar-equivalent) as well as the average speed over the tracked segment.
- Return: release_speed_kmh, avg_speed_kmh, error_estimate_kmh, method used, fps,
  and per-frame track points.

### Phase 6: Frontend result view
- Video player with the ball track overlaid on a canvas, release and bounce markers,
  and the speed readout with its error range.

## Quality bar
- Type hints everywhere. Pydantic models for API I/O. Strict TypeScript on the frontend.
- pytest tests, including a synthetic test video generated with OpenCV (a white ball
  moving at a known speed across a known-scale scene at 240 fps). The pipeline must recover
  that speed within 2%. Write this test early and use it to validate each phase.
- Clear error messages for bad input (fps too low, ball not found, calibration missing).
- Write a README covering setup, filming tips (fps, tripod, side-on vs behind-the-bowler),
  and the known accuracy limitations.
- Create a CLAUDE.md capturing the project structure, commands and conventions.

Start by proposing the folder structure, API endpoints, data models and the phase plan.
Flag anything you think is a bad idea or missing.
</pasted_content id="3afc">

Filming tips

The bug worth knowing

The first version said a 140 km/h test ball was 17.4. Slow-mo video plays back at 30 fps, but each frame was really 1/240 of a second apart. Reading the real frame rate from the file fixed it: 139.2.