How TrayCheck works
One sentence: a photo goes to a small food classifier, simple rules decide whether a person must check the answer, the person confirms or corrects it, and every decision is saved so the dashboard can show where the model goes wrong, how much checking it needs, and where food is wasted.
photo ──► food_model.py ──► review_rules.py ──► app.py (person checks) ──► records.py ──► app.py dashboard
4 dish scores flag or not, why confirm / correct SQLite file model view + operations view
The files you need to know
| File | What it does | Read it for |
|---|---|---|
food_model.py |
Loads the saved MobileNetV2 model and scores one photo (4 scores that add up to 1). | How a photo becomes numbers |
review_rules.py |
Decides if a photo needs a person, and why. Also builds the threshold table and the spot-check range (Wilson interval). | The core idea |
records.py |
Saves and loads records in records/traycheck.db (SQLite). Data checks for weights. |
What gets stored |
app.py |
The Streamlit app: Check a meal page and Dashboard page. | The demo |
vision_score.py |
One-off: scores all validation and test photos, chooses the threshold, writes vision/review_policy.json. |
Where the threshold comes from |
simulate_service.py |
Fills the database with a clearly labelled simulated month, so the dashboard has something to show. | What's real vs simulated |
tests/test_review_workflow.py |
Small tests for the rules and storage. | Proof the rules do what you say |
tests/test_app.py |
Drives the real app without a browser (Streamlit's test harness) on a temporary database: saving, flags, corrections, weights, dashboard. | Proof the demo paths work |
.streamlit/config.toml |
Hides Streamlit's developer toolbar and turns off usage statistics. | Demo polish |
Older files (vision_train.py, vision_prepare.py) built the model. analyze.py,
generate_sample.py and the HTML dashboard are an earlier, separate synthetic
experiment; you don't need them for the demo.
The three reasons a photo gets flagged
| Flag | Rule | Why it matters to a kitchen |
|---|---|---|
| Low score | Top score below the threshold (0.85). | The model itself is unsure. |
| Not on today's menu | The model's answer isn't one of today's dishes. | The kitchen already knows what it served. It's a free second opinion: either the model is wrong, or the wrong dish went out. The dashboard separates those two. |
| Spot check | A random 10% of photos that were not flagged. | You can't know how many unchecked records are wrong unless you check some of them. The spot-check correction rate estimates that. |
A person can correct any result, flagged or not, because a high score can
still be wrong. The original prediction is never overwritten: each record keeps
predicted_dish and final_dish, plus checked and corrected.
How the threshold was chosen
The model gives every photo a top score between 0 and 1. A higher threshold sends more photos to a person (more work) but lets fewer mistakes through. The rule:
Pick the lowest threshold where at most 5% of the unflagged validation photos are wrong.
| Validation photos (80) - used to choose | Test photos (160) - used only to report | |
|---|---|---|
| Threshold | 0.85 | 0.85 (not re-tuned) |
| Photos sent to a person | 34% | 24% (38 photos) |
| Mistakes caught by the flag | 10 of 12 | 13 of 18 |
| Mistakes that slip through | 2 | 5 |
| Error rate among unflagged photos | 3.8% | 4.1% |
Why choose on validation and not on test? If you tune the threshold on the test photos, the test result is no longer an honest check. (On test, 0.80 happens to work as well as 0.85, but you only know that because you looked; that's exactly the trap.)
The 5 mistakes that slip through are confident mistakes. The worst is a photo of ribs next to a big portion of rice, which the model calls bibimbap at 99%. No threshold catches these without sending almost everything to review, which is why the menu check and spot checks exist.
Validation accuracy (85%) is a bit lower than test (88.8%). The source dataset says its training-split labels are noisy and its test labels were checked by hand, and validation photos come from the training split.
What the simulated month shows
python simulate_service.py creates 811 records: 28 days, 2 fictional kitchens,
lunch and dinner, a menu of 3 of the 4 dishes per service, and about 4% of trays
carrying a dish that wasn't on the menu.
| Question | Answer from the simulated records |
|---|---|
| How much checking? | 261 of 811 photos (32%) checked by a person (low score 175, not on menu 61, spot check 55) |
| What did people correct? | 68 records, 26% of the checked ones; ribs ↔ bread pudding is the most common mix-up |
| Confident mistakes caught | 7 corrected records had a score of 0.85 or more: the menu check caught 5, spot checks 2. The score alone would have let all 7 through |
| How many unchecked records are wrong? | Spot checks: 2 of 55 corrected = 3.6%, so roughly 20 of the 550 unchecked records. With only 55 checks the plausible range is wide: 1.0% to 12.3% (95% Wilson interval) |
| Was that estimate right? | The simulation knows the truth: 2.9% (16 records), inside that range |
| Trends | Share of photos checked per day ranges 11%–46%; corrections per day 0–7 |
| What did the menu check find? | 61 alerts: 30 model mistakes, 31 dishes that really weren't on the menu (a kitchen issue, not a model issue) |
| Data problems | 21 plates with no returned weight, 5 with returned > served; left out of waste figures |
| Food waste | Leftover share 25.0% at dinner vs 15.6% at lunch; beet salad highest (30.5%). These numbers are invented by the simulation |
What is real and what is simulated
| Real | Simulated (and labelled as such) |
|---|---|
| The trained model, its scores and the test results | Kitchens, menus and which photo lands on which tray |
| The review rules and the threshold | The reviewer's decisions (assumed correct when they check) |
| Anything you save yourself in the app | All weights in simulated rows (quantity_source = 'simulated') |
Every simulated row has record_source = 'simulated', and the dashboard can hide them.
The simulated rows reuse the 160 test photos, so they show the workflow, not new evidence
about accuracy. Because the simulation knows each photo's real dish, it can check whether the
spot-check estimate is close to the true hidden error rate (see the expander on the dashboard).
Run it (Windows)
You need 64-bit Python 3.10, 3.11, 3.12 or 3.13 (TensorFlow 2.20 and Streamlit 1.65
both support these). Check with python --version.
python -m pip install -r requirements.txt -r requirements-vision.txt
python simulate_service.py
python -m streamlit run app.py
The app opens in your browser at http://localhost:8501. Scores, the threshold and test
results are already saved in vision/, so you don't need to download photos or retrain.
To rebuild everything from scratch: python vision_prepare.py, python vision_train.py,
python vision_score.py.
Run all tests (about a minute the first time, while TensorFlow loads):
python -m unittest discover -s tests -v
Before the interview (checklist)
git pullso you have the latest version.python -m unittest discover -s tests -v: everything should sayok.python simulate_service.py --fresh: deletes your practice records and reloads the simulated month, so record numbers start again from #812.- Start the app a few minutes early. The first photo takes up to a minute while TensorFlow loads the model; after that each photo takes about a second.
- Do one practice run of the 2-minute demo below, then run step 3 again.
- Have one food photo of your own ready to upload (any dish). An unfamiliar dish still gets one of the four labels, which is a good talking point: the model can only choose from what it was trained on.
If something goes wrong
| Problem | Fix |
|---|---|
pip can't find tensorflow-cpu==2.20.0 |
Your Python is 32-bit or not 3.10–3.13. Install 64-bit Python 3.12 from python.org. |
| TensorFlow fails to import on Windows | Install the Microsoft Visual C++ Redistributable (TensorFlow's Windows requirement), then retry. |
python is not recognised |
Use py instead of python in every command. |
| Port 8501 already in use | python -m streamlit run app.py --server.port 8502 |
| Dashboard says "No records yet" | Run python simulate_service.py. |
| You saved a practice record you don't want | python simulate_service.py --fresh resets everything. |
A 2-minute demo
- Check a meal. Leave the default menu (ribs, beet salad, bibimbap). Pick the beet salad example: no flags, quick save. "Most photos should be this fast." (1 time in 10 it gets picked for a random spot check instead; that's the spot-check rule working.)
- Pick the bread pudding example: the model is very sure, but bread pudding isn't on today's menu, so it's flagged. Try Save without choosing: it refuses. Choose the dish and save. "The model was right here; the kitchen sent out an off-menu dish. That's a kitchen finding, and the dashboard counts it separately from model mistakes."
- Pick the baby back ribs example 2134746: ribs next to rice, called bibimbap at 99%. Bibimbap is on the menu, so nothing flags it. Correct it to ribs and save. "This is the kind of mistake only a person, or a spot check, will find." (If it happens to be picked for a spot check, even better: "that's exactly what spot checks are for.")
- Dashboard → Model performance. Point at the confusion matrix, then the threshold table: "I chose 0.85 on validation photos. On the test photos, a quarter go to a person and 5 of 18 mistakes still slip through." Scroll to the right and wrong examples.
- Dashboard → Operations. Say first that this month is simulated (the yellow banner).
- Confident mistakes caught by people: "7 mistakes scored 0.85 or more. The score alone would have missed all of them; the menu check caught 5 and spot checks 2." Your ribs correction from step 3 appears here too.
- The spot-check line: "From 55 random checks we estimate about 3.6% of unchecked records are wrong, plausibly anywhere from 1% to 12%. The simulation's truth is 2.9%."
- Then not-on-menu alerts, data problems, trends by day and waste by meal period.
- Finally remove simulated from Record source to show only what you saved live.
Questions you should be ready for
Why not just send everything below 80% to review? 80% is a guess. I chose the threshold with a stated rule on validation photos, then checked it on test photos I hadn't used for anything else. The table shows the trade-off so the kitchen team can pick a different point if review time matters more.
What does the menu check add? It uses information the kitchen already has, for free. It catches some confident mistakes the score can't, and it also catches real service errors (wrong dish sent out). It is not a safety check: it doesn't verify allergens or diets.
Why spot-check photos the model is confident about? Otherwise you only ever see the mistakes you flagged. A small random sample of the unflagged photos estimates how many wrong records are sitting in the data unnoticed. With 55 checks the estimate is rough; more checks make it tighter.
What's stored for each photo? Model version, the model's answer and score, why it was flagged, whether a person checked it, the final dish, whether it was corrected, the service details and optional weights with their source. The original prediction is never overwritten.
What would you change for a real hospital? Train on local photos with whole trays (several foods per tray, so detection rather than one-label classification), agree the threshold and spot-check rate with the food-service team, weigh plates to a written protocol, and get privacy approval before collecting images.
Limits to say out loud
- Four dishes, public restaurant photos, one dish per photo. Not a hospital model.
- The operational month is simulated; only the model scores in it are real.
- The menu check assumes the menu is entered correctly.
- Grams come from a scale (or the simulation), never from the photo.
- No patient data, diets or allergens: those need approved clinical workflows.