code wiki / (root) / nx_presence_gate.nx

nx_presence_gate.nx source

↩ module page · 174 lines · 6820 B

1// nx_presence_gate.nx -- REFEREE for MEDIA-STUDIO R2 (nx_presence). 2// 3// 64x64 RGB frames, neutral-gray background (128,128,128), skin = (200,120,90). 4// PERSON : a 32x32 skin blob -> high skin + high concentration -> PRESENT. 5// EMPTY : bare gray room -> ~0 skin -> ABSENT. 6// OBJECT : a 32x32 BLUE block (40,80,200) moving in -> not skin -> ABSENT. 7// (operator's case: something moving but NOT a person = dead air.) 8// SCATTER : the SAME number of skin pixels as PERSON, sprinkled across the 9// whole frame. NEG-CONTROL: equal skin fraction but low 10// concentration -> ABSENT. Proves presence needs a coherent blob, 11// not merely skin-coloured pixels (metric != raw skin count). 12// Plus unit checks of the skin classifier on known colours. 13// 14// Every measured value PRINTED. stdout + knowledge/status/presence_gate.log. 15// Exit 0 GREEN / 1 RED. Sovereign: syscalls + nx_image + nx_presence. 16// license_tier: ORIGINAL 17import "syscalls.nx" 18import "nx_image.nx" 19import "nx_presence.nx" 20 21const W: i64 = 64 22const H: i64 = 64 23const MIN_SKIN: i64 = 50 24const MIN_CONC: i64 = 500 25 26func gp(logfd: i64, s: *u8) -> i64 { 27 var n: i64 = 0 28 while s[n] != (0 as u8) { n = n + 1 } 29 sys_write(1, s, n) 30 if logfd > 0 { sys_write(logfd, s, n) } 31 return 0 32} 33func gn(logfd: i64, v: i64) -> i64 { 34 let bb: *u8 = sys_mmap(28) 35 var m: i64 = v 36 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m } 37 let t: *u8 = sys_mmap(28) 38 var k: i64 = 0 39 if m == 0 { t[0] = 48 as u8; k = 1 } 40 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 41 var i: i64 = 0 42 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 43 sys_write(1, bb, k) 44 if logfd > 0 { sys_write(logfd, bb, k) } 45 return 0 46} 47 48func fill_rgb(img: *Image, r: i64, g: i64, b: i64) -> i64 { 49 var y: i64 = 0 50 while y < H { 51 var x: i64 = 0 52 while x < W { 53 nx_image_set(img, x, y, 0, r) 54 nx_image_set(img, x, y, 1, g) 55 nx_image_set(img, x, y, 2, b) 56 x = x + 1 57 } 58 y = y + 1 59 } 60 return 0 61} 62func block_rgb(img: *Image, x0: i64, y0: i64, bw: i64, bh: i64, r: i64, g: i64, b: i64) -> i64 { 63 var y: i64 = y0 64 while y < y0 + bh { 65 var x: i64 = x0 66 while x < x0 + bw { 67 nx_image_set(img, x, y, 0, r) 68 nx_image_set(img, x, y, 1, g) 69 nx_image_set(img, x, y, 2, b) 70 x = x + 1 71 } 72 y = y + 1 73 } 74 return 0 75} 76// sprinkle `count` skin pixels at LCG-random positions across the whole frame 77func scatter_skin(img: *Image, count: i64, seed: i64) -> i64 { 78 var s: i64 = seed 79 var i: i64 = 0 80 while i < count { 81 s = (s * 1103515245 + 12345) & 0x7fffffff 82 let x: i64 = (s * W) >> 31 // high bits, not low (LCG LSBs are poor) 83 s = (s * 1103515245 + 12345) & 0x7fffffff 84 let y: i64 = (s * H) >> 31 85 nx_image_set(img, x, y, 0, 200) 86 nx_image_set(img, x, y, 1, 120) 87 nx_image_set(img, x, y, 2, 90) 88 i = i + 1 89 } 90 return 0 91} 92 93func report(logfd: i64, label: *u8, o: *i64, cls: i64) -> i64 { 94 gp(logfd, label) 95 gp(logfd, " skin=\x00" as *u8); gn(logfd, o[0]) 96 gp(logfd, "permille conc=\x00" as *u8); gn(logfd, o[1]) 97 gp(logfd, "permille (n=\x00" as *u8); gn(logfd, o[2]); gp(logfd, ") -> \x00" as *u8) 98 if cls == NX_PRES_PRESENT { gp(logfd, "PRESENT\x00" as *u8) } 99 if cls == NX_PRES_ABSENT { gp(logfd, "ABSENT\x00" as *u8) } 100 gp(logfd, "\n\x00" as *u8) 101 return 0 102} 103 104func main() -> i64 { 105 let logfd: i64 = sys_openat_append("knowledge/status/presence_gate.log\x00" as *u8, 0x1a4) 106 gp(logfd, "PRESENCE-GATE MEDIA-STUDIO R2 frame=\x00" as *u8); gn(logfd, W); gp(logfd, "x\x00" as *u8); gn(logfd, H) 107 gp(logfd, " min_skin=\x00" as *u8); gn(logfd, MIN_SKIN) 108 gp(logfd, " min_conc=\x00" as *u8); gn(logfd, MIN_CONC); gp(logfd, "permille\n\x00" as *u8) 109 110 let imp: *Image = nx_image_alloc(W, H, 3) // person 111 let ime: *Image = nx_image_alloc(W, H, 3) // empty 112 let imo: *Image = nx_image_alloc(W, H, 3) // object (non-person) 113 let ims: *Image = nx_image_alloc(W, H, 3) // scatter 114 115 fill_rgb(imp, 128, 128, 128); block_rgb(imp, 16, 16, 32, 32, 200, 120, 90) 116 fill_rgb(ime, 128, 128, 128) 117 fill_rgb(imo, 128, 128, 128); block_rgb(imo, 16, 16, 32, 32, 40, 80, 200) 118 fill_rgb(ims, 128, 128, 128); scatter_skin(ims, 1024, 1234567) 119 120 let op: *i64 = sys_mmap(128) as *i64 121 let oe: *i64 = sys_mmap(128) as *i64 122 let oo: *i64 = sys_mmap(128) as *i64 123 let os: *i64 = sys_mmap(128) as *i64 124 125 nx_presence_scan(imp, op) 126 let cp: i64 = nx_presence_classify(op[0], op[1], MIN_SKIN, MIN_CONC) 127 report(logfd, " person \x00" as *u8, op, cp) 128 129 nx_presence_scan(ime, oe) 130 let ce: i64 = nx_presence_classify(oe[0], oe[1], MIN_SKIN, MIN_CONC) 131 report(logfd, " empty \x00" as *u8, oe, ce) 132 133 nx_presence_scan(imo, oo) 134 let co: i64 = nx_presence_classify(oo[0], oo[1], MIN_SKIN, MIN_CONC) 135 report(logfd, " object \x00" as *u8, oo, co) 136 137 nx_presence_scan(ims, os) 138 let cs: i64 = nx_presence_classify(os[0], os[1], MIN_SKIN, MIN_CONC) 139 report(logfd, " scatter\x00" as *u8, os, cs) 140 141 // skin classifier unit checks 142 let u_skin: i64 = nx_skin_is_pixel(200, 120, 90) 143 let u_gray: i64 = nx_skin_is_pixel(128, 128, 128) 144 let u_blue: i64 = nx_skin_is_pixel(40, 80, 200) 145 gp(logfd, " unit skin(200,120,90)=\x00" as *u8); gn(logfd, u_skin) 146 gp(logfd, " gray(128)=\x00" as *u8); gn(logfd, u_gray) 147 gp(logfd, " blue=\x00" as *u8); gn(logfd, u_blue); gp(logfd, "\n\x00" as *u8) 148 149 // ---- verdict ---- 150 var ok: i64 = 1 151 if cp != NX_PRES_PRESENT { ok = 0 } // person blob = present 152 if op[0] < MIN_SKIN { ok = 0 } 153 if op[1] < MIN_CONC { ok = 0 } 154 if ce != NX_PRES_ABSENT { ok = 0 } // empty room = absent 155 if co != NX_PRES_ABSENT { ok = 0 } // moving non-person = absent 156 if oo[0] >= MIN_SKIN { ok = 0 } // ...because it has no skin 157 if cs != NX_PRES_ABSENT { ok = 0 } // scattered skin = absent 158 if os[0] < MIN_SKIN { ok = 0 } // ...even though skin fraction passes 159 if op[1] <= os[1] { ok = 0 } // person blob far more concentrated than speckle 160 if u_skin != 1 { ok = 0 } 161 if u_gray != 0 { ok = 0 } 162 if u_blue != 0 { ok = 0 } 163 164 if ok == 1 { 165 gp(logfd, "PRESENCE-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8) 166 if logfd > 0 { sys_close(logfd) } 167 sys_exit(0) 168 return 0 169 } 170 gp(logfd, "PRESENCE-GATE result=FAIL verdict=RED\n\x00" as *u8) 171 if logfd > 0 { sys_close(logfd) } 172 sys_exit(1) 173 return 1 174}