-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_tracker.cpp
More file actions
70 lines (63 loc) · 2.26 KB
/
dynamic_tracker.cpp
File metadata and controls
70 lines (63 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <opencv2/core/ocl.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
#include "dynamic_tracker.h"
#include "manager.h"
using namespace std;
using namespace cv;
void DynamicTracker::init() {}
void DynamicTracker::startTracking(
const shared_ptr<Mat> &frame,
const vector<ObjectInformation> &detectionOutput)
{
trackers.clear();
failedCount.clear();
this->frame = frame;
// Create trackers for each detected object
for (const auto &objectInformation : detectionOutput) {
Ptr<Tracker> tracker = TrackerCSRT::create();
tracker->init(*frame, objectInformation.position);
trackers.push_back(tracker);
failedCount.push_back(0);
}
}
void DynamicTracker::tracking(const shared_ptr<Mat> &frame,
std::vector<ObjectInformation> &objectInformation)
{
this->frame = frame;
Rect bbox;
std::vector<size_t> toRemove; // Store indices to remove
// Update tracking results for each tracker
for (size_t i = 0; i < trackers.size(); ++i) {
bool ok = trackers[i]->update(*frame, bbox);
if (ok) {
objectInformation[i].prevPosition = objectInformation[i].position;
objectInformation[i].position = bbox;
failedCount[i] = 0; // Reset failure count on successful tracking
}
else {
failedCount[i]++;
if (failedCount[i] > maxFailures) {
toRemove.push_back(i); // Mark for removal
}
}
}
// Remove trackers, objectInformation, and failedCount entries after the loop
for (int i = toRemove.size() - 1; i >= 0; --i) {
size_t idx = toRemove[i];
trackers.erase(trackers.begin() + idx);
objectInformation.erase(objectInformation.begin() + idx);
failedCount.erase(failedCount.begin() + idx);
}
}
void DynamicTracker::drawTracking(shared_ptr<Mat> image,
vector<ObjectInformation> &objects)
{
for (const auto &objectInformation : objects) {
Scalar boxColor =
(objectInformation.distance < (Alerter::MIN_LEGAL_DISTANCE))
? Scalar(0, 0, 255)
: Scalar(0, 255, 0);
rectangle(*image, objectInformation.position, boxColor, 2);
}
}