product-recognition-demo/recognization/code/build_stock_all.py
tngo 0cf64a18bf Coverage-first live labelling, merged catalog, shelf-intelligence decision
The live path abstained too much: a detected product whose best candidate
missed the accept line was left blank, so a shelf where 59% of in-catalog
facings were shown read as broken. Show the best candidate immediately as
`provisional`, keep the track re-classifiable so a later attempt upgrades or
corrects it, and never blank a label already on screen.

- identity.py / identity_lanes.py: every decision now carries
  `provisional_sku` - the row the evidence supports well enough to show,
  separate from `best_sku`, which still means "cleared the gate". The accept
  line does not move: far-unknown false accepts stay at 0.066%.
- live.py: a below-gate candidate labels the track with
  `identity_level: provisional` and `confirmed = False`, so the planner and
  the crop lane keep offering it. Accepted labels stay frozen. Inherited
  provisional labels stay re-classifiable.
- Clients render provisional distinctly (amber chip, dashed box) via
  `LiveScanOverlayPolicy.isProvisional(identity)`, which now reads the wire
  level instead of only the state.
- build_stock_all.py merges stock, stock-lopia, stock-mediya and
  stock-milk150 into stock-all (111 images, 60 SKUs). The server could only
  read one folder, so anything outside it came back blank. stock-demo is
  byte-identical to mediya+milk150 and is not a source.

Measured on add/image6.jpg at live resolution: in-catalog facings shown
59.4% -> 86.0%, correct shown 38 -> 55, precision 97.4% -> 87.3%. On the
wire at 8fps 0.5x: 65 of 79 tracks labelled, 742 accepted + 309 provisional
observations, first label 1.27s.

docs/research/2026-09-29-shelf-intelligence/ carries the experiments and the
decision: group signals (combined AUC 0.991 sibling / 0.990 unrelated),
OCR scheduling (group representatives cut calls 4.4x and wall 3.3x), and the
finding that group propagation damages more labels than it rescues on this
shelf, so groups schedule compute rather than overwrite labels.

331 tests pass.
2026-09-29 13:45:14 +09:00

79 lines
2.9 KiB
Python

"""Merge every stock folder into one catalog the server can match against.
The demo had five stock folders and the server could only read one, so a shelf
holding anything outside the active folder came back blank. SKU ids are unique
across the folders (`sku_0xx`, `sku_lxx`, `sku_mxx`, `sku_kxx`), so the union is
a plain copy plus a CSV concat - no id rewriting, no dedup heuristics.
`stock-demo` is byte-identical to `stock-mediya` + `stock-milk150`, so it is not
a source: including it would only duplicate 62 images.
Run: .venv/bin/python code/build_stock_all.py
"""
from __future__ import annotations
import csv
import os
import shutil
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
SOURCES = ("stock", "stock-lopia", "stock-mediya", "stock-milk150")
DEST = os.path.join(ROOT, "stock-all")
IMAGE_SUFFIXES = (".jpg", ".jpeg", ".png")
FIELDS = ("SKU_ID", "product_name", "receipt_date", "expiration_date",
"number_in", "number_out", "number_on", "image_link", "price")
def main() -> int:
if os.path.isdir(DEST):
shutil.rmtree(DEST)
os.makedirs(DEST)
rows: list[dict] = []
seen_sku: dict[str, str] = {}
copied = 0
for source in SOURCES:
directory = os.path.join(ROOT, source)
if not os.path.isdir(directory):
print(f"skip {source}: not a directory", file=sys.stderr)
continue
for name in sorted(os.listdir(directory)):
if not name.lower().endswith(IMAGE_SUFFIXES):
continue
target = os.path.join(DEST, name)
if os.path.exists(target):
# Same filename in two folders is the same product photographed
# once; the first copy wins and the duplicate is reported.
print(f"duplicate image {name} from {source}", file=sys.stderr)
continue
shutil.copy2(os.path.join(directory, name), target)
copied += 1
csv_path = os.path.join(directory, "stock.csv")
if not os.path.exists(csv_path):
continue
with open(csv_path, newline="", encoding="utf-8") as handle:
for row in csv.DictReader(handle):
sku = (row.get("SKU_ID") or "").strip()
if not sku:
continue
if sku in seen_sku:
print(f"duplicate SKU {sku} in {source} (kept {seen_sku[sku]})",
file=sys.stderr)
continue
seen_sku[sku] = source
rows.append({field: (row.get(field) or "").strip() for field in FIELDS})
with open(os.path.join(DEST, "stock.csv"), "w", newline="", encoding="utf-8") as handle:
writer = csv.DictWriter(handle, fieldnames=FIELDS)
writer.writeheader()
writer.writerows(rows)
print(f"{DEST}: {copied} images, {len(rows)} SKUs")
return 0
if __name__ == "__main__":
raise SystemExit(main())