selftest_edge_v2.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. // 箱体竖波纹(真实集装箱有;也让 P0-Q4 顶部纹理门不把箱顶误当天空)
  47. for (int x = 60; x < 1240; x += 48) {
  48. fill_rect(&img, x, 50, x + 3, box_bottom - 4, 176);
  49. }
  50. fill_rect(&img, 0, box_bottom, 1280, box_bottom + 12, 55);
  51. const int wall_top = box_bottom + 12 + gap_px;
  52. fill_rect(&img, 0, box_bottom + 12, 1280, wall_top, 88);
  53. fill_rect(&img, 0, wall_top, 1280, wall_top + 4, 215);
  54. fill_rect(&img, 0, wall_top + 4, 1280, 720, 125);
  55. for (int x = 120; x < 1180; x += 110) {
  56. fill_rect(&img, x, wall_top + 20, x + 5, wall_top + 230, 55);
  57. }
  58. for (int dy = 30; dy < 240; dy += 20) {
  59. fill_rect(&img, 60, wall_top + dy, 1220, wall_top + dy + 2, 85);
  60. }
  61. return img;
  62. }
  63. portcv::GrayImage flat_scene(int gap_px = 4) {
  64. portcv::GrayImage img(1280, 720, 120);
  65. fill_rect(&img, 0, 40, 1280, 392, 185);
  66. for (int x = 60; x < 1240; x += 48) {
  67. fill_rect(&img, x, 50, x + 3, 388, 176);
  68. }
  69. fill_rect(&img, 0, 392, 1280, 404, 55);
  70. const int deck_top = 404 + gap_px;
  71. fill_rect(&img, 0, deck_top, 1280, deck_top + 5, 225);
  72. fill_rect(&img, 0, deck_top + 5, 1280, 520, 145);
  73. fill_rect(&img, 40, deck_top + 24, 1240, deck_top + 29, 70);
  74. for (int y = 520; y < 720; ++y) {
  75. for (int x = 0; x < 1280; ++x) {
  76. img.set(x, y, static_cast<uint8_t>(70 + ((x * 37 + y * 17) % 120)));
  77. }
  78. }
  79. return img;
  80. }
  81. portcv::GrayImage empty_flatbed_scene() {
  82. // P0-Q10:空平车(车底板裸露)—— 薄边梁 + 车底透空 + 光滑亮车面 + 沥青地。
  83. // 与 flat_scene(平车+箱)的差别:箱底暗带不再紧贴边梁,中间隔着整片车面。
  84. portcv::GrayImage img(1280, 720, 200);
  85. fill_rect(&img, 0, 60, 1280, 64, 30); // 车底板远端边缘(细暗线)
  86. fill_rect(&img, 0, 64, 1280, 300, 165); // 车底板(光滑亮面)
  87. for (int y = 120; y < 300; y += 120) { // 很淡的车面板缝
  88. fill_rect(&img, 0, y, 1280, y + 2, 159);
  89. }
  90. fill_rect(&img, 0, 300, 1280, 304, 228); // 近侧边梁(薄亮边)
  91. fill_rect(&img, 0, 304, 1280, 334, 40); // 车底透空(暗)
  92. fill_rect(&img, 0, 334, 1280, 430, 70); // 转向架/车底结构
  93. for (int y = 430; y < 720; ++y) { // 沥青地:亮度缓变 + 小抖动
  94. const double t = static_cast<double>(y - 430) / 290.0;
  95. const int base = 95 + static_cast<int>(40.0 * t);
  96. for (int x = 0; x < 1280; ++x) {
  97. const int j = ((x * 7919 + y * 104729) % 7) - 3;
  98. img.set(x, y, static_cast<uint8_t>(std::max(0, std::min(255, base + j))));
  99. }
  100. }
  101. return img;
  102. }
  103. portcv::GrayImage flat_contact_scene() {
  104. // 侧视 X70A:箱底与车面直接贴合(gap=0),车面边梁紧贴箱底;
  105. // 动态 ROI 从箱底下方开始,因此边梁本身落在 ROI 外。用于防止
  106. // “贴合时跳过相邻亮带 → gap 误取 60+ px → 255”的回归。
  107. // P0-Q3:边梁按**薄边**建(8 px)+ 下方暗带(车底空隙/阴影)——
  108. // 与现场 746×192 侧视帧同形态;厚亮面(地面/下层箱顶)由负样本覆盖。
  109. portcv::GrayImage img(1280, 720, 120);
  110. fill_rect(&img, 0, 40, 1280, 392, 185);
  111. for (int x = 60; x < 1240; x += 48) {
  112. fill_rect(&img, x, 50, x + 3, 388, 176);
  113. }
  114. fill_rect(&img, 0, 392, 1280, 404, 55);
  115. fill_rect(&img, 0, 404, 1280, 412, 225);
  116. fill_rect(&img, 0, 412, 1280, 442, 30);
  117. // 地面用均匀灰(本用例只验证“贴合车面”与 gap 取值;真实地面纹理另测)。
  118. fill_rect(&img, 0, 442, 1280, 720, 110);
  119. return img;
  120. }
  121. portcv::GrayImage hoist_scene() {
  122. // 箱体已落位(与 bed_scene 同结构),但箱顶上方有一条横跨画面的暗带
  123. // (吊具/臂架),暗带下方是更亮的天空 → P0-0 高悬门必须命中。
  124. // 暗带放在 y=8..32:bed_scene 的箱顶估计在 ~103 px(箱底 402.7 − 箱高 299.5),
  125. // 高悬门只检查箱顶以上,所以这条暗带落在检查范围内,同时留出 8 px 天空边,
  126. // 不改变箱体/车面那几条亮带的边界(否则 box 选择会变,判据就无从比较)。
  127. portcv::GrayImage img = bed_scene(18);
  128. fill_rect(&img, 0, 8, 1280, 32, 30);
  129. return img;
  130. }
  131. /// 箱直接坐地:箱底以下是低对比、连续的地面(P0-Q3 负样本)。
  132. portcv::GrayImage ground_scene() {
  133. portcv::GrayImage img(1280, 720, 120);
  134. fill_rect(&img, 0, 40, 1280, 392, 185);
  135. for (int x = 60; x < 1240; x += 48) {
  136. fill_rect(&img, x, 50, x + 3, 388, 176);
  137. }
  138. fill_rect(&img, 0, 392, 1280, 404, 55);
  139. for (int y = 404; y < 720; ++y) {
  140. for (int x = 0; x < 1280; ++x) {
  141. img.set(x, y, static_cast<uint8_t>(100 + ((x * 7 + y * 3) % 12)));
  142. }
  143. }
  144. return img;
  145. }
  146. /// 箱底紧贴一块**厚亮面**(地面/下层箱顶/月台)——旧版 flat_contact 会判 2。
  147. portcv::GrayImage thick_contact_scene() {
  148. portcv::GrayImage img = ground_scene();
  149. fill_rect(&img, 0, 404, 1280, 450, 165);
  150. return img;
  151. }
  152. /// 上层箱坐在下层箱上:下层箱顶亮面 + 侧壁瓦楞(无斜撑)——旧版会判 1。
  153. portcv::GrayImage stacked_scene() {
  154. portcv::GrayImage img = ground_scene();
  155. fill_rect(&img, 0, 404, 1280, 450, 165);
  156. fill_rect(&img, 0, 450, 1280, 720, 140);
  157. for (int x = 60; x < 1280; x += 118) {
  158. fill_rect(&img, x, 450, x + 6, 700, 95);
  159. }
  160. return img;
  161. }
  162. /// 天空电线 + 车体:电线是横向细线,车体有竖向立柱/门板(P0-Q4 回归,512×512)。
  163. portcv::GrayImage sky_wire_scene() {
  164. portcv::GrayImage img(512, 512, 200);
  165. for (int y : {20, 52, 84, 116}) {
  166. fill_rect(&img, 0, y, 512, y + 2, 30);
  167. }
  168. fill_rect(&img, 0, 165, 512, 405, 95);
  169. for (int x = 30; x < 480; x += 18) {
  170. fill_rect(&img, x, 175, x + 5, 400, 45);
  171. }
  172. for (int x = 30; x + 40 < 480; x += 50) {
  173. fill_rect(&img, x, 185, x + 40, 380, 110);
  174. fill_rect(&img, x, 185, x + 40, 188, 60);
  175. fill_rect(&img, x, 377, x + 40, 380, 60);
  176. fill_rect(&img, x, 185, x + 3, 380, 60);
  177. fill_rect(&img, x + 37, 185, x + 40, 380, 60);
  178. }
  179. for (int dy = 5; dy < 235; dy += 8) {
  180. fill_rect(&img, 20, 165 + dy, 492, 165 + dy + 2, 80);
  181. }
  182. for (int x = 40; x + 100 < 480; x += 70) {
  183. // 2 px 细“八”字斜撑:C++ 线段通道对 7×7 阶梯块不敏感(会数到 0 条斜撑),
  184. // 与 Python 合成场景的 `cv2.line(..., 30, 7)` 对齐后改用细线。
  185. for (int t = 0; t < 100; ++t) {
  186. fill_rect(&img, x + t, 190 + t, x + t + 2, 190 + t + 2, 30);
  187. }
  188. }
  189. fill_rect(&img, 0, 405, 512, 512, 140);
  190. return img;
  191. }
  192. portcv::V2CommitParams fast_commit() {
  193. portcv::V2CommitParams p;
  194. p.stable_frames = 3;
  195. p.structure_frames = 3;
  196. p.gap_px_hard_max = 8.0;
  197. return p;
  198. }
  199. } // namespace
  200. int main() {
  201. using portcv::EdgeV2;
  202. using portcv::EdgeV2Params;
  203. using portcv::V2CommitParams;
  204. using portcv::V2CommitTracker;
  205. std::printf("P1b/P1c edge_v2 selftest\n");
  206. // ---------------- commit 安全门 ----------------
  207. {
  208. V2CommitTracker t(fast_commit());
  209. portcv::V2CommitState s;
  210. for (int i = 0; i < 8; ++i) {
  211. s = t.update(i * 0.1, 1, 0.104, 12.0, 0.9, true, true);
  212. }
  213. check(!s.committed, "A-like gap 12px must not commit");
  214. check(s.phase == "T1", "A-like phase T1");
  215. }
  216. {
  217. V2CommitTracker t(fast_commit());
  218. portcv::V2CommitState s;
  219. for (int i = 0; i < 3; ++i) {
  220. s = t.update(i * 0.1, 1, 0.035, 4.0, 0.9, true, true);
  221. }
  222. check(s.committed, "B-like gap 4px commits");
  223. check(s.phase == "T2", "B-like phase T2");
  224. }
  225. {
  226. V2CommitParams p = fast_commit();
  227. p.allow_state2 = false;
  228. V2CommitTracker t(p);
  229. portcv::V2CommitState s;
  230. for (int i = 0; i < 8; ++i) {
  231. s = t.update(i * 0.1, 2, 0.03, 3.5, 0.9, true, true);
  232. }
  233. check(!s.committed, "state2 can be disabled explicitly");
  234. check(s.reason == "state2_disabled", "state2 disabled reason");
  235. }
  236. {
  237. V2CommitTracker t(fast_commit());
  238. portcv::V2CommitState s;
  239. for (int i = 0; i < 3; ++i) {
  240. s = t.update(i * 0.1, 2, 0.03, 3.5, 0.9, true, true);
  241. }
  242. check(s.committed, "state2 commits by default");
  243. }
  244. {
  245. // 侧视贴合:flat_contact 给出 gap_px=0,但没有 px_per_m → gap_m 为空。
  246. V2CommitTracker t(fast_commit());
  247. portcv::V2CommitState s;
  248. for (int i = 0; i < 3; ++i) {
  249. s = t.update(i * 0.1, 2, std::nullopt, 0.0, 0.9, true, true);
  250. }
  251. check(s.committed, "zero contact gap commits without px_per_m");
  252. }
  253. {
  254. V2CommitTracker t(fast_commit());
  255. portcv::V2CommitState s;
  256. for (int i = 0; i < 8; ++i) {
  257. s = t.update(i * 0.1, 1, 0.03, 3.0, 0.9, false, true);
  258. }
  259. check(!s.committed && s.phase == "T3", "motion is T3");
  260. }
  261. {
  262. V2CommitTracker t(fast_commit());
  263. portcv::V2CommitState s;
  264. for (int i = 0; i < 3; ++i) {
  265. s = t.update(i * 0.1, 1, 0.50, 60.0, 1.0, true, true, true, false);
  266. }
  267. check(s.committed && s.reason == "commit:plc_arrival", "PLC arrival bypasses gap");
  268. }
  269. // ---------------- 合成场景分类 ----------------
  270. EdgeV2Params p;
  271. p.px_per_m = 115.6;
  272. p.require_commit = false;
  273. p.allow_state2 = false;
  274. // 合成图纹理很少,分位阈值会过高;真机保持默认 0.90/0.97。
  275. p.canny_lo_pct = 0.50;
  276. p.canny_hi_pct = 0.90;
  277. {
  278. EdgeV2 ev(p, fast_commit());
  279. const auto r = ev.analyze(bed_scene(18), 0.0);
  280. std::printf(" A: %s diag=%d\n", r.evidence_summary().c_str(), r.diagonal_braces);
  281. check(r.state == 1, "A-like synthetic -> state 1");
  282. check(r.door_posts >= 5, "A-like has >=5 door posts");
  283. check(!r.hoist_high, "A-like is not flagged hoist_high");
  284. }
  285. {
  286. EdgeV2 ev(p, fast_commit());
  287. const auto r = ev.analyze(flat_scene(4), 0.0);
  288. std::printf(" B: %s\n", r.evidence_summary().c_str());
  289. check(r.state == 2, "B-like synthetic -> state 2");
  290. check(r.door_posts <= 4, "B-like has <=4 door posts");
  291. check(r.deck_edge, "B-like has deck edge");
  292. check(!r.hoist_high, "B-like is not flagged hoist_high");
  293. }
  294. {
  295. EdgeV2 ev(p, fast_commit());
  296. const auto r = ev.analyze(flat_contact_scene(), 0.0);
  297. std::printf(" B-contact: %s\n", r.evidence_summary().c_str());
  298. check(r.state == 2, "contact flatbed synthetic -> state 2");
  299. check(r.deck_edge, "contact flatbed has deck/contact edge");
  300. check(r.gap_px.has_value() && *r.gap_px <= 2.0,
  301. "contact flatbed gap stays at box bottom (<=2px)");
  302. }
  303. // ---------------- P0-Q11:作业帧(吊具高悬)绝不判车型 ----------------
  304. {
  305. // 同一幅 B 类平车画面,注入 hoist_high 后必须弃权(不得仍报 2)。
  306. EdgeV2 ev(p, fast_commit());
  307. const auto r = ev.analyze(flat_scene(4), 0.0, false, false, true);
  308. std::printf(" hoist: %s\n", r.evidence_summary().c_str());
  309. check(r.state != 2 && r.state != 1 && r.state != 3,
  310. "hoist_high blocks flatbed state 2");
  311. check(r.hoist_high, "hoist flag reported");
  312. check(r.class_reason == "hoist_high", "hoist_high reason recorded");
  313. }
  314. // ---------------- P0-Q3:地面/堆料/堆叠箱负样本 ----------------
  315. {
  316. EdgeV2 ev(p, fast_commit());
  317. const auto r = ev.analyze(ground_scene(), 0.0);
  318. std::printf(" ground: %s\n", r.evidence_summary().c_str());
  319. check(r.state != 1 && r.state != 2, "box on ground is never 1/2");
  320. }
  321. {
  322. EdgeV2 ev(p, fast_commit());
  323. const auto r = ev.analyze(thick_contact_scene(), 0.0);
  324. std::printf(" thick-contact: %s\n", r.evidence_summary().c_str());
  325. check(r.state != 1 && r.state != 2, "thick contact plane is not flatbed");
  326. check(!r.deck_edge, "thick contact plane does not set deck_edge");
  327. }
  328. {
  329. EdgeV2 ev(p, fast_commit());
  330. const auto r = ev.analyze(stacked_scene(), 0.0);
  331. std::printf(" stacked: %s\n", r.evidence_summary().c_str());
  332. check(r.state != 1 && r.state != 2, "stacked container is not bed/flatbed");
  333. }
  334. // ---------------- P0-Q4:天空电线/纹理顶 ----------------
  335. {
  336. portcv::EdgeV2Params tp;
  337. const int win[4] = {static_cast<int>(0.24 * 512), static_cast<int>(0.02 * 512),
  338. static_cast<int>(0.78 * 512), static_cast<int>(0.95 * 512)};
  339. const double top = portcv::find_texture_top_y(sky_wire_scene(), win, tp);
  340. std::printf(" sky-top=%.1f\n", top);
  341. check(top > 120.0 && top < 220.0, "sky wires: vehicle top below wires");
  342. portcv::GrayImage plain(512, 512, 200);
  343. for (int y : {20, 52, 84}) {
  344. fill_rect(&plain, 0, y, 512, y + 2, 30);
  345. }
  346. check(portcv::find_texture_top_y(plain, win, tp) < 0.0,
  347. "plain sky: no vehicle top");
  348. EdgeV2 ev(p, fast_commit());
  349. const auto r = ev.analyze(sky_wire_scene(), 0.0);
  350. std::printf(" sky-body: %s\n", r.evidence_summary().c_str());
  351. // 默认斜撑门 = 1,而 C++ 线段通道在这张 512×512 合成图上一条斜撑都数不到
  352. // (真实 02/03 图上实测 39~129 条)→ 按严格门应当弃权,绝不判 1/2。
  353. check(r.state == 255, "synthetic empty + braces gate -> 255 (not 1/2)");
  354. check(r.empty_reason == "empty_braces", "synthetic empty reason=empty_braces");
  355. // 把空车斜撑门放开(现场可按车型单独放宽):应判 3 并提交。
  356. portcv::EdgeV2Params p3 = p;
  357. p3.empty_min_diagonal_braces = 0;
  358. EdgeV2 ev3(p3, fast_commit());
  359. portcv::EvidenceV2Result last3;
  360. for (int i = 0; i < 6; ++i) { last3 = ev3.analyze(sky_wire_scene(), i * 0.1); }
  361. std::printf(" sky-body(3): %s\n", last3.evidence_summary().c_str());
  362. check(last3.state == 3, "empty C70 (+braces off) -> 3");
  363. check(last3.empty_vehicle, "empty C70 flags empty_vehicle");
  364. check(last3.committed, "empty C70 commits with fast_commit");
  365. check(last3.to_observation(0.6, 0, true).state == 3, "empty observation -> 3");
  366. // allow_state3=false:仍识别 3,但不提交(commit_reason=state3_disabled)。
  367. portcv::V2CommitParams no3 = fast_commit();
  368. no3.allow_state3 = false;
  369. EdgeV2 ev_no3(p3, no3);
  370. portcv::EvidenceV2Result last_no3;
  371. for (int i = 0; i < 6; ++i) { last_no3 = ev_no3.analyze(sky_wire_scene(), i * 0.1); }
  372. check(last_no3.state == 3, "allow_state3=false still classifies 3");
  373. check(!last_no3.committed, "allow_state3=false never commits 3");
  374. check(last_no3.commit_reason == "state3_disabled", "allow_state3=false reason=state3_disabled");
  375. check(last_no3.to_observation(0.6, 0, true).state == 255,
  376. "allow_state3=false observation abstains to 255");
  377. }
  378. // ---------------- P0-Q10:空平车(车面裸露) ----------------
  379. {
  380. EdgeV2 ev(p, fast_commit());
  381. portcv::EvidenceV2Result last;
  382. for (int i = 0; i < 6; ++i) { last = ev.analyze(empty_flatbed_scene(), i * 0.1); }
  383. std::printf(" empty-flatbed: %s\n", last.evidence_summary().c_str());
  384. check(last.state == 3, "empty flatbed -> 3");
  385. check(last.empty_vehicle, "empty flatbed flags empty_vehicle");
  386. check(last.class_reason == "empty_flatbed", "empty flatbed reason=empty_flatbed");
  387. check(last.committed, "empty flatbed commits with fast_commit");
  388. check(last.to_observation(0.6, 0, true).state == 3,
  389. "empty flatbed observation -> 3");
  390. // 平车+箱(真值 2)绝不能落 3
  391. EdgeV2 ev2(p, fast_commit());
  392. portcv::EvidenceV2Result r2;
  393. for (int i = 0; i < 6; ++i) { r2 = ev2.analyze(flat_scene(4), i * 0.1); }
  394. std::printf(" flatcar+box: %s\n", r2.evidence_summary().c_str());
  395. check(r2.state != 3, "flatcar+container never 3");
  396. // 开关关闭 → 不判 3
  397. portcv::EdgeV2Params off = p;
  398. off.empty_flatbed_enable = false;
  399. EdgeV2 ev3(off, fast_commit());
  400. const auto r3 = ev3.analyze(empty_flatbed_scene(), 0.0);
  401. check(r3.state != 3, "empty flatbed gate off -> not 3");
  402. // 负样本:边梁下方没有车底透空(月台边缘/挡墙)→ 不得 3
  403. portcv::GrayImage no_void = empty_flatbed_scene();
  404. fill_rect(&no_void, 0, 304, 1280, 334, 96);
  405. fill_rect(&no_void, 0, 334, 1280, 430, 100);
  406. EdgeV2 ev4(p, fast_commit());
  407. const auto r4 = ev4.analyze(no_void, 0.0);
  408. std::printf(" no-void: %s\n", r4.evidence_summary().c_str());
  409. check(r4.state != 3, "no under-deck void -> not 3");
  410. }
  411. // ---------------- P0-0 图像侧安全门 ----------------
  412. {
  413. // 直接测算子(与 Python `test_detect_hoist_high_finds_dark_bar_with_bright_sky_below`
  414. // 同一张合成图):上方暗带 40..140,下方天空 140..300 更亮。
  415. portcv::GrayImage img(1280, 720, 185);
  416. fill_rect(&img, 0, 40, 1280, 140, 25);
  417. const auto cues = portcv::detect_hoist_high(img, 300.0);
  418. check(cues.hoist_high, "detect_hoist_high: dark bar + bright sky below -> true");
  419. check(cues.horizontal_lines >= 1, "detect_hoist_high: horizontal_lines >= 1");
  420. check_eq(cues.limit_px, 298, "detect_hoist_high: probe height = box_top - 2");
  421. }
  422. {
  423. // 均匀场景必须不命中(否则真机所有低对比帧都会被弃权)。
  424. portcv::GrayImage img(1280, 720, 150);
  425. const auto cues = portcv::detect_hoist_high(img, 300.0);
  426. check(!cues.hoist_high, "detect_hoist_high: uniform scene -> false");
  427. check_eq(cues.horizontal_lines, 0, "detect_hoist_high: uniform horizontal_lines=0");
  428. }
  429. {
  430. // 箱顶以上区域太小(box_top≈0,箱顶被画幅裁掉)时必须放行 —— 这条是
  431. // A/B 正样本不被误弃权的保证。
  432. portcv::GrayImage img = bed_scene(18);
  433. const auto cues = portcv::detect_hoist_high(img, 6.0);
  434. check(!cues.hoist_high && cues.limit_px == 0, "detect_hoist_high: tiny ROI -> not checked");
  435. }
  436. {
  437. // 夹箱:吊具横梁紧贴箱顶(100..140 暗,箱体 140..450)。
  438. portcv::GrayImage img(600, 500, 180);
  439. fill_rect(&img, 100, 100, 500, 140, 30);
  440. fill_rect(&img, 100, 140, 500, 450, 150);
  441. const auto cues = portcv::detect_spreader_contact(img, 100, 140, 500, 450);
  442. check(cues.contact, "detect_spreader_contact: dark bar on box top -> true");
  443. check(cues.dark_run_px >= 20, "detect_spreader_contact: dark_run_px >= 20");
  444. }
  445. {
  446. // 干净的箱顶(上方是同亮度背景)不能命中。
  447. portcv::GrayImage img(600, 500, 180);
  448. fill_rect(&img, 100, 140, 500, 450, 150);
  449. const auto cues = portcv::detect_spreader_contact(img, 100, 140, 500, 450);
  450. check(!cues.contact, "detect_spreader_contact: plain box top -> false");
  451. }
  452. {
  453. // 端到端:高悬帧连续 6 帧都成立也必须 0 commit(不写窗口、不进投票)。
  454. EdgeV2Params cp = p;
  455. cp.require_commit = true;
  456. cp.allow_state2 = true;
  457. EdgeV2 ev(cp, fast_commit());
  458. portcv::EvidenceV2Result r;
  459. for (int i = 0; i < 6; ++i) { r = ev.analyze(hoist_scene(), i * 0.1); }
  460. std::printf(" hoist: %s\n", r.evidence_summary().c_str());
  461. check(r.hoist_high, "hoist scene flagged hoist_high");
  462. check(!r.hoist_injected, "hoist flag came from image (not injected)");
  463. check(r.hoist_lines >= 1, "hoist scene has >=1 horizontal hit");
  464. check(!r.committed, "hoist scene never commits");
  465. // P0-Q11:作业帧**不判车型**。旧口径是“分类仍给 1、只拦 commit”,但实拍
  466. // `root_test1`(司机视角、吊具吊着箱悬在平车上方)证明那样会把“吊具+箱体”
  467. // 直接当成空车 3 发给 PLC,所以现在 state 也一并置 255,reason=hoist_high。
  468. check(r.state != 1 && r.state != 2 && r.state != 3,
  469. "hoist scene never classifies a vehicle type");
  470. check(r.abstain_reason == "hoist_high", "hoist scene reason=hoist_high");
  471. check_eq(r.stable_frames, 0, "hoist scene does not accumulate stable frames");
  472. const auto obs = r.to_observation(0.6, 0, true);
  473. check(obs.state == 255, "hoist scene observation abstains");
  474. check(obs.reason.find("hoist_high") != std::string::npos,
  475. "observation reason carries hoist_high");
  476. }
  477. {
  478. // 注入路径(PLC/联锁直接给 hoist_high):不做图像检测也不消耗窗口。
  479. EdgeV2Params cp = p;
  480. cp.require_commit = true;
  481. EdgeV2 ev(cp, fast_commit());
  482. portcv::EvidenceV2Result r;
  483. for (int i = 0; i < 6; ++i) { r = ev.analyze(bed_scene(18), i * 0.1, false, false, true); }
  484. check(r.hoist_high && r.hoist_injected, "injected hoist_high is honoured");
  485. check(r.abstain_reason == "hoist_high", "injected hoist reason=hoist_high");
  486. check(!r.committed, "injected hoist never commits");
  487. }
  488. {
  489. // 关掉门只做对照:同一帧仍能出 1(说明弃权确实来自门,而不是几何退化)。
  490. EdgeV2Params cp = p;
  491. cp.reject_hoist_high = false;
  492. cp.reject_spreader_contact = false;
  493. EdgeV2 ev(cp, fast_commit());
  494. const auto r = ev.analyze(hoist_scene(), 0.0);
  495. check(!r.hoist_high, "gate disabled -> no hoist flag");
  496. check_eq(r.state, 1, "gate disabled -> geometry still says 1");
  497. }
  498. {
  499. // 远距小箱:箱底以下 ROI 太矮(这里把 1280×720 等比缩到 480×270,
  500. // ROI 只剩 ~60 px)时必须弃权,不能判 1 —— B 片段 f30/f31/f36~f38
  501. // 实测就是这种"远距 + 门板立柱只有 20~27 px"的帧被误判 1 并被投票发布。
  502. EdgeV2Params cp = p;
  503. cp.px_per_m = 0.0; // 用自标尺(缩放后 115.6 px/m 已不成立)
  504. EdgeV2 ev(cp, fast_commit());
  505. const portcv::GrayImage small = portcv::box_downscale(bed_scene(18), 480, 270);
  506. const auto r = ev.analyze(small, 0.0);
  507. std::printf(" far-A: %s\n", r.evidence_summary().c_str());
  508. const double roi_h = r.below_roi[3] - r.below_roi[1];
  509. check(roi_h < 90.0, "far-A below ROI is shorter than min_bed_roi_px");
  510. check(r.state != 1, "far gondola is never classified 1 (abstains instead)");
  511. }
  512. {
  513. EdgeV2 ev(p, fast_commit());
  514. const portcv::GrayImage empty;
  515. const auto r = ev.analyze(empty, 0.0);
  516. check(r.state == 255, "empty frame -> 255");
  517. check(r.abstain_reason == "no_image", "empty reason=no_image");
  518. }
  519. {
  520. EdgeV2 ev(p, fast_commit());
  521. const auto r = ev.analyze(bed_scene(18), 0.0);
  522. const auto obs = r.to_observation(0.0, 0, true);
  523. check(obs.state == 255, "require_commit abstains to 255");
  524. }
  525. std::printf("----\n%d passed, %d failed\n", g_pass, g_fail);
  526. return g_fail == 0 ? 0 : 1;
  527. }