Shirt numbers: our own model
Off-the-shelf OCR does not read shirt numbers. They are big, stretched and folded, and often half hidden. So we train a small classifier on our own photos. It is a ResNet18 that looks at the torso under a face and says 0–99 or "illegible". It is trained on a Mac with Apple Silicon (PyTorch on the GPU through MPS) and exported to ONNX. The worker then runs that ONNX file on the CPU, so the server needs no GPU and no Python.
The loop:
-
Collect. Every face in a group confirmed as a squad player who has a number becomes a sample: the torso crop, labelled with the squad's number.
-
Check. In the app, go to Caras → Números das camisolas. The keys are:
- ⏎ the number is right;
- digits then ⏎ to correct it;
- X it can't be read;
- S it isn't a shirt.
A few thousand checked crops is enough for a first model. The more numbers, teams, kits and angles, the better.
-
Train on the Mac (below).
-
Use. Copy the model folder to the server and set
JERSEY_MODEL_DIR. From then on the worker reads the number of every clear face. When a group of faces has no face match, it suggests the squad player of either team who wears the number most faces agree on. It is only a suggestion: someone still confirms it. -
Repeat. Confirmed groups produce more samples, so later models get better.
Training on the Mac
You need Python 3.11 or newer (brew install [email protected] if it's missing).
cd workers/ml/jersey
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
In the app, open Números das camisolas and click Descarregar conjunto de treino. You get
jersey-dataset-YYYY-MM-DD.json. It lists the checked samples with signed links to their crops; the crops
themselves stay on the server. Then run:
npm pack @fontsource/oswald @fontsource/anton @fontsource/bebas-neue @fontsource/teko @fontsource/saira-condensed
mkdir -p fonts && for f in fontsource-*.tgz; do tar xzf "$f" -C fonts; done
python synth.py --backgrounds cache/crops --fonts fonts --out synth --count 6000 # after a first train.py run filled the cache
python train.py \
--dataset ~/Downloads/jersey-dataset-2026-10-12.json \
--api https://app.photoreport.pt \
--extra synth/dataset.json \
--out models/jersey-v1
What happens:
- Download: the crops are downloaded once into
cache/crops. The first run also downloads the ImageNet starting weights (about 45 MB). - Synthetic data:
synth.pypaints numbers in sporty fonts on real crops and on plain shirts, warped like cloth. It is only an aid while there are few real examples. The check always uses real crops, from photos the model never trained on. - Reading digits: the model reads the digits (tens and units) rather than whole numbers, so every "20" also teaches a 2 and a 0.
- Time: on an M-series Mac a run takes minutes. On a 2-core server CPU, 6000 crops take about an hour.
The output folder has:
| File | What it is |
|---|---|
jersey.onnx |
The model the worker runs. It still gives one probability per number 0–99 plus "illegible". |
labels.json |
Classes, input size and normalisation. It must stay next to jersey.onnx. |
best.pt |
The PyTorch checkpoint, kept in case you want to continue from it. |
metrics.json |
On held-out real crops: accuracy (overall) and numbers_accuracy (legible numbers read right). |
How much data
The first test used 538 crops from four matches of October 2026, checked by hand: 165 with a legible number, spread over 34 numbers. Together with 5000 synthetic crops, it read 9 of 28 held-out numbers right (32%). At a score of 0.7 or more it answered only twice, and got one right. That is not enough to install. Two lessons:
- Sport matters. Handball shirts carry the number on the chest, so about one crop in three has one. Football shirts carry it on the back, and from the front only the shorts show a number, so fewer than one crop in ten has one.
- The volume has to come from the app. Each group confirmed in Caras, with the number in the squad, gives
ready-labelled crops that only need Enter. A few thousand is the target before the first model goes into
production. Until then,
JERSEY_MODEL_DIRstays empty and nothing changes for anyone.
Look at metrics.json before deploying. Numbers with few examples will be the weak ones; review more crops of
those and train again.
Deploying
rsync -a models/jersey-v1/ server:/srv/photoreport-data/models/jersey-v1/
# in /srv/photoreport/apps/api/.env
JERSEY_MODEL_DIR=/srv/photoreport-data/models/jersey-v1
pm2 restart photoreport-worker
Only photos analysed after the model is installed get numbers. Faces analysed earlier keep none.
Notes
- Privacy. The dataset and the crops are photos of people. Keep them off the repository and off shared
drives (
cache/,models/and*.jsonhere are git-ignored). Face analysis, and therefore these samples, only exist when biometrics are switched on with a recorded legal basis. - Front numbers only. Back-of-shirt numbers (players facing away, with no face) need a detector with boxes drawn by hand. That is a later step.
- We looked at public models trained on football and hockey jerseys. Most are research code under non-commercial licences, so we don't ship any of them.