diff --git a/src/CVObjectDetection.cpp b/src/CVObjectDetection.cpp index 1e646a15..7fe03387 100644 --- a/src/CVObjectDetection.cpp +++ b/src/CVObjectDetection.cpp @@ -429,11 +429,12 @@ void CVObjectDetection::postprocess(const cv::Size &frameDims, const std::vector const int maskCoefficientCount = attributes - 4 - classCount; const cv::Mat* prototype = nullptr; if (generateMasks && maskCoefficientCount > 0) { - for (const auto& out : outs) { - if (out.dims == 4 && out.size[0] == 1 && out.size[1] == maskCoefficientCount) { - prototype = &out; - break; - } + auto prototypeIt = std::find_if(outs.begin(), outs.end(), + [maskCoefficientCount](const cv::Mat& out) { + return out.dims == 4 && out.size[0] == 1 && out.size[1] == maskCoefficientCount; + }); + if (prototypeIt != outs.end()) { + prototype = &(*prototypeIt); } } const float* data = reinterpret_cast(det.data); diff --git a/src/sort_filter/sort.cpp b/src/sort_filter/sort.cpp index d89673f0..99657cc9 100644 --- a/src/sort_filter/sort.cpp +++ b/src/sort_filter/sort.cpp @@ -8,6 +8,11 @@ using namespace std; namespace { +struct DetectionBox : public TrackingBox +{ + std::vector classScores; +}; + double box_diagonal(const cv::Rect_& box) { return std::sqrt(box.width * box.width + box.height * box.height); @@ -47,7 +52,7 @@ bool box_shape_matches(const cv::Rect_& predicted_box, const cv::Rect_& predicted_box, - const TrackingBox& detection, + const DetectionBox& detection, double iou, double centroid_distance) { @@ -109,11 +114,11 @@ double SortTracker::GetCentroidsDistance( } // Function to apply NMS on detections -void apply_nms(vector& detections, double nms_iou_thresh) { +void apply_nms(vector& detections, double nms_iou_thresh) { if (detections.empty()) return; // Sort detections by confidence descending - std::sort(detections.begin(), detections.end(), [](const TrackingBox& a, const TrackingBox& b) { + std::sort(detections.begin(), detections.end(), [](const DetectionBox& a, const DetectionBox& b) { return a.confidence > b.confidence; }); @@ -134,7 +139,7 @@ void apply_nms(vector& detections, double nms_iou_thresh) { } // Remove suppressed detections - vector filtered; + vector filtered; for (size_t i = 0; i < detections.size(); ++i) { if (!suppressed[i]) { filtered.push_back(detections[i]); @@ -145,7 +150,7 @@ void apply_nms(vector& detections, double nms_iou_thresh) { void SortTracker::update(vector detections_cv, int frame_count, double image_diagonal, std::vector confidences, std::vector classIds, std::vector> classScores) { - vector detections; + vector detections; dead_trackers_id.clear(); if (classScores.size() != detections_cv.size()) classScores.resize(detections_cv.size()); @@ -157,7 +162,7 @@ void SortTracker::update(vector detections_cv, int frame_count, double { if (confidences[i] < _min_conf) continue; // filter low conf - TrackingBox tb; + DetectionBox tb; tb.box = cv::Rect_(detections_cv[i]); tb.classId = classIds[i]; @@ -181,7 +186,7 @@ void SortTracker::update(vector detections_cv, int frame_count, double { if (confidences[i] < _min_conf) continue; // filter low conf - TrackingBox tb; + DetectionBox tb; tb.box = cv::Rect_(detections_cv[i]); tb.classId = classIds[i]; tb.confidence = confidences[i]; @@ -320,7 +325,7 @@ void SortTracker::update(vector detections_cv, int frame_count, double { alive_tracker = true; TrackingBox res; - if (trackers[i].m_time_since_update > 0 && i < predictedBoxes.size()) + if (i < predictedBoxes.size() && trackers[i].m_time_since_update > 0) res.box = predictedBoxes[i]; else res.box = trackers[i].get_state(); diff --git a/src/sort_filter/sort.hpp b/src/sort_filter/sort.hpp index cc6742b7..04e44288 100644 --- a/src/sort_filter/sort.hpp +++ b/src/sort_filter/sort.hpp @@ -26,7 +26,6 @@ typedef struct TrackingBox int classId = 0; int id = 0; cv::Rect_ box = cv::Rect_(0.0, 0.0, 0.0, 0.0); - std::vector classScores; TrackingBox() {} TrackingBox(int _frame, float _confidence, int _classId, int _id) : frame(_frame), confidence(_confidence), classId(_classId), id(_id) {} } TrackingBox; @@ -35,7 +34,7 @@ class SortTracker { public: // Constructor - SortTracker(int max_age = 50, int min_hits = 5, int max_missed = 3, double min_iou = 0.1, double nms_iou_thresh = 0.5, double min_conf = 0.3); + explicit SortTracker(int max_age = 50, int min_hits = 5, int max_missed = 3, double min_iou = 0.1, double nms_iou_thresh = 0.5, double min_conf = 0.3); // Initialize tracker // Update position based on the new frame