You've already forked libopenshot
mirror of
https://github.com/OpenShot/libopenshot.git
synced 2026-06-08 22:17:28 -07:00
Merge branch 'object-detection-v2' into object-mask-effect
This commit is contained in:
@@ -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<const float*>(det.data);
|
||||
|
||||
@@ -8,6 +8,11 @@
|
||||
using namespace std;
|
||||
|
||||
namespace {
|
||||
struct DetectionBox : public TrackingBox
|
||||
{
|
||||
std::vector<ClassScore> classScores;
|
||||
};
|
||||
|
||||
double box_diagonal(const cv::Rect_<float>& box)
|
||||
{
|
||||
return std::sqrt(box.width * box.width + box.height * box.height);
|
||||
@@ -47,7 +52,7 @@ bool box_shape_matches(const cv::Rect_<float>& predicted_box, const cv::Rect_<fl
|
||||
bool detection_matches_track_gate(
|
||||
const KalmanTracker& tracker,
|
||||
const cv::Rect_<float>& 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<TrackingBox>& detections, double nms_iou_thresh) {
|
||||
void apply_nms(vector<DetectionBox>& 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<TrackingBox>& detections, double nms_iou_thresh) {
|
||||
}
|
||||
|
||||
// Remove suppressed detections
|
||||
vector<TrackingBox> filtered;
|
||||
vector<DetectionBox> 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<TrackingBox>& detections, double nms_iou_thresh) {
|
||||
|
||||
void SortTracker::update(vector<cv::Rect> detections_cv, int frame_count, double image_diagonal, std::vector<float> confidences, std::vector<int> classIds, std::vector<std::vector<ClassScore>> classScores)
|
||||
{
|
||||
vector<TrackingBox> detections;
|
||||
vector<DetectionBox> detections;
|
||||
dead_trackers_id.clear();
|
||||
if (classScores.size() != detections_cv.size())
|
||||
classScores.resize(detections_cv.size());
|
||||
@@ -157,7 +162,7 @@ void SortTracker::update(vector<cv::Rect> detections_cv, int frame_count, double
|
||||
{
|
||||
if (confidences[i] < _min_conf) continue; // filter low conf
|
||||
|
||||
TrackingBox tb;
|
||||
DetectionBox tb;
|
||||
|
||||
tb.box = cv::Rect_<float>(detections_cv[i]);
|
||||
tb.classId = classIds[i];
|
||||
@@ -181,7 +186,7 @@ void SortTracker::update(vector<cv::Rect> detections_cv, int frame_count, double
|
||||
{
|
||||
if (confidences[i] < _min_conf) continue; // filter low conf
|
||||
|
||||
TrackingBox tb;
|
||||
DetectionBox tb;
|
||||
tb.box = cv::Rect_<float>(detections_cv[i]);
|
||||
tb.classId = classIds[i];
|
||||
tb.confidence = confidences[i];
|
||||
@@ -320,7 +325,7 @@ void SortTracker::update(vector<cv::Rect> 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();
|
||||
|
||||
@@ -26,7 +26,6 @@ typedef struct TrackingBox
|
||||
int classId = 0;
|
||||
int id = 0;
|
||||
cv::Rect_<float> box = cv::Rect_<float>(0.0, 0.0, 0.0, 0.0);
|
||||
std::vector<ClassScore> 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
|
||||
|
||||
Reference in New Issue
Block a user