forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoloDarknetDetector.cpp
More file actions
183 lines (165 loc) · 5.14 KB
/
YoloDarknetDetector.cpp
File metadata and controls
183 lines (165 loc) · 5.14 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "YoloDarknetDetector.h"
#include "nms.h"
///
/// \brief YoloDarknetDetector::YoloDarknetDetector
/// \param gray
///
YoloDarknetDetector::YoloDarknetDetector(
cv::UMat& colorFrame
)
: BaseDetector(colorFrame)
{
m_classNames = { "background",
"aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse",
"motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor" };
}
///
/// \brief YoloDarknetDetector::~YoloDarknetDetector
///
YoloDarknetDetector::~YoloDarknetDetector(void)
{
}
///
/// \brief YoloDarknetDetector::Init
/// \return
///
bool YoloDarknetDetector::Init(const config_t& config)
{
auto modelConfiguration = config.find("modelConfiguration");
auto modelBinary = config.find("modelBinary");
if (modelConfiguration != config.end() && modelBinary != config.end())
{
m_detector = std::make_unique<Detector>(modelConfiguration->second, modelBinary->second);
m_detector->nms = 0.2f;
m_WHRatio = static_cast<float>(m_detector->get_net_width()) / static_cast<float>(m_detector->get_net_height());
}
auto classNames = config.find("classNames");
if (classNames != config.end())
{
std::ifstream classNamesFile(classNames->second);
if (classNamesFile.is_open())
{
m_classNames.clear();
std::string className;
for (; std::getline(classNamesFile, className); )
{
m_classNames.push_back(className);
}
}
}
auto confidenceThreshold = config.find("confidenceThreshold");
if (confidenceThreshold != config.end())
{
m_confidenceThreshold = std::stof(confidenceThreshold->second);
}
auto maxCropRatio = config.find("maxCropRatio");
if (maxCropRatio != config.end())
{
m_maxCropRatio = std::stof(maxCropRatio->second);
if (m_maxCropRatio < 1.f)
{
m_maxCropRatio = 1.f;
}
}
m_classesWhiteList.clear();
auto whiteRange = config.equal_range("white_list");
for (auto it = whiteRange.first; it != whiteRange.second; ++it)
{
m_classesWhiteList.insert(it->second);
}
bool correct = m_detector.get() != nullptr;
return correct;
}
///
/// \brief YoloDarknetDetector::Detect
/// \param gray
///
void YoloDarknetDetector::Detect(cv::UMat& colorFrame)
{
m_regions.clear();
cv::Mat colorMat = colorFrame.getMat(cv::ACCESS_READ);
int cropHeight = cvRound(m_maxCropRatio * m_detector->get_net_height());
int cropWidth = cvRound(m_maxCropRatio * m_detector->get_net_width());
if (colorFrame.cols / (float)colorFrame.rows > m_WHRatio)
{
if (m_maxCropRatio <= 0 || cropHeight >= colorFrame.rows)
{
cropHeight = colorFrame.rows;
}
cropWidth = cvRound(cropHeight * m_WHRatio);
}
else
{
if (m_maxCropRatio <= 0 || cropWidth >= colorFrame.cols)
{
cropWidth = colorFrame.cols;
}
cropHeight = cvRound(colorFrame.cols / m_WHRatio);
}
//std::cout << "Frame size " << colorFrame.size() << ", crop size = " << cv::Size(cropWidth, cropHeight) << ", ratio = " << m_maxCropRatio << std::endl;
cv::Rect crop(0, 0, cropWidth, cropHeight);
regions_t tmpRegions;
size_t cropsCount = 0;
int stepX = 3 * crop.width / 4;
int stepY = 3 * crop.height / 4;
for (; crop.y < colorMat.rows; crop.y += stepY)
{
bool needBreakY = false;
if (crop.y + crop.height >= colorMat.rows)
{
crop.y = colorMat.rows - crop.height;
needBreakY = true;
}
for (crop.x = 0; crop.x < colorMat.cols; crop.x += stepX)
{
bool needBreakX = false;
if (crop.x + crop.width >= colorMat.cols)
{
crop.x = colorMat.cols - crop.width;
needBreakX = true;
}
//std::cout << "Crop " << cropsCount++ << ": " << crop << std::endl;
DetectInCrop(colorMat, crop, tmpRegions);
if (needBreakX)
{
break;
}
}
if (needBreakY)
{
break;
}
}
//std::cout << "nms for " << tmpRegions.size() << " objects" << std::endl;
nms3<CRegion>(tmpRegions, m_regions, 0.4f,
[](const CRegion& reg) -> cv::Rect { return reg.m_brect; },
[](const CRegion& reg) -> float { return reg.m_confidence; },
[](const CRegion& reg) -> std::string { return reg.m_type; },
0, 0.f);
//std::cout << "Finally " << m_regions.size() << " objects" << std::endl;
}
///
/// \brief YoloDarknetDetector::DetectInCrop
/// \param colorFrame
/// \param crop
/// \param tmpRegions
///
void YoloDarknetDetector::DetectInCrop(cv::Mat colorFrame, const cv::Rect& crop, regions_t& tmpRegions)
{
#if 1
std::shared_ptr<image_t> detImage = m_detector->mat_to_image_resize(colorFrame(crop));
std::vector<bbox_t> detects = m_detector->detect_resized(*detImage, crop.width, crop.height, m_confidenceThreshold, false);
#else
std::vector<bbox_t> detects = m_detector->detect(colorFrame, m_confidenceThreshold, false);
#endif
for (const bbox_t& bbox : detects)
{
if (m_classesWhiteList.empty() || m_classesWhiteList.find(m_classNames[bbox.obj_id]) != std::end(m_classesWhiteList))
{
tmpRegions.emplace_back(cv::Rect(bbox.x + crop.x, bbox.y + crop.y, bbox.w, bbox.h), m_classNames[bbox.obj_id], bbox.prob);
}
}
}