code wiki / (root) / nx_media_run.nx

nx_media_run.nx source

↩ module page · 95 lines · 4133 B

1// nx_media_run.nx -- EXPOSURE rung X0: a real, runnable tool (not a KAT). 2// Reads a real image file from the staging path, runs person-presence (R2) on 3// it, prints a verdict + metrics, and writes an ANNOTATED image (skin bounding 4// box drawn in green) you can open and see. File-driven, like nx_write_run. 5// 6// input : knowledge/staging/media/frame.ppm (binary PPM) 7// output: stdout + knowledge/staging/media/out.txt (verdict + metrics) 8// knowledge/staging/media/annotated.ppm (input + bbox overlay) 9// 10// This is how the capability is USED: drop a real .ppm at the input path, run 11// ./_offc/nx_sov_build_run.elf nx_media_run 12// and read the verdict / open annotated.ppm. 13// Sovereign: syscalls + nx_image + nx_ppm + nx_presence. 14// license_tier: ORIGINAL 15import "syscalls.nx" 16import "nx_image.nx" 17import "nx_ppm.nx" 18import "nx_presence.nx" 19 20const MIN_SKIN: i64 = 50 21const MIN_CONC: i64 = 500 22 23func gp(logfd: i64, s: *u8) -> i64 { 24 var n: i64 = 0 25 while s[n] != (0 as u8) { n = n + 1 } 26 sys_write(1, s, n) 27 if logfd > 0 { sys_write(logfd, s, n) } 28 return 0 29} 30func gn(logfd: i64, v: i64) -> i64 { 31 let bb: *u8 = sys_mmap(28) 32 var m: i64 = v 33 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m } 34 let t: *u8 = sys_mmap(28) 35 var k: i64 = 0 36 if m == 0 { t[0] = 48 as u8; k = 1 } 37 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 38 var i: i64 = 0 39 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 40 sys_write(1, bb, k) 41 if logfd > 0 { sys_write(logfd, bb, k) } 42 return 0 43} 44 45// draw a green rectangle outline at [x0,y0]..[x1,y1] 46func draw_box(img: *Image, x0: i64, y0: i64, x1: i64, y1: i64) -> i64 { 47 var x: i64 = x0 48 while x <= x1 { 49 nx_image_set(img, x, y0, 0, 0); nx_image_set(img, x, y0, 1, 255); nx_image_set(img, x, y0, 2, 0) 50 nx_image_set(img, x, y1, 0, 0); nx_image_set(img, x, y1, 1, 255); nx_image_set(img, x, y1, 2, 0) 51 x = x + 1 52 } 53 var y: i64 = y0 54 while y <= y1 { 55 nx_image_set(img, x0, y, 0, 0); nx_image_set(img, x0, y, 1, 255); nx_image_set(img, x0, y, 2, 0) 56 nx_image_set(img, x1, y, 0, 0); nx_image_set(img, x1, y, 1, 255); nx_image_set(img, x1, y, 2, 0) 57 y = y + 1 58 } 59 return 0 60} 61 62func main() -> i64 { 63 let logfd: i64 = sys_openat_wr("knowledge/staging/media/out.txt\x00" as *u8, 420) 64 let szp: *i64 = sys_mmap(8) as *i64 65 let buf: *u8 = sys_read_file("knowledge/staging/media/frame.ppm\x00" as *u8, szp) 66 if (buf as i64) == 0 { 67 gp(logfd, "nx_media_run: NO INPUT at knowledge/staging/media/frame.ppm\n\x00" as *u8) 68 if logfd > 0 { sys_close(logfd) } 69 return 1 70 } 71 let img: *Image = ppm_parse(buf, szp[0]) 72 if (img as i64) == 0 { 73 gp(logfd, "nx_media_run: BAD PPM (not P6)\n\x00" as *u8) 74 if logfd > 0 { sys_close(logfd) } 75 return 2 76 } 77 let out: *i64 = sys_mmap(128) as *i64 78 nx_presence_scan(img, out) 79 let cls: i64 = nx_presence_classify(out[0], out[1], MIN_SKIN, MIN_CONC) 80 81 gp(logfd, "nx_media_run frame=\x00" as *u8); gn(logfd, img.width); gp(logfd, "x\x00" as *u8); gn(logfd, img.height) 82 gp(logfd, " bytes=\x00" as *u8); gn(logfd, szp[0]) 83 gp(logfd, " skin=\x00" as *u8); gn(logfd, out[0]); gp(logfd, "permille conc=\x00" as *u8); gn(logfd, out[1]) 84 gp(logfd, "permille n=\x00" as *u8); gn(logfd, out[2]) 85 gp(logfd, " bbox=[\x00" as *u8); gn(logfd, out[3]); gp(logfd, ",\x00" as *u8); gn(logfd, out[4]) 86 gp(logfd, "..\x00" as *u8); gn(logfd, out[5]); gp(logfd, ",\x00" as *u8); gn(logfd, out[6]); gp(logfd, "] -> \x00" as *u8) 87 if cls == NX_PRES_PRESENT { gp(logfd, "PERSON PRESENT\n\x00" as *u8) } 88 if cls == NX_PRES_ABSENT { gp(logfd, "NO PERSON (dead-air)\n\x00" as *u8) } 89 90 if out[2] > 0 { draw_box(img, out[3], out[4], out[5], out[6]) } 91 let wr: i64 = ppm_write(img, "knowledge/staging/media/annotated.ppm\x00" as *u8) 92 gp(logfd, "nx_media_run wrote annotated.ppm rc=\x00" as *u8); gn(logfd, wr); gp(logfd, "\n\x00" as *u8) 93 if logfd > 0 { sys_close(logfd) } 94 return 0 95}