Skip to content

Commit 6b36daf

Browse files
committed
Create new base class for objects id
1 parent 2df9553 commit 6b36daf

12 files changed

Lines changed: 268 additions & 172 deletions

File tree

async_detector/AsyncDetector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ void AsyncDetector::DrawTrack(cv::Mat frame,
176176

177177
if (drawTrajectory)
178178
{
179-
cv::Scalar cl = m_colors[track.m_ID % m_colors.size()];
179+
cv::Scalar cl = m_colors[track.m_ID.ID2Module(m_colors.size())];
180180

181181
for (size_t j = 0; j < track.m_trace.size() - 1; ++j)
182182
{

example/CarsCounting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void CarsCounting::DrawTrack(cv::Mat frame, const TrackingObject& track, bool dr
123123

124124
if (drawTrajectory)
125125
{
126-
cv::Scalar cl = m_colors[track.m_ID % m_colors.size()];
126+
cv::Scalar cl = m_colors[track.m_ID.ID2Module(m_colors.size())];
127127

128128
for (size_t j = 0; j < track.m_trace.size() - 1; ++j)
129129
{

example/CarsCounting.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <unordered_set>
34
#include "VideoExample.h"
45

56
///
@@ -180,7 +181,7 @@ class RoadLine
180181
/// \param pt2
181182
/// \return
182183
///
183-
int IsIntersect(size_t objID, cv::Point2f pt1, cv::Point2f pt2)
184+
int IsIntersect(track_id_t objID, cv::Point2f pt1, cv::Point2f pt2)
184185
{
185186
int direction = 0;
186187

@@ -240,7 +241,7 @@ class RoadLine
240241

241242
private:
242243

243-
std::set<size_t> m_lastIntersections;
244+
std::unordered_set<track_id_t> m_lastIntersections;
244245

245246
///
246247
/// \brief CheckIntersection

example/FileLogger.h

Lines changed: 157 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,165 +1,166 @@
11
#pragma once
22
#include <fstream>
33
#include <map>
4+
#include <unordered_set>
45

56
#include <opencv2/opencv.hpp>
67

78
#include "object_types.h"
8-
9-
///
10-
/// \brief The ResultsLog class
11-
///
12-
class ResultsLog
13-
{
14-
public:
15-
///
16-
ResultsLog(const std::string& fileName, int writeEachNFrame)
17-
: m_fileName(fileName), m_writeEachNFrame(writeEachNFrame)
18-
{
19-
}
20-
21-
///
22-
~ResultsLog()
23-
{
24-
WriteAll(true);
25-
}
26-
27-
///
28-
bool Open()
29-
{
30-
m_resCSV.close();
31-
if (m_fileName.size() > 5)
32-
{
33-
m_resCSV.open(m_fileName);
34-
return m_resCSV.is_open();
35-
}
36-
return false;
37-
}
38-
39-
///
40-
bool AddTrack(int framesCounter, size_t trackID, const cv::Rect& brect, objtype_t type, float confidence)
41-
{
42-
if (m_resCSV.is_open())
43-
{
44-
auto frame = m_frames.find(framesCounter);
45-
if (frame == std::end(m_frames))
46-
{
47-
DetectsOnFrame tmpFrame;
48-
tmpFrame.m_detects.emplace_back(trackID, brect, type, confidence);
49-
m_frames.emplace(framesCounter, tmpFrame);
50-
}
51-
else
52-
{
53-
frame->second.m_detects.emplace_back(trackID, brect, type, confidence);
54-
}
55-
return true;
56-
}
57-
return false;
58-
}
59-
60-
///
61-
void AddRobustTrack(size_t trackID)
62-
{
63-
m_robustIDs.insert(trackID);
64-
}
65-
66-
///
67-
void Flush()
68-
{
69-
WriteAll(true);
70-
m_frames.clear();
71-
}
72-
73-
private:
74-
std::string m_fileName;
75-
std::ofstream m_resCSV;
76-
77-
///
78-
struct Detection
79-
{
80-
cv::Rect m_rect;
81-
objtype_t m_type;
82-
float m_conf = 0.f;
83-
size_t m_trackID = 0;
84-
85-
Detection(size_t trackID, const cv::Rect& brect, objtype_t type, float confidence)
86-
{
87-
m_type = type;
88-
m_rect = brect;
89-
m_conf = confidence;
90-
m_trackID = trackID;
91-
}
92-
};
93-
94-
///
95-
struct DetectsOnFrame
96-
{
97-
std::vector<Detection> m_detects;
98-
};
99-
std::map<int, DetectsOnFrame> m_frames;
100-
std::set<size_t> m_robustIDs;
101-
int m_writeEachNFrame = 1;
102-
103-
///
104-
void WriteAll(bool byFrames)
105-
{
106-
if (byFrames)
107-
{
108-
#if 1
109-
char delim = ',';
110-
for (const auto& frame : m_frames)
111-
{
9+
10+
///
11+
/// \brief The ResultsLog class
12+
///
13+
class ResultsLog
14+
{
15+
public:
16+
///
17+
ResultsLog(const std::string& fileName, int writeEachNFrame)
18+
: m_fileName(fileName), m_writeEachNFrame(writeEachNFrame)
19+
{
20+
}
21+
22+
///
23+
~ResultsLog()
24+
{
25+
WriteAll(true);
26+
}
27+
28+
///
29+
bool Open()
30+
{
31+
m_resCSV.close();
32+
if (m_fileName.size() > 5)
33+
{
34+
m_resCSV.open(m_fileName);
35+
return m_resCSV.is_open();
36+
}
37+
return false;
38+
}
39+
40+
///
41+
bool AddTrack(int framesCounter, track_id_t trackID, const cv::Rect& brect, objtype_t type, float confidence)
42+
{
43+
if (m_resCSV.is_open())
44+
{
45+
auto frame = m_frames.find(framesCounter);
46+
if (frame == std::end(m_frames))
47+
{
48+
DetectsOnFrame tmpFrame;
49+
tmpFrame.m_detects.emplace_back(trackID, brect, type, confidence);
50+
m_frames.emplace(framesCounter, tmpFrame);
51+
}
52+
else
53+
{
54+
frame->second.m_detects.emplace_back(trackID, brect, type, confidence);
55+
}
56+
return true;
57+
}
58+
return false;
59+
}
60+
61+
///
62+
void AddRobustTrack(track_id_t trackID)
63+
{
64+
m_robustIDs.insert(trackID);
65+
}
66+
67+
///
68+
void Flush()
69+
{
70+
WriteAll(true);
71+
m_frames.clear();
72+
}
73+
74+
private:
75+
std::string m_fileName;
76+
std::ofstream m_resCSV;
77+
78+
///
79+
struct Detection
80+
{
81+
cv::Rect m_rect;
82+
objtype_t m_type;
83+
float m_conf = 0.f;
84+
track_id_t m_trackID = 0;
85+
86+
Detection(track_id_t trackID, const cv::Rect& brect, objtype_t type, float confidence)
87+
{
88+
m_type = type;
89+
m_rect = brect;
90+
m_conf = confidence;
91+
m_trackID = trackID;
92+
}
93+
};
94+
95+
///
96+
struct DetectsOnFrame
97+
{
98+
std::vector<Detection> m_detects;
99+
};
100+
std::map<int, DetectsOnFrame> m_frames;
101+
std::unordered_set<track_id_t> m_robustIDs;
102+
int m_writeEachNFrame = 1;
103+
104+
///
105+
void WriteAll(bool byFrames)
106+
{
107+
if (byFrames)
108+
{
109+
#if 1
110+
char delim = ',';
111+
for (const auto& frame : m_frames)
112+
{
112113
if (frame.first % m_writeEachNFrame == 0)
113-
{
114-
for (const auto& detect : frame.second.m_detects)
115-
{
116-
if (m_robustIDs.find(detect.m_trackID) != std::end(m_robustIDs))
117-
{
118-
m_resCSV << frame.first << delim << TypeConverter::Type2Str(detect.m_type) << delim << detect.m_rect.x << delim << detect.m_rect.y << delim <<
119-
detect.m_rect.width << delim << detect.m_rect.height << delim <<
120-
detect.m_conf << delim << std::endl;
121-
}
122-
}
123-
}
124-
}
125-
#else
126-
char delim = ' ';
127-
for (const auto& frame : m_frames)
128-
{
129-
for (const auto& detect : frame.second.m_detects)
130-
{
131-
if (m_robustIDs.find(detect.m_trackID) != std::end(m_robustIDs))
132-
{
133-
m_resCSV << frame.first << delim << TypeConverter::Type2Str(detect.m_type) << delim << detect.m_rect.x << delim << detect.m_rect.y << delim <<
134-
(detect.m_rect.x + detect.m_rect.width) << delim << (detect.m_rect.y + detect.m_rect.height) << delim <<
135-
detect.m_conf << delim << detect.m_trackID << std::endl;
136-
}
137-
}
138-
}
139-
#endif
140-
}
141-
else
142-
{
143-
char delim = ',';
144-
for (size_t id : m_robustIDs)
145-
{
146-
for (const auto& frame : m_frames)
147-
{
114+
{
115+
for (const auto& detect : frame.second.m_detects)
116+
{
117+
if (m_robustIDs.find(detect.m_trackID) != std::end(m_robustIDs))
118+
{
119+
m_resCSV << frame.first << delim << TypeConverter::Type2Str(detect.m_type) << delim << detect.m_rect.x << delim << detect.m_rect.y << delim <<
120+
detect.m_rect.width << delim << detect.m_rect.height << delim <<
121+
detect.m_conf << delim << std::endl;
122+
}
123+
}
124+
}
125+
}
126+
#else
127+
char delim = ' ';
128+
for (const auto& frame : m_frames)
129+
{
130+
for (const auto& detect : frame.second.m_detects)
131+
{
132+
if (m_robustIDs.find(detect.m_trackID) != std::end(m_robustIDs))
133+
{
134+
m_resCSV << frame.first << delim << TypeConverter::Type2Str(detect.m_type) << delim << detect.m_rect.x << delim << detect.m_rect.y << delim <<
135+
(detect.m_rect.x + detect.m_rect.width) << delim << (detect.m_rect.y + detect.m_rect.height) << delim <<
136+
detect.m_conf << delim << detect.m_trackID << std::endl;
137+
}
138+
}
139+
}
140+
#endif
141+
}
142+
else
143+
{
144+
char delim = ',';
145+
for (auto id : m_robustIDs)
146+
{
147+
for (const auto& frame : m_frames)
148+
{
148149
if (frame.first % m_writeEachNFrame == 0)
149-
{
150-
for (const auto& detect : frame.second.m_detects)
151-
{
152-
if (detect.m_trackID == id)
153-
{
154-
m_resCSV << frame.first << delim << id << delim << detect.m_rect.x << delim << detect.m_rect.y << delim <<
155-
detect.m_rect.width << delim << detect.m_rect.height << delim <<
156-
detect.m_conf << ",-1,-1,-1," << std::endl;
157-
break;
158-
}
159-
}
160-
}
161-
}
162-
}
163-
}
164-
}
165-
};
150+
{
151+
for (const auto& detect : frame.second.m_detects)
152+
{
153+
if (detect.m_trackID == id)
154+
{
155+
m_resCSV << frame.first << delim << id.ID2Str() << delim << detect.m_rect.x << delim << detect.m_rect.y << delim <<
156+
detect.m_rect.width << delim << detect.m_rect.height << delim <<
157+
detect.m_conf << ",-1,-1,-1," << std::endl;
158+
break;
159+
}
160+
}
161+
}
162+
}
163+
}
164+
}
165+
}
166+
};

example/VideoExample.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ void VideoExample::DrawTrack(cv::Mat frame,
479479
#endif
480480
if (drawTrajectory)
481481
{
482-
cv::Scalar cl = m_colors[track.m_ID % m_colors.size()];
482+
cv::Scalar cl = m_colors[track.m_ID.ID2Module(m_colors.size())];
483483

484484
for (size_t j = 0; j < track.m_trace.size() - 1; ++j)
485485
{

example/examples.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class MotionDetectorExample final : public VideoExample
167167
{
168168
DrawTrack(frame, track, false, framesCounter);
169169

170-
std::string label = "abandoned " + std::to_string(track.m_ID);
170+
std::string label = "abandoned " + track.m_ID.ID2Str();
171171
int baseLine = 0;
172172
cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
173173

0 commit comments

Comments
 (0)