Add barcode and nutrition label scanning
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m2s
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:
parent
8f6981c8a2
commit
80fc48b9f1
39 changed files with 4434 additions and 33 deletions
65
shared/barcode.js
Normal file
65
shared/barcode.js
Normal 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;
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import { z } from "zod";
|
||||
import { nutrientKeyPattern } from "./nutrients.js";
|
||||
|
||||
export const metricKeys = [
|
||||
"calories",
|
||||
|
|
@ -27,6 +28,15 @@ const food = z.object({
|
|||
name,
|
||||
serving: z.string().trim().min(1).max(100),
|
||||
nutrients: nutrientsSchema,
|
||||
barcode: z
|
||||
.string()
|
||||
.regex(/^\d{14}$/)
|
||||
.optional(),
|
||||
// Further nutrients per serving, keyed as in shared/nutrients.js.
|
||||
extra: z
|
||||
.record(z.string().regex(nutrientKeyPattern), amount)
|
||||
.refine((value) => Object.keys(value).length <= 200, "Too many nutrients")
|
||||
.optional(),
|
||||
});
|
||||
const entry = food
|
||||
.extend({
|
||||
|
|
|
|||
115
shared/nutrients.js
Normal file
115
shared/nutrients.js
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
// Nutrients beyond the seven tracked metrics. Keys follow Open Food Facts
|
||||
// naming, so both product databases and label scans share one vocabulary.
|
||||
// Amounts are stored in the listed unit; keys not listed here are stored in
|
||||
// grams.
|
||||
export const nutrientCatalog = [
|
||||
{ key: "saturated-fat", label: "Saturated fat", unit: "g" },
|
||||
{ key: "trans-fat", label: "Trans fat", unit: "g" },
|
||||
{ key: "monounsaturated-fat", label: "Monounsaturated fat", unit: "g" },
|
||||
{ key: "polyunsaturated-fat", label: "Polyunsaturated fat", unit: "g" },
|
||||
{ key: "omega-3-fat", label: "Omega-3 fat", unit: "g" },
|
||||
{ key: "omega-6-fat", label: "Omega-6 fat", unit: "g" },
|
||||
{ key: "omega-9-fat", label: "Omega-9 fat", unit: "g" },
|
||||
{ key: "cholesterol", label: "Cholesterol", unit: "mg" },
|
||||
{ key: "added-sugars", label: "Added sugars", unit: "g" },
|
||||
{ key: "polyols", label: "Sugar alcohols", unit: "g" },
|
||||
{ key: "soluble-fiber", label: "Soluble fiber", unit: "g" },
|
||||
{ key: "insoluble-fiber", label: "Insoluble fiber", unit: "g" },
|
||||
{ key: "starch", label: "Starch", unit: "g" },
|
||||
{ key: "vitamin-a", label: "Vitamin A", unit: "µg" },
|
||||
{ key: "vitamin-a-iu", label: "Vitamin A (IU)", unit: "IU" },
|
||||
{ key: "vitamin-c", label: "Vitamin C", unit: "mg" },
|
||||
{ key: "vitamin-d", label: "Vitamin D", unit: "µg" },
|
||||
{ key: "vitamin-e", label: "Vitamin E", unit: "mg" },
|
||||
{ key: "vitamin-k", label: "Vitamin K", unit: "µg" },
|
||||
{ key: "vitamin-b1", label: "Thiamin", unit: "mg" },
|
||||
{ key: "vitamin-b2", label: "Riboflavin", unit: "mg" },
|
||||
{ key: "vitamin-pp", label: "Niacin", unit: "mg" },
|
||||
{ key: "vitamin-b6", label: "Vitamin B6", unit: "mg" },
|
||||
{ key: "vitamin-b9", label: "Folate", unit: "µg" },
|
||||
{ key: "folates", label: "Folate, total", unit: "µg" },
|
||||
{ key: "folic-acid", label: "Folic acid", unit: "µg" },
|
||||
{ key: "vitamin-b12", label: "Vitamin B12", unit: "µg" },
|
||||
{ key: "biotin", label: "Biotin", unit: "µg" },
|
||||
{ key: "pantothenic-acid", label: "Pantothenic acid", unit: "mg" },
|
||||
{ key: "choline", label: "Choline", unit: "mg" },
|
||||
{ key: "calcium", label: "Calcium", unit: "mg" },
|
||||
{ key: "iron", label: "Iron", unit: "mg" },
|
||||
{ key: "potassium", label: "Potassium", unit: "mg" },
|
||||
{ key: "magnesium", label: "Magnesium", unit: "mg" },
|
||||
{ key: "phosphorus", label: "Phosphorus", unit: "mg" },
|
||||
{ key: "zinc", label: "Zinc", unit: "mg" },
|
||||
{ key: "copper", label: "Copper", unit: "mg" },
|
||||
{ key: "manganese", label: "Manganese", unit: "mg" },
|
||||
{ key: "selenium", label: "Selenium", unit: "µg" },
|
||||
{ key: "iodine", label: "Iodine", unit: "µg" },
|
||||
{ key: "chromium", label: "Chromium", unit: "µg" },
|
||||
{ key: "molybdenum", label: "Molybdenum", unit: "µg" },
|
||||
{ key: "chloride", label: "Chloride", unit: "mg" },
|
||||
{ key: "fluoride", label: "Fluoride", unit: "mg" },
|
||||
{ key: "caffeine", label: "Caffeine", unit: "mg" },
|
||||
{ key: "taurine", label: "Taurine", unit: "mg" },
|
||||
{ key: "alcohol", label: "Alcohol", unit: "g" },
|
||||
];
|
||||
|
||||
// Where each tracked metric lives in a product's nutrient map.
|
||||
export const coreNutrientKeys = {
|
||||
calories: "energy-kcal",
|
||||
protein: "proteins",
|
||||
carbs: "carbohydrates",
|
||||
fat: "fat",
|
||||
fiber: "fiber",
|
||||
sugar: "sugars",
|
||||
sodium: "sodium",
|
||||
};
|
||||
// Stored units for the tracked metrics inside a product's nutrient map.
|
||||
export const coreUnits = {
|
||||
"energy-kcal": "kcal",
|
||||
proteins: "g",
|
||||
carbohydrates: "g",
|
||||
fat: "g",
|
||||
fiber: "g",
|
||||
sugars: "g",
|
||||
sodium: "mg",
|
||||
};
|
||||
|
||||
const byKey = new Map(nutrientCatalog.map((item) => [item.key, item]));
|
||||
const gramsPer = { g: 1, mg: 1e-3, µg: 1e-6 };
|
||||
|
||||
export const nutrientKeyPattern = /^[a-z0-9][a-z0-9-]{0,47}$/;
|
||||
export function nutrientUnit(key) {
|
||||
return coreUnits[key] || byKey.get(key)?.unit || "g";
|
||||
}
|
||||
/** Converts grams to the stored unit for `key`; null when it isn't a mass. */
|
||||
export function fromGrams(key, grams) {
|
||||
const factor = gramsPer[nutrientUnit(key)];
|
||||
return factor ? grams / factor : null;
|
||||
}
|
||||
export function convertMass(amount, from, to) {
|
||||
const a = gramsPer[from];
|
||||
const b = gramsPer[to];
|
||||
return a && b ? (amount * a) / b : null;
|
||||
}
|
||||
export function nutrientLabel(key) {
|
||||
if (byKey.has(key)) return byKey.get(key).label;
|
||||
const words = key.replace(/-/g, " ");
|
||||
return words.charAt(0).toUpperCase() + words.slice(1);
|
||||
}
|
||||
/** Catalog order first, then any other keys alphabetically. */
|
||||
export function sortNutrientKeys(keys) {
|
||||
const order = new Map(nutrientCatalog.map((item, i) => [item.key, i]));
|
||||
return [...keys].sort(
|
||||
(a, b) =>
|
||||
(order.get(a) ?? 1e3) - (order.get(b) ?? 1e3) || a.localeCompare(b),
|
||||
);
|
||||
}
|
||||
/** A readable amount: grams of uncatalogued nutrients scale to mg or µg. */
|
||||
export function displayAmount(key, amount) {
|
||||
let unit = nutrientUnit(key);
|
||||
let value = amount;
|
||||
if (!byKey.has(key) && !coreUnits[key] && value > 0 && value < 1) {
|
||||
unit = value < 0.001 ? "µg" : "mg";
|
||||
value = convertMass(amount, "g", unit);
|
||||
}
|
||||
return { value, unit };
|
||||
}
|
||||
74
shared/products.js
Normal file
74
shared/products.js
Normal 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(),
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue