code wiki / (root) / nx_facefind_gate.nx

nx_facefind_gate.nx source

↩ module page · 206 lines · 14367 B

1// nx_facefind_gate.nx -- the referee for the sovereign cascade engine and its loader (aesthetictwin AT30, 2026-09-16). 2// In-process: the decimal-to-Q24 parser on the data's own spellings against values computed outside once; the loader on 3// the two mirrored cascades (window, stage, classifier and feature counts, stumps versus trees); planted refusals (a file 4// without the <cascade> root, a tilted feature assembled at run time so this source never spells it). End to end: two 5// mirrored stills whose landmarks an OUTSIDE oracle placed (knowledge/refintake/diora_baird.oracle, MediaPipe, laptop): 6// the face box must contain the four canthi and the nose, each eye box its iris centre; a flat field must yield no face; 7// the mirrored still must find the face and both eyes again. Values are printed with gv_kv. 8// license_tier: ORIGINAL No hw writes (Rule 26). 9import "nx_syscalls.nx" 10import "nx_gate_verdict.nx" 11import "nx_jpeg_ascii.nx" 12import "nx_facefind_lib.nx" 13 14const FG_I64: i64 = 8 15const FG_TOL_Q24: i64 = 3 16const FG_Q24_A: i64 = 0 - 528684 17const FG_Q24_B: i64 = 35023076 18const FG_Q24_C: i64 = 0 - 37198612 19const FG_Q24_D: i64 = 0 - 84599952 20const FG_Q24_M1: i64 = 0 - 16777216 21const FG_Q24_3: i64 = 50331648 22const FG_Q24_2: i64 = 33554432 23const FG_Q24_E: i64 = 1678 24const FG_INT_A: i64 = 12 25const FG_INT_B: i64 = 0 - 1 26const FG_FACE_WIN: i64 = 24 27const FG_FACE_STAGES: i64 = 25 28const FG_FACE_WEAK: i64 = 2913 29const FG_FACE_FEATS: i64 = 2913 30const FG_EYE_WIN: i64 = 20 31const FG_EYE_STAGES: i64 = 24 32const FG_EYE_WEAK: i64 = 1066 33const FG_FIX1: *u8 = "knowledge/fetched/ri_diora_baird_18.jpg" 34const FG_F1_W: i64 = 551 35const FG_F1_H: i64 = 1200 36const FG_F1_RLAT_X: i64 = 231 37const FG_F1_RLAT_Y: i64 = 190 38const FG_F1_RMED_X: i64 = 255 39const FG_F1_RMED_Y: i64 = 194 40const FG_F1_LMED_X: i64 = 288 41const FG_F1_LMED_Y: i64 = 196 42const FG_F1_LLAT_X: i64 = 323 43const FG_F1_LLAT_Y: i64 = 194 44const FG_F1_NOSE_X: i64 = 244 45const FG_F1_NOSE_Y: i64 = 231 46const FG_F1_IRIS_R_X: i64 = 248 47const FG_F1_IRIS_R_Y: i64 = 190 48const FG_F1_IRIS_L_X: i64 = 311 49const FG_F1_IRIS_L_Y: i64 = 192 50const FG_FIX2: *u8 = "knowledge/fetched/cmp_aesthetictwin_babepedia_diora_baird_pic1.jpg" 51const FG_F2_W: i64 = 1200 52const FG_F2_H: i64 = 1600 53const FG_F2_RLAT_X: i64 = 564 54const FG_F2_RLAT_Y: i64 = 252 55const FG_F2_LLAT_X: i64 = 694 56const FG_F2_LLAT_Y: i64 = 258 57const FG_F2_NOSE_X: i64 = 604 58const FG_F2_NOSE_Y: i64 = 324 59const FG_F2_IRIS_R_X: i64 = 586 60const FG_F2_IRIS_R_Y: i64 = 254 61const FG_F2_IRIS_L_X: i64 = 676 62const FG_F2_IRIS_L_Y: i64 = 258 63const FG_FLAT_W: i64 = 320 64const FG_FLAT_H: i64 = 400 65const FG_FLAT_V: i64 = 128 66const FG_FLAT_PERMIL: i64 = 100 67const FG_E_UNREADABLE: i64 = 0 - 1 68 69func fg_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } 70func fg_near(a: i64, b: i64, tol: i64) -> i64 { if fg_abs(a - b) <= tol { return 1 } return 0 } 71func fg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 72func fg_q24(s: *u8) -> i64 { 73 let out: *i64 = sys_mmap(2 * FG_I64) as *i64 74 if hx_num_q24(s, fg_slen(s), 0, out) == 0 { return 0 - 999999999 } 75 return out[0] 76} 77func fg_int(s: *u8) -> i64 { 78 let out: *i64 = sys_mmap(2 * FG_I64) as *i64 79 if hx_int(s, fg_slen(s), 0, out) == 0 { return 0 - 999999999 } 80 return out[0] 81} 82// a point inside a box (inclusive) 83func fg_inside(px: i64, py: i64, x: i64, y: i64, w: i64, h: i64) -> i64 { 84 if px < x { return 0 } 85 if py < y { return 0 } 86 if px > x + w { return 0 } 87 if py > y + h { return 0 } 88 return 1 89} 90// decode a still to its luma plane: out = plane, w, h, stride; returns the decoder's verdict or FG_E_UNREADABLE 91func fg_luma(path: *u8, out: *i64) -> i64 { 92 let fl: *i64 = sys_mmap(16) as *i64 93 fl[0] = 0 94 let jpeg: *u8 = sys_read_file(path, fl) 95 if (jpeg as i64) == 0 { return FG_E_UNREADABLE } 96 if fl[0] <= 0 { return FG_E_UNREADABLE } 97 let pp: *i64 = sys_mmap(FG_I64) as *i64 98 let pw: *i64 = sys_mmap(FG_I64) as *i64 99 let ph: *i64 = sys_mmap(FG_I64) as *i64 100 let ps: *i64 = sys_mmap(FG_I64) as *i64 101 let rc: i64 = nx_jpeg_decode_luma(jpeg, fl[0], pp, pw, ph, ps) 102 out[0] = pp[0] 103 out[1] = pw[0] 104 out[2] = ph[0] 105 out[3] = ps[0] 106 return rc 107} 108func fg_cat(dst: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { dst[o + i] = s[i]; i = i + 1 } dst[o + i] = 0 as u8; return o + i } 109func main() -> i64 { 110 let ctr: *i64 = gv_ctr() 111 // the number parser on the data's own spellings 112 gv_check("q24-negative-exponent-threshold" as *u8, fg_near(fg_q24("-3.1511999666690826e-02" as *u8), FG_Q24_A, FG_TOL_Q24), ctr) 113 gv_check("q24-positive-leaf" as *u8, fg_near(fg_q24("2.0875380039215088e+00" as *u8), FG_Q24_B, FG_TOL_Q24), ctr) 114 gv_check("q24-negative-leaf" as *u8, fg_near(fg_q24("-2.2172100543975830e+00" as *u8), FG_Q24_C, FG_TOL_Q24), ctr) 115 gv_check("q24-stage-threshold" as *u8, fg_near(fg_q24("-5.0425500869750977e+00" as *u8), FG_Q24_D, FG_TOL_Q24), ctr) 116 gv_check("q24-integer-weight-minus-one" as *u8, (fg_q24("-1." as *u8) == FG_Q24_M1) as i64, ctr) 117 gv_check("q24-integer-weight-three" as *u8, (fg_q24("3." as *u8) == FG_Q24_3) as i64, ctr) 118 gv_check("q24-integer-weight-two" as *u8, (fg_q24("2." as *u8) == FG_Q24_2) as i64, ctr) 119 gv_check("q24-tiny-value" as *u8, fg_near(fg_q24("1.0000000000000001e-04" as *u8), FG_Q24_E, FG_TOL_Q24), ctr) 120 gv_check("int-parser-positive" as *u8, (fg_int("12" as *u8) == FG_INT_A) as i64, ctr) 121 gv_check("int-parser-negative" as *u8, (fg_int(" -1" as *u8) == FG_INT_B) as i64, ctr) 122 // the loader on the two mirrored cascades 123 let rc: *i64 = sys_mmap(FG_I64) as *i64 124 let face_c: *HaarCascade = ff_load_face(rc) 125 gv_check("face-cascade-loads" as *u8, ((face_c as i64) != 0) as i64, ctr) 126 if (face_c as i64) == 0 { gv_kv("face_load_rc" as *u8, rc[0]); return gv_verdict("nx_facefind_gate" as *u8, ctr, "face cascade refused" as *u8) } 127 gv_check("face-window-24" as *u8, (face_c.hdr[HC_H_WINW] == FG_FACE_WIN) as i64 * (face_c.hdr[HC_H_WINH] == FG_FACE_WIN) as i64, ctr) 128 gv_check("face-25-stages" as *u8, (face_c.hdr[HC_H_NSTAGES] == FG_FACE_STAGES) as i64, ctr) 129 gv_check("face-2913-weak-classifiers" as *u8, (face_c.hdr[HC_H_NWEAK] == FG_FACE_WEAK) as i64, ctr) 130 gv_check("face-2913-features" as *u8, (face_c.hdr[HC_H_NFEATS] == FG_FACE_FEATS) as i64, ctr) 131 gv_check("face-stumps-one-node-per-classifier" as *u8, (face_c.hdr[HC_H_NNODES] == FG_FACE_WEAK) as i64 * (face_c.hdr[HC_H_NLEAVES] == 2 * FG_FACE_WEAK) as i64, ctr) 132 let eye_c: *HaarCascade = ff_load_eye(rc) 133 gv_check("eye-cascade-loads" as *u8, ((eye_c as i64) != 0) as i64, ctr) 134 if (eye_c as i64) == 0 { gv_kv("eye_load_rc" as *u8, rc[0]); return gv_verdict("nx_facefind_gate" as *u8, ctr, "eye cascade refused" as *u8) } 135 gv_check("eye-window-20" as *u8, (eye_c.hdr[HC_H_WINW] == FG_EYE_WIN) as i64, ctr) 136 gv_check("eye-24-stages" as *u8, (eye_c.hdr[HC_H_NSTAGES] == FG_EYE_STAGES) as i64, ctr) 137 gv_check("eye-1066-weak-classifiers" as *u8, (eye_c.hdr[HC_H_NWEAK] == FG_EYE_WEAK) as i64, ctr) 138 // planted refusals, assembled at run time 139 let bad: *u8 = sys_mmap(256) 140 fg_cat(bad, 0, "<opencv_storage><stages></stages></opencv_storage>" as *u8) 141 let rcb: *i64 = sys_mmap(FG_I64) as *i64 142 let cb: *HaarCascade = hx_parse(bad, fg_slen(bad), rcb) 143 gv_check("neg-control-no-cascade-root-refused-by-name" as *u8, ((cb as i64) == 0) as i64 * (rcb[0] == HX_E_NOT_CASCADE) as i64, ctr) 144 let tilt: *u8 = sys_mmap(256) 145 var o: i64 = fg_cat(tilt, 0, "<cascade><height>24</height><width>24</width><features><_><rects><_>0 0 4 4 -1.</_></rects><til" as *u8) 146 o = fg_cat(tilt, o, "ted>" as *u8) 147 o = fg_cat(tilt, o, "1</tilted></_></features></cascade>" as *u8) 148 let ct: *HaarCascade = hx_parse(tilt, o, rcb) 149 gv_check("neg-control-tilted-feature-refused-by-name" as *u8, ((ct as i64) == 0) as i64 * (rcb[0] == HX_E_TILTED) as i64, ctr) 150 // end to end: the oracle-placed still 151 let l1: *i64 = sys_mmap(4 * FG_I64) as *i64 152 let d1: i64 = fg_luma(FG_FIX1, l1) 153 gv_check("fixture-ri-18-decodes" as *u8, (d1 == NX_JPEG_ASCII_OK) as i64, ctr) 154 gv_check("fixture-ri-18-dims" as *u8, (l1[1] == FG_F1_W) as i64 * (l1[2] == FG_F1_H) as i64, ctr) 155 let r1: *i64 = sys_mmap(FF_R_N * FG_I64) as *i64 156 let f1: i64 = ff_find(face_c, eye_c, l1[0] as *u8, l1[1], l1[2], l1[3], FF_MIN_FACE_PERMIL, r1) 157 gv_check("ri-18-face-found" as *u8, (r1[FF_R_FACE] == 1) as i64, ctr) 158 gv_check("ri-18-face-box-contains-right-lateral-canthus" as *u8, fg_inside(FG_F1_RLAT_X, FG_F1_RLAT_Y, r1[FF_R_FX], r1[FF_R_FY], r1[FF_R_FW], r1[FF_R_FH]), ctr) 159 gv_check("ri-18-face-box-contains-right-medial-canthus" as *u8, fg_inside(FG_F1_RMED_X, FG_F1_RMED_Y, r1[FF_R_FX], r1[FF_R_FY], r1[FF_R_FW], r1[FF_R_FH]), ctr) 160 gv_check("ri-18-face-box-contains-left-medial-canthus" as *u8, fg_inside(FG_F1_LMED_X, FG_F1_LMED_Y, r1[FF_R_FX], r1[FF_R_FY], r1[FF_R_FW], r1[FF_R_FH]), ctr) 161 gv_check("ri-18-face-box-contains-left-lateral-canthus" as *u8, fg_inside(FG_F1_LLAT_X, FG_F1_LLAT_Y, r1[FF_R_FX], r1[FF_R_FY], r1[FF_R_FW], r1[FF_R_FH]), ctr) 162 gv_check("ri-18-face-box-contains-nose" as *u8, fg_inside(FG_F1_NOSE_X, FG_F1_NOSE_Y, r1[FF_R_FX], r1[FF_R_FY], r1[FF_R_FW], r1[FF_R_FH]), ctr) 163 gv_check("ri-18-both-eyes-found" as *u8, (f1 == FF_OK) as i64 * (r1[FF_R_EYES] == 2) as i64, ctr) 164 gv_check("ri-18-right-eye-box-contains-right-iris" as *u8, fg_inside(FG_F1_IRIS_R_X, FG_F1_IRIS_R_Y, r1[FF_R_RX], r1[FF_R_RY], r1[FF_R_RW], r1[FF_R_RH]), ctr) 165 gv_check("ri-18-left-eye-box-contains-left-iris" as *u8, fg_inside(FG_F1_IRIS_L_X, FG_F1_IRIS_L_Y, r1[FF_R_LX], r1[FF_R_LY], r1[FF_R_LW], r1[FF_R_LH]), ctr) 166 gv_check("ri-18-detection-table-not-capped" as *u8, (r1[FF_R_CAPPED] == 0) as i64, ctr) 167 // the mirrored still: the face again, the eyes swap sides 168 let mw: i64 = l1[1] 169 let mh: i64 = l1[2] 170 let src: *u8 = l1[0] as *u8 171 let mir: *u8 = sys_mmap(mw * mh + 16) 172 var y: i64 = 0 173 while y < mh { var x: i64 = 0; while x < mw { mir[y * mw + x] = src[y * l1[3] + (mw - 1 - x)]; x = x + 1 } y = y + 1 } 174 let r1m: *i64 = sys_mmap(FF_R_N * FG_I64) as *i64 175 let f1m: i64 = ff_find(face_c, eye_c, mir, mw, mh, mw, FF_MIN_FACE_PERMIL, r1m) 176 gv_check("mirrored-ri-18-face-and-eyes-found" as *u8, (f1m == FF_OK) as i64, ctr) 177 gv_check("mirrored-ri-18-right-eye-box-contains-mirrored-left-iris" as *u8, fg_inside(mw - 1 - FG_F1_IRIS_L_X, FG_F1_IRIS_L_Y, r1m[FF_R_RX], r1m[FF_R_RY], r1m[FF_R_RW], r1m[FF_R_RH]), ctr) 178 // the second still, larger, lighter irises 179 let l2: *i64 = sys_mmap(4 * FG_I64) as *i64 180 let d2: i64 = fg_luma(FG_FIX2, l2) 181 gv_check("fixture-bp1-decodes" as *u8, (d2 == NX_JPEG_ASCII_OK) as i64, ctr) 182 gv_check("fixture-bp1-dims" as *u8, (l2[1] == FG_F2_W) as i64 * (l2[2] == FG_F2_H) as i64, ctr) 183 let r2: *i64 = sys_mmap(FF_R_N * FG_I64) as *i64 184 let f2: i64 = ff_find(face_c, eye_c, l2[0] as *u8, l2[1], l2[2], l2[3], FF_MIN_FACE_PERMIL, r2) 185 gv_check("bp1-face-found" as *u8, (r2[FF_R_FACE] == 1) as i64, ctr) 186 gv_check("bp1-face-box-contains-both-lateral-canthi-and-nose" as *u8, fg_inside(FG_F2_RLAT_X, FG_F2_RLAT_Y, r2[FF_R_FX], r2[FF_R_FY], r2[FF_R_FW], r2[FF_R_FH]) * fg_inside(FG_F2_LLAT_X, FG_F2_LLAT_Y, r2[FF_R_FX], r2[FF_R_FY], r2[FF_R_FW], r2[FF_R_FH]) * fg_inside(FG_F2_NOSE_X, FG_F2_NOSE_Y, r2[FF_R_FX], r2[FF_R_FY], r2[FF_R_FW], r2[FF_R_FH]), ctr) 187 gv_check("bp1-both-eyes-found" as *u8, (f2 == FF_OK) as i64, ctr) 188 gv_check("bp1-right-eye-box-contains-right-iris" as *u8, fg_inside(FG_F2_IRIS_R_X, FG_F2_IRIS_R_Y, r2[FF_R_RX], r2[FF_R_RY], r2[FF_R_RW], r2[FF_R_RH]), ctr) 189 gv_check("bp1-left-eye-box-contains-left-iris" as *u8, fg_inside(FG_F2_IRIS_L_X, FG_F2_IRIS_L_Y, r2[FF_R_LX], r2[FF_R_LY], r2[FF_R_LW], r2[FF_R_LH]), ctr) 190 // neg-control: a flat field has no face 191 let flat: *u8 = sys_mmap(FG_FLAT_W * FG_FLAT_H + 16) 192 var i: i64 = 0 193 while i < FG_FLAT_W * FG_FLAT_H { flat[i] = FG_FLAT_V as u8; i = i + 1 } 194 let r3: *i64 = sys_mmap(FF_R_N * FG_I64) as *i64 195 let f3: i64 = ff_find(face_c, eye_c, flat, FG_FLAT_W, FG_FLAT_H, FG_FLAT_W, FG_FLAT_PERMIL, r3) 196 gv_check("neg-control-flat-field-no-face" as *u8, (f3 == FF_E_NOFACE) as i64 * (r3[FF_R_GROUPS] == 0) as i64, ctr) 197 gv_values_head() 198 gv_kv("face_stages" as *u8, face_c.hdr[HC_H_NSTAGES]); gv_kv("face_weak" as *u8, face_c.hdr[HC_H_NWEAK]); gv_kv("face_nodes" as *u8, face_c.hdr[HC_H_NNODES]); gv_kv("face_leaves" as *u8, face_c.hdr[HC_H_NLEAVES]) 199 gv_kv("eye_stages" as *u8, eye_c.hdr[HC_H_NSTAGES]); gv_kv("eye_weak" as *u8, eye_c.hdr[HC_H_NWEAK]); gv_kv("eye_nodes" as *u8, eye_c.hdr[HC_H_NNODES]) 200 gv_kv("ri18_raw" as *u8, r1[FF_R_RAW]); gv_kv("ri18_groups" as *u8, r1[FF_R_GROUPS]); gv_kv("ri18_face_x" as *u8, r1[FF_R_FX]); gv_kv("ri18_face_y" as *u8, r1[FF_R_FY]); gv_kv("ri18_face_w" as *u8, r1[FF_R_FW]); gv_kv("ri18_face_h" as *u8, r1[FF_R_FH]); gv_kv("ri18_votes" as *u8, r1[FF_R_FVOTES]) 201 gv_kv("ri18_eye_raw" as *u8, r1[FF_R_EYE_RAW]); gv_kv("ri18_eye_groups" as *u8, r1[FF_R_EYE_GROUPS]); gv_kv("ri18_R_x" as *u8, r1[FF_R_RX]); gv_kv("ri18_R_y" as *u8, r1[FF_R_RY]); gv_kv("ri18_R_w" as *u8, r1[FF_R_RW]); gv_kv("ri18_L_x" as *u8, r1[FF_R_LX]); gv_kv("ri18_L_y" as *u8, r1[FF_R_LY]); gv_kv("ri18_L_w" as *u8, r1[FF_R_LW]) 202 gv_kv("bp1_raw" as *u8, r2[FF_R_RAW]); gv_kv("bp1_groups" as *u8, r2[FF_R_GROUPS]); gv_kv("bp1_face_x" as *u8, r2[FF_R_FX]); gv_kv("bp1_face_y" as *u8, r2[FF_R_FY]); gv_kv("bp1_face_w" as *u8, r2[FF_R_FW]); gv_kv("bp1_face_h" as *u8, r2[FF_R_FH]); gv_kv("bp1_votes" as *u8, r2[FF_R_FVOTES]) 203 gv_kv("bp1_eye_raw" as *u8, r2[FF_R_EYE_RAW]); gv_kv("bp1_eye_groups" as *u8, r2[FF_R_EYE_GROUPS]); gv_kv("bp1_R_x" as *u8, r2[FF_R_RX]); gv_kv("bp1_R_y" as *u8, r2[FF_R_RY]); gv_kv("bp1_R_w" as *u8, r2[FF_R_RW]); gv_kv("bp1_L_x" as *u8, r2[FF_R_LX]); gv_kv("bp1_L_y" as *u8, r2[FF_R_LY]); gv_kv("bp1_L_w" as *u8, r2[FF_R_LW]) 204 gv_kv("flat_raw" as *u8, r3[FF_R_RAW]); gv_kv("mirror_rc" as *u8, f1m) 205 return gv_verdict("nx_facefind_gate" as *u8, ctr, "the Q24 parser on the data's own spellings, the loader's counts on both mirrored cascades, two planted refusals, the face box and both eye boxes containing the oracle's canthi, nose and irises on two stills, the mirrored still found again, and a flat field with no face" as *u8) 206}