← back to Dw Photo Capture

scripts/verify-vision.py

36 lines

# TK-12090 prod vision verifier — piped over ssh stdin by STEVE-PASTE-tk12090-deploy2.sh
# (runs ON Kamatera). Sends one synthetic grasscloth-ish JPEG to /api/recognize with
# visualMatch:false (no Gemini visual-match spend) and prints which engine/provider served it.
# Needs VU / VP (the dwphoto Basic-Auth pair) in the environment.
from PIL import Image, ImageDraw
import base64, io, json, os, re, sys, urllib.request

vu, vp = os.environ.get("VU", ""), os.environ.get("VP", "")
if not vu or not vp:
    sys.exit("ERR: VU/VP (AUTH_USER/AUTH_PASS) empty — refusing to call with blank Basic Auth")

im = Image.new("RGB", (384, 384), (198, 178, 142))
d = ImageDraw.Draw(im)
for y in range(0, 384, 24):
    d.line([(0, y), (384, y)], fill=(150, 130, 96), width=6)
for x in range(0, 384, 24):
    d.line([(x, 0), (x, 384)], fill=(170, 150, 112), width=3)
b = io.BytesIO()
im.save(b, "JPEG", quality=88)

auth = base64.b64encode(f"{vu}:{vp}".encode()).decode()
payload = json.dumps({
    "dataUrl": "data:image/jpeg;base64," + base64.b64encode(b.getvalue()).decode(),
    "visualMatch": False,
}).encode()
req = urllib.request.Request(
    "http://127.0.0.1:9912/api/recognize",
    data=payload,
    headers={"Content-Type": "application/json", "Authorization": "Basic " + auth},
)
r = json.loads(urllib.request.urlopen(req, timeout=200).read())  # seconds; vision can be slow

err = re.sub(r"(key=)[^&\s\"]+", r"\1REDACTED", str(r.get("err") or ""))[:220]
print("ENGINE:", r.get("engine"), " PROVIDER:", r.get("provider"), " cost_usd:", r.get("cost_usd"), " ok:", r.get("ok"))
print("MOTIF:", (r.get("recognized") or {}).get("motif"), " ERR:", err or "-")