-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectdetector.cpp
More file actions
78 lines (70 loc) · 2.72 KB
/
objectdetector.cpp
File metadata and controls
78 lines (70 loc) · 2.72 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
#include "objectdetector.hpp"
#include <iostream>
#include <chrono>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
using namespace std;
using namespace cv;
ObjectDetector::ObjectDetector(Config& config)
{
newDetection = false;
classes = config.classes;
confidenceThreshold = config.confidenceThreshold;
indices = config.indices;
modelConfiguration = config.modelConfiguration;
modelWeights = config.modelWeights;
modelSize = config.modelSize;
if (config.embedded)
net = dnn::readNetFromCaffe(modelConfiguration, modelWeights);
else
net = dnn::readNetFromTensorflow(modelWeights, modelConfiguration);
if (net.empty())
{
std::cerr << "Can't load the network, sth went wrong" << std::endl;
exit(-1);
}
}
bool ObjectDetector::detect(cv::Mat frame)
{
newDetection = false;
inputBlob = dnn::blobFromImage(frame, 0.007843, Size(modelSize, modelSize), Scalar(127.5, 127.5, 127.5), false);
net.setInput(inputBlob);
detection = net.forward("detection_out");
Mat detectionOut(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
for (int i = 0; i < detectionOut.rows; i++)
{
float confidence = detectionOut.at<float>(i, 2);
int idx = static_cast<int>(detectionOut.at<float>(i, 1));
it = std::find(indices.begin(), indices.end(), idx);
if ((confidence > confidenceThreshold) && (it != indices.end()))
{
newDetection = true;
idx = std::distance(indices.begin(), it);
int xLeftBottom = static_cast<int>(detectionOut.at<float>(i, 3) * frame.cols);
int yLeftBottom = static_cast<int>(detectionOut.at<float>(i, 4) * frame.rows);
int xRightTop = static_cast<int>(detectionOut.at<float>(i, 5) * frame.cols);
int yRightTop = static_cast<int>(detectionOut.at<float>(i, 6) * frame.rows);
Rect2d object((int)xLeftBottom, (int)yLeftBottom, (int)(xRightTop - xLeftBottom), (int)(yRightTop - yLeftBottom));
ostringstream ss;
ss.str("");
ss << (int)(confidence * 100) << "%";
String conf(ss.str());
String label = classes[idx] + ": " + conf;
int baseLine = 0;
detectionClasses.push_back(idx);
detectionRectangles.push_back(object);
detectionConfidence.push_back((int)confidence);
detectionLabels.push_back(label);
labelPoints.push_back(Point(xLeftBottom + 5, yLeftBottom - 10));
}
}
return newDetection;
}
void ObjectDetector::clearList()
{
detectionLabels.clear();
detectionRectangles.clear();
labelPoints.clear();
detectionConfidence.clear();
detectionClasses.clear();
}