forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNNDetector.cpp
More file actions
166 lines (137 loc) · 5.22 KB
/
DNNDetector.cpp
File metadata and controls
166 lines (137 loc) · 5.22 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
#include "DNNDetector.h"
#include "nms.h"
///
/// \brief DNNDetector::DNNDetector
/// \param collectPoints
/// \param gray
///
DNNDetector::DNNDetector(
bool collectPoints,
cv::UMat& colorFrame
)
:
BaseDetector(collectPoints, colorFrame),
m_WHRatio(InWidth / (float)InHeight),
m_inScaleFactor(0.007843f),
m_meanVal(127.5),
m_confidenceThreshold(0.2f)
{
m_classNames = { "background",
"aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse",
"motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor" };
}
///
/// \brief DNNDetector::~DNNDetector
///
DNNDetector::~DNNDetector(void)
{
}
///
/// \brief DNNDetector::Init
/// \return
///
bool DNNDetector::Init(const config_t& config)
{
auto modelConfiguration = config.find("modelConfiguration");
auto modelBinary = config.find("modelBinary");
if (modelConfiguration != config.end() && modelBinary != config.end())
{
m_net = cv::dnn::readNetFromCaffe(modelConfiguration->second, modelBinary->second);
}
auto confidenceThreshold = config.find("confidenceThreshold");
if (confidenceThreshold != config.end())
{
m_confidenceThreshold = std::stof(confidenceThreshold->second);
}
return !m_net.empty();
}
///
/// \brief DNNDetector::Detect
/// \param gray
///
void DNNDetector::Detect(cv::UMat& colorFrame)
{
m_regions.clear();
regions_t tmpRegions;
cv::Mat colorMat = colorFrame.getMat(cv::ACCESS_READ);
if (colorFrame.cols / (float)colorFrame.rows > m_WHRatio)
{
cv::Rect crop(0, 0, cvRound(colorFrame.rows * m_WHRatio), colorFrame.rows);
for (; crop.x < colorMat.cols; crop.x += crop.width)
{
if (crop.x + crop.width >= colorMat.cols)
{
crop.x = colorMat.cols - crop.width;
}
DetectInCrop(colorMat, crop, tmpRegions);
}
}
else
{
cv::Rect crop(0, 0, colorFrame.cols, cvRound(colorFrame.cols / m_WHRatio));
for (; crop.y < colorMat.rows; crop.y += crop.height)
{
if (crop.y + crop.height >= colorMat.rows)
{
crop.y = colorMat.rows - crop.height;
}
DetectInCrop(colorMat, crop, tmpRegions);
}
}
nms3<CRegion>(tmpRegions, m_regions, 0.5f,
[](const CRegion& reg) -> cv::Rect { return reg.m_rect; },
[](const CRegion& reg) -> float { return reg.m_confidence; },
0, 0.f);
if (m_collectPoints)
{
for (auto& region : m_regions)
{
CollectPoints(region);
}
}
}
///
/// \brief DNNDetector::DetectInCrop
/// \param colorFrame
/// \param crop
/// \param tmpRegions
///
void DNNDetector::DetectInCrop(cv::Mat colorFrame, const cv::Rect& crop, regions_t& tmpRegions)
{
//Convert Mat to batch of images
cv::Mat inputBlob = cv::dnn::blobFromImage(cv::Mat(colorFrame, crop), m_inScaleFactor, cv::Size(InWidth, InHeight), m_meanVal, false, true);
m_net.setInput(inputBlob, "data"); //set the network input
cv::Mat detection = m_net.forward("detection_out"); //compute output
//std::vector<double> layersTimings;
//double freq = cv::getTickFrequency() / 1000;
//double time = m_net.getPerfProfile(layersTimings) / freq;
cv::Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
//cv::Mat frame = colorFrame(crop);
//ss << "FPS: " << 1000/time << " ; time: " << time << " ms";
//putText(frame, ss.str(), Point(20,20), 0, 0.5, Scalar(0,0,255));
//std::cout << "Inference time, ms: " << time << endl;
//cv::Point correctPoint((colorFrame.cols - crop.width) / 2, (colorFrame.rows - crop.height) / 2);
for (int i = 0; i < detectionMat.rows; ++i)
{
float confidence = detectionMat.at<float>(i, 2);
if (confidence > m_confidenceThreshold)
{
size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));
int xLeftBottom = cvRound(detectionMat.at<float>(i, 3) * crop.width) + crop.x;
int yLeftBottom = cvRound(detectionMat.at<float>(i, 4) * crop.height) + crop.y;
int xRightTop = cvRound(detectionMat.at<float>(i, 5) * crop.width) + crop.x;
int yRightTop = cvRound(detectionMat.at<float>(i, 6) * crop.height) + crop.y;
cv::Rect object(xLeftBottom, yLeftBottom, xRightTop - xLeftBottom, yRightTop - yLeftBottom);
tmpRegions.push_back(CRegion(object, m_classNames[objectClass], confidence));
//cv::rectangle(frame, object, Scalar(0, 255, 0));
//std::string label = classNames[objectClass] + ": " + std::to_string(confidence);
//int baseLine = 0;
//cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
//cv::rectangle(frame, cv::Rect(cv::Point(xLeftBottom, yLeftBottom - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), CV_FILLED);
//cv::putText(frame, label, Point(xLeftBottom, yLeftBottom), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0,0,0));
}
}
}