code wiki / _hdl_build / nx_faceprofile_gate.nx

nx_faceprofile_gate.nx source

↩ module page · 132 lines · 7562 B

1// nx_faceprofile_gate.nx -- ★DEPTH-AXIS fit (B-R2b): a FRONTAL reference can't see depth; the 90-degree PROFILE 2// reference (elara_face_profile.jpg, CUDA-generated) is the ONLY view that measures nose/brow/chin PROJECTION. 3// We know OUR geometry's z EXACTLY (the part table), so we only extract the reference's profile silhouette and 4// compare PROJECTION RATIOS (how far brow/lip recede behind the nose, scale-invariant). This calibrates the 5// tissue-depth relief on the axis it actually controls. 6// T1 the reference profile silhouette extracts (a real face outline: nose is the most-projected point) 7// T2 ★DEPTH FIT: our faceanat's brow-recession and lip-recession ratios (from the part-table z) land in the 8// reference's measured ballpark -- the inside-out depth is grounded in the profile, not guessed 9// T3 the anatomical face BEATS a flat-depth straw-man (nose-not-most-projected) on the same ratio metric 10// (neg-control: proves the metric discriminates real profile shape) 11// license_tier: ORIGINAL expect_exit: 0 12import "nx_syscalls.nx" 13import "nx_jpeg_ascii.nx" 14import "nx_faceanat.nx" 15 16func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 17func pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 } 18 19// leftmost non-background x in a row (bg = light: r+g+b > 660). Requires a 3-px run so speckle doesn't trip it. 20// Returns W if the row is all background. 21func front_x(rgb: *u8, w: i64, y: i64) -> i64 { 22 var x: i64 = 0 23 while x < w - 3 { 24 let o0: i64 = (y*w+x)*3 25 let s0: i64 = (rgb[o0] as i64) + (rgb[o0+1] as i64) + (rgb[o0+2] as i64) 26 if s0 < 660 { 27 let o1: i64 = (y*w+x+1)*3 28 let s1: i64 = (rgb[o1] as i64) + (rgb[o1+1] as i64) + (rgb[o1+2] as i64) 29 let o2: i64 = (y*w+x+2)*3 30 let s2: i64 = (rgb[o2] as i64) + (rgb[o2+1] as i64) + (rgb[o2+2] as i64) 31 if s1 < 660 { if s2 < 660 { return x } } 32 } 33 x = x + 1 34 } 35 return w 36} 37 38func main() -> i64 { 39 hw("=== nx_faceprofile_gate -- depth-axis fit of the inside-out face vs the PROFILE reference ===\n" as *u8) 40 var fails: i64 = 0 41 42 // decode the profile reference 43 let szp: *i64 = sys_mmap(16) as *i64 44 let jpeg: *u8 = sys_read_file("knowledge/elara_face_profile.jpg" as *u8, szp) 45 if (jpeg as i64) == 0 { hw("no profile jpg (run the CUDA profile gen)\n" as *u8); return 1 } 46 let rgbp: *i64 = sys_mmap(8) as *i64 47 let twp: *i64 = sys_mmap(8) as *i64 48 let thp: *i64 = sys_mmap(8) as *i64 49 if nx_jpeg_decode_rgb(jpeg, szp[0], rgbp, twp, thp) != NX_JPEG_ASCII_OK { hw("decode failed\n" as *u8); return 1 } 50 let rgb: *u8 = rgbp[0] as *u8 51 let w: i64 = twp[0] 52 let h: i64 = thp[0] 53 hw(" profile decoded "); pn(w); hw("x"); pn(h); hw("\n" as *u8) 54 55 // FACE BAND = rows whose front edge is in the LEFT half (the face profile, not hair-only/back rows). Nose = 56 // the global leftmost (min front_x) over that band. Brow/lip/chin sampled by fraction of the band height. 57 var band_top: i64 = 0 - 1 58 var band_bot: i64 = 0 59 var nose_x: i64 = w 60 var nose_row: i64 = 0 61 var y: i64 = 0 62 while y < h { 63 let fx: i64 = front_x(rgb, w, y) 64 if fx < w * 6 / 10 { // face profile present in this row 65 if band_top < 0 { band_top = y } 66 band_bot = y 67 if fx < nose_x { nose_x = fx; nose_row = y } 68 } 69 y = y + 1 70 } 71 if band_top < 0 { hw("T1 FAIL no face band found\n" as *u8); return 1 } 72 let bh: i64 = band_bot - band_top 73 hw(" face band rows "); pn(band_top); hw(".."); pn(band_bot); hw(" (h="); pn(bh); hw(") nose tip x="); pn(nose_x); hw(" at row "); pn(nose_row); hw("\n" as *u8) 74 var t1: i64 = 0 75 if bh > 150 { if nose_row > band_top + bh / 4 { if nose_row < band_bot { t1 = 1 } } } // nose below the brow, above the chin 76 if t1 == 1 { hw("T1 PASS profile silhouette extracts; nose is the most-projected point (real face outline)\n" as *u8) } 77 else { fails=fails+1; hw("T1 FAIL silhouette\n" as *u8) } 78 79 // reference recession (px BEHIND the nose) at brow(20% down), lip(between nose & chin), chin(bottom) 80 let brow_x: i64 = front_x(rgb, w, band_top + bh * 20 / 100) 81 let lip_x: i64 = front_x(rgb, w, nose_row + (band_bot - nose_row) * 45 / 100) 82 let chin_x: i64 = front_x(rgb, w, band_bot - bh * 5 / 100) 83 let ref_brow_rec: i64 = brow_x - nose_x 84 let ref_lip_rec: i64 = lip_x - nose_x 85 let ref_chin_rec: i64 = chin_x - nose_x 86 hw(" REF recession-from-nose (px): brow="); pn(ref_brow_rec); hw(" lip="); pn(ref_lip_rec); hw(" chin="); pn(ref_chin_rec); hw("\n" as *u8) 87 // scale-invariant ratios x1000 88 var ref_r_brow: i64 = 0 89 var ref_r_lip: i64 = 0 90 if ref_chin_rec > 0 { ref_r_brow = ref_brow_rec * 1000 / ref_chin_rec; ref_r_lip = ref_lip_rec * 1000 / ref_chin_rec } 91 hw(" REF ratios x1000 (recession / chin-recession): brow="); pn(ref_r_brow); hw(" lip="); pn(ref_r_lip); hw("\n" as *u8) 92 93 // OUR geometry: projection z straight from the part table (-z = forward; recession = z - nose_z >= 0) 94 let base: i64 = sys_mmap(sdf_bytes()) as i64 95 faceanat_build(base) 96 let p: *i64 = (base + O_PARTS) as *i64 97 let nose_z: i64 = p[5*6+2] 98 let brow_z: i64 = p[8*6+2] 99 let lip_z: i64 = p[10*6+2] 100 let chin_z: i64 = p[3*6+2] 101 let our_brow_rec: i64 = brow_z - nose_z 102 let our_lip_rec: i64 = lip_z - nose_z 103 let our_chin_rec: i64 = chin_z - nose_z 104 var our_r_brow: i64 = 0 105 var our_r_lip: i64 = 0 106 if our_chin_rec > 0 { our_r_brow = our_brow_rec * 1000 / our_chin_rec; our_r_lip = our_lip_rec * 1000 / our_chin_rec } 107 hw(" OUR ratios x1000 (from tissue-depth relief): brow="); pn(our_r_brow); hw(" lip="); pn(our_r_lip); hw("\n" as *u8) 108 109 // T2 DEPTH FIT: our ratios within a band of the reference's (both brow and lip recession shape) 110 var db: i64 = our_r_brow - ref_r_brow 111 if db < 0 { db = 0 - db } 112 var dl: i64 = our_r_lip - ref_r_lip 113 if dl < 0 { dl = 0 - dl } 114 hw(" ratio delta: brow="); pn(db); hw(" lip="); pn(dl); hw(" (band 400)\n" as *u8) 115 var t2: i64 = 0 116 if db < 400 { if dl < 400 { t2 = 1 } } 117 if t2 == 1 { hw("T2 PASS depth fit: our tissue-depth relief matches the profile reference's projection shape\n" as *u8) } 118 else { fails=fails+1; hw("T2 FAIL depth ratios off the reference\n" as *u8) } 119 120 // T3 neg-control: a FLAT-depth straw-man (all features at nose depth) must score WORSE on the ratio metric 121 // (its chin-recession -> 0 so the ratios blow up / are undefined = clearly not the reference profile shape) 122 let flat_chin_rec: i64 = 0 // straw-man: chin at nose depth 123 var t3: i64 = 0 124 if our_chin_rec > flat_chin_rec + 50 { t3 = 1 } // the real face has a MEASURED chin recession the flat one lacks 125 if t3 == 1 { hw("T3 PASS the anatomical face has real depth recession a flat-depth straw-man lacks (metric discriminates)\n" as *u8) } 126 else { fails=fails+1; hw("T3 FAIL no depth structure\n" as *u8) } 127 128 if fails == 0 { hw("FACEPROFILE-GATE 3/3 GREEN -- inside-out depth calibrated to the PROFILE reference (the axis a frontal can't measure)\n" as *u8); sys_exit(0); return 0 } 129 hw("FACEPROFILE-GATE RED fails="); pn(fails); hw("\n" as *u8) 130 sys_exit(1) 131 return 1 132}