| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- #include "portcv/scene.hpp"
- #include <cctype>
- #include <cmath>
- #include <cstdio>
- #include <map>
- namespace portcv {
- namespace {
- constexpr int kUnknown = static_cast<int>(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<std::string>& in) {
- std::vector<std::string> 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<std::string, int> 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<char>(std::tolower(static_cast<unsigned char>(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<std::string>& 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<Point> SceneClassifier::roi_polygon(int width, int height) const {
- return normalized_to_pixel_polygon(p_.roi, width, height);
- }
- Observation SceneClassifier::classify(const std::vector<Box>& detections, int width, int height,
- double ts, long frame_id) const {
- std::vector<Box> boxes;
- boxes.reserve(detections.size());
- for (const Box& box : detections) {
- if (static_cast<double>(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<double>(wagon.score) < p_.support_score_ratio * container.score) { return false; }
- // 车厢顶边不应明显高于集装箱顶边(否则多半是别的车厢/背景)
- if (static_cast<double>(wagon.top()) <
- static_cast<double>(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<double>(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<std::pair<Box, double>>& 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<double>(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<Box>& 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<Point> 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<double>(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<int>(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<double>(best_box.score));
- obs.state = best_state;
- obs.confidence = std::fmin(1.0, static_cast<double>(best_box.score));
- obs.reason = buf;
- return obs;
- }
- Observation SceneClassifier::classify_composed(std::vector<Box>& 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<int>(SceneState::NOT_ON_TRAIN);
- obs.confidence = p_.no_container_conf;
- }
- return obs;
- }
- const std::vector<Point> poly = roi_polygon(width, height);
- std::vector<std::pair<Box, double>> containers;
- std::vector<std::pair<Box, double>> beds;
- std::vector<std::pair<Box, double>> 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<int>(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<double>(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<int>(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<double>(container.score));
- obs.reason = buf;
- return obs;
- }
- int state = static_cast<int>(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<int>(SceneState::FLATBED);
- wagon = flat_wagon;
- ov_x = flat_ov;
- tag = "flatbed";
- }
- } else if (has_flat) {
- state = static_cast<int>(SceneState::FLATBED);
- wagon = flat_wagon;
- ov_x = flat_ov;
- tag = "flatbed";
- }
- double conf =
- std::sqrt(static_cast<double>(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<double>(container.score), tag, static_cast<double>(wagon.score), ov_x,
- ambiguous ? " ambiguous" : "");
- obs.state = state;
- obs.confidence = std::fmin(1.0, conf);
- obs.reason = buf;
- return obs;
- }
- } // namespace portcv
|