code wiki / (root) / nx_pose_keypoints.nx

nx_pose_keypoints.nx source

↩ module page · 59 lines · 2956 B

1// nx_pose_keypoints.nx -- T2 POSE, the WEIGHTS-FREE back half of a heatmap pose estimator (HRNet/OpenPose-class, 2// per the banked primary literature knowledge/library/vidclass_lit_pose.txt: heatmap -> keypoint, part-affinity). 3// A pose CNN emits one confidence HEATMAP per joint; this extracts the keypoint = argmax location + the standard 4// HRNet QUARTER-PIXEL subpixel refinement (shift 0.25px toward the higher neighbor). Pure integer, deterministic, 5// gate-able, and reusable under ANY heatmap producer -- so it composes with the future sovereign f32 pose CNN 6// (nx_f32_conv2d/resblock/groupnorm substrate confirmed present) WITHOUT needing its weights. Coordinates are 7// returned in QUARTER-PIXEL units (pixel*4 + {-1,0,+1}); divide by 4 for pixels. license_tier: ORIGINAL 8import "nx_syscalls.nx" 9 10// peak location of ONE heatmap (row-major W*H). fills *px,*py with the integer peak cell; returns its value (conf). 11func pose_argmax(hm: *i64, W: i64, H: i64, px: *i64, py: *i64) -> i64 { 12 var best: i64 = hm[0]; var bx: i64 = 0; var by: i64 = 0 13 var y: i64 = 0 14 while y < H { 15 var x: i64 = 0 16 while x < W { 17 let v: i64 = hm[y*W + x] 18 if v > best { best = v; bx = x; by = y } 19 x = x + 1 20 } 21 y = y + 1 22 } 23 px[0] = bx; py[0] = by 24 return best 25} 26// one keypoint: peak + quarter-pixel refinement. conf < thresh -> (-1,-1) conf 0 (joint not visible / occluded). 27func pose_keypoint(hm: *i64, W: i64, H: i64, thresh: i64, qx: *i64, qy: *i64) -> i64 { 28 let pxb: *i64 = sys_mmap(8) as *i64; let pyb: *i64 = sys_mmap(8) as *i64 29 let conf: i64 = pose_argmax(hm, W, H, pxb, pyb) 30 let bx: i64 = pxb[0]; let by: i64 = pyb[0] 31 if conf < thresh { qx[0] = 0 - 1; qy[0] = 0 - 1; return 0 } 32 var sx: i64 = 0 33 if bx > 0 { if bx < W - 1 { 34 if hm[by*W + bx+1] > hm[by*W + bx-1] { sx = 1 } 35 if hm[by*W + bx+1] < hm[by*W + bx-1] { sx = 0 - 1 } 36 } } 37 var sy: i64 = 0 38 if by > 0 { if by < H - 1 { 39 if hm[(by+1)*W + bx] > hm[(by-1)*W + bx] { sy = 1 } 40 if hm[(by+1)*W + bx] < hm[(by-1)*W + bx] { sy = 0 - 1 } 41 } } 42 qx[0] = bx*4 + sx; qy[0] = by*4 + sy 43 return conf 44} 45// J stacked heatmaps (each W*H, contiguous) -> J keypoints (quarter-pixel) + confidences. Returns the count of 46// CONFIDENT joints (conf >= thresh) = a coarse skeleton-validity signal for the downstream position/dance classifier. 47func pose_extract(heatmaps: *i64, J: i64, W: i64, H: i64, thresh: i64, out_qx: *i64, out_qy: *i64, out_conf: *i64) -> i64 { 48 var nvis: i64 = 0 49 let qxb: *i64 = sys_mmap(8) as *i64; let qyb: *i64 = sys_mmap(8) as *i64 50 var j: i64 = 0 51 while j < J { 52 let hm: *i64 = ((heatmaps as i64) + j*W*H*8) as *i64 53 let c: i64 = pose_keypoint(hm, W, H, thresh, qxb, qyb) 54 out_qx[j] = qxb[0]; out_qy[j] = qyb[0]; out_conf[j] = c 55 if c >= thresh { nvis = nvis + 1 } 56 j = j + 1 57 } 58 return nvis 59}