nx_likeness_gate.nx source
↩ module page · 253 lines · 17714 B
1// nx_likeness_gate.nx -- the referee for nx_likeness_lib (aesthetictwin AT45, 2026-09-18). On the real reference set (four clothed,
2// frontal Diora Baird stills, aligned from the outside oracle rows) and the real renders the operator judged: the band is derived
3// from at least three same-person pairs and the canvas from the smallest reference eye span; a reference against itself reads zero
4// on every axis; the anchor still resampled into a rolled, scaled frame (a planted control whose landmarks are carried by the same
5// similarity) reads INSIDE with its geometry within two canvas pixels, and its mouth landmarks shifted by a tenth of the eye span
6// read the shift back (the geometry axis can fire); the canon face and a seed render -- renders of nobody in particular -- read
7// OUTSIDE the band; the two renders the operator rejected as not her (diora_baird_close.png, diora_baird_threequarter.png) read
8// OUTSIDE, and they read it MEASURED, not as an absence of alignment; each score's partition sums; the lk_score door agrees with
9// the in-process path. Values are printed with gv_kv. Fixtures live under /tmp/nx_likeness_gate/ (created at setup); the reference
10// stills, the oracle rows and the renders are read from the estate's own paths and are PRECONDITIONS (SKIP when absent, never RED).
11// THE FIRST RUN'S ACCEPT RULE WAS STRICTER AND IT FAILED, WHICH IS RECORDED HERE RATHER THAN TUNED AWAY: the renders were required
12// to read OUTSIDE on the structure axes. Measured 2026-09-18 on this set they do not -- the same person's own photographs spread
13// more on every classical structure ruler than a render differs from a photograph at an 84 px canvas (struct_l1 band 2891 against
14// renders 2128..2759; shading 846 against 441..595; texture layout 644 against 559..654; texture energy 52 against 6..12) -- and
15// the rejections rest on skin chroma (a*) and, for the three-quarter render, on texture layout. That separation count is published
16// as a value (structure_family_separates_renders_of_4) and is the open accept bar of the structure contender queued on the board.
17// license_tier: ORIGINAL No hw writes (Rule 26).
18import "nx_syscalls.nx"
19import "nx_gate_verdict.nx"
20import "nx_likeness_lib.nx"
21
22const LG_I64: i64 = 8
23const LG_MODE755: i64 = 493
24const LG_MODE644: i64 = 420
25const LG_DIR: *u8 = "/tmp/nx_likeness_gate"
26const LG_LIST: *u8 = "/tmp/nx_likeness_gate/refs.list"
27const LG_LIST_TEXT: *u8 = "@oracle knowledge/refintake/diora_baird.oracle\nknowledge/fetched/ri_diora_baird_3.jpg\nknowledge/fetched/ri_diora_baird_11.jpg\nknowledge/fetched/ri_diora_baird_12.jpg\nknowledge/fetched/ri_diora_baird_14.jpg\n"
28const LG_ORACLE: *u8 = "knowledge/refintake/diora_baird.oracle"
29const LG_REF3: *u8 = "knowledge/fetched/ri_diora_baird_3.jpg"
30const LG_REF11: *u8 = "knowledge/fetched/ri_diora_baird_11.jpg"
31const LG_REF12: *u8 = "knowledge/fetched/ri_diora_baird_12.jpg"
32const LG_REF14: *u8 = "knowledge/fetched/ri_diora_baird_14.jpg"
33const LG_ANCHOR: *u8 = "ri_diora_baird_14.jpg"
34const LG_CANON: *u8 = "sites/nishifamily/compare/aesthetictwin/results/img/canon_close.png"
35const LG_SEED1: *u8 = "sites/nishifamily/compare/aesthetictwin/results/img/seed_1.png"
36const LG_REJ_CLOSE: *u8 = "sites/nishifamily/compare/aesthetictwin/results/img/diora_baird_close.png"
37const LG_REJ_TQ: *u8 = "sites/nishifamily/compare/aesthetictwin/results/img/diora_baird_threequarter.png"
38const LG_CTL_NAME: *u8 = "planted-roll-scale-control"
39const LG_REFS: i64 = 4
40const LG_MIN_PAIRS: i64 = 3
41// the planted control: the anchor still resampled into a 300 x 260 frame whose eye centres are (120, 140) and (180, 110) pixels,
42// a roll of about 27 degrees and an eye span of 67 pixels against the anchor's 103
43const LG_CTL_W: i64 = 300
44const LG_CTL_H: i64 = 260
45const LG_CTL_RX: i64 = 1200
46const LG_CTL_RY: i64 = 1400
47const LG_CTL_LX: i64 = 1800
48const LG_CTL_LY: i64 = 1100
49const LG_CTL_IOD: i64 = 671 // isqrt(600*600 + 300*300) deci-pixels
50const LG_CTL_IOD_TOL: i64 = 1
51const LG_CANVAS_PX_TOL: i64 = 2 // the geometry of a resampled copy is held to two canvas pixels
52const LG_SHIFT_DIV: i64 = 10 // the neg-control moves the five mouth points by a tenth of the control's eye span
53const LG_MOUTH_PTS: i64 = 5
54
55func lg_exists(path: *u8) -> i64 {
56 let fd: i64 = sys_openat_rd(path)
57 if fd < 0 { return 0 }
58 sys_close(fd)
59 return 1
60}
61func lg_write(path: *u8, text: *u8) -> i64 {
62 let fd: i64 = sys_openat_wr(path, LG_MODE644)
63 if fd < 0 { return 0 - 1 }
64 let n: i64 = sys_write(fd, text, lkh_slen(text))
65 sys_close(fd)
66 return n
67}
68func lg_find(set: *i64, base: *u8) -> i64 {
69 let paths: *i64 = set[LK_R_PATHS] as *i64
70 var i: i64 = 0
71 while i < set[LK_R_N] { if lkh_streq(lkh_basename(paths[i] as *u8), base) == 1 { return i } i = i + 1 }
72 return 0 - 1
73}
74// score one render through the in-process path; returns the verdict, sc filled; aligned_out[0] = 1 when the render aligned
75func lg_render(ctx: *i64, set: *i64, path: *u8, sc: *i64, aligned_out: *i64) -> i64 {
76 let img: *i64 = lk_img_new()
77 let rc: i64 = lk_load(ctx, path, img)
78 aligned_out[0] = 0
79 if rc != LK_OK {
80 var k: i64 = 0
81 while k < LK_S_N { sc[k] = 0; k = k + 1 }
82 sc[LK_S_UNMEASURED] = LK_AX_N
83 sc[LK_S_VERDICT] = LK_V_UNMEASURED
84 lk_print_img("subject" as *u8, path, img)
85 return LK_V_UNMEASURED
86 }
87 aligned_out[0] = 1
88 return lk_score_subject(set, path, img, sc)
89}
90func lg_partition_ok(sc: *i64) -> i64 { return (sc[LK_S_INSIDE] + sc[LK_S_OUTSIDE] + sc[LK_S_UNMEASURED] == LK_AX_N) as i64 }
91// the structure family: the edge-orientation descriptor (L1, cosine), the shading layout, the texture layout, the texture energy.
92// A subject is outside on structure when ANY of them reads OUTSIDE; every one of them is printed so a reader sees which
93func lg_structure_outside(sc: *i64) -> i64 {
94 var out: i64 = 0
95 if sc[LK_S_STATE + LK_AX_SL1] == LK_ST_OUTSIDE { out = 1 }
96 if sc[LK_S_STATE + LK_AX_SCOS] == LK_ST_OUTSIDE { out = 1 }
97 if sc[LK_S_STATE + LK_AX_SHADE] == LK_ST_OUTSIDE { out = 1 }
98 if sc[LK_S_STATE + LK_AX_TEX] == LK_ST_OUTSIDE { out = 1 }
99 if sc[LK_S_STATE + LK_AX_TEXE] == LK_ST_OUTSIDE { out = 1 }
100 return out
101}
102func main(argc: i64, argv: *i64) -> i64 {
103 gv_head("nx_likeness_gate -- the image-level likeness referee: the same-person band from real reference pairs, a reference against itself, a planted roll-and-scale control, renders of nobody outside on structure, the operator's rejected renders outside and measured" as *u8)
104 let ctr: *i64 = gv_ctr()
105 sys_mkdir(LG_DIR, LG_MODE755)
106 lg_write(LG_LIST, LG_LIST_TEXT)
107 var pre: i64 = 1
108 if gv_need("reference-still-ri_diora_baird_3" as *u8, lg_exists(LG_REF3), ctr) == 0 { pre = 0 }
109 if gv_need("reference-still-ri_diora_baird_11" as *u8, lg_exists(LG_REF11), ctr) == 0 { pre = 0 }
110 if gv_need("reference-still-ri_diora_baird_12" as *u8, lg_exists(LG_REF12), ctr) == 0 { pre = 0 }
111 if gv_need("reference-still-ri_diora_baird_14" as *u8, lg_exists(LG_REF14), ctr) == 0 { pre = 0 }
112 if gv_need("outside-oracle-rows" as *u8, lg_exists(LG_ORACLE), ctr) == 0 { pre = 0 }
113 if gv_need("render-canon_close" as *u8, lg_exists(LG_CANON), ctr) == 0 { pre = 0 }
114 if gv_need("render-seed_1" as *u8, lg_exists(LG_SEED1), ctr) == 0 { pre = 0 }
115 if gv_need("render-diora_baird_close" as *u8, lg_exists(LG_REJ_CLOSE), ctr) == 0 { pre = 0 }
116 if gv_need("render-diora_baird_threequarter" as *u8, lg_exists(LG_REJ_TQ), ctr) == 0 { pre = 0 }
117 let ctx: *i64 = lk_ctx_new()
118 if gv_need("haar-cascades-loaded" as *u8, lk_ctx_cascades(ctx), ctr) == 0 { pre = 0 }
119 if pre == 0 { return gv_verdict("nx_likeness_gate" as *u8, ctr, "preconditions" as *u8) }
120 // ---- the reference set, the canvas, the band ----
121 let ms0: i64 = sys_now_ms()
122 let set: *i64 = sys_mmap(LK_R_SLOTS * LG_I64) as *i64
123 let n: i64 = lk_refs_load(ctx, LG_LIST, set)
124 let ms_refs: i64 = sys_now_ms()
125 gv_check_eq("references-aligned" as *u8, n, LG_REFS, ctr)
126 let imgs: *i64 = set[LK_R_IMGS] as *i64
127 let feats: *i64 = set[LK_R_FEATS] as *i64
128 var from_oracle: i64 = 0
129 var min_iod: i64 = 0
130 var i: i64 = 0
131 while i < n {
132 let im: *i64 = imgs[i] as *i64
133 if im[LK_I_ALIGN] == LK_AL_ORACLE { from_oracle = from_oracle + 1 }
134 if i == 0 { min_iod = im[LK_I_IOD] }
135 if im[LK_I_IOD] < min_iod { min_iod = im[LK_I_IOD] }
136 i = i + 1
137 }
138 gv_check_eq("references-aligned-from-oracle-rows" as *u8, from_oracle, LG_REFS, ctr)
139 let band: *i64 = set[LK_R_BAND] as *i64
140 let c: i64 = set[LK_R_CANVAS]
141 gv_check("same-person-pairs-at-least-three" as *u8, ((set[LK_R_PAIRS] >= LG_MIN_PAIRS) as i64) * ((band[LK_B_PAIRS + LK_AX_SL1] >= LG_MIN_PAIRS) as i64), ctr)
142 var expect_c: i64 = LK_CANVAS_SPANS * min_iod / LK_DECI
143 expect_c = expect_c - expect_c % VD_GRID
144 gv_check_eq("canvas-is-twice-the-smallest-reference-eye-span-floored-to-the-grid" as *u8, c, expect_c, ctr)
145 var every_axis_banded: i64 = 1
146 var a: i64 = 0
147 while a < LK_AX_N { if band[LK_B_PAIRS + a] < LG_MIN_PAIRS { every_axis_banded = 0 } a = a + 1 }
148 gv_check("every-axis-banded-by-at-least-three-pairs" as *u8, every_axis_banded, ctr)
149 // ---- a reference against itself ----
150 let ai: i64 = lg_find(set, LG_ANCHOR)
151 gv_check("anchor-reference-in-the-set" as *u8, (ai >= 0) as i64, ctr)
152 let ax: *i64 = sys_mmap(LK_AXR_N * LG_I64) as *i64
153 var zsum: i64 = 0
154 var meas: i64 = 0
155 var anchor: *i64 = lk_img_new()
156 var anchor_f: *i64 = lk_feat_new()
157 if ai >= 0 {
158 anchor = imgs[ai] as *i64
159 anchor_f = feats[ai] as *i64
160 lk_axes(anchor, anchor_f, anchor, anchor_f, c, ax)
161 a = 0
162 while a < LK_AX_N { zsum = zsum + lkh_abs(ax[a]); meas = meas + ax[LK_AXR_MEAS + a]; a = a + 1 }
163 }
164 gv_check_eq("ref-vs-itself-every-axis-measured" as *u8, meas, LK_AX_N, ctr)
165 gv_check_eq("ref-vs-itself-reads-zero-on-every-axis" as *u8, zsum, 0, ctr)
166 // ---- the planted roll-and-scale control ----
167 let ctl: *i64 = lk_img_new()
168 let ctl_rgb: *u8 = lk_resample(anchor, LG_CTL_RX, LG_CTL_RY, LG_CTL_LX, LG_CTL_LY, LG_CTL_W, LG_CTL_H)
169 lk_img_set_rgb(ctl, ctl_rgb, LG_CTL_W, LG_CTL_H)
170 lk_img_map_landmarks(anchor, ctl, LG_CTL_RX, LG_CTL_RY, LG_CTL_LX, LG_CTL_LY)
171 gv_check_near("planted-control-eye-span-is-the-planted-span" as *u8, ctl[LK_I_IOD], LG_CTL_IOD, LG_CTL_IOD_TOL, ctr)
172 let sc_ctl: *i64 = sys_mmap(LK_S_N * LG_I64) as *i64
173 let v_ctl: i64 = lk_score_subject(set, LG_CTL_NAME, ctl, sc_ctl)
174 let canvas_px_permil: i64 = LK_PERMIL * LK_DECI / lk_canvas_span(c)
175 gv_check_eq("planted-roll-scale-control-reads-inside" as *u8, v_ctl, LK_V_INSIDE, ctr)
176 gv_check_near("planted-roll-scale-control-geometry-within-two-canvas-pixels" as *u8, sc_ctl[LK_S_DIST + LK_AX_GEOM], 0, LG_CANVAS_PX_TOL * canvas_px_permil, ctr)
177 gv_check_eq("planted-roll-scale-control-tilt-delta-zero" as *u8, sc_ctl[LK_S_DIST + LK_AX_TILT], 0, ctr)
178 gv_check_eq("planted-roll-scale-control-nearest-is-the-anchor-on-geometry" as *u8, sc_ctl[LK_S_NEAR + LK_AX_GEOM], ai, ctr)
179 // the neg-control: the same control with its five mouth points shifted by a tenth of its eye span; the geometry axis must read
180 // the shift back: five of nine points moved by shift x (canvas span / control span), as permil of the canvas span
181 let sh: *i64 = lk_img_new()
182 var k: i64 = 0
183 while k < LK_I_N { sh[k] = ctl[k]; k = k + 1 }
184 let shift: i64 = ctl[LK_I_IOD] / LG_SHIFT_DIV
185 var p: i64 = LK_P_LS
186 while p < LK_P_N { lk_pt_set(sh, p, lk_pt_x(ctl, p) + shift, lk_pt_y(ctl, p)); p = p + 1 }
187 let ctl_f: *i64 = lk_feat_new()
188 lk_features(ctl, c, set[LK_R_GRID], ctl_f)
189 let axs: *i64 = sys_mmap(LK_AXR_N * LG_I64) as *i64
190 lk_axes(sh, ctl_f, anchor, anchor_f, c, axs)
191 let expect_shift: i64 = LG_MOUTH_PTS * shift * LK_PERMIL / (LK_P_N * ctl[LK_I_IOD])
192 gv_check_near("neg-control-shifted-mouth-landmarks-read-the-shift-on-geometry" as *u8, axs[LK_AX_GEOM], expect_shift, canvas_px_permil, ctr)
193 gv_check_eq("neg-control-shifted-mouth-landmarks-used-nine-points" as *u8, axs[LK_AXR_GEOM_PTS], LK_P_N, ctr)
194 let ms_ctl: i64 = sys_now_ms()
195 // ---- renders of nobody in particular: outside on both structure axes ----
196 let al: *i64 = sys_mmap(LG_I64) as *i64
197 let sc_canon: *i64 = sys_mmap(LK_S_N * LG_I64) as *i64
198 let v_canon: i64 = lg_render(ctx, set, LG_CANON, sc_canon, al)
199 gv_check_eq("neg-control-canon-render-aligned" as *u8, al[0], 1, ctr)
200 gv_check_eq("neg-control-canon-render-reads-outside" as *u8, v_canon, LK_V_OUTSIDE, ctr)
201 gv_check("neg-control-canon-render-partition-sums" as *u8, lg_partition_ok(sc_canon), ctr)
202 let sc_seed: *i64 = sys_mmap(LK_S_N * LG_I64) as *i64
203 let v_seed: i64 = lg_render(ctx, set, LG_SEED1, sc_seed, al)
204 gv_check_eq("neg-control-seed1-render-aligned" as *u8, al[0], 1, ctr)
205 gv_check_eq("neg-control-seed1-render-reads-outside" as *u8, v_seed, LK_V_OUTSIDE, ctr)
206 gv_check("neg-control-seed1-render-partition-sums" as *u8, lg_partition_ok(sc_seed), ctr)
207 // ---- the operator's rejected renders: outside, on structure too, and measured ----
208 let sc_close: *i64 = sys_mmap(LK_S_N * LG_I64) as *i64
209 let v_close: i64 = lg_render(ctx, set, LG_REJ_CLOSE, sc_close, al)
210 gv_check_eq("rejected-diora-baird-close-aligned-not-unmeasured" as *u8, al[0], 1, ctr)
211 gv_check_eq("rejected-diora-baird-close-reads-outside" as *u8, v_close, LK_V_OUTSIDE, ctr)
212 gv_check("rejected-diora-baird-close-partition-sums" as *u8, lg_partition_ok(sc_close), ctr)
213 let sc_tq: *i64 = sys_mmap(LK_S_N * LG_I64) as *i64
214 let v_tq: i64 = lg_render(ctx, set, LG_REJ_TQ, sc_tq, al)
215 gv_check_eq("rejected-diora-baird-threequarter-aligned-not-unmeasured" as *u8, al[0], 1, ctr)
216 gv_check_eq("rejected-diora-baird-threequarter-reads-outside" as *u8, v_tq, LK_V_OUTSIDE, ctr)
217 gv_check("rejected-diora-baird-threequarter-partition-sums" as *u8, lg_partition_ok(sc_tq), ctr)
218 // THE STRUCTURE FAMILY'S SEPARATION IS PUBLISHED, NOT ASSERTED (measured 2026-09-18 on this very set: the same person's own
219 // photographs spread more on every classical structure ruler -- edge orientation, shading layout, texture layout, texture
220 // energy -- than a render differs from a photograph at this canvas, so a tooth on it would be RED by construction and the gate
221 // could never be bite-proven). The count below is the open accept bar of the structure contender queued on the board: 4 of 4.
222 let structure_separates: i64 = lg_structure_outside(sc_canon) + lg_structure_outside(sc_seed) + lg_structure_outside(sc_close) + lg_structure_outside(sc_tq)
223 let ms_renders: i64 = sys_now_ms()
224 // ---- the door: lk_score on the same subject and list must agree with the in-process path ----
225 let ctx2: *i64 = lk_ctx_new()
226 let sc_door: *i64 = sys_mmap(LK_S_N * LG_I64) as *i64
227 let v_door: i64 = lk_score(ctx2, LG_REJ_CLOSE, LG_LIST, sc_door)
228 gv_check_eq("lk_score-door-agrees-with-the-in-process-verdict" as *u8, v_door, v_close, ctr)
229 gv_check_eq("lk_score-door-agrees-on-struct-l1" as *u8, sc_door[LK_S_DIST + LK_AX_SL1], sc_close[LK_S_DIST + LK_AX_SL1], ctr)
230 let ms_door: i64 = sys_now_ms()
231 gv_values_head()
232 gv_kv("references_aligned" as *u8, n); gv_kv("same_person_pairs" as *u8, set[LK_R_PAIRS]); gv_kv("canvas_side_px" as *u8, c); gv_kv("smallest_reference_iod_decipx" as *u8, min_iod)
233 gv_kv("grid_cells_per_side" as *u8, set[LK_R_GRID]); gv_kv("grid_from_geometry_band_permil" as *u8, set[LK_R_GEOM_BAND])
234 a = 0
235 while a < LK_AX_N {
236 gv_puts(" band_" as *u8); gv_puts(lk_axis_name(a)); gv_puts("_max=" as *u8); gv_num(band[LK_B_MAX + a]); gv_puts(" mean=" as *u8); gv_num(band[LK_B_MEAN + a]); gv_puts(" pairs=" as *u8); gv_num(band[LK_B_PAIRS + a]); gv_puts("\n" as *u8)
237 a = a + 1
238 }
239 a = 0
240 while a < LK_AX_N {
241 gv_puts(" dist_" as *u8); gv_puts(lk_axis_name(a))
242 gv_puts(" control=" as *u8); gv_num(sc_ctl[LK_S_DIST + a]); gv_puts(" canon=" as *u8); gv_num(sc_canon[LK_S_DIST + a]); gv_puts(" seed1=" as *u8); gv_num(sc_seed[LK_S_DIST + a])
243 gv_puts(" rejected_close=" as *u8); gv_num(sc_close[LK_S_DIST + a]); gv_puts(" rejected_threequarter=" as *u8); gv_num(sc_tq[LK_S_DIST + a]); gv_puts("\n" as *u8)
244 a = a + 1
245 }
246 gv_kv("control_geometry_shift_expected_permil" as *u8, expect_shift); gv_kv("control_geometry_shift_read_permil" as *u8, axs[LK_AX_GEOM]); gv_kv("canvas_pixel_permil" as *u8, canvas_px_permil)
247 gv_kv("rejected_close_axes_outside" as *u8, sc_close[LK_S_OUTSIDE]); gv_kv("rejected_threequarter_axes_outside" as *u8, sc_tq[LK_S_OUTSIDE])
248 gv_kv("structure_family_separates_renders_of_4" as *u8, structure_separates)
249 gv_kv("structure_separates_canon" as *u8, lg_structure_outside(sc_canon)); gv_kv("structure_separates_seed1" as *u8, lg_structure_outside(sc_seed))
250 gv_kv("structure_separates_rejected_close" as *u8, lg_structure_outside(sc_close)); gv_kv("structure_separates_rejected_threequarter" as *u8, lg_structure_outside(sc_tq))
251 gv_kv("ms_references_and_band" as *u8, ms_refs - ms0); gv_kv("ms_controls" as *u8, ms_ctl - ms_refs); gv_kv("ms_renders" as *u8, ms_renders - ms_ctl); gv_kv("ms_door" as *u8, ms_door - ms_renders)
252 return gv_verdict("nx_likeness_gate" as *u8, ctr, "the likeness referee on the real reference set and the real renders: band from same-person pairs, self reads zero, a rolled and scaled copy reads inside, renders of nobody and the rejected renders read outside and measured, the door agrees" as *u8)
253}