Files
spark 706aa8a5df
ci / rust (push) Successful in 7m51s
ci / typescript (push) Successful in 11m8s
Handball officials' cards (Oficial A-F) are learnt and read like shirt numbers
Team officials wear an accreditation card with a letter; B is usually the head coach. A person whose number
is a letter A-F (or "Oficial B") gets samples labelled with it, the review accepts the letter, and the model has a
letter head next to the digits (labels 0-99, A-F, illegible). Synthetic data paints cards on lanyards too.

Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
2026-10-13 21:44:25 +01:00

5.6 KiB
Raw Permalink Blame History

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:

  1. Collect. Every face in a group confirmed as a squad player who has a number becomes two samples, both labelled with the squad's number: the chest (where handball kits carry the front number) and the shorts (where football kits carry it; Liga Portugal shirts have none on the chest). Handball team officials wear an accreditation card with a letter (Oficial A to F; B is usually the head coach). Give an official the letter as their number ("B") and their cards are learnt and read like shirt numbers.

  2. 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.

  3. Train on the Mac (below).

  4. 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.

  5. 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.py paints 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_DIR stays 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 *.json here 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.