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

65
shared/barcode.js Normal file
View file

@ -0,0 +1,65 @@
// Barcodes are stored as 14-digit GTINs so UPC-A, EAN-13 and EAN-8 forms of
// the same product always match.
export function checkDigitValid(digits) {
let sum = 0;
for (let i = 0; i < digits.length - 1; i++)
sum += Number(digits[digits.length - 2 - i]) * (i % 2 === 0 ? 3 : 1);
return (10 - (sum % 10)) % 10 === Number(digits[digits.length - 1]);
}
/** Expands an 8-digit UPC-E code (number system, 6 digits, check) to UPC-A. */
export function expandUpcE(code) {
if (!/^[01]\d{7}$/.test(code)) return null;
const [s, d1, d2, d3, d4, d5, d6, check] = code;
const body =
d6 <= "2"
? `${d1}${d2}${d6}0000${d3}${d4}${d5}`
: d6 === "3"
? `${d1}${d2}${d3}00000${d4}${d5}`
: d6 === "4"
? `${d1}${d2}${d3}${d4}00000${d5}`
: `${d1}${d2}${d3}${d4}${d5}0000${d6}`;
return s + body + check;
}
/**
* Normalizes a scanned or typed barcode to GTIN-14, or returns null when it
* isn't a valid retail product code.
* @param {string} raw
* @param {string} [format] BarcodeDetector format, e.g. "upc_e"
*/
export function normalizeBarcode(raw, format) {
let digits = String(raw).replace(/\D/g, "");
if (format === "upc_e" || (digits.length === 6 && format === undefined)) {
if (digits.length === 6) {
// Six printed digits omit the number system (0) and check digit.
for (let check = 0; check < 10; check++) {
const upcA = expandUpcE("0" + digits + check);
if (upcA && checkDigitValid(upcA)) {
digits = upcA;
break;
}
}
} else digits = expandUpcE(digits) || "";
}
if (![8, 12, 13, 14].includes(digits.length) || !checkDigitValid(digits))
return null;
return digits.padStart(14, "0");
}
/** Pads any 8–14 digit database code to GTIN-14 without checksum validation. */
export function databaseGtin(code) {
const digits = String(code).trim();
if (!/^\d{8,14}$/.test(digits)) return null;
const gtin = digits.padStart(14, "0");
return /^0+$/.test(gtin) ? null : gtin;
}
/** The shortest standard form: EAN-8, UPC-A or EAN-13/GTIN-14. */
export function displayBarcode(gtin) {
if (gtin.startsWith("000000")) return gtin.slice(6);
if (gtin.startsWith("00")) return gtin.slice(2);
if (gtin.startsWith("0")) return gtin.slice(1);
return gtin;
}