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

74
shared/products.js Normal file
View file

@ -0,0 +1,74 @@
import { coreNutrientKeys } from "./nutrients.js";
// Label precision: 1 decimal from 10 up, 2 from 0.1, finer below.
const round = (value) => {
const places = value >= 10 ? 10 : value >= 0.1 ? 100 : 1e4;
return Math.round(value * places) / places;
};
/**
* A product database row describes nutrition per 100 g (or 100 ml) plus a
* serving size. This turns it into a per-serving food for the editor.
* @param {{gtin: string, name?: string|null, brand?: string|null,
* serving?: string|null, servingQty?: number|null, servingUnit?: string|null,
* nutrients: Record<string, number>, source?: string}} product
*/
export function productToFood(product) {
const qty =
product.servingQty > 0 && product.servingQty <= 5000
? product.servingQty
: null;
const unit = product.servingUnit === "ml" ? "ml" : "g";
const factor = qty ? qty / 100 : 1;
let serving = `100 ${unit}`;
if (qty) {
const text = (product.serving || "").trim();
// Prefer the label's own wording ("2/3 cup (55 g)") when it has any.
serving =
text && /[a-z]/i.test(text) && text.length <= 100
? text
: `${round(qty)} ${unit}`;
}
const nutrients = {};
const core = new Set(Object.values(coreNutrientKeys));
for (const [metric, key] of Object.entries(coreNutrientKeys))
nutrients[metric] = round((product.nutrients[key] || 0) * factor);
const extra = {};
for (const [key, value] of Object.entries(product.nutrients))
if (!core.has(key) && Number.isFinite(value) && value >= 0)
extra[key] = Math.min(round(value * factor), 100000);
for (const metric of Object.keys(nutrients))
nutrients[metric] = Math.min(nutrients[metric], 100000);
const brand = (product.brand || "").trim();
const title = (product.name || "").trim();
// "Cheerios" + "Cheerios Cereal" reads as "Cheerios Cereal", and a brand
// owner such as "Coca-Cola Services SA/NV" never replaces "Coca-Cola".
const overlaps =
title.toLowerCase().includes(brand.toLowerCase()) ||
brand.toLowerCase().includes(title.toLowerCase());
const name = !title
? brand
: !brand || overlaps
? title
: `${brand} ${title}`;
return {
name: name.slice(0, 160) || undefined,
brand: product.brand || undefined,
serving,
nutrients,
...(Object.keys(extra).length ? { extra } : {}),
barcode: product.gtin,
source: product.source,
};
}
/** USDA descriptions are often upper case; make them readable. */
export function titleCase(text) {
if (!text || /[a-z]/.test(text)) return text;
return text
.toLowerCase()
.replace(
/(^|[\s(/&-])([a-z])/g,
(_, gap, letter) => gap + letter.toUpperCase(),
);
}