Skip to content

Commit b4813e6

Browse files
committed
Add C++ DNN face detection sample: resnet_ssd_face.cpp
1 parent 21c8e6d commit b4813e6

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed

samples/dnn/resnet_ssd_face.cpp

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include <opencv2/dnn.hpp>
2+
#include <opencv2/imgproc.hpp>
3+
#include <opencv2/highgui.hpp>
4+
5+
using namespace cv;
6+
using namespace cv::dnn;
7+
8+
#include <iostream>
9+
#include <cstdlib>
10+
using namespace std;
11+
12+
const size_t inWidth = 300;
13+
const size_t inHeight = 300;
14+
const double inScaleFactor = 1.0;
15+
const Scalar meanVal(104.0, 177.0, 123.0);
16+
17+
const char* about = "This sample uses Single-Shot Detector "
18+
"(https://arxiv.org/abs/1512.02325) "
19+
"with ResNet-10 architecture to detect faces on camera/video/image.\n"
20+
"More information about the training is available here: "
21+
"<OPENCV_SRC_DIR>/samples/dnn/face_detector/how_to_train_face_detector.txt\n"
22+
".caffemodel model's file is available here: "
23+
"<OPENCV_SRC_DIR>/samples/dnn/face_detector/res10_300x300_ssd_iter_140000.caffemodel\n"
24+
".prototxt file is available here: "
25+
"<OPENCV_SRC_DIR>/samples/dnn/face_detector/deploy.prototxt\n";
26+
27+
const char* params
28+
= "{ help | false | print usage }"
29+
"{ proto | | model configuration (deploy.prototxt) }"
30+
"{ model | | model weights (res10_300x300_ssd_iter_140000.caffemodel) }"
31+
"{ camera_device | 0 | camera device number }"
32+
"{ video | | video or image for detection }"
33+
"{ min_confidence | 0.5 | min confidence }";
34+
35+
int main(int argc, char** argv)
36+
{
37+
CommandLineParser parser(argc, argv, params);
38+
39+
if (parser.get<bool>("help"))
40+
{
41+
cout << about << endl;
42+
parser.printMessage();
43+
return 0;
44+
}
45+
46+
String modelConfiguration = parser.get<string>("proto");
47+
String modelBinary = parser.get<string>("model");
48+
49+
//! [Initialize network]
50+
dnn::Net net = readNetFromCaffe(modelConfiguration, modelBinary);
51+
//! [Initialize network]
52+
53+
if (net.empty())
54+
{
55+
cerr << "Can't load network by using the following files: " << endl;
56+
cerr << "prototxt: " << modelConfiguration << endl;
57+
cerr << "caffemodel: " << modelBinary << endl;
58+
cerr << "Models are available here:" << endl;
59+
cerr << "<OPENCV_SRC_DIR>/samples/dnn/face_detector" << endl;
60+
cerr << "or here:" << endl;
61+
cerr << "https://github.com/opencv/opencv/tree/master/samples/dnn/face_detector" << endl;
62+
exit(-1);
63+
}
64+
65+
VideoCapture cap;
66+
if (parser.get<String>("video").empty())
67+
{
68+
int cameraDevice = parser.get<int>("camera_device");
69+
cap = VideoCapture(cameraDevice);
70+
if(!cap.isOpened())
71+
{
72+
cout << "Couldn't find camera: " << cameraDevice << endl;
73+
return -1;
74+
}
75+
}
76+
else
77+
{
78+
cap.open(parser.get<String>("video"));
79+
if(!cap.isOpened())
80+
{
81+
cout << "Couldn't open image or video: " << parser.get<String>("video") << endl;
82+
return -1;
83+
}
84+
}
85+
86+
for(;;)
87+
{
88+
Mat frame;
89+
cap >> frame; // get a new frame from camera/video or read image
90+
91+
if (frame.empty())
92+
{
93+
waitKey();
94+
break;
95+
}
96+
97+
if (frame.channels() == 4)
98+
cvtColor(frame, frame, COLOR_BGRA2BGR);
99+
100+
//! [Prepare blob]
101+
Mat inputBlob = blobFromImage(frame, inScaleFactor,
102+
Size(inWidth, inHeight), meanVal, false, false); //Convert Mat to batch of images
103+
//! [Prepare blob]
104+
105+
//! [Set input blob]
106+
net.setInput(inputBlob, "data"); //set the network input
107+
//! [Set input blob]
108+
109+
//! [Make forward pass]
110+
Mat detection = net.forward("detection_out"); //compute output
111+
//! [Make forward pass]
112+
113+
vector<double> layersTimings;
114+
double freq = getTickFrequency() / 1000;
115+
double time = net.getPerfProfile(layersTimings) / freq;
116+
117+
Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
118+
119+
ostringstream ss;
120+
ss << "FPS: " << 1000/time << " ; time: " << time << " ms";
121+
putText(frame, ss.str(), Point(20,20), 0, 0.5, Scalar(0,0,255));
122+
123+
float confidenceThreshold = parser.get<float>("min_confidence");
124+
for(int i = 0; i < detectionMat.rows; i++)
125+
{
126+
float confidence = detectionMat.at<float>(i, 2);
127+
128+
if(confidence > confidenceThreshold)
129+
{
130+
int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
131+
int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
132+
int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
133+
int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);
134+
135+
Rect object((int)xLeftBottom, (int)yLeftBottom,
136+
(int)(xRightTop - xLeftBottom),
137+
(int)(yRightTop - yLeftBottom));
138+
139+
rectangle(frame, object, Scalar(0, 255, 0));
140+
141+
ss.str("");
142+
ss << confidence;
143+
String conf(ss.str());
144+
String label = "Face: " + conf;
145+
int baseLine = 0;
146+
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
147+
rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom - labelSize.height),
148+
Size(labelSize.width, labelSize.height + baseLine)),
149+
Scalar(255, 255, 255), CV_FILLED);
150+
putText(frame, label, Point(xLeftBottom, yLeftBottom),
151+
FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,0));
152+
}
153+
}
154+
155+
imshow("detections", frame);
156+
if (waitKey(1) >= 0) break;
157+
}
158+
159+
return 0;
160+
} // main

0 commit comments

Comments
 (0)