FoodLens Advisor is my capstone project: it recognizes food in photos inside the browser and only sends the food names to a cloud model. But its project page had only text and screenshots, with no way to try it yourself. This post is about putting a live demo into the project page, and the most useful part is this: the model I originally wanted to use turned out, when measured, to be almost unusable.
Where the original model was
The project used a YOLOv8 with 46 Central Asian dish classes (plov, manti-style dumplings, lagman noodles, kebabs and the like). Its 43 MB file was never put into git, and I could not find it on my desktop either.
Later I found a public version on Hugging Face: UzDataLab/milliy-taomlar-detector, 46 classes, CC BY 4.0 (redistributable), 22 MB of weights. Converted to ONNX it is about 43 MB, and the output shape is [1, 50, 8400], the same as the project documentation says, so it should be the same model.
Measured: 4 photos, not one box
The model card is very clear about it: this is a test version, trained on only 212 images, with an mAP50 of 29.7%. I ran 4 Central Asian food photos from Wikimedia Commons (two of plov, one of lagman noodles, one of kebab):
| Threshold | Result |
|---|---|
| 0.25 (the project's original setting) | no boxes at all on all 4 photos |
| 0.1 | only one photo got two boxes, with confidence around 0.12; another plov photo was labeled unknown |
Put on a project page, a model like this would show visitors nothing when they press the button, and look broken. So I did not use it.
Swap in a general model, and say so on the page
I switched to YOLOX-S (Megvii, Apache-2.0, COCO's 80 classes), and show only 10 food classes of those: banana, apple, orange, broccoli, carrot, sandwich/burger, hot dog, pizza, donut, cake.
Why not the most common Ultralytics YOLOv8n: its weights are AGPL-3.0, and this site's source code is public, and I did not want to deal with license questions for a demo. YOLOX is Apache-2.0 and fits right in.
I first verified 6 demo photos in Python: pizza, donuts, fruit, vegetables and burgers were all recognized, and the cake photo only recognized the dining table, so I removed it. This demo only recognizes these 10 kinds; Taiwanese snacks and full-table meals mostly go unrecognized, and the page says so.
Implementation
- Inference: ONNX Runtime Web (WASM), running in the browser; the photo never leaves this tab.
- Preprocessing: scale proportionally to 640×640, pad the bottom and right with gray 114. YOLOX takes BGR, 0–255, not divided by 255, which is different from YOLOv8, and the first time it is very easy to reflexively write
/255. - Decoding: YOLOX's output is
[1, 8400, 85], coordinates have to be converted using the grid plus strides (8, 16, 32), width and height areexp(x) × stride, and the score is objectness times class score. Then NMS within the same class. - A 36 MB model: it is downloaded only after the visitor presses "try it", with a progress percentage, and afterwards comes from the browser cache.
- No npm dependency: I put ONNX Runtime Web's runtime files (
ort.wasm.min.mjsand two WASM-related files, about 15 MB in total) directly inpublic/vendor/ort/and load them with dynamicimport(). The reason is practical: the rule for this site is that dependencies are only installed during the CI deploy, and I did not want to touch the lockfile for a demo. The cost is about 50 MB more of static files in the repo. - Single thread: multi-threaded WASM needs cross-origin isolation, which this site does not enable, so
numThreads = 1.
CSP needs 'wasm-unsafe-eval'
The site has a strict Content-Security-Policy. For the browser to compile WebAssembly it needs 'wasm-unsafe-eval', added to script-src. It only allows compiling WASM and does not open up eval(), which is different from 'unsafe-eval'. This site had no unsafe-eval before (only dev mode does), so this one is the smallest new relaxation.
What actually happened in the browser
On desktop Chrome, inference on one image takes about 0.8 to 1.2 seconds (not counting the model download):
| Demo image | Result |
|---|---|
| Pizza | pizza ×1 |
| Donuts | donut ×14 |
| Fruit | apple ×6, orange ×1 |
| Vegetables | carrot ×24, broccoli ×6 |
| Burger | sandwich/burger ×1 |
Consistent with the Python results. I only looked at it in desktop Chrome and a 390px-wide emulated phone, and I have not measured the speed on a real phone; load time and memory on a low-end phone I do not know.
What this post taught me
- Measure before putting it on the page. For a test version whose model card says mAP50 29.7%, I first wanted to use it directly. Running 4 photos takes a few minutes and avoided shipping a broken demo.
- Look at the license when choosing the model. AGPL weights in a public repo make things complicated, and an Apache-2.0 model with a similar effect was right next to it.
- Don't present a replacement as the original. The page says this is not the model from the project, why it was swapped, and which 10 kinds it recognizes.
Sources
- Models: YOLOX (Apache-2.0), ONNX Runtime Web (MIT)
- The project's original model: UzDataLab Milliy Taomlar Detector (CC BY 4.0)
- Demo photos are from Wikimedia Commons, with license and author given in the "demo photo credits" on the project page