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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// WARNING: this sample is under construction! Use it on your own risk.
#include <opencv2/contrib/contrib.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/gpu/gpu.hpp>
#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;
using namespace cv;
using namespace cv::gpu;
void help()
{
cout << "Usage: ./cascadeclassifier <cascade_file> <image_or_video_or_cameraid>\n"
"Using OpenCV version " << CV_VERSION << endl << endl;
}
void DetectAndDraw(Mat& img, CascadeClassifier_GPU& cascade);
String cascadeName = "../../data/haarcascades/haarcascade_frontalface_alt.xml";
String nestedCascadeName = "../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml";
template<class T> void convertAndReseize(const T& src, T& gray, T& resized, double scale = 2.0)
{
if (src.channels() == 3)
cvtColor( src, gray, CV_BGR2GRAY );
else
gray = src;
Size sz(cvRound(gray.cols * scale), cvRound(gray.rows * scale));
if (scale != 1)
resize(gray, resized, sz);
else
resized = gray;
}
int main( int argc, const char** argv )
{
if (argc != 3)
return help(), -1;
if (cv::gpu::getCudaEnabledDeviceCount() == 0)
return cerr << "No GPU found or the library is compiled without GPU support" << endl, -1;
VideoCapture capture;
string cascadeName = argv[1];
string inputName = argv[2];
cv::gpu::CascadeClassifier_GPU cascade_gpu;
if( !cascade_gpu.load( cascadeName ) )
return cerr << "ERROR: Could not load cascade classifier \"" << cascadeName << "\"" << endl, help(), -1;
cv::CascadeClassifier cascade_cpu;
if( !cascade_cpu.load( cascadeName ) )
return cerr << "ERROR: Could not load cascade classifier \"" << cascadeName << "\"" << endl, help(), -1;
Mat image = imread( inputName);
if( image.empty() )
if (!capture.open(inputName))
{
int camid = 0;
sscanf(inputName.c_str(), "%d", &camid);
if(!capture.open(camid))
cout << "Can't open source" << endl;
}
namedWindow( "result", 1 );
Mat frame, frame_cpu, gray_cpu, resized_cpu, faces_downloaded, frameDisp;
vector<Rect> facesBuf_cpu;
GpuMat frame_gpu, gray_gpu, resized_gpu, facesBuf_gpu;
/* parameters */
bool useGPU = true;
double scale_factor = 1;
double font_scale = 0.8;
bool visualizeInPlace = false;
bool findLargestObject = false;
int minNeighbors = 4;
printf("\t<space> - toggle GPU/CPU\n");
printf("\tL - toggle lagest faces\n");
printf("\tV - toggle visualisation in-place (for GPU only)\n");
printf("\t1/q - inc/dec scale\n");
int detections_num;
for(;;)
{
if( capture.isOpened() )
{
capture >> frame;
if( frame.empty())
break;
}
(image.empty() ? frame : image).copyTo(frame_cpu);
frame_gpu.upload( image.empty() ? frame : image);
convertAndReseize(frame_gpu, gray_gpu, resized_gpu, scale_factor);
convertAndReseize(frame_cpu, gray_cpu, resized_cpu, scale_factor);
cv::TickMeter tm;
tm.start();
if (useGPU)
{
cascade_gpu.visualizeInPlace = visualizeInPlace;
cascade_gpu.findLargestObject = findLargestObject;
detections_num = cascade_gpu.detectMultiScale( resized_gpu, facesBuf_gpu, 1.2, minNeighbors);
facesBuf_gpu.colRange(0, detections_num).download(faces_downloaded);
}
else /* so use CPU */
{
Size minSize = cascade_gpu.getClassifierSize();
if (findLargestObject)
{
float ratio = (float)std::min(frame.cols / minSize.width, frame.rows / minSize.height);
ratio = std::max(ratio / 2.5f, 1.f);
minSize = Size(cvRound(minSize.width * ratio), cvRound(minSize.height * ratio));
}
cascade_cpu.detectMultiScale(resized_cpu, facesBuf_cpu, 1.2, minNeighbors, (findLargestObject ? CV_HAAR_FIND_BIGGEST_OBJECT : 0) | CV_HAAR_SCALE_IMAGE, minSize);
detections_num = (int)facesBuf_cpu.size();
}
tm.stop();
printf( "detection time = %g ms\n", tm.getTimeMilli() );
if (useGPU)
resized_gpu.download(resized_cpu);
if (!visualizeInPlace || !useGPU)
if (detections_num)
{
Rect* faces = useGPU ? faces_downloaded.ptr<Rect>() : &facesBuf_cpu[0];
for(int i = 0; i < detections_num; ++i)
cv::rectangle(resized_cpu, faces[i], Scalar(255));
}
int tickness = font_scale > 0.75 ? 2 : 1;
Point text_pos(5, 25);
Scalar color = CV_RGB(255, 0, 0);
Size fontSz = cv::getTextSize("T[]", FONT_HERSHEY_SIMPLEX, font_scale, tickness, 0);
int offs = fontSz.height + 5;
cv::cvtColor(resized_cpu, frameDisp, CV_GRAY2BGR);
char buf[4096];
sprintf(buf, "%s, FPS = %0.3g", useGPU ? "GPU (device) " : "CPU (host)", 1.0/tm.getTimeSec());
putText(frameDisp, buf, text_pos, FONT_HERSHEY_SIMPLEX, font_scale, color, tickness);
sprintf(buf, "scale = %0.3g, [%d x %d] x scale, Min neighbors = %d", scale_factor, frame.cols, frame.rows, minNeighbors);
putText(frameDisp, buf, text_pos+=Point(0,offs), FONT_HERSHEY_SIMPLEX, font_scale, color, tickness);
putText(frameDisp, "Hotkeys: space, 1/Q, 2/E, 3/E, L, V, Esc", text_pos+=Point(0,offs), FONT_HERSHEY_SIMPLEX, font_scale, color, tickness);
if (findLargestObject)
putText(frameDisp, "FindLargestObject", text_pos+=Point(0,offs), FONT_HERSHEY_SIMPLEX, font_scale, color, tickness);
if (visualizeInPlace && useGPU)
putText(frameDisp, "VisualizeInPlace", text_pos+Point(0,offs), FONT_HERSHEY_SIMPLEX, font_scale, color, tickness);
cv::imshow( "result", frameDisp);
int key = waitKey( 5 );
if( key == 27)
break;
switch ((char)key)
{
case ' ': useGPU = !useGPU; printf("Using %s\n", useGPU ? "GPU" : "CPU");break;
case 'v': case 'V': visualizeInPlace = !visualizeInPlace; printf("VisualizeInPlace = %d\n", visualizeInPlace); break;
case 'l': case 'L': findLargestObject = !findLargestObject; printf("FindLargestObject = %d\n", findLargestObject); break;
case '1': scale_factor*=1.05; printf("Scale factor = %g\n", scale_factor); break;
case 'q': case 'Q':scale_factor/=1.05; printf("Scale factor = %g\n", scale_factor); break;
case '3': font_scale*=1.05; printf("Fond scale = %g\n", font_scale); break;
case 'e': case 'E':font_scale/=1.05; printf("Fond scale = %g\n", font_scale); break;
case '2': ++minNeighbors; printf("Min Neighbors = %d\n", minNeighbors); break;
case 'w': case 'W':minNeighbors = max(minNeighbors-1, 0); printf("Min Neighbors = %d\n", minNeighbors); break;
}
}
return 0;
}