# FoodLens Advisor

> Browser-side YOLOv8 (ONNX Runtime) detects food in-device; only text labels are sent to Gemini for dietary analysis. Thin client + cloud brain architecture.

- Result: Images stay on device; ~50–150 tokens per analysis vs. 5,000–10,000 for an image
- Stack: Python, FastAPI, YOLOv8, ONNX Runtime, Gemini API, TensorFlow.js
- Source: https://github.com/kbs0830/FoodLens-Advisor

FoodLens Advisor is a lightweight diet assistant built on "in-browser detection + cloud AI analysis" — a thin-client, cloud-brain hybrid: upload a photo of your food and get a calorie estimate, a macronutrient breakdown and personalised diet advice.

## Problem

Most "snap a photo, get the calories" services upload the whole photo to a cloud model. Photos leak details of people's homes, and a single image costs thousands of tokens, so cost and latency are both high. The goal of this final project was a system that gives nutrition analysis and diet advice without the photo ever leaving the device.

## Key decisions

1. **Run food detection in the browser**: ONNX Runtime Web (WASM) runs YOLOv8 directly in the browser, so the photo never has to be uploaded to any server.
2. **Send only text labels to the language model**: The back end passes the detected food names to Gemini 2.5 Flash Lite for nutrition analysis — about 50–150 tokens per request, versus 5,000–10,000 tokens for sending the image.
3. **Put a FastAPI back end (BFF) between the front end and the model**: The API key lives only in server environment variables, out of the browser's reach; requests and responses are typed with Pydantic v2, and malformed ones are rejected.
4. **A fallback at every layer**: If the in-browser ONNX model finds nothing, it switches to TensorFlow.js COCO-SSD; the back end also has YOLOv8 and a ResNet-50 (Food-101) classifier, so any layer can fail over to the next.

## Trade-offs

- The main model knows 46 classes of Central Asian dishes; common Taiwanese food falls back to COCO-SSD's generic classes, so precise recognition is limited
- The language model only sees food names — not portions or cooking methods — so calories are estimates based on the name
- Errors show the real cause instead of filling the layout with fake data; the cost is that users see an error message when an external service fails

## Results

- The photo stays in the user's browser the whole time
- The data sent to the language model is about 0.5–3% of sending the image (50–150 vs. 5,000–10,000 tokens)
- The front end needs no framework or bundler — plain HTML, CSS and JavaScript

## Architecture

```mermaid
flowchart TD
  A["User uploads a photo"] --> B["In the browser<br/>ONNX Runtime Web (YOLOv8)"]
  B -->|"food detected"| D["Text labels"]
  B -->|"nothing detected"| C["TensorFlow.js<br/>COCO-SSD fallback"]
  C --> D
  D -->|"POST text labels only<br/>no image upload"| E["FastAPI back end"]
  E --> F["Gemini 2.5 Flash Lite"]
  F --> G["Calories / nutrients / diet advice"]
```

## Pipeline

- In the browser: ONNX Runtime Web (WASM) runs a YOLOv8 ONNX model that detects Central Asian dishes (46 classes), with 640×640 letterbox input and a 0.25 confidence threshold
- Fallback: when ONNX detects nothing, it switches automatically to TensorFlow.js + COCO-SSD (80 generic classes, loaded from a CDN)
- Back-end analysis: the detected food labels (plain text, not images) are POSTed to a FastAPI back end, which calls Google Gemini 2.5 Flash Lite for in-depth analysis
- Back-end fallback: the FastAPI side also has YOLOv8 (server-side inference) and a ResNet-50 Food-101 classifier as extra layers

## Highlights

- Images stay in the browser; only text labels leave the device — privacy first
- Text labels instead of a base64 image: the labels themselves are about 10–30 tokens and each request about 50–150 tokens, versus 5,000–10,000 tokens for an image (figures from the project README)
- An ONNX → COCO-SSD → Gemini fallback chain keeps the system working if any one layer fails
- No front-end framework: plain HTML5 / CSS3 / vanilla JS, no Node.js or bundler required

## Stack

- In-browser inference: ONNX Runtime Web (WASM) + TensorFlow.js COCO-SSD
- Back end: FastAPI + Pydantic v2 (typed request/response models)
- AI analysis: Google Gemini 2.5 Flash Lite
- Back-end fallback models: Ultralytics YOLOv8, Hugging Face ResNet-50 Food-101

> This was also the final project for the Applied AI and Deep Learning course.

---

Author: PINGWEI LI (李秉威, Li Ping-Wei)
Original page: https://kbs0830.com/en/projects/foodlens-advisor
