|
1 | 1 | #pragma once |
2 | 2 | #include <fstream> |
3 | 3 | #include <map> |
| 4 | +#include <unordered_set> |
4 | 5 |
|
5 | 6 | #include <opencv2/opencv.hpp> |
6 | 7 |
|
7 | 8 | #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 | + { |
112 | 113 | 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 | + { |
148 | 149 | 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 | +}; |
0 commit comments