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
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 };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue