Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/FaceDetectionComparison/face_detection_dlib_mmod.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
9
#include <dlib/opencv.h>
10
#include <dlib/image_processing.h>
11
#include <dlib/dnn.h>
12
#include <dlib/data_io.h>
13
14
using namespace cv;
15
using namespace std;
16
using namespace dlib;
17
18
// Network Definition
19
/////////////////////////////////////////////////////////////////////////////////////////////////////
20
template <long num_filters, typename SUBNET> using con5d = con<num_filters,5,5,2,2,SUBNET>;
21
template <long num_filters, typename SUBNET> using con5 = con<num_filters,5,5,1,1,SUBNET>;
22
23
template <typename SUBNET> using downsampler = relu<affine<con5d<32, relu<affine<con5d<32, relu<affine<con5d<16,SUBNET>>>>>>>>>;
24
template <typename SUBNET> using rcon5 = relu<affine<con5<45,SUBNET>>>;
25
26
using net_type = loss_mmod<con<1,9,9,1,1,rcon5<rcon5<rcon5<downsampler<input_rgb_image_pyramid<pyramid_down<6>>>>>>>>;
27
/////////////////////////////////////////////////////////////////////////////////////////////////////
28
29
void detectFaceDlibMMOD(net_type mmodFaceDetector, Mat &frameDlibMmod, int inHeight=300, int inWidth=0)
30
{
31
32
int frameHeight = frameDlibMmod.rows;
33
int frameWidth = frameDlibMmod.cols;
34
if (!inWidth)
35
inWidth = (int)((frameWidth / (float)frameHeight) * inHeight);
36
37
float scaleHeight = frameHeight / (float)inHeight;
38
float scaleWidth = frameWidth / (float)inWidth;
39
40
Mat frameDlibMmodSmall;
41
resize(frameDlibMmod, frameDlibMmodSmall, Size(inWidth, inHeight));
42
43
// Convert OpenCV image format to Dlib's image format
44
cv_image<bgr_pixel> dlibIm(frameDlibMmodSmall);
45
matrix<rgb_pixel> dlibMatrix;
46
assign_image(dlibMatrix, dlibIm);
47
48
// Detect faces in the image
49
std::vector<dlib::mmod_rect> faceRects = mmodFaceDetector(dlibMatrix);
50
51
for ( size_t i = 0; i < faceRects.size(); i++ )
52
{
53
int x1 = (int)(faceRects[i].rect.left() * scaleWidth);
54
int y1 = (int)(faceRects[i].rect.top() * scaleHeight);
55
int x2 = (int)(faceRects[i].rect.right() * scaleWidth);
56
int y2 = (int)(faceRects[i].rect.bottom() * scaleHeight);
57
cv::rectangle(frameDlibMmod, Point(x1, y1), Point(x2, y2), Scalar(0,255,0), (int)(frameHeight/150.0), 4);
58
}
59
}
60
61
int main( int argc, const char** argv )
62
{
63
String mmodModelPath = "models/mmod_human_face_detector.dat";
64
net_type mmodFaceDetector;
65
deserialize(mmodModelPath) >> mmodFaceDetector;
66
67
VideoCapture source;
68
if (argc == 1)
69
source.open(0, CAP_V4L);
70
else
71
source.open(argv[1]);
72
73
Mat frame;
74
75
double tt_dlibMmod = 0;
76
double fpsDlibMmod = 0;
77
while (true)
78
{
79
source >> frame;
80
if (frame.empty())
81
break;
82
83
double t = cv::getTickCount();
84
detectFaceDlibMMOD ( mmodFaceDetector, frame );
85
tt_dlibMmod = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
86
fpsDlibMmod = 1/tt_dlibMmod;
87
88
putText(frame, format("DLIB MMOD; FPS = %.2f",fpsDlibMmod), Point(10, 50), FONT_HERSHEY_SIMPLEX, 1.3, Scalar(0, 0, 255), 4);
89
imshow("DLIB - MMOD Face Detection", frame);
90
91
int k = waitKey(5);
92
if(k == 27)
93
{
94
destroyAllWindows();
95
break;
96
}
97
}
98
}
99
100