Skip to content

Add admin-only lotto ticket counter: photo labeling site + Mac training pipeline - #38

Merged
zzuse merged 8 commits into
mainfrom
claude/lotto-number-recognition-training-q8yi3x
Nov 6, 2026
Merged

zzuse merged 8 commits into
mainfrom
claude/lotto-number-recognition-training-q8yi3x

Conversation

@zzuse

@zzuse zzuse commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Summary

Collect and label photos of the scratch-ticket display on the website, then train a model on the Mac that counts tickets per value ($1, $2, $3, $4, $5, $7, $10, $20, $30, $50, $100) from a photo. Each ticket shows its price in its top-left corner, so each visible corner is one ticket.

Website (/lotto, admin-only, linked from an "Admin tools" card on MGMT)

  • Upload photos any time. They wait in To label with no deadline, and Label next works through the backlog oldest first. Save keeps drafts.
  • Labeling page:
    • rotate the photo upright
    • pick a value (button, or type it: 1 0 = $10)
    • tap each price corner to stamp a box
    • live tally per value
    • up to 400 boxes per photo (tested with 300: loads in under 0.3 s, also at phone width)
  • Originals are stored untouched. Rotation and boxes are saved alongside them, so a labeling mistake never destroys data.
  • Train/val/test split (70/15/15) is assigned per calendar day, so near-identical shots of one scene never land in both train and test.
  • Dashboard:
    • milestones (10 / 25 / 80 labeled photos), with how much of each the backlog already covers
    • boxed tickets per value, with rare values highlighted
    • after predictions are uploaded, how often the model's count was exactly right
  • /lotto/export.zip gives the originals plus manifest.jsonl. /lotto/predictions imports the model output: waiting photos get pre-filled, and human labels are never overwritten (model output is stored in a separate model_json column).
  • The server does no image processing: no GPU and no new server dependencies.

Mac scripts (scripts/lotto/, PyTorch on Apple GPU)

  • prepare_dataset.py: export zip → YOLO dataset + value crops
  • baseline_ocr.py --count: scores Apple Vision on whole photos before any training
  • train_detector.py: fine-tunes a one-class YOLO "price corner" detector
  • train_crnn.py: CRNN + CTC reader for the value in each corner (learns digits, so rare values borrow from common ones)
  • predict.py: detect → read → tally, writes predictions.json

Full plan, photo tips (one photo per tray), labeling rules and timeline: docs/lotto-training.md.

Deploy

docker-compose up -d --build app
docker compose exec app python scripts/migrate.py   # creates lotto_photos (+ model_json column)

Testing

  • pytest -q passes. Total coverage is 86% (was 84%), src/safe_family/lotto/lotto.py is at 99%, and helpers.py is unchanged at 95%. ruff check . is clean.
  • New tests/test_lotto.py covers upload/dedupe, labeling and drafts, export, predictions import, progress math, and access control (a normal user is redirected from every /lotto route, and the nav has no Lotto link).
  • Browser-checked with Playwright on desktop and phone width:
    • stamping and switching values (including a bug found and fixed: switching value no longer relabels the box you just stamped)
    • rotation, saving, the dashboard
    • a 250–300 box stress test on a real display photo
  • End-to-end on 46 synthetic display photos (~1,550 tickets, CPU training):
    • detector: exact corner count on 6/6 test photos
    • reader: 100% (213/213) on test corners
    • every value counted exactly right on 40/40 labeled photos
    • a first run read every $30 as $20 (val had no $30, so an early checkpoint was kept). Fixed by keeping later checkpoints on ties, and train_crnn.py now warns when val/test lack a value.
  • Not tested: the MGMT page live, since it needs Postgres. Its template was rendered directly and shows the card. Real-photo accuracy is unknown until real photos are labeled; the synthetic photos are cleaner than real glare and holographic tickets.

Generated by Claude Code

Upload photos (phone or desktop), then label each one in the browser:
rotate it upright, draw a box around every ticket number, type the number.
Originals are stored untouched; rotation and boxes are saved alongside, so
labels can always be fixed. New uploads reuse the last photo's rotation and
box positions, and splits (70/15/15) are assigned per calendar day so shots
of one scene never straddle train and test.

The dashboard shows progress toward three milestones (baseline OCR check,
first model, solid model) with a days-left estimate at 2 photos/day.

The server does no image processing and needs no GPU: /lotto/export.zip
hands originals + manifest.jsonl to the Mac, where scripts/lotto/ crops,
scores off-the-shelf OCR, trains a small CRNN on the Apple GPU (MPS), and
writes predictions.json, which /lotto/predictions imports to pre-fill
boxes. Full plan in docs/lotto-training.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012k19dCKBK4J2UdDMJd5gLv
A verification run on 258 synthetic training numbers reached 83% on test,
after the usual CTC plateau for the first ~20 epochs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012k19dCKBK4J2UdDMJd5gLv
The goal is a count of tickets per price ($5, $10, $20, ...) from one photo
of the display, not reading ticket numbers. Every ticket shows its price in
its top-left corner, so each visible corner is one ticket.

Labeling: pick a value and tap each price corner; a live tally shows the
count per value. New photos start empty (ticket positions change daily) and
a photo can hold up to 400 boxes.

Dashboard: milestones count labeled photos and say how much of each goal
the "To label" backlog covers, so photos can be uploaded for weeks and
labeled later. Shows boxed tickets per value, flags rare values, and once
predictions are uploaded, how often the model's count was exactly right.

Mac side: prepare_dataset.py also writes a YOLO dataset; new
train_detector.py fine-tunes a one-class corner detector; the CRNN now reads
the value in each corner; predict.py detects, reads and tallies, and its
predictions pre-box waiting photos (model output is stored apart from human
labels, which are never overwritten). baseline_ocr.py --count scores Apple
Vision on whole photos first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012k19dCKBK4J2UdDMJd5gLv
A test run showed every $30 read as $20: the val split held no $30, scored
100% at epoch 14, and that early checkpoint was kept. Ties now keep the
later checkpoint, and train_crnn.py warns when val/test lack a value seen
in train. predict.py sorts tallies numerically; the guide no longer claims
rare values always come free (a digit unique to one value, like the 3 of
$30, needs its own examples).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012k19dCKBK4J2UdDMJd5gLv
Value buttons now match the Store Accounting tool ($1, $2, $3, $4, $5, $7,
$10, $20, $30, $50, $100), and values can be typed on the keyboard (1 then 0
for $10; a lone 1 waits briefly for $10/$100).

A whole-case photo holds ~250 tickets with price corners ~25 px tall: too
small to label comfortably or to detect after downscaling. The page and
guide now recommend one photo per tray (~60 tickets, corners ~2.5x bigger);
the estimate assumes 4 tray photos a day. Tested: 300 boxes on one photo
load in under 0.3 s and save fine, so whole-case photos still work.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012k19dCKBK4J2UdDMJd5gLv
The Lotto tab is gone from the top navigation; an "Admin tools" card on
the (already admin-only) MGMT page links to it, and MGMT stays highlighted
while on lotto pages. Hiding the link isn't access control, so every
/lotto route now uses admin_required: a normal user typing /lotto is sent
to the login page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012k19dCKBK4J2UdDMJd5gLv
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T20:44:17.748655Z 601c05d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 601c05d228

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/safe_family/lotto/lotto.py Resolved
HEIC uploads passed the image/* check, but the Mac scripts open photos with
plain Pillow, so prepare_dataset.py would abort on the first HEIC photo.

The upload page now converts HEIC/HEIF to full-quality JPEG in the browser
(capped at 16 MP for the iOS canvas limit) and drops, with a message, any
HEIC the browser can't decode. The server no longer trusts the declared
type: it sniffs the bytes, stores only JPEG/PNG/WebP with the sniffed type,
and refuses HEIC with instructions (Safari, or iPhone "Most Compatible").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012k19dCKBK4J2UdDMJd5gLv
CI pins ruff 0.15.20, where PLR0917 isn't enabled, so the
"noqa: PLR0913, PLR0917" comments failed with RUF100 (unused noqa).
Newer ruff does enable PLR0917. Making the trailing parameters of
write_yolo_image() and count_check() keyword-only satisfies PLR0917 on
both versions, so the noqa only needs PLR0913.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012k19dCKBK4J2UdDMJd5gLv
@zzuse
zzuse merged commit d7d017a into main Nov 6, 2026
2 checks passed
@zzuse
zzuse deleted the claude/lotto-number-recognition-training-q8yi3x branch November 6, 2026 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants