Skip to main content

FoodLens Advisor

フードレンズDone
FoodLens Advisor screenshot
Images stay on device; ~50–150 tokens per analysis vs. 5,000–10,000 for an image

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.

The Chinese version of this page has a live demo with real data. Open it (in Chinese) →

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

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

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

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

    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

food detected

nothing detected

POST text labels only
no image upload

User uploads a photo

In the browser
ONNX Runtime Web (YOLOv8)

Text labels

TensorFlow.js
COCO-SSD fallback

FastAPI back end

Gemini 2.5 Flash Lite

Calories / nutrients / diet advice

food detected

nothing detected

POST text labels only
no image upload

User uploads a photo

In the browser
ONNX Runtime Web (YOLOv8)

Text labels

TensorFlow.js
COCO-SSD fallback

FastAPI back end

Gemini 2.5 Flash Lite

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.

Type to search projects and posts too