Add barcode and nutrition label scanning
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m2s

Barcode scanning
- Scan barcode in the food sheet reads UPC-A/E and EAN-13/8 live from
  the camera with ZXing WebAssembly (bundled, precached, CSP allows
  wasm), with photo and typed-digit fallbacks
- Lookups check saved foods first (offline), then local copies of USDA
  FoodData Central Branded Foods and Open Food Facts, then the live
  Open Food Facts API; API answers and misses are cached
- Server imports refresh in a low-priority child process: USDA when a
  new archive is published, Open Food Facts weekly by streaming its CSV
  export; each import builds a new table and swaps it in atomically
- Every available nutrient is kept: foods and entries gain optional
  barcode (GTIN-14) and extra nutrients, shown under More nutrients and
  included in JSON exports
- Settings shows the size, date and any error for each product copy

Label scanning
- Scan label sends a downscaled photo to the server, read with
  PaddleOCR PP-OCRv5 (server detection, English recognition) on ONNX
  Runtime CPU, about 1.3 s median
- Parser handles US/Canadian, linear, Spanish and EU per-100 g panels,
  tilted photos, OCR misspellings and %DV cross-checks
- Models are pinned and checksum-verified at image build time

Container
- Runtime moves to Debian slim for ONNX Runtime's glibc build; other
  platforms' binaries are pruned
- Smoke tests now read a fixture label inside the container
- README and Unraid docs cover disk use, network access and refresh

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Elijah 2026-10-22 13:41:44 -07:00
parent 8f6981c8a2
commit 80fc48b9f1
39 changed files with 4434 additions and 33 deletions

View file

@ -1,6 +1,6 @@
# Calio
A single-user, self-hosted nutrition journal. PWA first, with a responsive desktop layout, light/dark/system themes, configurable accent colors, and no third-party analytics, fonts, or nutrition services. Dark mode uses neutral near-black and charcoal surfaces; choose a preset or custom accent under Settings → Look & feel; it applies immediately. Accent preferences sync between devices and remain available offline.
A single-user, self-hosted nutrition journal. PWA first, with a responsive desktop layout, light/dark/system themes, configurable accent colors, and no third-party analytics or fonts. Barcode lookups use local copies of USDA FoodData Central and Open Food Facts kept on your server, with Open Food Facts' public API only as a fallback; nutrition labels are read on your server, never by an outside service. Dark mode uses neutral near-black and charcoal surfaces; choose a preset or custom accent under Settings → Look & feel; it applies immediately. Accent preferences sync between devices and remain available offline.
## Included
@ -24,7 +24,33 @@ Phone controls use larger type, 44–56 px tap targets, and 51×31 px switches.
The iPhone Home Screen icon uses a dark background and high-contrast mark so automatic tinting has a dark base instead of a bright colored square. The versioned Apple touch-icon URL lets new installations request the revised asset; Android keeps its existing regular and maskable icons. iOS owns the final tinted/clear rendering, so a PWA cannot guarantee the native Icon Composer appearance. If an existing Home Screen shortcut retains its old cached icon, first sync all pending entries, then add Calio to the Home Screen again. The server's database is unchanged.
Barcode lookup and nutrition-label OCR are deferred. This release uses manual nutrition entry and saved foods. It does not include Apple Watch integration, HealthKit, fasting, reminders or water tracking; those screenshot features were not part of the requested core.
## Barcode and label scanning
The food sheet's **Quick entry** tab starts with **Scan barcode** and **Scan label**.
**Scan barcode** opens the rear camera and reads UPC-A, UPC-E, EAN-13 and EAN-8 codes continuously in the browser (ZXing compiled to WebAssembly, bundled with the app and cached for offline use; Safari has no built-in barcode reader). Line the code up inside the frame: it is read as soon as it is sharp, with no shutter. If the camera is unavailable or permission is off, **Take a photo instead** or type the digits. A scan is looked up in this order:
1. Your saved foods, on the device; this also works offline.
2. The server's copy of **USDA FoodData Central Branded Foods** (~435,000 US products from manufacturers' label submissions). Its values win when both copies have a product.
3. The server's copy of **Open Food Facts** (~3.4 million products worldwide, community-entered).
4. The live Open Food Facts API, for products newer than the copies. Answers, including "not found", are cached on the server.
A find fills the form with the product's name, serving and every nutrient the source has: the seven tracked ones plus extras such as saturated fat, cholesterol, added sugars, vitamins and minerals, under **More nutrients**. **Save this food to my library** is pre-checked so the next scan is instant. When nothing is found, scan the label instead; the barcode stays attached to the food.
**Scan label** opens the iPhone camera app (full resolution, focus and flash, no permission prompt), or a file picker on desktop. The photo is downscaled on the device and read on the server with PaddleOCR PP-OCRv5 (server detection, English recognition) on ONNX Runtime, CPU only, typically in 1–3 seconds. A parser then pulls out the serving size and each nutrient, undoes common OCR confusions (O/0, l/1, a unit read as a digit), checks amounts against their printed %DV, and handles US/Canadian panels, linear labels, tilted photos, Spanish labels and EU "per 100 g" tables (salt → sodium). Values it found replace the form's values; check them against the package before saving. Fill the frame with the panel and keep it flat and sharp: curved containers and glare are the usual causes of misses.
Extra nutrients are stored per serving with each food and entry, are scaled by servings, sync like everything else, and appear in JSON exports as `otherNutrients`.
### Product data refresh
The server checks for new data a minute after start-up and every six hours, in a low-priority child process so the app stays responsive:
- **USDA**: when the download page lists a newer Branded Foods archive (published about twice a year), it downloads the ~450 MB zip to `/data/tmp`, imports it in about a minute, and deletes it.
- **Open Food Facts**: weekly, it streams the ~1.5 GB daily CSV export straight into the database (about 5 minutes; nothing is written to disk first).
Each import builds a new table beside the old one and swaps it in at the end, so lookups keep working throughout and a failed download changes nothing. The data lives in `/data/products.sqlite` (~1.6 GB; up to about twice that while a refresh is being built). It is a cache: deleting it is safe, and it is rebuilt by the next check. It does not need backing up. **Settings → Barcode database** shows each copy's size, date and any download error. Until the first import finishes (roughly 10 minutes after the first start), scans fall back to the live API.
This release does not include Apple Watch integration, HealthKit, fasting, reminders or water tracking; those screenshot features were not part of the requested core.
## Deploy on Unraid
@ -50,7 +76,7 @@ Sessions expire after 30 days and persist in SQLite. Changing credentials does n
## Persistent data, updates and backups
SQLite lives at `/data/calio.sqlite`, mapped to `/mnt/user/appdata/calio` on Unraid. Keep this directory and mapping when replacing the image. The optional Compose setup uses a named `calio-data` volume instead. Schema migrations are versioned and transactional; an older app refuses to open a newer schema.
SQLite lives at `/data/calio.sqlite` (with the regenerable product cache in `/data/products.sqlite`), mapped to `/mnt/user/appdata/calio` on Unraid. Keep this directory and mapping when replacing the image. The optional Compose setup uses a named `calio-data` volume instead. Schema migrations are versioned and transactional; an older app refuses to open a newer schema.
Before updating, make a consistent backup while Calio is running:
@ -101,7 +127,7 @@ The repository configuration is ready, but a successful live publish still requi
## Develop and verify
Node 24+ is required for built-in SQLite. `npm ci` installs the lockfile. `npm test` covers authentication, validation, deterministic merges, tombstones, persistence, and client outbox handling.
Node 24+ is required for built-in SQLite. `npm ci` installs the lockfile. `npm test` covers authentication, validation, deterministic merges, tombstones, persistence, client outbox handling, barcode normalization, the product importers and lookup order, and the label parser. `node scripts/fetch-models.js` downloads the pinned, checksummed OCR models into `models/` for local runs (the Docker build does this itself).
For a production preview, run `npm run build`, configure the same environment variables above, then `npm start`. For live UI editing, run `npm run dev:server` on port 3000 and `npm run dev` on port 5173, with `APP_ORIGIN=http://localhost:5173` and `COOKIE_SECURE=false`. Vite proxies `/api`; the service worker is registered only in production builds.