selftest_edge_v2.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // P1b/P1c 自检:v2 证据分类 + commit 安全门。
  2. //
  3. // 运行:
  4. // ctest --test-dir build -R portcv_edge_v2_selftest --output-on-failure
  5. // ./build/portcv_edge_v2_selftest
  6. //
  7. // 覆盖:
  8. // * A 类(门板立柱 + 不透明墙)→ 1;
  9. // * B 类(无立柱 + 车面边梁)→ 2;
  10. // * A 的 9–12 px 间隙不提交;B 的 3–5 px 间隙可提交;
  11. // * 贴合车面 gap=0 且无 px_per_m 时仍可提交(侧视 X70A);
  12. // * allow_state2=false / motion / PLC 到位信号 / reset。
  13. #include <cstdio>
  14. #include <string>
  15. #include "portcv/edge_v2.hpp"
  16. namespace {
  17. int g_pass = 0;
  18. int g_fail = 0;
  19. void check(bool ok, const std::string& what) {
  20. if (ok) {
  21. ++g_pass;
  22. std::printf(" [OK] %s\n", what.c_str());
  23. } else {
  24. ++g_fail;
  25. std::printf(" [FAIL] %s\n", what.c_str());
  26. }
  27. }
  28. void check_eq(long long got, long long want, const std::string& what) {
  29. if (got == want) {
  30. ++g_pass;
  31. std::printf(" [OK] %s (=%lld)\n", what.c_str(), got);
  32. } else {
  33. ++g_fail;
  34. std::printf(" [FAIL] %s: got=%lld want=%lld\n", what.c_str(), got, want);
  35. }
  36. }
  37. void fill_rect(portcv::GrayImage* img, int x0, int y0, int x1, int y1, uint8_t value) {
  38. for (int y = y0; y < y1; ++y) {
  39. for (int x = x0; x < x1; ++x) { img->set(x, y, value); }
  40. }
  41. }
  42. portcv::GrayImage bed_scene(int gap_px = 18) {
  43. portcv::GrayImage img(1280, 720, 120);
  44. const int box_bottom = 392;
  45. fill_rect(&img, 0, 40, 1280, box_bottom, 185);
  46. fill_rect(&img, 0, box_bottom, 1280, box_bottom + 12, 55);
  47. const int wall_top = box_bottom + 12 + gap_px;
  48. fill_rect(&img, 0, box_bottom + 12, 1280, wall_top, 88);
  49. fill_rect(&img, 0, wall_top, 1280, wall_top + 4, 215);
  50. fill_rect(&img, 0, wall_top + 4, 1280, 720, 125);
  51. for (int x = 120; x < 1180; x += 110) {
  52. fill_rect(&img, x, wall_top + 20, x + 5, wall_top + 230, 55);
  53. }
  54. for (int dy = 30; dy < 240; dy += 20) {
  55. fill_rect(&img, 60, wall_top + dy, 1220, wall_top + dy + 2, 85);
  56. }
  57. return img;
  58. }
  59. portcv::GrayImage flat_scene(int gap_px = 4) {
  60. portcv::GrayImage img(1280, 720, 120);
  61. fill_rect(&img, 0, 40, 1280, 392, 185);
  62. fill_rect(&img, 0, 392, 1280, 404, 55);
  63. const int deck_top = 404 + gap_px;
  64. fill_rect(&img, 0, deck_top, 1280, deck_top + 5, 225);
  65. fill_rect(&img, 0, deck_top + 5, 1280, 520, 145);
  66. fill_rect(&img, 40, deck_top + 24, 1240, deck_top + 29, 70);
  67. for (int y = 520; y < 720; ++y) {
  68. for (int x = 0; x < 1280; ++x) {
  69. img.set(x, y, static_cast<uint8_t>(70 + ((x * 37 + y * 17) % 120)));
  70. }
  71. }
  72. return img;
  73. }
  74. portcv::GrayImage flat_contact_scene() {
  75. // 侧视 X70A:箱底与车面直接贴合(gap=0),车面亮带紧贴箱底;
  76. // 动态 ROI 从箱底下方开始,因此边梁本身落在 ROI 外。用于防止
  77. // “贴合时跳过相邻亮带 → gap 误取 60+ px → 255”的回归。
  78. portcv::GrayImage img(1280, 720, 120);
  79. fill_rect(&img, 0, 40, 1280, 392, 185);
  80. fill_rect(&img, 0, 392, 1280, 404, 55);
  81. fill_rect(&img, 0, 404, 1280, 470, 150);
  82. fill_rect(&img, 0, 470, 1280, 500, 70);
  83. // 地面用均匀灰(本用例只验证“贴合车面”与 gap 取值;真实地面纹理另测)。
  84. fill_rect(&img, 0, 500, 1280, 720, 110);
  85. return img;
  86. }
  87. portcv::GrayImage hoist_scene() {
  88. // 箱体已落位(与 bed_scene 同结构),但箱顶上方有一条横跨画面的暗带
  89. // (吊具/臂架),暗带下方是更亮的天空 → P0-0 高悬门必须命中。
  90. // 暗带放在 y=8..32:bed_scene 的箱顶估计在 ~103 px(箱底 402.7 − 箱高 299.5),
  91. // 高悬门只检查箱顶以上,所以这条暗带落在检查范围内,同时留出 8 px 天空边,
  92. // 不改变箱体/车面那几条亮带的边界(否则 box 选择会变,判据就无从比较)。
  93. portcv::GrayImage img = bed_scene(18);
  94. fill_rect(&img, 0, 8, 1280, 32, 30);
  95. return img;
  96. }
  97. portcv::V2CommitParams fast_commit() {
  98. portcv::V2CommitParams p;
  99. p.stable_frames = 3;
  100. p.structure_frames = 3;
  101. p.gap_px_hard_max = 8.0;
  102. return p;
  103. }
  104. } // namespace
  105. int main() {
  106. using portcv::EdgeV2;
  107. using portcv::EdgeV2Params;
  108. using portcv::V2CommitParams;
  109. using portcv::V2CommitTracker;
  110. std::printf("P1b/P1c edge_v2 selftest\n");
  111. // ---------------- commit 安全门 ----------------
  112. {
  113. V2CommitTracker t(fast_commit());
  114. portcv::V2CommitState s;
  115. for (int i = 0; i < 8; ++i) {
  116. s = t.update(i * 0.1, 1, 0.104, 12.0, 0.9, true, true);
  117. }
  118. check(!s.committed, "A-like gap 12px must not commit");
  119. check(s.phase == "T1", "A-like phase T1");
  120. }
  121. {
  122. V2CommitTracker t(fast_commit());
  123. portcv::V2CommitState s;
  124. for (int i = 0; i < 3; ++i) {
  125. s = t.update(i * 0.1, 1, 0.035, 4.0, 0.9, true, true);
  126. }
  127. check(s.committed, "B-like gap 4px commits");
  128. check(s.phase == "T2", "B-like phase T2");
  129. }
  130. {
  131. V2CommitParams p = fast_commit();
  132. p.allow_state2 = false;
  133. V2CommitTracker t(p);
  134. portcv::V2CommitState s;
  135. for (int i = 0; i < 8; ++i) {
  136. s = t.update(i * 0.1, 2, 0.03, 3.5, 0.9, true, true);
  137. }
  138. check(!s.committed, "state2 can be disabled explicitly");
  139. check(s.reason == "state2_disabled", "state2 disabled reason");
  140. }
  141. {
  142. V2CommitTracker t(fast_commit());
  143. portcv::V2CommitState s;
  144. for (int i = 0; i < 3; ++i) {
  145. s = t.update(i * 0.1, 2, 0.03, 3.5, 0.9, true, true);
  146. }
  147. check(s.committed, "state2 commits by default");
  148. }
  149. {
  150. // 侧视贴合:flat_contact 给出 gap_px=0,但没有 px_per_m → gap_m 为空。
  151. V2CommitTracker t(fast_commit());
  152. portcv::V2CommitState s;
  153. for (int i = 0; i < 3; ++i) {
  154. s = t.update(i * 0.1, 2, std::nullopt, 0.0, 0.9, true, true);
  155. }
  156. check(s.committed, "zero contact gap commits without px_per_m");
  157. }
  158. {
  159. V2CommitTracker t(fast_commit());
  160. portcv::V2CommitState s;
  161. for (int i = 0; i < 8; ++i) {
  162. s = t.update(i * 0.1, 1, 0.03, 3.0, 0.9, false, true);
  163. }
  164. check(!s.committed && s.phase == "T3", "motion is T3");
  165. }
  166. {
  167. V2CommitTracker t(fast_commit());
  168. portcv::V2CommitState s;
  169. for (int i = 0; i < 3; ++i) {
  170. s = t.update(i * 0.1, 1, 0.50, 60.0, 1.0, true, true, true, false);
  171. }
  172. check(s.committed && s.reason == "commit:plc_arrival", "PLC arrival bypasses gap");
  173. }
  174. // ---------------- 合成场景分类 ----------------
  175. EdgeV2Params p;
  176. p.px_per_m = 115.6;
  177. p.require_commit = false;
  178. p.allow_state2 = false;
  179. // 合成图纹理很少,分位阈值会过高;真机保持默认 0.90/0.97。
  180. p.canny_lo_pct = 0.50;
  181. p.canny_hi_pct = 0.90;
  182. {
  183. EdgeV2 ev(p, fast_commit());
  184. const auto r = ev.analyze(bed_scene(18), 0.0);
  185. std::printf(" A: %s roi=[%d,%d,%d,%d] edges=%d segs=%d vrun=%d box_bottom=%.1f bed_top=%.1f\n",
  186. r.evidence_summary().c_str(), r.below_roi[0], r.below_roi[1],
  187. r.below_roi[2], r.below_roi[3], r.edge_pixels, r.line_segments,
  188. r.vertical_runs, r.box_bottom_y.value_or(-1.0), r.bed_top_y.value_or(-1.0));
  189. check(r.state == 1, "A-like synthetic -> state 1");
  190. check(r.door_posts >= 5, "A-like has >=5 door posts");
  191. check(!r.hoist_high, "A-like is not flagged hoist_high");
  192. }
  193. {
  194. EdgeV2 ev(p, fast_commit());
  195. const auto r = ev.analyze(flat_scene(4), 0.0);
  196. std::printf(" B: %s\n", r.evidence_summary().c_str());
  197. check(r.state == 2, "B-like synthetic -> state 2");
  198. check(r.door_posts <= 4, "B-like has <=4 door posts");
  199. check(r.deck_edge, "B-like has deck edge");
  200. check(!r.hoist_high, "B-like is not flagged hoist_high");
  201. }
  202. {
  203. EdgeV2 ev(p, fast_commit());
  204. const auto r = ev.analyze(flat_contact_scene(), 0.0);
  205. std::printf(" B-contact: %s\n", r.evidence_summary().c_str());
  206. check(r.state == 2, "contact flatbed synthetic -> state 2");
  207. check(r.deck_edge, "contact flatbed has deck/contact edge");
  208. check(r.gap_px.has_value() && *r.gap_px <= 2.0,
  209. "contact flatbed gap stays at box bottom (<=2px)");
  210. }
  211. // ---------------- P0-0 图像侧安全门 ----------------
  212. {
  213. // 直接测算子(与 Python `test_detect_hoist_high_finds_dark_bar_with_bright_sky_below`
  214. // 同一张合成图):上方暗带 40..140,下方天空 140..300 更亮。
  215. portcv::GrayImage img(1280, 720, 185);
  216. fill_rect(&img, 0, 40, 1280, 140, 25);
  217. const auto cues = portcv::detect_hoist_high(img, 300.0);
  218. check(cues.hoist_high, "detect_hoist_high: dark bar + bright sky below -> true");
  219. check(cues.horizontal_lines >= 1, "detect_hoist_high: horizontal_lines >= 1");
  220. check_eq(cues.limit_px, 298, "detect_hoist_high: probe height = box_top - 2");
  221. }
  222. {
  223. // 均匀场景必须不命中(否则真机所有低对比帧都会被弃权)。
  224. portcv::GrayImage img(1280, 720, 150);
  225. const auto cues = portcv::detect_hoist_high(img, 300.0);
  226. check(!cues.hoist_high, "detect_hoist_high: uniform scene -> false");
  227. check_eq(cues.horizontal_lines, 0, "detect_hoist_high: uniform horizontal_lines=0");
  228. }
  229. {
  230. // 箱顶以上区域太小(box_top≈0,箱顶被画幅裁掉)时必须放行 —— 这条是
  231. // A/B 正样本不被误弃权的保证。
  232. portcv::GrayImage img = bed_scene(18);
  233. const auto cues = portcv::detect_hoist_high(img, 6.0);
  234. check(!cues.hoist_high && cues.limit_px == 0, "detect_hoist_high: tiny ROI -> not checked");
  235. }
  236. {
  237. // 夹箱:吊具横梁紧贴箱顶(100..140 暗,箱体 140..450)。
  238. portcv::GrayImage img(600, 500, 180);
  239. fill_rect(&img, 100, 100, 500, 140, 30);
  240. fill_rect(&img, 100, 140, 500, 450, 150);
  241. const auto cues = portcv::detect_spreader_contact(img, 100, 140, 500, 450);
  242. check(cues.contact, "detect_spreader_contact: dark bar on box top -> true");
  243. check(cues.dark_run_px >= 20, "detect_spreader_contact: dark_run_px >= 20");
  244. }
  245. {
  246. // 干净的箱顶(上方是同亮度背景)不能命中。
  247. portcv::GrayImage img(600, 500, 180);
  248. fill_rect(&img, 100, 140, 500, 450, 150);
  249. const auto cues = portcv::detect_spreader_contact(img, 100, 140, 500, 450);
  250. check(!cues.contact, "detect_spreader_contact: plain box top -> false");
  251. }
  252. {
  253. // 端到端:高悬帧连续 6 帧都成立也必须 0 commit(不写窗口、不进投票)。
  254. EdgeV2Params cp = p;
  255. cp.require_commit = true;
  256. cp.allow_state2 = true;
  257. EdgeV2 ev(cp, fast_commit());
  258. portcv::EvidenceV2Result r;
  259. for (int i = 0; i < 6; ++i) { r = ev.analyze(hoist_scene(), i * 0.1); }
  260. std::printf(" hoist: %s\n", r.evidence_summary().c_str());
  261. check(r.hoist_high, "hoist scene flagged hoist_high");
  262. check(!r.hoist_injected, "hoist flag came from image (not injected)");
  263. check(r.hoist_lines >= 1, "hoist scene has >=1 horizontal hit");
  264. check(!r.committed, "hoist scene never commits");
  265. // 分类本身仍是 1(几何证据没坏),安全门只拦 commit —— 对外的 `edge_state`
  266. // 走 `to_observation(require_commit=true)`,所以必须是 255(见下一条)。
  267. check_eq(r.state, 1, "hoist scene still classifies 1 (geometry intact)");
  268. check(r.abstain_reason == "hoist_high", "hoist scene reason=hoist_high");
  269. check_eq(r.stable_frames, 0, "hoist scene does not accumulate stable frames");
  270. const auto obs = r.to_observation(0.6, 0, true);
  271. check(obs.state == 255, "hoist scene observation abstains");
  272. check(obs.reason.find("hoist_high") != std::string::npos,
  273. "observation reason carries hoist_high");
  274. }
  275. {
  276. // 注入路径(PLC/联锁直接给 hoist_high):不做图像检测也不消耗窗口。
  277. EdgeV2Params cp = p;
  278. cp.require_commit = true;
  279. EdgeV2 ev(cp, fast_commit());
  280. portcv::EvidenceV2Result r;
  281. for (int i = 0; i < 6; ++i) { r = ev.analyze(bed_scene(18), i * 0.1, false, false, true); }
  282. check(r.hoist_high && r.hoist_injected, "injected hoist_high is honoured");
  283. check(r.abstain_reason == "hoist_high", "injected hoist reason=hoist_high");
  284. check(!r.committed, "injected hoist never commits");
  285. }
  286. {
  287. // 关掉门只做对照:同一帧仍能出 1(说明弃权确实来自门,而不是几何退化)。
  288. EdgeV2Params cp = p;
  289. cp.reject_hoist_high = false;
  290. cp.reject_spreader_contact = false;
  291. EdgeV2 ev(cp, fast_commit());
  292. const auto r = ev.analyze(hoist_scene(), 0.0);
  293. check(!r.hoist_high, "gate disabled -> no hoist flag");
  294. check_eq(r.state, 1, "gate disabled -> geometry still says 1");
  295. }
  296. {
  297. // 远距小箱:箱底以下 ROI 太矮(这里把 1280×720 等比缩到 480×270,
  298. // ROI 只剩 ~60 px)时必须弃权,不能判 1 —— B 片段 f30/f31/f36~f38
  299. // 实测就是这种"远距 + 门板立柱只有 20~27 px"的帧被误判 1 并被投票发布。
  300. EdgeV2Params cp = p;
  301. cp.px_per_m = 0.0; // 用自标尺(缩放后 115.6 px/m 已不成立)
  302. EdgeV2 ev(cp, fast_commit());
  303. const portcv::GrayImage small = portcv::box_downscale(bed_scene(18), 480, 270);
  304. const auto r = ev.analyze(small, 0.0);
  305. std::printf(" far-A: %s\n", r.evidence_summary().c_str());
  306. const double roi_h = r.below_roi[3] - r.below_roi[1];
  307. check(roi_h < 90.0, "far-A below ROI is shorter than min_bed_roi_px");
  308. check(r.state != 1, "far gondola is never classified 1 (abstains instead)");
  309. }
  310. {
  311. EdgeV2 ev(p, fast_commit());
  312. const portcv::GrayImage empty;
  313. const auto r = ev.analyze(empty, 0.0);
  314. check(r.state == 255, "empty frame -> 255");
  315. check(r.abstain_reason == "no_image", "empty reason=no_image");
  316. }
  317. {
  318. EdgeV2 ev(p, fast_commit());
  319. const auto r = ev.analyze(bed_scene(18), 0.0);
  320. const auto obs = r.to_observation(0.0, 0, true);
  321. check(obs.state == 255, "require_commit abstains to 255");
  322. }
  323. std::printf("----\n%d passed, %d failed\n", g_pass, g_fail);
  324. return g_fail == 0 ? 0 : 1;
  325. }