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
142
tests/label.test.js
Normal file
142
tests/label.test.js
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { clean, parseLabel, rows } from "../server/ocr/label.js";
|
||||
|
||||
// Lays out label rows as OCR boxes: each row is [text, ...more boxes].
|
||||
function boxes(lines, { angle = 0 } = {}) {
|
||||
const result = [];
|
||||
lines.forEach((parts, row) => {
|
||||
let x = 0;
|
||||
for (const text of [].concat(parts)) {
|
||||
const width = text.length * 10;
|
||||
const cx = x + width / 2;
|
||||
const cy = row * 30 + 10 + cx * Math.tan(angle);
|
||||
result.push({
|
||||
text,
|
||||
angle,
|
||||
cx,
|
||||
cy,
|
||||
height: 20,
|
||||
x0: x,
|
||||
x1: x + width,
|
||||
y0: cy - 10,
|
||||
y1: cy + 10,
|
||||
});
|
||||
x += width + 40;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
test("a US Nutrition Facts panel is read in full", () => {
|
||||
const label = parseLabel(
|
||||
boxes([
|
||||
"Nutrition Facts",
|
||||
"About 31 servings per container",
|
||||
["Serving size", "1 cup (54g)"],
|
||||
"Amount per serving",
|
||||
["Calories", "230"],
|
||||
"% Daily Value*",
|
||||
["Total Fat 5g", "6%"],
|
||||
["Saturated Fat 0.5g", "3%"],
|
||||
"Trans Fat 0g",
|
||||
["Cholesterol Omg", "0%"],
|
||||
["Sodium 1,230mg", "53%"],
|
||||
["Total Carbohy ydrate 42g", "15%"],
|
||||
["Dietary Fiber <1g", "2%"],
|
||||
"Total Sugars 15g",
|
||||
["Includes 10g Added Sugars", "20%"],
|
||||
"Protein 4g",
|
||||
["Vitamin D 2mcg 10%", "Calcium 260mg 20%"],
|
||||
["Iron 8mg 45%", "Potassium 235mg 6%"],
|
||||
["Thiamin", "60%"],
|
||||
]),
|
||||
);
|
||||
assert.equal(label.serving, "1 cup (54 g)");
|
||||
assert.deepEqual(label.nutrients, {
|
||||
calories: 230,
|
||||
fat: 5,
|
||||
sodium: 1230,
|
||||
carbs: 42,
|
||||
fiber: 0.5,
|
||||
sugar: 15,
|
||||
protein: 4,
|
||||
});
|
||||
assert.equal(label.extra["saturated-fat"], 0.5);
|
||||
assert.equal(label.extra["trans-fat"], 0);
|
||||
assert.equal(label.extra.cholesterol, 0);
|
||||
assert.equal(label.extra["added-sugars"], 10);
|
||||
assert.equal(label.extra["vitamin-d"], 2);
|
||||
assert.equal(label.extra.calcium, 260);
|
||||
assert.equal(label.extra.iron, 8);
|
||||
assert.equal(label.extra.potassium, 235);
|
||||
assert.equal(label.extra["vitamin-b1"], 0.72); // 60% of 1.2 mg
|
||||
});
|
||||
|
||||
test("misread units are corrected by the %DV", () => {
|
||||
const label = parseLabel(
|
||||
boxes([
|
||||
["Total Fat 89", "10%"], // 8g with the g read as 9
|
||||
["Sodium 160", "7%"],
|
||||
["Calcium 20mg", "0%"], // under 2% legitimately rounds to 0
|
||||
]),
|
||||
);
|
||||
assert.equal(label.nutrients.fat, 8);
|
||||
assert.equal(label.nutrients.sodium, 160);
|
||||
assert.equal(label.extra.calcium, 20);
|
||||
});
|
||||
|
||||
test("a large calorie figure on its own row is found", () => {
|
||||
const label = parseLabel(
|
||||
boxes(["Amount per serving 130", "Calories", ["Total Fat 6g", "8%"]]),
|
||||
);
|
||||
assert.equal(label.nutrients.calories, 130);
|
||||
});
|
||||
|
||||
test("rows follow the text angle of a tilted photo", () => {
|
||||
const tilted = boxes(
|
||||
[
|
||||
["Sodium", "160mg", "7%"],
|
||||
["Total Carbohydrate", "37g", "13%"],
|
||||
],
|
||||
{ angle: 0.12 },
|
||||
);
|
||||
assert.deepEqual(rows(tilted), [
|
||||
"Sodium 160mg 7%",
|
||||
"Total Carbohydrate 37g 13%",
|
||||
]);
|
||||
});
|
||||
|
||||
test("an EU per-100 g table converts salt to sodium", () => {
|
||||
const label = parseLabel(
|
||||
boxes([
|
||||
"Nutrition per 100g",
|
||||
"Energy 1046kJ / 250kcal",
|
||||
"Fat 8,0g",
|
||||
"of which saturates 1,2g",
|
||||
"Carbohydrate 37g",
|
||||
"of which sugars 12g",
|
||||
"Fibre 4g",
|
||||
"Protein 3g",
|
||||
"Salt 0,5g",
|
||||
]),
|
||||
);
|
||||
assert.equal(label.serving, "100 g");
|
||||
assert.deepEqual(label.nutrients, {
|
||||
calories: 250,
|
||||
fat: 8,
|
||||
carbs: 37,
|
||||
sugar: 12,
|
||||
fiber: 4,
|
||||
protein: 3,
|
||||
sodium: 200,
|
||||
});
|
||||
assert.equal(label.extra["saturated-fat"], 1.2);
|
||||
});
|
||||
|
||||
test("OCR letter/digit confusions are undone", () => {
|
||||
assert.equal(clean("Trans Fat Og"), "Trans Fat 0g");
|
||||
assert.equal(clean("Iron 1O.8mg"), "Iron 10.8mg");
|
||||
assert.equal(clean("Vitamin D 2µg"), "Vitamin D 2mcg");
|
||||
assert.equal(clean("Sugars less than 1g"), "Sugars < 1g");
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue