Record a table-tennis match on an Android phone and get back the score, rally events, shot speeds and highlight clips. A Spring Boot backend runs a YOLO ball detector and a rules-based game-state tracker over the video, and the app plays the results back in sync with the footage.
Built by a team of five. I was the lead backend contributor: I wrote the Spring Boot backend and auth, integrated the YOLO model into the processing pipeline, optimized the OpenCV stages, and built the visualizer. I also worked on the Android app.
flowchart LR
Rec["CameraX recorder / picker"] -->|"Retrofit multipart + JWT"| API["Spring Boot API"]
API --> Store[("storage/videos")]
API --> Async["MatchProcessingService (@Async)"]
Async -->|"ProcessBuilder, timeout"| Py["model_service.py (YOLO per frame)"]
Py --> GS["GameStateTracker (serve/rally rules)"]
GS -->|"inference JSON"| Async
Async -->|"stats + highlights"| DB[("MongoDB")]
DB --> API
API -->|"events, highlights, video stream"| Player["ExoPlayer + Canvas overlay"]
An upload returns 202 Accepted with a match ID right away. Processing runs on its own thread pool, and the match goes from PROCESSING to COMPLETE or FAILED. The Python model runs as a separate process and its only contract with the backend is a JSON file, so the CV code can change without touching the Java service.
- Getting events out of ball boxes. The detector only says where the ball is in each frame.
model_service.pykeeps the best box per frame in a 40-frame history and calls it a bounce when vertical velocity flips from down to up, with thresholds that scale with frame rate and an 80 ms debounce. A net hit is a sudden drop or reversal in horizontal velocity within 30 px of the net line. - Scoring by the rules.
GameStateTrackeris a state machine (WAITING_FOR_SERVE→SERVE_IN_PROGRESS→RALLY_IN_PROGRESS→POINT_SCORED). A serve has to land on the server's side then the receiver's, a rally ball has to alternate sides, and a wrong-side or off-table bounce gives away the point. The server switches every two points, and a rally closes once the ball has been missing for 30 frames. - Speeds in km/h.
TableDetectorfinds the table with HSV segmentation (blue or green), cleans the mask and takes the largest contour, then gets pixels-per-metre from the table's known dimensions. If detection fails,table_calibration.pylets you click the four corners and passes them in throughTABLE_BOUNDS.
- Failing cleanly between Java and Python.
ModelEventDetectionServicebuilds the command from a template inapplication.properties, checks the script exists, runs it with a timeout (5 minutes by default) and callsdestroyForcibly()if it hangs. A crash, non-zero exit or missing JSON marks that one matchFAILEDand the API keeps responding. AfterwardsMatchProcessingServicemerges detections within 180 ms into one shot, aligns events to shots within 250 ms and ranks events to pick the highlights. - Stats that follow the video.
VideoPlayerWithOverlaysamples the ExoPlayer position every 50 ms, andMatchDetailViewModelfinds the nearby shot and event, the score at that point and the cumulative stats so far. A ComposeCanvasscales the stored boxes onto the video, so scrubbing moves the overlay, scoreboard and stats together. Highlights loop just the event window with Media3ClippingConfiguration.
Credits: the ball-detector weights come from the ping-pong-deep-learning project and were trained on the OpenTTGames dataset. The event-spotting design follows TTNet (Voeikov et al., CVPR 2020 Workshops). best.pt is committed at backend/backend/cv/best.pt; docs/model-and-api.md covers retraining it, the inference JSON contract and the REST endpoints.
You need JDK 21, MongoDB on localhost:27017, Python 3 (the backend calls python on your PATH) and Android Studio with SDK 36.
# CV dependencies (and optionally run the analyzer on its own)
pip install -r backend/backend/cv/requirements.txt
python backend/backend/cv/model_service.py match.mp4 out.json 0.25
# Backend: the JWT secret is required. Run the jar from the REPO ROOT.
export ALLANAI_SECURITY_JWT_SECRET="$(openssl rand -base64 48)"
(cd backend/backend && ./mvnw -DskipTests package)
java -jar backend/backend/target/backend-0.0.1-SNAPSHOT.jar # serves on :8080
# Android: defaults to http://10.0.2.2:8080/ (the emulator). On a real phone, use your LAN IP:
echo "allanai.baseUrl=http://<your-lan-ip>:8080/" >> Frontend/local.properties
cd Frontend && ./gradlew installDebugALLANAI_SECURITY_JWT_SECRETmust be at least 32 bytes; keep it out of git.SPRING_DATA_MONGODB_URIis optional.- Python reads
YOLO_MODEL_WEIGHTS,YOLO_FALLBACK_MODEL(used ifbest.ptis missing) andTABLE_BOUNDS. - If your interpreter is
python3, changeallanai.model.command[0]inapplication.properties.
Known gaps: matches aren't scoped per user yet (any signed-in user can list every match), table detection assumes a side-on camera and a blue or green table, and MatchPipelineIntegrationTest predates the JWT layer (it also needs the secret set).
Stack: Kotlin, Jetpack Compose, Hilt, Retrofit, Media3 ExoPlayer, CameraX, Java 21, Spring Boot 3.5, jjwt, MongoDB, Python, Ultralytics YOLO, PyTorch, OpenCV.