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
|
|
@ -6,7 +6,11 @@ import { resolve, join } from "node:path";
|
|||
import { existsSync } from "node:fs";
|
||||
import { z } from "zod";
|
||||
import { recordSchema } from "../shared/domain.js";
|
||||
import { normalizeBarcode } from "../shared/barcode.js";
|
||||
import { productToFood } from "../shared/products.js";
|
||||
import { checkPassword, tokenHash } from "./auth.js";
|
||||
import { fetchOffProduct } from "./products/off.js";
|
||||
import { parseLabel } from "./ocr/label.js";
|
||||
|
||||
export function createApp({
|
||||
store,
|
||||
|
|
@ -15,6 +19,9 @@ export function createApp({
|
|||
origin,
|
||||
secureCookies = true,
|
||||
dist = resolve("dist"),
|
||||
products,
|
||||
fetchProduct = fetchOffProduct,
|
||||
ocr,
|
||||
}) {
|
||||
const app = express();
|
||||
app.disable("x-powered-by");
|
||||
|
|
@ -22,9 +29,10 @@ export function createApp({
|
|||
helmet({
|
||||
contentSecurityPolicy: {
|
||||
directives: {
|
||||
"script-src": ["'self'"],
|
||||
// The barcode reader is WebAssembly, served from this origin.
|
||||
"script-src": ["'self'", "'wasm-unsafe-eval'"],
|
||||
"style-src": ["'self'", "'unsafe-inline'"],
|
||||
"img-src": ["'self'", "data:"],
|
||||
"img-src": ["'self'", "data:", "blob:"],
|
||||
"connect-src": ["'self'"],
|
||||
"object-src": ["'none'"],
|
||||
"upgrade-insecure-requests": secureCookies ? [] : null,
|
||||
|
|
@ -146,6 +154,77 @@ export function createApp({
|
|||
});
|
||||
}
|
||||
});
|
||||
app.get("/api/products/:code", requireAuth, async (req, res) => {
|
||||
const gtin = normalizeBarcode(req.params.code);
|
||||
if (!gtin)
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "That isn’t a valid product barcode." });
|
||||
if (!products)
|
||||
return res
|
||||
.status(503)
|
||||
.json({ error: "The product database isn’t available." });
|
||||
let product = products.lookup(gtin);
|
||||
if (product === undefined) {
|
||||
try {
|
||||
product = await fetchProduct(gtin);
|
||||
} catch {
|
||||
return res.status(502).json({
|
||||
error:
|
||||
"This product isn’t in the local database yet, and Open Food Facts couldn’t be reached.",
|
||||
gtin,
|
||||
});
|
||||
}
|
||||
products.remember(gtin, product);
|
||||
}
|
||||
if (!product)
|
||||
return res
|
||||
.status(404)
|
||||
.json({ error: "No nutrition information for this barcode.", gtin });
|
||||
res.json({ food: productToFood(product) });
|
||||
});
|
||||
app.get("/api/products", requireAuth, (_, res) =>
|
||||
res.json({ sources: products?.status() || [] }),
|
||||
);
|
||||
// One label at a time: recognition already uses every core.
|
||||
let labelQueue = Promise.resolve();
|
||||
app.post(
|
||||
"/api/label",
|
||||
requireAuth,
|
||||
express.raw({
|
||||
type: ["image/jpeg", "image/png", "image/webp"],
|
||||
limit: "16mb",
|
||||
}),
|
||||
async (req, res) => {
|
||||
if (!Buffer.isBuffer(req.body) || !req.body.length)
|
||||
return res
|
||||
.status(415)
|
||||
.json({ error: "Send a JPEG, PNG or WebP photo of the label." });
|
||||
if (!ocr)
|
||||
return res
|
||||
.status(503)
|
||||
.json({ error: "Label scanning isn’t available on this server." });
|
||||
const run = labelQueue.then(async () => parseLabel(await ocr(req.body)));
|
||||
labelQueue = run.catch(() => {});
|
||||
try {
|
||||
const label = await run;
|
||||
if (!label.found.length)
|
||||
return res.status(422).json({
|
||||
error:
|
||||
"No nutrition facts were found. Fill the frame with the label and keep it sharp.",
|
||||
});
|
||||
res.json({ label });
|
||||
} catch (error) {
|
||||
if (error?.unavailable)
|
||||
return res
|
||||
.status(503)
|
||||
.json({ error: "Label scanning isn’t available on this server." });
|
||||
res
|
||||
.status(422)
|
||||
.json({ error: "That photo couldn’t be read. Try another one." });
|
||||
}
|
||||
},
|
||||
);
|
||||
app.use("/api", (_, res) => res.status(404).json({ error: "Not found" }));
|
||||
if (existsSync(dist)) {
|
||||
// The service worker is also served without a file extension. Reverse
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue