apopt.comCBE 493R Data-Driven Athletics

Course / Tutorials / Week 10 / Q-30

Quick tutorial Q-30 · Week 10 · about 20 min · Core

Track a ball and fit its path

Detect a tossed tennis ball in every frame, fit a line to its horizontal path and a parabola to its height, and use gravity to check the clip's clock.

  1. 1Gather (used)
  2. 2Statistics
  3. 3Visualize
  4. 4Cleanse
  5. 5Features (used)
  6. 6Balance
  7. 7Scale
  8. 8Split
  9. 9Deploy
  1. Ask
  2. Collect (this week)
  3. Check (this week)
  4. Interpret
  5. Coach
  6. Reassess

2 of 3 cells run in this page. The rest need a model that does not run in the browser: run them in the notebook on your computer or in Colab (File → Upload notebook); their output from the course build is shown, and the files they write are published so the later cells still run here. Helpful first: Q-22 Detect a person in one image, Q-29 Median and spread of ten trials.

The decision

A player wants to know how high and how consistently they toss the ball on the serve. Before measuring the toss, check that the clip's clock is right; the ball's own fall is the check.

The raw data

Try it

Once a ball leaves the hand it follows the projectile equations of Chapter 3: its horizontal position changes at a steady rate and its vertical position follows a parabola set by gravity. The apps find the ball as an object, the "sports ball" class of the YOLO detector, and track its center frame by frame (Section 10.5). Here you do the same on a tennis serve toss from the course's example clips, tennis1_60fps.mp4.

The first cell needs ultralytics and opencv, so it runs on your computer or in Colab. The clip shows a person, so it stays there; only the table of ball positions is published.

Detect the ball in every frame

Runs in the notebook (needs a model that does not run in the browser). Output from the course build:

import os
import cv2
import pandas as pd
from ultralytics import YOLO

model = YOLO("yolo26n.pt")                      # COCO class 32 is "sports ball"
cap = cv2.VideoCapture("tennis1_60fps.mp4")
print(f"header: {cap.get(cv2.CAP_PROP_FPS):.0f} fps, {int(cap.get(cv2.CAP_PROP_FRAME_COUNT))} frames")
rows, i = [], 0
while True:
    ok, frame = cap.read()
    if not ok:
        break
    r = model(frame, verbose=False, device="cpu", classes=[32], conf=0.1)[0]
    if len(r.boxes):
        k = int(r.boxes.conf.argmax())              # the most confident ball
        x1, y1, x2, y2 = r.boxes.xyxy[k].tolist()
        rows.append([i, (x1 + x2) / 2, (y1 + y2) / 2, x2 - x1, y2 - y1, float(r.boxes.conf[k])])
    i += 1
os.makedirs("clean", exist_ok=True)
track = pd.DataFrame(rows, columns=["frame", "x", "y", "w", "h", "conf"]).round(2)
track.to_csv("clean/tennis1_ball.csv", index=False)
print(f"ball found in {len(track)} of {i} frames, from frame {track.frame.min()} to {track.frame.max()}")
header: 30 fps, 137 frames
ball found in 50 of 137 frames, from frame 16 to 77

Fit the path

From here on everything runs in this page from the table. Keep the frames of the toss itself: the longest run of detections with no gap over three frames. Fit a straight line to x and a parabola to y, both against frame number.

PythonA line for x, a parabola for y

Type a prediction to enable Run.

Show the expected result without running
toss: frames 28 to 77 (48 detections)
x: -1.74 px per frame; y: curvature 1.425 px per frame squared; apex at frame 57.1; residual SD 9.3 px

Gravity as a check on the clock

The curvature is an acceleration in pixels per frame squared. Turn pixels into meters with the ball itself: a tennis ball is 6.54 to 6.86 cm across (ITF Rules of Tennis, Appendix I), so use 6.7 cm and the median box size. Turn frames into seconds with a frame rate. Gravity is known, 9.81 m/s², so the right frame rate is the one that gives it back.

PythonWhat frame rate gives gravity back?

Show the expected result without running
at 30 fps: gravity comes out 3.10 m/s²
at 60 fps: gravity comes out 12.41 m/s²
ball scale 413 px/m; frame rate that gives 9.81 m/s²: 53.3 fps
The ball's image height against frame number, axis turned over so up is up, with the fitted parabola through the points and the residuals in a small lower panel.

The path is a parabola (the apex at frame 57.1), and the curvature is positive because image y grows downward. But neither frame rate on offer gives gravity back: the header's 30 gives 3.10 m/s², the 60 in the file name 12.41 m/s², and the rate that fits is 53.3 frames per second. Something in the clip's clock is not what either label says. Look at the points, too: every sixth or so comes in a pair at the same height (frames 30 and 31, 42 and 43, ...), where the file shows the same picture twice, and the path jumps at frame 69. The residual SD, 9.3 px, comes mostly from those places, not from the detector. CS-14 finds the cause and what it means for a release speed.

For the athlete: a ball's path checks the video before the video measures the ball; film your toss with a known frame rate and the whole flight in the frame.

With your own data

Film your own toss with the phone still and the whole flight in the frame, at a frame rate you set and write down. Run the first cell on your clip; the gravity check should come out within a few percent of 9.81 m/s².

Check yourself

  1. The ball box is 27.5 pixels across and a tennis ball is 6.7 cm. What is the scale?

    px/m

  2. A curvature of 1.425 px per frame² and 413 px/m give 3.10 m/s² at 30 fps. What would doubling the frame rate do?

0 of 2 answered · 0 right the first time

The same method in another field

A falling object is a free calibration: engineers check a high-speed camera's frame rate or a scale with a dropped ball for the same reason. The Data-Driven Engineering page on object detection runs an earlier YOLO detector on a photo and a video.

Acknowledgements

Course example clip tennis1_60fps.mp4 (not published). Detection uses Ultralytics YOLO (AGPL-3.0).

References

Built 2026-11-30 with Python packages numpy 2.5.5, pandas 3.1.1, matplotlib 3.11.2, ultralytics 8.4.219, cv2 5.1.0. In the browser the same code runs on Pyodide, so the last digit can differ.