-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdetect2.cpp
More file actions
94 lines (83 loc) · 3.09 KB
/
detect2.cpp
File metadata and controls
94 lines (83 loc) · 3.09 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
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core/utils/trace.hpp>
using namespace cv;
using namespace cv::dnn;
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
string CLASSES[] = {"background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "tvmonitor"};
int main(int argc, char **argv)
{
CV_TRACE_FUNCTION();
String modelTxt = "MobileNetSSD_deploy.prototxt.txt";
String modelBin = "MobileNetSSD_deploy.caffemodel";
String imageFile = (argc > 1) ? argv[1] : "re.jpg";
Net net = dnn::readNetFromCaffe(modelTxt, modelBin);
if (net.empty())
{
std::cerr << "Can't load network by using the following files: " << std::endl;
std::cerr << "prototxt: " << modelTxt << std::endl;
std::cerr << "caffemodel: " << modelBin << std::endl;
exit(-1);
}
VideoCapture cap;
cap.open(0);
// Mat img = imread(imageFile);
clock_t begin_time;
while(1)
{
// if (img.empty())
// {
// std::cerr << "Can't read image from the file: " << imageFile << std::endl;
// exit(-1);
// }
begin_time = clock();
// do something
Mat img, img2;
cap >> img;
resize(img, img2, Size(300,300));
Mat inputBlob = blobFromImage(img2, 0.007843, Size(300,300), Scalar(127.5, 127.5, 127.5), false);
net.setInput(inputBlob, "data");
Mat detection = net.forward("detection_out");
Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
ostringstream ss;
float confidenceThreshold = 0.2;
for (int i = 0; i < detectionMat.rows; i++)
{
float confidence = detectionMat.at<float>(i, 2);
if (confidence > confidenceThreshold)
{
int idx = static_cast<int>(detectionMat.at<float>(i, 1));
int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * img.cols);
int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * img.rows);
int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * img.cols);
int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * img.rows);
Rect object((int)xLeftBottom, (int)yLeftBottom,
(int)(xRightTop - xLeftBottom),
(int)(yRightTop - yLeftBottom));
rectangle(img, object, Scalar(0, 255, 0), 2);
// cout << CLASSES[idx] << ": " << confidence << endl;
ss.str("");
ss << confidence;
String conf(ss.str());
String label = CLASSES[idx] + ": " + conf;
int baseLine = 0;
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
putText(img2, label, Point(xLeftBottom, yLeftBottom),
FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,0));
}
}
imshow("detections", img2);
//if (waitKey(1) && 0xFF)
//break;
//std::cout << 1 / (float(clock () - begin_time ) / CLOCKS_PER_SEC)<<endl;
// waitKey();
}
return 0;
}