Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/FaceDetectionComparison/face_detection_dlib_hog.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/image_processing/frontal_face_detector.h>
12
13
using namespace cv;
14
using namespace std;
15
using namespace dlib;
16
17
void detectFaceDlibHog(frontal_face_detector hogFaceDetector, Mat &frameDlibHog, int inHeight=300, int inWidth=0)
18
{
19
20
int frameHeight = frameDlibHog.rows;
21
int frameWidth = frameDlibHog.cols;
22
if (!inWidth)
23
inWidth = (int)((frameWidth / (float)frameHeight) * inHeight);
24
25
float scaleHeight = frameHeight / (float)inHeight;
26
float scaleWidth = frameWidth / (float)inWidth;
27
28
Mat frameDlibHogSmall;
29
resize(frameDlibHog, frameDlibHogSmall, Size(inWidth, inHeight));
30
31
// Convert OpenCV image format to Dlib's image format
32
cv_image<bgr_pixel> dlibIm(frameDlibHogSmall);
33
34
// Detect faces in the image
35
std::vector<dlib::rectangle> faceRects = hogFaceDetector(dlibIm);
36
37
for ( size_t i = 0; i < faceRects.size(); i++ )
38
{
39
int x1 = (int)(faceRects[i].left() * scaleWidth);
40
int y1 = (int)(faceRects[i].top() * scaleHeight);
41
int x2 = (int)(faceRects[i].right() * scaleWidth);
42
int y2 = (int)(faceRects[i].bottom() * scaleHeight);
43
cv::rectangle(frameDlibHog, Point(x1, y1), Point(x2, y2), Scalar(0,255,0), (int)(frameHeight/150.0), 4);
44
}
45
}
46
47
48
int main( int argc, const char** argv )
49
{
50
frontal_face_detector hogFaceDetector = get_frontal_face_detector();
51
52
VideoCapture source;
53
if (argc == 1)
54
source.open(0, CAP_V4L);
55
else
56
source.open(argv[1]);
57
58
Mat frame;
59
60
double tt_dlibHog = 0;
61
double fpsDlibHog = 0;
62
63
while (true)
64
{
65
source >> frame;
66
if (frame.empty())
67
break;
68
69
double t = cv::getTickCount();
70
detectFaceDlibHog(hogFaceDetector, frame);
71
tt_dlibHog = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
72
fpsDlibHog = 1/tt_dlibHog;
73
74
putText(frame, format("DLIB HoG; FPS = %.2f",fpsDlibHog), Point(10, 50), FONT_HERSHEY_SIMPLEX, 1.3, Scalar(0, 0, 255), 4);
75
imshow("DLIB - HoG Face Detection", frame);
76
77
int k = waitKey(5);
78
if(k == 27)
79
{
80
destroyAllWindows();
81
break;
82
}
83
}
84
}
85
86