Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/FaceDetectionComparison/face_detection_opencv_dnn.cpp
3118 views
1
#include <iostream>
2
#include <string>
3
#include <vector>
4
#include <stdlib.h>
5
#include <opencv2/core.hpp>
6
#include <opencv2/imgproc.hpp>
7
#include <opencv2/highgui.hpp>
8
#include <opencv2/dnn.hpp>
9
#include <boost/algorithm/string.hpp>
10
11
using namespace cv;
12
using namespace cv::dnn;
13
using namespace std;
14
15
const size_t inWidth = 300;
16
const size_t inHeight = 300;
17
const double inScaleFactor = 1.0;
18
const float confidenceThreshold = 0.7;
19
const cv::Scalar meanVal(104.0, 177.0, 123.0);
20
21
const std::string caffeConfigFile = "models/deploy.prototxt";
22
const std::string caffeWeightFile = "models/res10_300x300_ssd_iter_140000_fp16.caffemodel";
23
24
const std::string tensorflowConfigFile = "models/opencv_face_detector.pbtxt";
25
const std::string tensorflowWeightFile = "models/opencv_face_detector_uint8.pb";
26
27
void detectFaceOpenCVDNN(Net net, Mat &frameOpenCVDNN, string framework)
28
{
29
int frameHeight = frameOpenCVDNN.rows;
30
int frameWidth = frameOpenCVDNN.cols;
31
32
cv::Mat inputBlob;
33
if (framework == "caffe")
34
inputBlob = cv::dnn::blobFromImage(frameOpenCVDNN, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, false, false);
35
else
36
inputBlob = cv::dnn::blobFromImage(frameOpenCVDNN, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, true, false);
37
38
net.setInput(inputBlob, "data");
39
cv::Mat detection = net.forward("detection_out");
40
41
cv::Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
42
43
for(int i = 0; i < detectionMat.rows; i++)
44
{
45
float confidence = detectionMat.at<float>(i, 2);
46
47
if(confidence > confidenceThreshold)
48
{
49
int x1 = static_cast<int>(detectionMat.at<float>(i, 3) * frameWidth);
50
int y1 = static_cast<int>(detectionMat.at<float>(i, 4) * frameHeight);
51
int x2 = static_cast<int>(detectionMat.at<float>(i, 5) * frameWidth);
52
int y2 = static_cast<int>(detectionMat.at<float>(i, 6) * frameHeight);
53
54
cv::rectangle(frameOpenCVDNN, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 255, 0),2, 4);
55
}
56
}
57
58
}
59
60
61
int main( int argc, const char** argv )
62
{
63
string videoFileName;
64
string device;
65
string framework;
66
67
// Take arguments from command line
68
if (argc == 4)
69
{
70
videoFileName = argv[1];
71
device = argv[2];
72
framework = argv[3];
73
}
74
else if (argc == 3)
75
{
76
videoFileName = argv[1];
77
device = argv[2];
78
framework = "caffe";
79
}
80
else if (argc == 2)
81
{
82
videoFileName = argv[1];
83
device = "cpu";
84
framework = "caffe";
85
}
86
else
87
{
88
videoFileName = "";
89
device = "cpu";
90
framework = "caffe";
91
}
92
93
boost::to_upper(device);
94
cout << "Configuration:" << endl;
95
cout << "Device - "<< device << endl;
96
if (framework == "caffe")
97
cout << "Network type - Caffe" << endl;
98
else
99
cout << "Network type - TensorFlow" << endl;
100
if (videoFileName == "")
101
cout << "No video found, using camera stream" << endl;
102
else
103
cout << "Video file - " << videoFileName << endl;
104
105
Net net;
106
107
if (framework == "caffe")
108
net = cv::dnn::readNetFromCaffe(caffeConfigFile, caffeWeightFile);
109
else
110
net = cv::dnn::readNetFromTensorflow(tensorflowWeightFile, tensorflowConfigFile);
111
112
#if (CV_MAJOR_VERSION >= 4)
113
if (device == "CPU")
114
{
115
net.setPreferableBackend(DNN_TARGET_CPU);
116
}
117
else
118
{
119
net.setPreferableBackend(DNN_BACKEND_CUDA);
120
net.setPreferableTarget(DNN_TARGET_CUDA);
121
}
122
#else
123
// force CPU backend for OpenCV 3.x as CUDA backend is not supported there
124
net.setPreferableBackend(DNN_BACKEND_DEFAULT);
125
device = "cpu";
126
#endif
127
128
cv::VideoCapture source;
129
if (videoFileName != "")
130
source.open(videoFileName);
131
else
132
source.open(0, CAP_V4L);
133
134
Mat frame;
135
136
double tt_opencvDNN = 0;
137
double fpsOpencvDNN = 0;
138
139
while (true)
140
{
141
source >> frame;
142
if (frame.empty())
143
break;
144
145
double t = cv::getTickCount();
146
detectFaceOpenCVDNN(net, frame, framework);
147
tt_opencvDNN = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
148
fpsOpencvDNN = 1/tt_opencvDNN;
149
150
putText(frame, format("OpenCV DNN %s FPS = %.2f", device.c_str(), fpsOpencvDNN), Point(10, 50), FONT_HERSHEY_SIMPLEX, 1.3, Scalar(0, 0, 255), 4);
151
152
imshow("OpenCV - DNN Face Detection", frame);
153
154
int k = waitKey(5);
155
if(k == 27)
156
{
157
destroyAllWindows();
158
break;
159
}
160
}
161
}
162
163