#include "portcv/scene.hpp" #include #include #include #include namespace portcv { namespace { constexpr int kUnknown = static_cast(SceneState::UNKNOWN); std::string fmt(const char* pattern, double a) { char buf[128]; std::snprintf(buf, sizeof(buf), pattern, a); return std::string(buf); } } // namespace SceneClassifier::SceneClassifier(const SceneParams& params) : p_(params) { auto norm_list = [](const std::vector& in) { std::vector out; for (const std::string& name : in) { const std::string n = SceneClassifier::normalize(name); if (!n.empty()) { out.push_back(n); } } return out; }; container_ = norm_list(p_.container_names); bed_ = norm_list(p_.bed_names); flatbed_ = norm_list(p_.flatbed_names); ignore_ = norm_list(p_.ignore_names); std::map direct; for (const auto& kv : p_.direct_map) { direct[normalize(kv.first)] = kv.second; } // 与 Python 一致:三个标准标签始终可用 if (direct.find("not_on_train") == direct.end()) { direct["not_on_train"] = 0; } if (direct.find("bed") == direct.end()) { direct["bed"] = 1; } if (direct.find("flatbed") == direct.end()) { direct["flatbed"] = 2; } p_.direct_map = direct; } std::string SceneClassifier::normalize(const std::string& name) { std::string out; out.reserve(name.size()); for (char ch : name) { if (ch == '-' || ch == ' ') { out.push_back('_'); } else { out.push_back(static_cast(std::tolower(static_cast(ch)))); } } // 去首尾空白(简单实现,覆盖 config 手写场景) std::size_t begin = out.find_first_not_of(" \t\r\n"); if (begin == std::string::npos) { return std::string(); } std::size_t end = out.find_last_not_of(" \t\r\n"); return out.substr(begin, end - begin + 1); } std::string SceneClassifier::group_of(const std::string& class_name) const { const std::string name = normalize(class_name); auto has = [&name](const std::vector& list) { return std::find(list.begin(), list.end(), name) != list.end(); }; if (has(container_)) { return "container"; } if (has(bed_)) { return "bed"; } if (has(flatbed_)) { return "flatbed"; } if (has(ignore_)) { return "ignore"; } return "unknown"; } std::vector SceneClassifier::roi_polygon(int width, int height) const { return normalized_to_pixel_polygon(p_.roi, width, height); } Observation SceneClassifier::classify(const std::vector& detections, int width, int height, double ts, long frame_id) const { std::vector boxes; boxes.reserve(detections.size()); for (const Box& box : detections) { if (static_cast(box.score) >= p_.min_class_conf) { boxes.push_back(box); } } if (p_.mode == "direct") { return classify_direct(boxes, width, height, ts, frame_id); } return classify_composed(boxes, width, height, ts, frame_id); } double SceneClassifier::overlap_x1d(double a1, double a2, double b1, double b2) { const double inter = (a2 < b2 ? a2 : b2) - (a1 > b1 ? a1 : b1); if (inter <= 0.0) { return 0.0; } const double shorter = (a2 - a1) < (b2 - b1) ? (a2 - a1) : (b2 - b1); return shorter > 0.0 ? inter / shorter : 0.0; } bool SceneClassifier::support_quality(const Box& container, const Box& wagon, double* ov_x, double* quality) const { if (container.height() <= 0.0f) { return false; } if (static_cast(wagon.score) < p_.support_score_ratio * container.score) { return false; } // 车厢顶边不应明显高于集装箱顶边(否则多半是别的车厢/背景) if (static_cast(wagon.top()) < static_cast(container.top()) - 0.05 * container.height()) { return false; } const double ov = overlap_x1d(container.x1, container.x2, wagon.x1, wagon.x2); if (ov < p_.support_overlap) { return false; } const double dy_ratio = std::fabs(static_cast(wagon.top()) - container.bottom()) / container.height(); if (dy_ratio > p_.support_dy_ratio) { return false; } const double tol = p_.support_dy_ratio > 1e-6 ? p_.support_dy_ratio : 1e-6; *ov_x = ov; *quality = ov * std::fmax(0.2, 1.0 - 0.5 * (dy_ratio / tol)); return true; } bool SceneClassifier::best_support(const Box& container, const std::vector>& wagons, Box* wagon_out, double* score_out, double* ovx_out) const { bool found = false; double best_score = 0.0; for (const auto& item : wagons) { double ov_x = 0.0; double quality = 0.0; if (!support_quality(container, item.first, &ov_x, &quality)) { continue; } const double score = static_cast(item.first.score) * quality; if (!found || score > best_score) { found = true; best_score = score; *wagon_out = item.first; *score_out = score; *ovx_out = ov_x; } } return found; } Observation SceneClassifier::classify_direct(std::vector& boxes, int width, int height, double ts, long frame_id) const { Observation obs; obs.ts = ts; obs.frame_id = frame_id; obs.boxes = boxes; const std::vector poly = roi_polygon(width, height); bool found = false; Box best_box; int best_state = kUnknown; double best_score = 0.0; for (const Box& box : boxes) { const std::string name = normalize(box.class_name); const auto it = p_.direct_map.find(name); if (it == p_.direct_map.end()) { continue; } const double ratio = box_in_polygon_ratio(box, poly); if (ratio < p_.roi_min_ratio) { continue; } const double score = static_cast(box.score) * (0.5 + 0.5 * ratio); if (!found || score > best_score) { found = true; best_box = box; best_state = it->second; best_score = score; } } if (!found) { obs.reason = "no_direct_hit"; if (p_.unknown_on_no_detection) { obs.state = kUnknown; obs.confidence = 0.0; } else { obs.state = static_cast(SceneState::NOT_ON_TRAIN); obs.confidence = p_.no_container_conf; } return obs; } char buf[128]; std::snprintf(buf, sizeof(buf), "direct:%s=%.2f", best_box.class_name.c_str(), static_cast(best_box.score)); obs.state = best_state; obs.confidence = std::fmin(1.0, static_cast(best_box.score)); obs.reason = buf; return obs; } Observation SceneClassifier::classify_composed(std::vector& boxes, int width, int height, double ts, long frame_id) const { Observation obs; obs.ts = ts; obs.frame_id = frame_id; obs.boxes = boxes; if (boxes.empty()) { obs.reason = "no_detection"; if (p_.unknown_on_no_detection) { obs.state = kUnknown; obs.confidence = 0.0; } else { obs.state = static_cast(SceneState::NOT_ON_TRAIN); obs.confidence = p_.no_container_conf; } return obs; } const std::vector poly = roi_polygon(width, height); std::vector> containers; std::vector> beds; std::vector> flats; for (const Box& box : boxes) { const std::string group = group_of(box.class_name); if (group == "ignore" || group == "unknown") { continue; } const double ratio = box_in_polygon_ratio(box, poly); if (ratio < p_.roi_min_ratio) { continue; } if (group == "container") { containers.push_back({box, ratio}); } else if (group == "bed") { beds.push_back({box, ratio}); } else { flats.push_back({box, ratio}); } } if (containers.empty()) { // ROI 内没有集装箱 → "集装箱不在车上" double wagon_score = 0.0; for (const auto& item : beds) { wagon_score = std::fmax(wagon_score, item.first.score); } for (const auto& item : flats) { wagon_score = std::fmax(wagon_score, item.first.score); } obs.state = static_cast(SceneState::NOT_ON_TRAIN); obs.confidence = std::fmin(1.0, 0.55 + 0.35 * wagon_score); std::string reason = "no_container"; if (wagon_score > 0.0) { char buf[64]; std::snprintf(buf, sizeof(buf), "_wagon_seen=%.2f", wagon_score); reason += buf; } obs.reason = reason; return obs; } // 主箱:得分 × (0.5 + 0.5 × ROI 占比) 最大者 Box container = containers[0].first; double best_container = -1.0; for (const auto& item : containers) { const double score = static_cast(item.first.score) * (0.5 + 0.5 * item.second); if (score > best_container) { best_container = score; container = item.first; } } Box bed_wagon; Box flat_wagon; double bed_score = 0.0; double bed_ov = 0.0; double flat_score = 0.0; double flat_ov = 0.0; const bool has_bed = best_support(container, beds, &bed_wagon, &bed_score, &bed_ov); const bool has_flat = best_support(container, flats, &flat_wagon, &flat_score, &flat_ov); if (!has_bed && !has_flat) { // 有箱,但底下既不是车斗也不是平板车(吊起来/在卡车上/堆场) obs.state = static_cast(SceneState::NOT_ON_TRAIN); obs.confidence = std::fmin(0.75, 0.40 + 0.25 * container.score); char buf[128]; std::snprintf(buf, sizeof(buf), "container_without_wagon(c=%.2f)", static_cast(container.score)); obs.reason = buf; return obs; } int state = static_cast(SceneState::BED); Box wagon = bed_wagon; double ov_x = bed_ov; const char* tag = "bed"; if (has_bed && has_flat) { if (flat_score > bed_score) { // 平票时保持 bed(与 Python 的 max 语义一致) state = static_cast(SceneState::FLATBED); wagon = flat_wagon; ov_x = flat_ov; tag = "flatbed"; } } else if (has_flat) { state = static_cast(SceneState::FLATBED); wagon = flat_wagon; ov_x = flat_ov; tag = "flatbed"; } double conf = std::sqrt(static_cast(container.score) * wagon.score) * (0.55 + 0.45 * ov_x); bool ambiguous = false; if (has_bed && has_flat) { const double hi = std::fmax(bed_score, flat_score); const double lo = std::fmin(bed_score, flat_score); ambiguous = hi < 1.10 * lo; } if (ambiguous) { conf *= 0.85; } // 两种车厢都像 → 降权,交给时序滤波去稳定 char buf[160]; std::snprintf(buf, sizeof(buf), "c=%.2f %s=%.2f ov=%.2f%s", static_cast(container.score), tag, static_cast(wagon.score), ov_x, ambiguous ? " ambiguous" : ""); obs.state = state; obs.confidence = std::fmin(1.0, conf); obs.reason = buf; return obs; } } // namespace portcv