"""Build a REAL evaluation set for the Product Label Interpreter (no synthetic images).
Ground truth NEVER comes from this project's own model output. Sources:
ndl/ openfoodfacts/nutrient-detection-layout (Hugging Face, CC BY-SA 3.0).
Nutrition-table photos whose nutrient values were tagged by professional
annotators (BIO token tags on Google Cloud Vision OCR tokens). Rows with
meta.checked = true only; only *_100G tags are converted (per-serving values
are not converted). Indian barcodes (890...) first, then the held-out test split.
-> "source": "nutrient-detection-layout (human-annotated)", "verified": true
off_india/ photos downloaded earlier from Open Food Facts (volunteer data, UNVERIFIED). The download code
was removed with the Open Food Facts integration (ADR-005); existing entries stay.
my_photos/ your own photos; empty ground truth for you to fill in ("verified": false).
Only entries with "verified": true are used by scripts/eval_product_label.py.
Run from backend/:
venv/bin/python scripts/build_label_eval_set.py build [--ndl 20]
venv/bin/python scripts/build_label_eval_set.py skeleton # add my_photos/* entries
venv/bin/python scripts/build_label_eval_set.py review # review page on 127.0.0.1:8765
Network: Hugging Face datasets-server only (rows API; the 635 MB parquet files are NOT downloaded),
with pauses between requests. Images are stored at max 2000 px (JPEG q90) to save disk.
"""
from __future__ import annotations
import argparse
import csv
import html
import io
import json
import re
import shutil
import sys
import time
import urllib.parse
import urllib.request
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
BACKEND_ROOT = Path(__file__).resolve().parents[1]
if str(BACKEND_ROOT) not in sys.path:
sys.path.insert(0, str(BACKEND_ROOT))
EVAL_DIR = BACKEND_ROOT / "data" / "eval" / "product_labels"
GT_FILE = "ground_truth.json"
IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".webp"}
MAX_SIDE = 2000
HF_DATASET = "openfoodfacts/nutrient-detection-layout"
HF_ROWS = "https://datasets-server.huggingface.co/rows"
HF_FILTER = "https://datasets-server.huggingface.co/filter"
SRC_NDL = "nutrient-detection-layout (human-annotated)"
SRC_OFF = "open_food_facts (volunteer, UNVERIFIED)"
SRC_OWN = "own photo"
LIC_NDL = "CC BY-SA 3.0 (dataset openfoodfacts/nutrient-detection-layout; images from Open Food Facts)"
LIC_OFF = "CC BY-SA 3.0 (Open Food Facts images); ODbL (Open Food Facts data)"
# nutrient-detection-layout tag -> our per-100 key and the unit our key is in
NDL_TAGS = {
"ENERGY_KCAL_100G": ("energy_kcal", "kcal"), "ENERGY_KJ_100G": ("energy_kj", "kj"),
"FAT_100G": ("fat_g", "g"), "SATURATED_FAT_100G": ("saturated_fat_g", "g"),
"TRANS_FAT_100G": ("trans_fat_g", "g"), "CARBOHYDRATES_100G": ("carbohydrate_g", "g"),
"SUGARS_100G": ("sugars_g", "g"), "ADDED_SUGARS_100G": ("added_sugars_g", "g"),
"FIBER_100G": ("fibre_g", "g"), "PROTEINS_100G": ("protein_g", "g"),
"SALT_100G": ("salt_g", "g"), "SODIUM_100G": ("sodium_mg", "mg"),
}
NUTRIENT_KEYS = ["energy_kcal", "energy_kj", "fat_g", "saturated_fat_g", "carbohydrate_g", "sugars_g",
"fibre_g", "protein_g", "salt_g", "sodium_mg"]
# ---------------------------------------------------------------------------
# helpers
# ---------------------------------------------------------------------------
def user_agent() -> str:
from app.config import settings
return settings.off_user_agent
def http_get(url: str, timeout: float = 60.0, ua: Optional[str] = None, retries: int = 3) -> bytes:
last = None
for attempt in range(retries):
req = urllib.request.Request(url, headers={"User-Agent": ua or user_agent()})
try:
with urllib.request.urlopen(req, timeout=timeout) as resp:
return resp.read()
except Exception as exc: # temporary HF / OFF errors happen; back off and retry
last = exc
time.sleep(5 * (attempt + 1))
raise RuntimeError(f"GET failed after {retries} tries: {url.split('?')[0]} ({last})")
def save_image(data: bytes, path: Path) -> Tuple[int, int]:
from PIL import Image, ImageOps
img = ImageOps.exif_transpose(Image.open(io.BytesIO(data))).convert("RGB")
img.thumbnail((MAX_SIDE, MAX_SIDE))
path.parent.mkdir(parents=True, exist_ok=True)
img.save(path, format="JPEG", quality=90)
return img.size
def load_gt(eval_dir: Path) -> Dict[str, Any]:
p = eval_dir / GT_FILE
if p.exists():
data = json.loads(p.read_text())
if data.get("version") == 2:
return data
raise SystemExit(f"{p} is in the old format; move it aside first (nothing was changed).")
return {"version": 2, "note": "Only entries with verified=true are evaluated. Ground truth never comes "
"from this project's model output.", "entries": {}}
def save_gt(eval_dir: Path, gt: Dict[str, Any]) -> None:
p = eval_dir / GT_FILE
if p.exists():
shutil.copy2(p, p.with_suffix(".json.bak"))
p.write_text(json.dumps(gt, indent=2, ensure_ascii=False))
def empty_entry(source: str, licence: str, images: Dict[str, str], verified: bool) -> Dict[str, Any]:
return {"source": source, "licence": licence, "verified": verified, "images": images, "barcode": None,
"product_name": None, "allergens": None, "nutrients_per_100": {}, "origin": {}, "notes": []}
# ---------------------------------------------------------------------------
# 1. nutrient-detection-layout (Hugging Face)
# ---------------------------------------------------------------------------
# A number must be whole (not part of a longer number) and its unit must end the word, so OCR
# noise like "10gl" is rejected instead of being read as 1.0 (never guess ground truth).
NUM = re.compile(r"(?≤≥]|less than)?\s*(\d+(?:[.,]\d+)?)(?![\d.,]\d)\s*(kcal|kj|mg|µg|mcg|g)?(?![a-z0-9])", re.I)
def parse_value(text: str, unit_expected: str) -> Tuple[Optional[float], Optional[str]]:
"""Annotated span text -> value in our unit, or (None, reason)."""
nums = NUM.findall(text.replace(" ", " "))
if len(nums) > 1 and unit_expected in ("kcal", "kj") and all(n[2] for n in nums):
# only when EVERY number has its own unit ("2 345 kJ" must stay ambiguous)
# "294kJ/70kcal" tagged as one span: keep the single number whose unit is the tagged one
same = [n for n in nums if (n[2] or "").lower() == unit_expected]
nums = same if len(same) == 1 else nums
if len(nums) != 1:
return None, f"{len(nums)} numbers in '{text.strip()}'"
cmp_, num, unit = nums[0]
if cmp_:
return None, f"'{text.strip()}' is a bound, not a value"
v = float(num.replace(",", "."))
unit = (unit or "").lower()
if unit_expected in ("kcal", "kj"):
if unit and unit != unit_expected:
return None, f"unit {unit} in '{text.strip()}', expected {unit_expected}"
return v, None
if unit_expected == "g":
if unit in ("", "g"):
return v, None
if unit == "mg":
return round(v / 1000, 4), None
return None, f"unit {unit} in '{text.strip()}'"
if unit_expected == "mg": # sodium
if unit == "mg":
return v, None
if unit == "g":
return round(v * 1000, 3), None
return None, f"no unit for sodium in '{text.strip()}'"
return None, "unknown unit"
def ndl_spans(row: Dict[str, Any], names: List[str]) -> List[Tuple[str, str]]:
"""BIO tags -> [(TAG, text)]."""
spans, cur_tag, cur = [], None, []
for tok, tid in zip(row["tokens"], row["ner_tags"]):
name = names[tid]
if name.startswith("B-") or name == "O" or (name.startswith("I-") and name[2:] != cur_tag):
if cur_tag:
spans.append((cur_tag, "".join(cur)))
cur_tag, cur = (name[2:], [tok]) if name != "O" else (None, [])
else:
cur.append(tok)
if cur_tag:
spans.append((cur_tag, "".join(cur)))
return spans
def ndl_values(row: Dict[str, Any], names: List[str]) -> Tuple[Dict[str, float], List[str]]:
values: Dict[str, float] = {}
notes: List[str] = []
seen: Dict[str, List[float]] = {}
for tag, text in ndl_spans(row, names):
if tag not in NDL_TAGS:
continue
key, unit = NDL_TAGS[tag]
v, why = parse_value(text, unit)
if v is None:
notes.append(f"{key} not converted: {why}")
continue
seen.setdefault(key, []).append(v)
for key, vals in seen.items():
if len(set(vals)) == 1:
values[key] = vals[0]
else:
notes.append(f"{key} not converted: annotated twice with different values {vals}")
return values, notes
def hf_get(url: str, params: Dict[str, Any]) -> Dict[str, Any]:
q = urllib.parse.urlencode({"dataset": HF_DATASET, "config": "default", **params})
for attempt in range(6): # the datasets-server sometimes answers "index is loading" / auth hiccups
try:
data = json.loads(http_get(f"{url}?{q}", ua="VisionAI-eval-builder/1.0"))
except RuntimeError as exc:
data = {"error": str(exc)}
if "error" not in data:
return data
time.sleep(15)
raise RuntimeError(f"Hugging Face datasets-server: {data.get('error')}")
def build_ndl(eval_dir: Path, gt: Dict[str, Any], n: int) -> List[str]:
from model.product_label.sanity import check as sanity_check
have = sum(1 for e in gt["entries"].values() if e["source"] == SRC_NDL)
if have >= n:
print(f"ndl: already {have} entries")
return []
names = None
candidates: List[Tuple[str, Dict[str, Any]]] = []
# Indian barcodes first (both splits), then the held-out test split, then train.
for split in ("train", "test"):
try:
d = hf_get(HF_FILTER, {"split": split, "where": "\"meta\".\"barcode\" LIKE '890%'", "length": 100})
names = names or d["features"][0]["type"]["feature"]["names"]
candidates += [(split, r) for r in d["rows"]]
except RuntimeError as exc:
print("ndl: Indian filter failed:", exc)
print(f"ndl: {len(candidates)} rows with Indian barcodes")
for split in ("test", "train"):
offset = 0
while len(candidates) < 4 * n:
d = hf_get(HF_ROWS, {"split": split, "offset": offset, "length": 100})
names = names or d["features"][0]["type"]["feature"]["names"]
if not d["rows"]:
break
candidates += [(split, r) for r in d["rows"]]
offset += 100
time.sleep(1)
if len(candidates) >= 4 * n:
break
added: List[str] = []
for split, r in candidates:
if have + len(added) >= n:
break
row, meta = r["row"], r["row"]["meta"]
key = f"ndl/{meta['barcode']}_{meta['image_id']}.jpg"
if key in gt["entries"] or not meta.get("checked") or meta.get("no_nutrition_table"):
continue
values, notes = ndl_values(row, names)
if "sugars_g" not in values or not ({"energy_kcal", "energy_kj"} & set(values)):
continue # need at least sugar and energy per 100 g to be useful
# Auto-verify only clean conversions that also pass our deterministic sanity check
# (the annotations sit on OCR tokens, so OCR errors can hide in them).
sane = sanity_check(values, "label")
auto_ok = not notes and sane["ok"]
if not sane["ok"]:
notes.append("Sanity check failed: " + "; ".join(f["detail"] for f in sane["failures"])
+ " — check against the photo.")
size = save_image(http_get(row["image"]["src"], ua="VisionAI-eval-builder/1.0"), eval_dir / key)
e = empty_entry(SRC_NDL, LIC_NDL, {"label": key}, verified=auto_ok)
e.update(barcode=meta["barcode"], nutrients_per_100=values, notes=notes + [
"Nutrition-table photo only: allergens are not annotated (not scored)."])
e["origin"] = {"dataset": HF_DATASET, "split": split, "row_idx": r["row_idx"], "image_url": meta["image_url"],
"label_studio_id": meta.get("label_studio_id"), "checked": meta.get("checked"),
"stored_size": list(size)}
e["verified_by"] = ("dataset annotators (meta.checked = true) + clean conversion + sanity check; "
"untick in the review page if you disagree") if auto_ok else None
gt["entries"][key] = e
added.append(key)
print(f"ndl: + {key} {values}")
time.sleep(1)
return added
# ---------------------------------------------------------------------------
# 3. own photos
# ---------------------------------------------------------------------------
def skeleton(eval_dir: Path, gt: Dict[str, Any]) -> List[str]:
own = eval_dir / "my_photos"
own.mkdir(parents=True, exist_ok=True)
added = []
for p in sorted(own.iterdir()):
key = f"my_photos/{p.name}"
if p.suffix.lower() in IMAGE_EXTS and key not in gt["entries"]:
e = empty_entry(SRC_OWN, "your own photo", {"label": key}, verified=False)
e["nutrients_per_100"] = {k: None for k in NUTRIENT_KEYS}
e["allergens"] = {"contains": None, "may_contain": None}
e["notes"] = ["Fill in by hand from the printed label (per 100 g / 100 mL), then set verified to true. "
"allergens.contains / allergens.may_contain: lists of allergen names, [] if none, "
"null to skip. Leave a nutrient null if it is not printed."]
gt["entries"][key] = e
added.append(key)
return added
# ---------------------------------------------------------------------------
# 4. credits
# ---------------------------------------------------------------------------
def write_credits(eval_dir: Path, gt: Dict[str, Any]) -> None:
rows = []
for key, e in gt["entries"].items():
for kind, path in e["images"].items():
o = e.get("origin") or {}
url = o.get("image_url") or (o.get("image_urls") or {}).get(kind) or ""
rows.append({"image": path, "entry": key, "kind": kind, "source": e["source"], "licence": e["licence"],
"origin_url": url, "product_url": o.get("product_url") or
(f"https://world.openfoodfacts.org/product/{e['barcode']}" if e.get("barcode") else "")})
with open(eval_dir / "CREDITS.csv", "w", newline="") as f:
w = csv.DictWriter(f, fieldnames=list(rows[0].keys()) if rows else ["image"])
w.writeheader()
w.writerows(rows)
lines = ["# Credits — product label evaluation images", "",
"- `ndl/`: images and annotations from the Hugging Face dataset "
"[openfoodfacts/nutrient-detection-layout](https://huggingface.co/datasets/openfoodfacts/nutrient-detection-layout), "
"licence CC BY-SA 3.0. Images originally from Open Food Facts contributors.",
"- `off_india/`: photos from [Open Food Facts](https://world.openfoodfacts.org) contributors, CC BY-SA 3.0; "
"nutrition values and allergen tags from the Open Food Facts database, ODbL 1.0.",
"- `my_photos/`: the project owner's own photos.", "",
"Images were resized to at most 2000 px. Per-image sources: `CREDITS.csv`.", ""]
(eval_dir / "CREDITS.md").write_text("\n".join(lines))
# ---------------------------------------------------------------------------
# 5. review page
# ---------------------------------------------------------------------------
def review_html(gt: Dict[str, Any]) -> str:
cols = NUTRIENT_KEYS
rows = []
for key, e in gt["entries"].items():
imgs = "".join(f'
Check each value against the photo (per 100 g / 100 mL as printed). Fix wrong values, clear values that are not printed, then tick verified. Only verified entries are evaluated. Ground truth must come from the label, never from this app's output.