vibedgames 0.10.0 → 0.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/rebuild-object/.cache/spec-search/core_3d.json +1 -0
- package/assets/rebuild-object/.cache/spec-search/cs2.json +1 -0
- package/assets/rebuild-object/LICENSE +201 -0
- package/assets/rebuild-object/NOTICE +7 -0
- package/assets/rebuild-object/docs/materials/material-reference.json +2324 -0
- package/assets/rebuild-object/docs/specs/render-profile.v2.example.json +63 -0
- package/assets/rebuild-object/docs/specs/render-profile.v2.schema.json +56 -0
- package/assets/rebuild-object/docs/specs/vocabulary/README.md +86 -0
- package/assets/rebuild-object/docs/specs/vocabulary/core_3d.jsonl +12 -0
- package/assets/rebuild-object/docs/specs/vocabulary/core_3d_reconstruction.jsonl +37 -0
- package/assets/rebuild-object/docs/specs/vocabulary/cs2.jsonl +8 -0
- package/assets/rebuild-object/docs/specs/vocabulary/cs2_reconstruction.jsonl +62 -0
- package/assets/rebuild-object/forge/_shared/artifact_cache.py +54 -0
- package/assets/rebuild-object/forge/_shared/chirality.py +272 -0
- package/assets/rebuild-object/forge/_shared/color_metrics.py +107 -0
- package/assets/rebuild-object/forge/_shared/feature_acceptance_policy.py +113 -0
- package/assets/rebuild-object/forge/_shared/image_hash.py +113 -0
- package/assets/rebuild-object/forge/_shared/jpeg.py +448 -0
- package/assets/rebuild-object/forge/_shared/material_physics.py +253 -0
- package/assets/rebuild-object/forge/_shared/pipeline_routing.py +177 -0
- package/assets/rebuild-object/forge/_shared/scalp_field.py +302 -0
- package/assets/rebuild-object/forge/_shared/sdf_primitives.py +172 -0
- package/assets/rebuild-object/forge/_shared/spec_search.py +1792 -0
- package/assets/rebuild-object/forge/_shared/spec_search_profiles.json +58 -0
- package/assets/rebuild-object/forge/_shared/status_banner.py +35 -0
- package/assets/rebuild-object/forge/_shared/subdivision.py +232 -0
- package/assets/rebuild-object/forge/_shared/vertex_paint.py +236 -0
- package/assets/rebuild-object/forge/_shared/workflow_state.py +406 -0
- package/assets/rebuild-object/forge/materials/__init__.py +17 -0
- package/assets/rebuild-object/forge/materials/compatibility.py +106 -0
- package/assets/rebuild-object/forge/materials/reference.py +240 -0
- package/assets/rebuild-object/forge/next.py +116 -0
- package/assets/rebuild-object/forge/report.py +38 -0
- package/assets/rebuild-object/forge/requirements.txt +6 -0
- package/assets/rebuild-object/forge/stage1_intake/analyze_texture.py +255 -0
- package/assets/rebuild-object/forge/stage1_intake/bind_detail_properties.py +99 -0
- package/assets/rebuild-object/forge/stage1_intake/build_detail_inventory.py +349 -0
- package/assets/rebuild-object/forge/stage1_intake/camera_fitting_math.py +133 -0
- package/assets/rebuild-object/forge/stage1_intake/camera_fitting_solver.py +197 -0
- package/assets/rebuild-object/forge/stage1_intake/camera_fitting_types.py +291 -0
- package/assets/rebuild-object/forge/stage1_intake/camera_image_helpers.py +133 -0
- package/assets/rebuild-object/forge/stage1_intake/check_intake_correctness.py +116 -0
- package/assets/rebuild-object/forge/stage1_intake/check_reference_admission.py +203 -0
- package/assets/rebuild-object/forge/stage1_intake/cs2_foundation.py +139 -0
- package/assets/rebuild-object/forge/stage1_intake/cs2_manifest.py +230 -0
- package/assets/rebuild-object/forge/stage1_intake/cs2_review_contract.py +42 -0
- package/assets/rebuild-object/forge/stage1_intake/delight_albedo.py +352 -0
- package/assets/rebuild-object/forge/stage1_intake/detect_cs2.py +276 -0
- package/assets/rebuild-object/forge/stage1_intake/detect_reference_effects.py +275 -0
- package/assets/rebuild-object/forge/stage1_intake/extract_cs2_textures.py +126 -0
- package/assets/rebuild-object/forge/stage1_intake/extract_gradient_stops.py +149 -0
- package/assets/rebuild-object/forge/stage1_intake/extract_hair_evidence.py +385 -0
- package/assets/rebuild-object/forge/stage1_intake/extract_landmarks.py +456 -0
- package/assets/rebuild-object/forge/stage1_intake/extract_part_color_recipe.py +496 -0
- package/assets/rebuild-object/forge/stage1_intake/extract_pbr_evidence.py +843 -0
- package/assets/rebuild-object/forge/stage1_intake/fetch_cs2_metadata.py +147 -0
- package/assets/rebuild-object/forge/stage1_intake/label_glb_nodes.py +318 -0
- package/assets/rebuild-object/forge/stage1_intake/locate_cs2_vpk.py +73 -0
- package/assets/rebuild-object/forge/stage1_intake/material_region_analysis.py +227 -0
- package/assets/rebuild-object/forge/stage1_intake/probe_glb.py +357 -0
- package/assets/rebuild-object/forge/stage1_intake/probe_image.py +168 -0
- package/assets/rebuild-object/forge/stage1_intake/run_vision_adapter.py +45 -0
- package/assets/rebuild-object/forge/stage1_intake/search_specs.py +258 -0
- package/assets/rebuild-object/forge/stage1_intake/semantic_decomposition.py +119 -0
- package/assets/rebuild-object/forge/stage1_intake/solve_camera_pose.py +254 -0
- package/assets/rebuild-object/forge/stage2_spec/apply_material_analysis.py +192 -0
- package/assets/rebuild-object/forge/stage2_spec/cs2_adapters.py +62 -0
- package/assets/rebuild-object/forge/stage2_spec/derive_geometry.py +118 -0
- package/assets/rebuild-object/forge/stage2_spec/hair_profile.py +306 -0
- package/assets/rebuild-object/forge/stage2_spec/humanoid_proportions.py +214 -0
- package/assets/rebuild-object/forge/stage2_spec/new_pre_spec_assessment.py +343 -0
- package/assets/rebuild-object/forge/stage2_spec/new_sculpt_spec.py +2396 -0
- package/assets/rebuild-object/forge/stage2_spec/validate_sculpt_spec.py +2871 -0
- package/assets/rebuild-object/forge/stage3_build/bake_projected_texture.py +181 -0
- package/assets/rebuild-object/forge/stage3_build/decimate.py +272 -0
- package/assets/rebuild-object/forge/stage3_build/generate_threejs_factory.py +4099 -0
- package/assets/rebuild-object/forge/stage3_build/module_cache.py +155 -0
- package/assets/rebuild-object/forge/stage3_build/morph_targets.py +178 -0
- package/assets/rebuild-object/forge/stage3_build/orchestrate_passes.py +653 -0
- package/assets/rebuild-object/forge/stage3_build/uv_unwrap.py +622 -0
- package/assets/rebuild-object/forge/stage3_build/visual_hull.py +357 -0
- package/assets/rebuild-object/forge/stage4_review/_fit_divine_eye.py +64 -0
- package/assets/rebuild-object/forge/stage4_review/append_review.py +453 -0
- package/assets/rebuild-object/forge/stage4_review/attachment_anchor.py +349 -0
- package/assets/rebuild-object/forge/stage4_review/calibrate_eye.py +131 -0
- package/assets/rebuild-object/forge/stage4_review/check_part_coverage.py +298 -0
- package/assets/rebuild-object/forge/stage4_review/compare_region_passes.py +169 -0
- package/assets/rebuild-object/forge/stage4_review/correction_loop.py +258 -0
- package/assets/rebuild-object/forge/stage4_review/cs2_review.py +224 -0
- package/assets/rebuild-object/forge/stage4_review/diagnose_render.py +356 -0
- package/assets/rebuild-object/forge/stage4_review/diagnose_render_multi_angle.py +143 -0
- package/assets/rebuild-object/forge/stage4_review/divine_eye.py +461 -0
- package/assets/rebuild-object/forge/stage4_review/fit_params.py +381 -0
- package/assets/rebuild-object/forge/stage4_review/geometry_integrity.py +294 -0
- package/assets/rebuild-object/forge/stage4_review/hair_gate.py +216 -0
- package/assets/rebuild-object/forge/stage4_review/interior_difference.py +188 -0
- package/assets/rebuild-object/forge/stage4_review/joint_loops.py +209 -0
- package/assets/rebuild-object/forge/stage4_review/make_comparison_sheet.py +292 -0
- package/assets/rebuild-object/forge/stage4_review/material_comparator.py +181 -0
- package/assets/rebuild-object/forge/stage4_review/material_feedback.py +143 -0
- package/assets/rebuild-object/forge/stage4_review/material_gate.py +126 -0
- package/assets/rebuild-object/forge/stage4_review/material_views.py +184 -0
- package/assets/rebuild-object/forge/stage4_review/mesh_reference_compare.py +362 -0
- package/assets/rebuild-object/forge/stage4_review/multi_pass.py +102 -0
- package/assets/rebuild-object/forge/stage4_review/objectness.py +143 -0
- package/assets/rebuild-object/forge/stage4_review/pairwise_penetration.py +376 -0
- package/assets/rebuild-object/forge/stage4_review/per_feature.py +215 -0
- package/assets/rebuild-object/forge/stage4_review/render_bridge.py +556 -0
- package/assets/rebuild-object/forge/stage4_review/scalp_exposure.py +257 -0
- package/assets/rebuild-object/forge/stage4_review/self_intersection.py +592 -0
- package/assets/rebuild-object/forge/stage4_review/swept_arc_gate.py +336 -0
- package/assets/rebuild-object/forge/stage4_review/turntable_gate.py +415 -0
- package/assets/rebuild-object/forge/stage4_review/validate_render_profile.py +182 -0
- package/assets/rebuild-object/forge/stage4_review/vertex_region_gate.py +456 -0
- package/assets/rebuild-object/forge/stage4_review/vlm_gate.py +196 -0
- package/assets/rebuild-object/forge/stage5_rig/NOTES.md +149 -0
- package/assets/rebuild-object/forge/stage5_rig/__init__.py +6 -0
- package/assets/rebuild-object/forge/stage5_rig/emit_rig.py +420 -0
- package/assets/rebuild-object/forge/stage5_rig/geodesic_skinning.py +508 -0
- package/assets/rebuild-object/forge/stage5_rig/rig_spec.py +174 -0
- package/assets/rebuild-object/forge/stage5_rig/validate_rig_payload.py +195 -0
- package/assets/rebuild-object/forge/state.py +98 -0
- package/assets/rebuild-object/steps.json +381 -0
- package/dist/{chunk-FXXVXDTW.js → chunk-24TITAFS.js} +1 -1
- package/dist/{chunk-OGX4DKIS.js → chunk-2C5PG2QO.js} +1 -1
- package/dist/{chunk-HYOXGNWS.js → chunk-2DEVOKI7.js} +1 -1
- package/dist/{chunk-RRI23NK2.js → chunk-2GHPYT2P.js} +1 -1
- package/dist/{chunk-S7HYWITX.js → chunk-2HXGMXGC.js} +1 -1
- package/dist/{chunk-N2UHAQXC.js → chunk-36FORP7T.js} +0 -1
- package/dist/{chunk-VB4WLJR2.js → chunk-3IXRQTRF.js} +1 -1
- package/dist/{chunk-2KVQJKIZ.js → chunk-4ENHS32B.js} +23 -36
- package/dist/{chunk-SXYQL4GK.js → chunk-4YHA32OE.js} +1 -1
- package/dist/{chunk-4FJXCS3K.js → chunk-4ZGQKPK5.js} +1 -1
- package/dist/{chunk-SBXCK2I2.js → chunk-6CLUPFQA.js} +2 -2
- package/dist/{chunk-TXOE5UY5.js → chunk-7WTF2BA4.js} +1 -1
- package/dist/{chunk-R2WVORGD.js → chunk-AAXZNK4Z.js} +1 -1
- package/dist/{chunk-DOOQUFPG.js → chunk-ALLBT4FB.js} +1 -1
- package/dist/{chunk-HRKVAXNN.js → chunk-BAZDPPIG.js} +5 -5
- package/dist/{chunk-RPJR3S5S.js → chunk-CYDENLZ4.js} +5 -8
- package/dist/{chunk-I5LELJSK.js → chunk-DKSUD44G.js} +1 -1
- package/dist/{chunk-43P4U7CB.js → chunk-DWWSJKTE.js} +1 -1
- package/dist/{chunk-SUSX3QIA.js → chunk-E72TFJZP.js} +1 -1
- package/dist/{chunk-E3FJUPLT.js → chunk-EUAAQMUA.js} +2 -0
- package/dist/{chunk-U6DTE5IX.js → chunk-F7LYJSZE.js} +1 -1
- package/dist/{chunk-MEAO33XW.js → chunk-FMON3C74.js} +8 -8
- package/dist/{chunk-GLEW7VJQ.js → chunk-FPILHRLZ.js} +1 -1
- package/dist/{chunk-OPUCUUID.js → chunk-G63MXDX2.js} +4 -0
- package/dist/{chunk-4UU622RK.js → chunk-GGO6ZYYI.js} +1 -3
- package/dist/{chunk-2ZKS7V72.js → chunk-GZCRYYS6.js} +5 -5
- package/dist/{chunk-E7NDOZ27.js → chunk-LE4GVTYI.js} +8 -8
- package/dist/{chunk-RKS7SI5L.js → chunk-NO6KPV64.js} +1 -1
- package/dist/{chunk-KJK5CB6E.js → chunk-NXW3Z4Y4.js} +1 -1
- package/dist/{chunk-7RPLQWR2.js → chunk-OHOAZGO4.js} +1 -1
- package/dist/chunk-PYJNI5WD.js +83 -0
- package/dist/{chunk-NCX6LYNJ.js → chunk-Q4XAGTFX.js} +1 -1
- package/dist/{chunk-3UGGMFJJ.js → chunk-QMZBSVKF.js} +1 -1
- package/dist/{chunk-ABTUEIRC.js → chunk-QO66UBOS.js} +2 -2
- package/dist/{chunk-LONQWRTR.js → chunk-S4KVGQQO.js} +8 -8
- package/dist/{chunk-HX5Q63UA.js → chunk-T777EXAO.js} +1 -1
- package/dist/{chunk-UQ7VO6B3.js → chunk-TZZNRA3S.js} +1 -1
- package/dist/{chunk-CTY2WZQK.js → chunk-UC3QLWMQ.js} +1 -1
- package/dist/{chunk-MR5KIFI7.js → chunk-UDJH2RED.js} +3 -3
- package/dist/{chunk-ZHLPE6LI.js → chunk-UIKYK7KI.js} +1 -1
- package/dist/{chunk-KAGVI3CE.js → chunk-UJRSQO2B.js} +1 -1
- package/dist/{chunk-Q2QC6KAZ.js → chunk-V7LEYVXB.js} +29 -25
- package/dist/{chunk-DB7WVFS7.js → chunk-WRMQEXUC.js} +1 -1
- package/dist/{chunk-HSMG6U7J.js → chunk-WXSBNC67.js} +3 -9
- package/dist/{chunk-QLCVLCPM.js → chunk-X4QIOXNQ.js} +1 -1
- package/dist/{chunk-5YPLKGY3.js → chunk-Y6WHUK2O.js} +1 -1
- package/dist/{chunk-VZEJ2ZHE.js → chunk-Z3E7ORGQ.js} +5 -5
- package/dist/index.js +20 -20
- package/package.json +32 -22
- package/dist/{chunk-Q3XQPNF6.js → chunk-BN7TR6OR.js} +3 -3
- package/dist/{chunk-LORGE2NB.js → chunk-U2NCN5RO.js} +3 -3
- package/dist/{chunk-UPHB5HWO.js → chunk-VQTTSPA3.js} +3 -3
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""The Divine Eye — deterministic multi-signal render↔reference evaluator.
|
|
3
|
+
|
|
4
|
+
Plan 1.3 Phase 3 core (§3.1 ensemble, §3.3 combination + self-uncertainty). This is
|
|
5
|
+
the single authority the correction loop asks "how close is this render to the
|
|
6
|
+
reference, and what's wrong?". It is DETERMINISTIC and ZERO-TOKEN: pure Python +
|
|
7
|
+
pixel math, reusing the Tier-1 primitives (diagnose_render) + the shared pHash. No
|
|
8
|
+
LLM/VLM call lives here — the VLM layer (§3.4) is a separate, gated, subordinate step.
|
|
9
|
+
|
|
10
|
+
Signals (each → normalized [0,1] agreement + a defect tag when it fails):
|
|
11
|
+
HARD gates (a fail cannot be averaged away):
|
|
12
|
+
- silhouette IoU (< 0.85 ⇒ reject)
|
|
13
|
+
- scale delta (> 0.08 ⇒ reject)
|
|
14
|
+
SOFT signals (ensemble-weighted):
|
|
15
|
+
- proportion / aspect ratio
|
|
16
|
+
- bilateral symmetry
|
|
17
|
+
- pHash structural similarity
|
|
18
|
+
- global SSIM (luma)
|
|
19
|
+
- edge-map overlap (Sobel linework)
|
|
20
|
+
- blowout parity (QA: blown-highlight fraction vs reference)
|
|
21
|
+
- flat-region ratio (QA: material reacting to light, not a dead flat fill)
|
|
22
|
+
- tonal/contrast parity (QA: luma-histogram match)
|
|
23
|
+
|
|
24
|
+
Deferred to later Phase-3 increments (documented, not silently missing):
|
|
25
|
+
Directional Chamfer Distance, OSIM objectness (numpy+weights, R-DEP/R-OSIM-EFFORT),
|
|
26
|
+
multi-angle browser capture (diagnose_render_multi_angle.py), CIE-Lab per-region ΔE
|
|
27
|
+
wiring (available via extract_part_color_recipe; folded in with per-feature §3.8).
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
from __future__ import annotations
|
|
31
|
+
|
|
32
|
+
import argparse
|
|
33
|
+
import json
|
|
34
|
+
import math
|
|
35
|
+
import sys
|
|
36
|
+
from pathlib import Path
|
|
37
|
+
from typing import Any
|
|
38
|
+
|
|
39
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
40
|
+
from diagnose_render import ( # noqa: E402
|
|
41
|
+
bbox_of,
|
|
42
|
+
bilateral_symmetry_error,
|
|
43
|
+
load_mask,
|
|
44
|
+
mask_is_inverted,
|
|
45
|
+
proportion_delta,
|
|
46
|
+
silhouette_iou,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "stage1_intake"))
|
|
50
|
+
from extract_pbr_evidence import build_foreground_mask, load_image # noqa: E402
|
|
51
|
+
|
|
52
|
+
from objectness import objectness_similarity # noqa: E402 (stdlib OSIM-lite, same dir)
|
|
53
|
+
|
|
54
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "_shared"))
|
|
55
|
+
from color_metrics import ciede2000, srgb_to_lab # noqa: E402
|
|
56
|
+
from image_hash import normalized_similarity, phash_from_image # noqa: E402
|
|
57
|
+
|
|
58
|
+
# Hard-gate thresholds (shared with diagnose_render; calibratable in Phase 5).
|
|
59
|
+
IOU_HARD_MIN = 0.85
|
|
60
|
+
SCALE_HARD_MAX = 0.08
|
|
61
|
+
# Ensemble: fidelity target + disagreement (self-uncertainty) spread.
|
|
62
|
+
FIDELITY_TARGET = 0.85
|
|
63
|
+
DISAGREEMENT_SPREAD = 0.35 # if soft-signal spread exceeds this ⇒ low-confidence → probe
|
|
64
|
+
ASPECT_SOFT_MAX = 0.05 # aspect-ratio delta allowed for a reconstruction-mode soft pass.
|
|
65
|
+
RECON_OBJ_MIN = 0.48 # objectness ≥ this rescues an IoU-only hard reject → probe (recon mode).
|
|
66
|
+
# Separates same-object-different-framing (real pairs ~0.53–0.58) from a
|
|
67
|
+
# genuinely different shape (~0.43). Rescue only ever downgrades reject→probe.
|
|
68
|
+
# RESOLUTION CEILING, and it is a hard limit on what this module can ever report.
|
|
69
|
+
# Every signal below -- SSIM, tonal, blowout, flat, edge overlap -- is computed on these grids, so a
|
|
70
|
+
# feature a few pixels wide in a 1920px reference is not scored badly, it is ABSENT before any
|
|
71
|
+
# comparison happens. No threshold tuning recovers it. per_feature.py cannot compensate either: it
|
|
72
|
+
# consumes a scores dict and never opens an image, so its critical-feature gate is sound and starved.
|
|
73
|
+
# Feature-scale fidelity needs zoom patches instead: grimoire/review/divine_eye_microscope.md.
|
|
74
|
+
LUMA_SIZE = 64 # SSIM / tonal / blowout / flat work on this downsampled luma grid
|
|
75
|
+
EDGE_SIZE = 96 # edge overlap grid
|
|
76
|
+
HUE_ZONE_DELTA_E = 2.3 # per-band CIEDE2000 "same hue zone" tolerance (Context Part 2.2)
|
|
77
|
+
HUE_ZONE_BANDS = 8 # bands sampled along the axis for hue_zone_parity
|
|
78
|
+
COLOR_SAMPLE = 160 # coarse per-axis subsample cap for colour helpers (perf on full-res refs)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _banded_median_lab(png_path: Path, axis: str, bands: int) -> list[tuple[float, float, float] | None]:
|
|
82
|
+
"""Median CIELAB per foreground-masked band along the axis (axis 'u'=x, 'v'=y).
|
|
83
|
+
Colour-aware (not luma) — used only by hue_zone_parity, which is report-only until calibrated."""
|
|
84
|
+
width, height, pixels, _ = load_image(png_path)
|
|
85
|
+
mask, _meta, _warn = build_foreground_mask(width, height, pixels)
|
|
86
|
+
span = width if axis == "u" else height
|
|
87
|
+
band = max(1, span // bands)
|
|
88
|
+
# Subsample on a coarse grid (≤ COLOR_SAMPLE px/axis) so full-res references stay O(fast) —
|
|
89
|
+
# median hue is stable under downsampling (Context Part 2.5 perf note).
|
|
90
|
+
sx = max(1, width // COLOR_SAMPLE)
|
|
91
|
+
sy = max(1, height // COLOR_SAMPLE)
|
|
92
|
+
out: list[tuple[float, float, float] | None] = []
|
|
93
|
+
for b in range(bands):
|
|
94
|
+
lo = b * band
|
|
95
|
+
hi = span if b == bands - 1 else (b + 1) * band
|
|
96
|
+
rs: list[int] = []
|
|
97
|
+
gs: list[int] = []
|
|
98
|
+
bs: list[int] = []
|
|
99
|
+
for y in range(0, height, sy):
|
|
100
|
+
for x in range(0, width, sx):
|
|
101
|
+
coord = x if axis == "u" else y
|
|
102
|
+
if coord < lo or coord >= hi:
|
|
103
|
+
continue
|
|
104
|
+
idx = y * width + x
|
|
105
|
+
if idx >= len(mask) or not mask[idx]:
|
|
106
|
+
continue
|
|
107
|
+
r, g, bl, _a = pixels[idx]
|
|
108
|
+
rs.append(r)
|
|
109
|
+
gs.append(g)
|
|
110
|
+
bs.append(bl)
|
|
111
|
+
if not rs:
|
|
112
|
+
out.append(None)
|
|
113
|
+
continue
|
|
114
|
+
rs.sort(); gs.sort(); bs.sort()
|
|
115
|
+
m = len(rs) // 2
|
|
116
|
+
out.append(srgb_to_lab((rs[m], gs[m], bs[m])))
|
|
117
|
+
return out
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _foreground_hsv_stats(png_path: Path) -> tuple[float, float]:
|
|
121
|
+
"""Saturation-weighted mean (hueDeg, saturation) over the foreground. Colour-aware."""
|
|
122
|
+
import colorsys
|
|
123
|
+
import math as _m
|
|
124
|
+
width, height, pixels, _ = load_image(png_path)
|
|
125
|
+
mask, _meta, _warn = build_foreground_mask(width, height, pixels)
|
|
126
|
+
sx = max(1, width // COLOR_SAMPLE)
|
|
127
|
+
sy = max(1, height // COLOR_SAMPLE)
|
|
128
|
+
sc = ss = wsum = sat_sum = 0.0
|
|
129
|
+
n = 0
|
|
130
|
+
for y in range(0, height, sy):
|
|
131
|
+
for x in range(0, width, sx):
|
|
132
|
+
idx = y * width + x
|
|
133
|
+
if idx >= len(mask) or not mask[idx]:
|
|
134
|
+
continue
|
|
135
|
+
r, g, b, _a = pixels[idx]
|
|
136
|
+
h, s, _v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
|
|
137
|
+
sat_sum += s
|
|
138
|
+
n += 1
|
|
139
|
+
if s > 0.15:
|
|
140
|
+
sc += s * _m.cos(2 * _m.pi * h)
|
|
141
|
+
ss += s * _m.sin(2 * _m.pi * h)
|
|
142
|
+
wsum += s
|
|
143
|
+
mean_sat = sat_sum / n if n else 0.0
|
|
144
|
+
mean_hue = (_m.degrees(_m.atan2(ss, sc)) % 360.0) if wsum > 0 else 0.0
|
|
145
|
+
return mean_hue, mean_sat
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def specular_wash(reference_png: Path, render_png: Path) -> dict[str, Any]:
|
|
149
|
+
"""Detect the envMap/metalness 'hue theft': the render desaturates a saturated reference AND
|
|
150
|
+
drifts its hue toward cyan (~180°). Report-only — advisory, never a gate (lighting legitimately
|
|
151
|
+
shifts hue). Returns {satRatio, hueDriftDeg, towardCyan, flagged}. (Context Part 3.2)."""
|
|
152
|
+
ref_hue, ref_sat = _foreground_hsv_stats(reference_png)
|
|
153
|
+
ren_hue, ren_sat = _foreground_hsv_stats(render_png)
|
|
154
|
+
sat_ratio = (ren_sat / ref_sat) if ref_sat > 1e-6 else 1.0
|
|
155
|
+
# circular hue drift toward cyan (180°): did the render move closer to 180 than the reference?
|
|
156
|
+
ref_to_cyan = min(abs(ref_hue - 180.0), 360.0 - abs(ref_hue - 180.0))
|
|
157
|
+
ren_to_cyan = min(abs(ren_hue - 180.0), 360.0 - abs(ren_hue - 180.0))
|
|
158
|
+
toward_cyan = ren_to_cyan < ref_to_cyan
|
|
159
|
+
flagged = ref_sat > 0.35 and sat_ratio < 0.6 and toward_cyan
|
|
160
|
+
return {
|
|
161
|
+
"satRatio": round(sat_ratio, 3),
|
|
162
|
+
"hueDriftDeg": round(ref_to_cyan - ren_to_cyan, 1),
|
|
163
|
+
"towardCyan": toward_cyan,
|
|
164
|
+
"flagged": flagged,
|
|
165
|
+
"advice": "lower metalness / envMapIntensity (candy-coat dielectric recipe)" if flagged else None,
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def hue_zone_parity(reference_png: Path, render_png: Path, axis: str = "u",
|
|
170
|
+
bands: int = HUE_ZONE_BANDS) -> float:
|
|
171
|
+
"""Fraction of along-axis bands whose median colour matches the reference within CIEDE2000
|
|
172
|
+
≤ HUE_ZONE_DELTA_E. Catches "purple rendered blue" that luma/structure signals miss.
|
|
173
|
+
Report-only (no ensemble weight) until calibrated on the labeled corpus."""
|
|
174
|
+
ref = _banded_median_lab(reference_png, axis, bands)
|
|
175
|
+
ren = _banded_median_lab(render_png, axis, bands)
|
|
176
|
+
matched = 0
|
|
177
|
+
counted = 0
|
|
178
|
+
for a, b in zip(ref, ren):
|
|
179
|
+
if a is None or b is None:
|
|
180
|
+
continue
|
|
181
|
+
counted += 1
|
|
182
|
+
if ciede2000(a, b) <= HUE_ZONE_DELTA_E:
|
|
183
|
+
matched += 1
|
|
184
|
+
return matched / counted if counted else 0.0
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def load_luma(png_path: Path, size: int) -> list[float]:
|
|
188
|
+
"""Box-average downsample of Rec.709 luma to size×size, normalized 0..1."""
|
|
189
|
+
width, height, pixels, _warn = load_image(png_path)
|
|
190
|
+
acc = [0.0] * (size * size)
|
|
191
|
+
cnt = [0] * (size * size)
|
|
192
|
+
for idx, (r, g, b, _a) in enumerate(pixels):
|
|
193
|
+
x = idx % width
|
|
194
|
+
y = idx // width
|
|
195
|
+
if y >= height:
|
|
196
|
+
break
|
|
197
|
+
cell = min(size - 1, y * size // height) * size + min(size - 1, x * size // width)
|
|
198
|
+
acc[cell] += (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255.0
|
|
199
|
+
cnt[cell] += 1
|
|
200
|
+
return [acc[i] / cnt[i] if cnt[i] else 0.0 for i in range(size * size)]
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _mean(xs: list[float]) -> float:
|
|
204
|
+
return sum(xs) / len(xs) if xs else 0.0
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def global_ssim(a: list[float], b: list[float]) -> float:
|
|
208
|
+
"""Single-window SSIM over the whole downsampled luma image (structure signal)."""
|
|
209
|
+
n = len(a)
|
|
210
|
+
if n == 0 or len(b) != n:
|
|
211
|
+
return 0.0
|
|
212
|
+
mu_a, mu_b = _mean(a), _mean(b)
|
|
213
|
+
var_a = _mean([(x - mu_a) ** 2 for x in a])
|
|
214
|
+
var_b = _mean([(x - mu_b) ** 2 for x in b])
|
|
215
|
+
cov = _mean([(a[i] - mu_a) * (b[i] - mu_b) for i in range(n)])
|
|
216
|
+
c1, c2 = 0.01 ** 2, 0.03 ** 2
|
|
217
|
+
ssim = ((2 * mu_a * mu_b + c1) * (2 * cov + c2)) / (
|
|
218
|
+
(mu_a ** 2 + mu_b ** 2 + c1) * (var_a + var_b + c2)
|
|
219
|
+
)
|
|
220
|
+
return max(0.0, min(1.0, ssim))
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _sobel_edges(luma: list[float], size: int, thresh: float = 0.12) -> list[bool]:
|
|
224
|
+
edges = [False] * (size * size)
|
|
225
|
+
for y in range(1, size - 1):
|
|
226
|
+
for x in range(1, size - 1):
|
|
227
|
+
def g(dx, dy):
|
|
228
|
+
return luma[(y + dy) * size + (x + dx)]
|
|
229
|
+
gx = (g(-1, -1) + 2 * g(-1, 0) + g(-1, 1)) - (g(1, -1) + 2 * g(1, 0) + g(1, 1))
|
|
230
|
+
gy = (g(-1, -1) + 2 * g(0, -1) + g(1, -1)) - (g(-1, 1) + 2 * g(0, 1) + g(1, 1))
|
|
231
|
+
if math.hypot(gx, gy) > thresh:
|
|
232
|
+
edges[y * size + x] = True
|
|
233
|
+
return edges
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def edge_overlap(a: list[float], b: list[float], size: int) -> float:
|
|
237
|
+
ea, eb = _sobel_edges(a, size), _sobel_edges(b, size)
|
|
238
|
+
inter = union = 0
|
|
239
|
+
for i in range(len(ea)):
|
|
240
|
+
if ea[i] or eb[i]:
|
|
241
|
+
union += 1
|
|
242
|
+
if ea[i] and eb[i]:
|
|
243
|
+
inter += 1
|
|
244
|
+
return inter / union if union else 0.0
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def _blown_fraction(luma: list[float], hi: float = 0.95) -> float:
|
|
248
|
+
return sum(1 for v in luma if v >= hi) / max(1, len(luma))
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def blowout_parity(ref: list[float], ren: list[float]) -> float:
|
|
252
|
+
diff = abs(_blown_fraction(ren) - _blown_fraction(ref))
|
|
253
|
+
return max(0.0, 1.0 - diff * 4.0) # 25% extra blown pixels ⇒ score 0
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def flat_fraction(luma: list[float], size: int, eps: float = 0.02) -> float:
|
|
257
|
+
"""Fraction of pixels whose local gradient is ~0 (a dead flat fill)."""
|
|
258
|
+
flat = 0
|
|
259
|
+
total = 0
|
|
260
|
+
for y in range(1, size - 1):
|
|
261
|
+
for x in range(1, size - 1):
|
|
262
|
+
c = luma[y * size + x]
|
|
263
|
+
grad = abs(c - luma[y * size + x - 1]) + abs(c - luma[(y - 1) * size + x])
|
|
264
|
+
total += 1
|
|
265
|
+
if grad < eps:
|
|
266
|
+
flat += 1
|
|
267
|
+
return flat / total if total else 0.0
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def tonal_parity(ref: list[float], ren: list[float], bins: int = 16) -> float:
|
|
271
|
+
def hist(xs):
|
|
272
|
+
h = [0] * bins
|
|
273
|
+
for v in xs:
|
|
274
|
+
h[min(bins - 1, int(v * bins))] += 1
|
|
275
|
+
tot = sum(h) or 1
|
|
276
|
+
return [c / tot for c in h]
|
|
277
|
+
ha, hb = hist(ref), hist(ren)
|
|
278
|
+
l1 = sum(abs(ha[i] - hb[i]) for i in range(bins))
|
|
279
|
+
return max(0.0, 1.0 - l1 / 2.0) # L1 over two distributions ∈ [0,2]
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def evaluate(reference_png: Path, render_png: Path) -> dict[str, Any]:
|
|
283
|
+
"""Run all deterministic signals and combine into a verdict + routing action."""
|
|
284
|
+
ref_mask, ref_mask_warnings = load_mask(reference_png)
|
|
285
|
+
ren_mask, ren_mask_warnings = load_mask(render_png)
|
|
286
|
+
ref_luma = load_luma(reference_png, LUMA_SIZE)
|
|
287
|
+
ren_luma = load_luma(render_png, LUMA_SIZE)
|
|
288
|
+
ref_edge = load_luma(reference_png, EDGE_SIZE)
|
|
289
|
+
ren_edge = load_luma(render_png, EDGE_SIZE)
|
|
290
|
+
rw, rh, rpx, _ = load_image(reference_png)
|
|
291
|
+
vw, vh, vpx, _ = load_image(render_png)
|
|
292
|
+
|
|
293
|
+
iou = silhouette_iou(ref_mask, ren_mask)
|
|
294
|
+
prop = proportion_delta(bbox_of(ref_mask), bbox_of(ren_mask))
|
|
295
|
+
# proportion_delta returns snake_case keys; reading camelCase here silently defaulted both
|
|
296
|
+
# to 0.0, which dead-coded the scale HARD gate and pinned the proportion soft signal at 1.0.
|
|
297
|
+
scale_delta = prop.get("scale_delta", 0.0)
|
|
298
|
+
aspect_delta = prop.get("aspect_ratio_delta", 0.0)
|
|
299
|
+
# symmetry + flat-region are PARITY signals (render vs reference), NOT absolute —
|
|
300
|
+
# a legitimately asymmetric or flat-lit subject must not be penalized when the
|
|
301
|
+
# render matches the reference. score = 1 when render is as (a)symmetric / as flat
|
|
302
|
+
# as the reference; drops when the render diverges (e.g. render flatter ⇒ material
|
|
303
|
+
# not reacting to light).
|
|
304
|
+
sym_ref = bilateral_symmetry_error(ref_mask)
|
|
305
|
+
sym_ren = bilateral_symmetry_error(ren_mask)
|
|
306
|
+
sym = max(0.0, 1.0 - abs(sym_ren - sym_ref) / 0.10)
|
|
307
|
+
flat_ref = flat_fraction(ref_luma, LUMA_SIZE)
|
|
308
|
+
flat_ren = flat_fraction(ren_luma, LUMA_SIZE)
|
|
309
|
+
flat = max(0.0, 1.0 - abs(flat_ren - flat_ref) * 4.0)
|
|
310
|
+
phash_sim = normalized_similarity(phash_from_image(rw, rh, rpx), phash_from_image(vw, vh, vpx))
|
|
311
|
+
ssim = global_ssim(ref_luma, ren_luma)
|
|
312
|
+
edges = edge_overlap(ref_edge, ren_edge, EDGE_SIZE)
|
|
313
|
+
blow = blowout_parity(ref_luma, ren_luma)
|
|
314
|
+
tonal = tonal_parity(ref_luma, ren_luma)
|
|
315
|
+
# OSIM-lite objectness (stdlib HOG-like, bg/pose/scale/brightness-invariant). Graceful:
|
|
316
|
+
# if it errors it degrades to absent (must-not-block, R-OSIM-EFFORT). It is the one
|
|
317
|
+
# signal that stays meaningful for photo-vs-procedural where IoU/SSIM/edge collapse.
|
|
318
|
+
try:
|
|
319
|
+
objectness: float | None = objectness_similarity(reference_png, render_png)
|
|
320
|
+
except Exception:
|
|
321
|
+
objectness = None
|
|
322
|
+
|
|
323
|
+
# hue_zone_parity: colour-aware along-axis hue match (CIEDE2000). REPORT-ONLY — not in the
|
|
324
|
+
# weighted ensemble until calibrated on the labeled corpus. Catches "purple→blue" that
|
|
325
|
+
# every luma/structure signal above is blind to. Graceful: degrades to None on error.
|
|
326
|
+
try:
|
|
327
|
+
hue_zone: float | None = hue_zone_parity(reference_png, render_png)
|
|
328
|
+
except Exception:
|
|
329
|
+
hue_zone = None
|
|
330
|
+
try:
|
|
331
|
+
spec_wash: dict[str, Any] | None = specular_wash(reference_png, render_png)
|
|
332
|
+
except Exception:
|
|
333
|
+
spec_wash = None
|
|
334
|
+
|
|
335
|
+
# HARD gates: a fail is an immediate reject with a specific numeric reason.
|
|
336
|
+
hard_failures: list[str] = []
|
|
337
|
+
if mask_is_inverted(ref_mask_warnings) or mask_is_inverted(ren_mask_warnings):
|
|
338
|
+
hard_failures.append(
|
|
339
|
+
"foreground mask fell back to whole-frame coverage; silhouette, scale and aspect "
|
|
340
|
+
"signals are not measuring the subject"
|
|
341
|
+
)
|
|
342
|
+
if iou < IOU_HARD_MIN:
|
|
343
|
+
hard_failures.append(f"silhouette IoU {iou:.3f} < {IOU_HARD_MIN}")
|
|
344
|
+
if scale_delta > SCALE_HARD_MAX:
|
|
345
|
+
hard_failures.append(f"scale delta {scale_delta:.3f} > {SCALE_HARD_MAX}")
|
|
346
|
+
|
|
347
|
+
# SOFT signals → weighted ensemble fidelity. Weights are provisional (Phase 5
|
|
348
|
+
# calibrates them on the known-good/known-bad corpus); recorded here so they are
|
|
349
|
+
# auditable, not magic.
|
|
350
|
+
soft = {
|
|
351
|
+
"proportion": (max(0.0, 1.0 - aspect_delta / 0.05), 1.0),
|
|
352
|
+
"symmetry": (sym, 0.5),
|
|
353
|
+
"phash": (phash_sim, 1.0),
|
|
354
|
+
"ssim": (ssim, 1.5),
|
|
355
|
+
"edgeOverlap": (edges, 1.0),
|
|
356
|
+
"blowoutParity": (blow, 0.8),
|
|
357
|
+
"flatRegion": (flat, 0.8),
|
|
358
|
+
"tonalParity": (tonal, 1.0),
|
|
359
|
+
}
|
|
360
|
+
if objectness is not None:
|
|
361
|
+
soft["objectness"] = (objectness, 1.5) # strongest structural signal when present
|
|
362
|
+
weighted = sum(s * w for s, w in soft.values())
|
|
363
|
+
total_w = sum(w for _s, w in soft.values())
|
|
364
|
+
fidelity = weighted / total_w if total_w else 0.0
|
|
365
|
+
|
|
366
|
+
soft_scores = [s for s, _w in soft.values()]
|
|
367
|
+
spread = max(soft_scores) - min(soft_scores) if soft_scores else 0.0
|
|
368
|
+
|
|
369
|
+
# Verdict + routing (deterministic function of which signal failed — §3.5).
|
|
370
|
+
if hard_failures:
|
|
371
|
+
verdict, action = "reject", "refine-code"
|
|
372
|
+
elif spread > DISAGREEMENT_SPREAD and fidelity < FIDELITY_TARGET:
|
|
373
|
+
verdict, action = "low-confidence", "probe"
|
|
374
|
+
elif fidelity >= FIDELITY_TARGET:
|
|
375
|
+
verdict, action = "pass", "continue"
|
|
376
|
+
else:
|
|
377
|
+
verdict, action = "reject", "refine-code"
|
|
378
|
+
|
|
379
|
+
# Reconstruction-mode rescue: a *photo* reference vs a *procedural* render fails the
|
|
380
|
+
# silhouette-IoU hard gate purely from framing/background/scale mismatch. When the only
|
|
381
|
+
# hard failure is IoU AND objectness says "same object" (high, brightness/bg-invariant),
|
|
382
|
+
# downgrade the confident reject to a probe rather than hard-failing a faithful build.
|
|
383
|
+
# Never rescues a scale-delta failure or a genuinely different object (low objectness).
|
|
384
|
+
reconstruction_suspected = False
|
|
385
|
+
if hard_failures and objectness is not None and objectness >= RECON_OBJ_MIN:
|
|
386
|
+
if all("silhouette IoU" in f for f in hard_failures):
|
|
387
|
+
reconstruction_suspected = True
|
|
388
|
+
# Per-track calibration: silhouette IoU 0.85 is unreachable for a procedural-primitive
|
|
389
|
+
# reconstruction of a detailed photo (solid primitives structurally over-fill cutouts,
|
|
390
|
+
# serrations and AA curves), so IoU alone must not veto forever — otherwise the loop can
|
|
391
|
+
# only ever bounded-stop. When objectness confirms the same object AND the soft ensemble
|
|
392
|
+
# already meets the fidelity target AND scale/aspect are within gate, promote the
|
|
393
|
+
# IoU-only reject to a real pass; otherwise route to probe (human/VLM look). Genuinely
|
|
394
|
+
# wrong geometry still fails: low objectness isn't rescued at all, and a weak soft
|
|
395
|
+
# ensemble (< target) or an out-of-gate scale/aspect can only reach probe, never pass.
|
|
396
|
+
if fidelity >= FIDELITY_TARGET and scale_delta <= SCALE_HARD_MAX and aspect_delta <= ASPECT_SOFT_MAX:
|
|
397
|
+
verdict, action = "pass", "continue"
|
|
398
|
+
else:
|
|
399
|
+
verdict, action = "low-confidence", "probe"
|
|
400
|
+
|
|
401
|
+
return {
|
|
402
|
+
"verdict": verdict,
|
|
403
|
+
"action": action,
|
|
404
|
+
"fidelity": round(fidelity, 4),
|
|
405
|
+
"fidelityTarget": FIDELITY_TARGET,
|
|
406
|
+
"hardGateFailures": hard_failures,
|
|
407
|
+
"maskWarnings": (
|
|
408
|
+
[f"reference: {w}" for w in ref_mask_warnings]
|
|
409
|
+
+ [f"render: {w}" for w in ren_mask_warnings]
|
|
410
|
+
),
|
|
411
|
+
"disagreementSpread": round(spread, 4),
|
|
412
|
+
"signals": {
|
|
413
|
+
"silhouetteIoU": round(iou, 4),
|
|
414
|
+
"scaleDelta": round(scale_delta, 4),
|
|
415
|
+
"aspectRatioDelta": round(aspect_delta, 4),
|
|
416
|
+
"symmetryParity": round(sym, 4),
|
|
417
|
+
"phashSimilarity": round(phash_sim, 4),
|
|
418
|
+
"ssim": round(ssim, 4),
|
|
419
|
+
"edgeOverlap": round(edges, 4),
|
|
420
|
+
"blowoutParity": round(blow, 4),
|
|
421
|
+
"flatRegionScore": round(flat, 4),
|
|
422
|
+
"tonalParity": round(tonal, 4),
|
|
423
|
+
"objectness": round(objectness, 4) if objectness is not None else None,
|
|
424
|
+
"hueZoneParity": round(hue_zone, 4) if hue_zone is not None else None,
|
|
425
|
+
},
|
|
426
|
+
"specularWash": spec_wash,
|
|
427
|
+
"reportOnlySignals": ["hueZoneParity", "specularWash"],
|
|
428
|
+
"reconstructionModeSuspected": reconstruction_suspected,
|
|
429
|
+
"weights": {k: w for k, (_s, w) in soft.items()},
|
|
430
|
+
"reference": str(reference_png.resolve()),
|
|
431
|
+
"render": str(render_png.resolve()),
|
|
432
|
+
"note": "deterministic ensemble; zero VLM/token. hueZoneParity is REPORT-ONLY (colour-aware, "
|
|
433
|
+
"CIEDE2000) — not yet in the weighted fidelity; promote after corpus calibration. "
|
|
434
|
+
"VLM layer (§3.4) runs only if this passes.",
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
def main(argv: list[str]) -> int:
|
|
439
|
+
parser = argparse.ArgumentParser(description=__doc__)
|
|
440
|
+
parser.add_argument("--reference", required=True, type=Path)
|
|
441
|
+
parser.add_argument("--render", required=True, type=Path)
|
|
442
|
+
parser.add_argument("--json", action="store_true")
|
|
443
|
+
args = parser.parse_args(argv)
|
|
444
|
+
try:
|
|
445
|
+
result = evaluate(args.reference.expanduser().resolve(), args.render.expanduser().resolve())
|
|
446
|
+
except Exception as exc: # noqa: BLE001
|
|
447
|
+
print(f"error: {exc}", file=sys.stderr)
|
|
448
|
+
return 2
|
|
449
|
+
if args.json:
|
|
450
|
+
print(json.dumps(result, indent=2, ensure_ascii=False))
|
|
451
|
+
else:
|
|
452
|
+
print(f"{result['verdict'].upper()} → {result['action']} fidelity={result['fidelity']} "
|
|
453
|
+
f"(target {result['fidelityTarget']})")
|
|
454
|
+
for f in result["hardGateFailures"]:
|
|
455
|
+
print(f" HARD: {f}")
|
|
456
|
+
# exit 0 only on a clean pass; non-zero otherwise so a pipeline can gate on it.
|
|
457
|
+
return 0 if result["verdict"] == "pass" else 1
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
if __name__ == "__main__":
|
|
461
|
+
raise SystemExit(main(sys.argv[1:]))
|