|
|
@@ -222,6 +222,59 @@ double pstdev(const std::vector<double>& values, double mean) {
|
|
|
return std::sqrt(sum / static_cast<double>(values.size()));
|
|
|
}
|
|
|
|
|
|
+/// 车体 ROI(空车/无箱)上的门板/立柱证据(P0-Q4 兜底用)。
|
|
|
+GrayImage crop_gray(const GrayImage& src, int x0, int y0, int x1, int y1);
|
|
|
+
|
|
|
+struct BodyEvidence {
|
|
|
+ int posts = 0;
|
|
|
+ double span_ratio = 0.0;
|
|
|
+ double opacity = 0.0;
|
|
|
+ int diagonal = 0;
|
|
|
+ double height = 0.0;
|
|
|
+};
|
|
|
+
|
|
|
+BodyEvidence measure_body(const GrayImage& work, int x0, int y0, int x1, int y1,
|
|
|
+ const EdgeV2Params& p, double scale) {
|
|
|
+ BodyEvidence out;
|
|
|
+ GrayImage crop = crop_gray(work, x0, y0, x1, y1);
|
|
|
+ if (crop.empty() || crop.height < 8) { return out; }
|
|
|
+ out.height = static_cast<double>(crop.height);
|
|
|
+ const SobelResult grad = sobel(crop);
|
|
|
+ const std::vector<uint8_t> edges =
|
|
|
+ canny_lite(crop, grad, p.canny_lo_pct, p.canny_hi_pct);
|
|
|
+ LineParams lp;
|
|
|
+ lp.min_points = 12;
|
|
|
+ lp.grad_pct = 0.90;
|
|
|
+ const std::vector<Segment> segs = extract_segments(crop, grad, edges, lp);
|
|
|
+ const std::vector<Segment> runs = extract_vertical_runs(
|
|
|
+ crop, grad, edges, lp, std::max(8.0, 0.60 * crop.height), 8, nullptr);
|
|
|
+ std::vector<Segment> all_segs = segs;
|
|
|
+ all_segs.insert(all_segs.end(), runs.begin(), runs.end());
|
|
|
+ const double post_min_len = p.min_post_len_ratio * crop.height;
|
|
|
+ std::vector<double> post_x;
|
|
|
+ int diagonal = 0;
|
|
|
+ for (const Segment& s : all_segs) {
|
|
|
+ if (std::fabs(s.angle_deg) >= 65.0 && s.length >= post_min_len) {
|
|
|
+ post_x.push_back(s.cx());
|
|
|
+ } else if (std::fabs(s.angle_deg) >= 20.0 && std::fabs(s.angle_deg) <= 70.0) {
|
|
|
+ ++diagonal;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const std::vector<double> posts =
|
|
|
+ cluster_positions(post_x, std::max(3.0, 9.0 * scale));
|
|
|
+ out.posts = static_cast<int>(posts.size());
|
|
|
+ out.diagonal = diagonal;
|
|
|
+ if (posts.size() >= 2) {
|
|
|
+ out.span_ratio = (posts.back() - posts.front()) /
|
|
|
+ std::max(1.0, static_cast<double>(crop.width));
|
|
|
+ }
|
|
|
+ const double ground = ground_like_ratio(crop, grad, all_segs, p.ground_block_px,
|
|
|
+ p.ground_density_lo, p.ground_density_hi);
|
|
|
+ out.opacity = std::max(0.0, std::min(1.0, 1.0 - ground));
|
|
|
+ return out;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
std::string fmt(const char* pattern, double value) {
|
|
|
char buf[128];
|
|
|
std::snprintf(buf, sizeof(buf), pattern, value);
|
|
|
@@ -230,6 +283,62 @@ std::string fmt(const char* pattern, double value) {
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
+double find_texture_top_y(const GrayImage& gray, const int window[4],
|
|
|
+ const EdgeV2Params& p) {
|
|
|
+ if (!p.texture_top_enable || gray.empty()) { return -1.0; }
|
|
|
+ const SobelResult grad = sobel(gray);
|
|
|
+ const int x0 = std::max(0, std::min(window[0], gray.width));
|
|
|
+ const int x1 = std::min(gray.width, std::max(x0, window[2]));
|
|
|
+ const int block = std::max(4, p.texture_block_px);
|
|
|
+ const int nb = (x1 - x0) / block;
|
|
|
+ if (nb < 2) { return -1.0; }
|
|
|
+ std::vector<double> cov(static_cast<std::size_t>(gray.height), 0.0);
|
|
|
+ for (int y = 0; y < gray.height; ++y) {
|
|
|
+ int hit = 0;
|
|
|
+ for (int i = 0; i < nb; ++i) {
|
|
|
+ const int bx = x0 + i * block;
|
|
|
+ int cnt = 0;
|
|
|
+ for (int x = bx; x < bx + block; ++x) {
|
|
|
+ const std::size_t idx =
|
|
|
+ static_cast<std::size_t>(y) * static_cast<std::size_t>(gray.width) +
|
|
|
+ static_cast<std::size_t>(x);
|
|
|
+ if (std::fabs(grad.gx[idx]) >= p.texture_grad_th) { ++cnt; }
|
|
|
+ }
|
|
|
+ if (static_cast<double>(cnt) / static_cast<double>(block) >=
|
|
|
+ p.texture_block_hit_ratio) {
|
|
|
+ ++hit;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cov[static_cast<std::size_t>(y)] =
|
|
|
+ static_cast<double>(hit) / static_cast<double>(nb);
|
|
|
+ }
|
|
|
+ const int smooth = std::max(1, p.texture_smooth_rows);
|
|
|
+ std::vector<double> sm(static_cast<std::size_t>(gray.height), 0.0);
|
|
|
+ for (int y = 0; y < gray.height; ++y) {
|
|
|
+ const int a = std::max(0, y - smooth / 2);
|
|
|
+ const int b = std::min(gray.height - 1, y + smooth / 2);
|
|
|
+ double sum = 0.0;
|
|
|
+ int n = 0;
|
|
|
+ for (int k = a; k <= b; ++k) {
|
|
|
+ sum += cov[static_cast<std::size_t>(k)];
|
|
|
+ ++n;
|
|
|
+ }
|
|
|
+ sm[static_cast<std::size_t>(y)] = n > 0 ? sum / static_cast<double>(n) : 0.0;
|
|
|
+ }
|
|
|
+ const int run = std::max(1, p.texture_run_rows);
|
|
|
+ for (int y = std::max(0, window[1]); y + run <= gray.height; ++y) {
|
|
|
+ bool ok = true;
|
|
|
+ for (int k = 0; k < run; ++k) {
|
|
|
+ if (sm[static_cast<std::size_t>(y + k)] < p.texture_coverage_min) {
|
|
|
+ ok = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (ok) { return static_cast<double>(y); }
|
|
|
+ }
|
|
|
+ return -1.0;
|
|
|
+}
|
|
|
+
|
|
|
bool EvidenceV2Result::publishable(bool require_commit) const {
|
|
|
if (state != 0 && state != 1 && state != 2) { return false; }
|
|
|
return !require_commit || committed;
|
|
|
@@ -254,10 +363,14 @@ Observation EvidenceV2Result::to_observation(double ts, long frame_id,
|
|
|
|
|
|
std::string EvidenceV2Result::evidence_summary() const {
|
|
|
std::string out = "state=" + std::to_string(state) + " view=" + view +
|
|
|
+ " top=" + (texture_top_y > 0.0 ? fmt("%.1f", texture_top_y) : "n/a") +
|
|
|
" posts=" + std::to_string(door_posts) + " opacity=" +
|
|
|
fmt("%.2f", wall_opacity) + " ground=" + fmt("%.2f", ground_ratio);
|
|
|
out += " deck=" + std::string(deck_edge ? "1" : "0");
|
|
|
out += " under_deck=" + std::string(under_deck ? "1" : "0");
|
|
|
+ out += " body=(" + std::to_string(body_posts) + "," +
|
|
|
+ fmt("%.2f", body_span) + "," + fmt("%.2f", body_opacity) + "," +
|
|
|
+ std::to_string(body_diagonal) + ")";
|
|
|
out += " gap=" + (gap_px.has_value() ? fmt("%.1f", *gap_px) + "px" : "n/a");
|
|
|
out += " gap_m=" + (gap_m.has_value() ? fmt("%.3f", *gap_m) : "n/a");
|
|
|
out += " box=" + (box_bottom_y.has_value() ? fmt("%.1f", *box_bottom_y) : "n/a") +
|
|
|
@@ -667,10 +780,32 @@ EvidenceV2Result EdgeV2::analyze(const GrayImage& gray, double ts, bool plc_arri
|
|
|
};
|
|
|
std::vector<V2Band> bright;
|
|
|
std::vector<V2Band> dark;
|
|
|
+ // P0-Q4:先找“竖向纹理车体顶”,把车体顶以上的天空电线/水印从候选带里剔除。
|
|
|
+ // 不移动窗口:近距画面车体顶≈窗口顶,门槛自然不触发;远距/带天空画面才生效。
|
|
|
+ const double texture_top = find_texture_top_y(work, window, p_);
|
|
|
+ out.texture_top_y =
|
|
|
+ texture_top > 0.0 ? texture_top / std::max(scale, 1e-6) : 0.0;
|
|
|
horizontal_bands(work, window[0], window[1], window[2], window[3],
|
|
|
std::max(1, static_cast<int>(p_.band_min_h * scale)),
|
|
|
std::max(6, static_cast<int>(p_.band_max_h_ratio * work.height)),
|
|
|
&bright, &dark);
|
|
|
+ if (texture_top > 0.0 &&
|
|
|
+ texture_top > window[1] + 0.05 * work.height) {
|
|
|
+ const double margin = p_.texture_top_margin_ratio * work.height;
|
|
|
+ const std::vector<V2Band> bright_all = bright;
|
|
|
+ const std::vector<V2Band> dark_all = dark;
|
|
|
+ const auto filter_above = [&](std::vector<V2Band>& bands) {
|
|
|
+ std::vector<V2Band> kept;
|
|
|
+ for (const V2Band& b : bands) {
|
|
|
+ if (b.y1 >= texture_top - margin) { kept.push_back(b); }
|
|
|
+ }
|
|
|
+ if (!kept.empty()) { bands.swap(kept); }
|
|
|
+ };
|
|
|
+ filter_above(bright);
|
|
|
+ filter_above(dark);
|
|
|
+ if (bright.empty()) { bright = bright_all; }
|
|
|
+ if (dark.empty()) { dark = dark_all; }
|
|
|
+ }
|
|
|
|
|
|
const double box_hint = std::max(80.0, 0.40 * work.height);
|
|
|
V2Band box;
|
|
|
@@ -969,31 +1104,98 @@ EvidenceV2Result EdgeV2::analyze(const GrayImage& gray, double ts, bool plc_arri
|
|
|
static_cast<int>(posts.size()), spacing_cv, opacity, ground, deck_edge, 0, gap_px,
|
|
|
still, p_.view, crop.height / std::max(scale, 1e-6), under_deck,
|
|
|
post_spacing_px_orig, diagonal, post_span_ratio);
|
|
|
+
|
|
|
+ // P0-Q4:空车/无箱兜底。正常“箱底以下”路径给不出 1(例如空 C70 的
|
|
|
+ // 箱底是车体内部的一条横梁,下面数不到立柱)时,用纹理车体 ROI 再数一次
|
|
|
+ // 门板立柱;只有“上方能看到天空/车体顶”且正常路径不是 1 时才启用。
|
|
|
+ int final_state = cls.state;
|
|
|
+ double final_conf = cls.confidence;
|
|
|
+ double final_hits = cls.structure_hits;
|
|
|
+ std::string final_reason = cls.reason;
|
|
|
+ std::optional<double> commit_gap_px = gap_px;
|
|
|
+ std::optional<double> commit_gap_m = gap_m;
|
|
|
+ BodyEvidence body;
|
|
|
+ bool body_override = false;
|
|
|
+ int body_y0 = 0;
|
|
|
+ int body_y1 = 0;
|
|
|
+ if (p_.body_fallback_enable && texture_top > 0.0 && cls.state != 1 &&
|
|
|
+ texture_top > window[1] + 0.05 * work.height) {
|
|
|
+ body_y0 = std::max(window[1], static_cast<int>(std::lround(texture_top)));
|
|
|
+ body_y1 = std::min(
|
|
|
+ window[3], static_cast<int>(std::lround(
|
|
|
+ texture_top + p_.body_bottom_ratio *
|
|
|
+ (window[3] - texture_top))));
|
|
|
+ if (body_y1 > body_y0) {
|
|
|
+ body = measure_body(work, window[0], body_y0, window[2], body_y1, p_, scale);
|
|
|
+ const double body_h_orig = body.height / std::max(scale, 1e-6);
|
|
|
+ if (body_h_orig >= static_cast<double>(p_.body_min_roi_px) &&
|
|
|
+ body.posts >= p_.min_door_posts &&
|
|
|
+ body.span_ratio >= p_.min_post_span_ratio &&
|
|
|
+ body.opacity >= p_.body_min_opacity &&
|
|
|
+ body.diagonal >= p_.min_diagonal_braces) {
|
|
|
+ body_override = true;
|
|
|
+ final_state = 1;
|
|
|
+ final_conf = std::min(
|
|
|
+ 1.0, 0.55 + 0.25 * std::min(1.0, body.posts / 8.0) +
|
|
|
+ 0.15 * body.opacity);
|
|
|
+ final_hits = std::min(
|
|
|
+ 1.0, 0.40 + 0.05 * body.posts + 0.20 * body.opacity);
|
|
|
+ final_reason = "body_fallback_wall";
|
|
|
+ commit_gap_px = 0.0;
|
|
|
+ commit_gap_m = 0.0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 安全兜底:能看到“车体顶(上方有天空)”且正常路径给 2,但箱底以下
|
|
|
+ // 一根立柱都没有、箱体还在车体内部 → 这是空车/无箱的歧义帧,绝不判 2。
|
|
|
+ if (!body_override && texture_top > 0.0 && cls.state == 2 &&
|
|
|
+ static_cast<int>(posts.size()) == 0 &&
|
|
|
+ box.y1 > texture_top + 0.02 * work.height) {
|
|
|
+ final_state = 255;
|
|
|
+ final_conf = 0.0;
|
|
|
+ final_hits = 0.0;
|
|
|
+ final_reason = "ambiguous_empty_vehicle";
|
|
|
+ }
|
|
|
+
|
|
|
const V2CommitState commit = tracker_.update(
|
|
|
- ts, cls.state, gap_m, gap_px, cls.structure_hits, still, p_.view != "unknown",
|
|
|
- plc_arrived, plc_spreader_released, hoist_flag);
|
|
|
+ ts, final_state, commit_gap_m, commit_gap_px, final_hits, still,
|
|
|
+ p_.view != "unknown", plc_arrived, plc_spreader_released, hoist_flag);
|
|
|
|
|
|
- out.state = cls.state;
|
|
|
- out.confidence = cls.confidence;
|
|
|
+ out.state = final_state;
|
|
|
+ out.confidence = final_conf;
|
|
|
out.view = p_.view;
|
|
|
+ out.body_posts = body.posts;
|
|
|
+ out.body_span = body.span_ratio;
|
|
|
+ out.body_opacity = body.opacity;
|
|
|
+ out.body_diagonal = body.diagonal;
|
|
|
out.box_bottom_y = box.y1 / std::max(scale, 1e-6);
|
|
|
out.box_top_y = box_top;
|
|
|
out.box_h_px = box_h;
|
|
|
out.px_per_m = px_per_m;
|
|
|
out.scale_ok = px_per_m.has_value();
|
|
|
if (bed.height() > 0.0) { out.bed_top_y = bed.y0 / std::max(scale, 1e-6); }
|
|
|
- out.gap_px = gap_px;
|
|
|
- out.gap_m = gap_m;
|
|
|
- out.door_posts = static_cast<int>(posts.size());
|
|
|
- if (spacing_median.has_value()) {
|
|
|
- out.post_spacing_px = *spacing_median / std::max(scale, 1e-6);
|
|
|
- }
|
|
|
- out.post_spacing_cv = spacing_cv;
|
|
|
- out.diagonal_braces = diagonal;
|
|
|
- out.wall_opacity = opacity;
|
|
|
- out.deck_edge = deck_edge;
|
|
|
- out.under_deck = under_deck;
|
|
|
- out.ground_ratio = ground;
|
|
|
+ out.gap_px = commit_gap_px;
|
|
|
+ out.gap_m = commit_gap_m;
|
|
|
+ if (body_override) {
|
|
|
+ out.door_posts = body.posts;
|
|
|
+ out.diagonal_braces = body.diagonal;
|
|
|
+ out.wall_opacity = body.opacity;
|
|
|
+ out.deck_edge = false;
|
|
|
+ out.under_deck = false;
|
|
|
+ out.ground_ratio = 1.0 - body.opacity;
|
|
|
+ } else {
|
|
|
+ out.door_posts = static_cast<int>(posts.size());
|
|
|
+ if (spacing_median.has_value()) {
|
|
|
+ out.post_spacing_px = *spacing_median / std::max(scale, 1e-6);
|
|
|
+ }
|
|
|
+ out.post_spacing_cv = spacing_cv;
|
|
|
+ out.diagonal_braces = diagonal;
|
|
|
+ out.wall_opacity = opacity;
|
|
|
+ out.deck_edge = deck_edge;
|
|
|
+ out.under_deck = under_deck;
|
|
|
+ out.ground_ratio = ground;
|
|
|
+ }
|
|
|
out.phase = still ? commit.phase : "T3";
|
|
|
out.committed = commit.committed;
|
|
|
out.commit_reason = commit.reason;
|
|
|
@@ -1010,13 +1212,20 @@ EvidenceV2Result EdgeV2::analyze(const GrayImage& gray, double ts, bool plc_arri
|
|
|
// 弃权原因要能直接回答"为什么不用这一帧":吊具未松开与吊具高悬是两件事,
|
|
|
// 现场排查方向不同(前者看开锁信号,后者看起升高度),所以这里分开写。
|
|
|
out.abstain_reason = !hoist_flag
|
|
|
- ? (cls.state == 255 ? cls.reason : "")
|
|
|
+ ? (final_state == 255 ? final_reason : "")
|
|
|
: (spreader_cues.contact ? "spreader_contact" : "hoist_high");
|
|
|
- out.below_roi[0] = static_cast<int>(std::lround(x0w / scale));
|
|
|
- out.below_roi[1] = static_cast<int>(std::lround(y0w / scale));
|
|
|
- out.below_roi[2] = static_cast<int>(std::lround(x1w / scale));
|
|
|
- out.below_roi[3] = static_cast<int>(std::lround((y0w + crop.height) / scale));
|
|
|
- out.class_reason = cls.reason;
|
|
|
+ if (body_override) {
|
|
|
+ out.below_roi[0] = static_cast<int>(std::lround(window[0] / scale));
|
|
|
+ out.below_roi[1] = static_cast<int>(std::lround(body_y0 / scale));
|
|
|
+ out.below_roi[2] = static_cast<int>(std::lround(window[2] / scale));
|
|
|
+ out.below_roi[3] = static_cast<int>(std::lround(body_y1 / scale));
|
|
|
+ } else {
|
|
|
+ out.below_roi[0] = static_cast<int>(std::lround(x0w / scale));
|
|
|
+ out.below_roi[1] = static_cast<int>(std::lround(y0w / scale));
|
|
|
+ out.below_roi[2] = static_cast<int>(std::lround(x1w / scale));
|
|
|
+ out.below_roi[3] = static_cast<int>(std::lround((y0w + crop.height) / scale));
|
|
|
+ }
|
|
|
+ out.class_reason = final_reason;
|
|
|
last_ = out;
|
|
|
(void)edge_density;
|
|
|
(void)motion_diff;
|