&1 source of the problem
When running the official website of the Facedetect this demo, always will not come out of the result of the graphics, the computer lower right corner of the error is "The monitor driver has stopped responding, and has been restored Windows 8 (R)."
&2 Pre-treatment
- Modify the code, debugging all kinds of code have tried, demo run failed;
- Baidu on the Disabled visual effect scheme, that is, the theme of modifying the computer is "Windows Classic" theme, demo run failed;
- Baidu on the re-installation of graphics drivers, that is, re-installed integrated network card driver, resulting in a black screen display, Daoteng a day before the whole back, failure;
&3 successfully resolved
First, open the registry, find HKEY_LOCAL_MACHINE, in the system in the CurrentControlSet control of the Grphicsdrivers right-click, new Qeord (64-bit) value (Q), The value name is: Tdrdelay, the Value data is: 8, the base does not change, select Hex.
Then, add four files within your project compilation folder, Haarcascade_eye_tree_eyeglasses.xml and Haarcascade_frontalface_alt.xml, opencv_ffmpeg310_64. DLLs and Opencv_world310d.dll;
In the OPENCV environment configuration, (described in the previous blog), remove the executable directory, remove the additional dependencies of the opencv_world310.lib, so far, all the environment configuration has been completed.
&4 Demo code and running results
Note: The file Facedetect.cpp in the OpenCV installation folder Sources\samples\cpp is the source code.
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace Std;
using namespace CV;
static void Help ()
{
cout << "\nthis program demonstrates the Cascade recognizer. Now you can use Haar or LBP features.\n "
"This classifier can recognize many kinds of rigid objects, once the appropriate classifier is trained.\n"
"It's most known use are for faces.\n"
"Usage:\n"
"./facedetect [--cascade=<cascade_path> This is the primary trained classifier such as frontal face]\n"
"[--nested-cascade[=nested_cascade_path This an optional secondary classifier such as eyes]]\n"
"[--scale=<image scale greater or equal to 1, try 1.3 for example>]\n"
"[--try-flip]\n]
"[filename|camera_index]\n\n]
"See Facedetect.cmd for one call:\n"
"./facedetect--cascade=\". /.. /data/haarcascades/haarcascade_frontalface_alt.xml\ "--nested-cascade=\". /.. /data/haarcascades/haarcascade_eye_tree_eyeglasses.xml\ "--scale=1.3\n\n"
"During execution:\n\thit any key to quit.\n"
"\tusing OpenCV Version" << cv_version << "\ n" << Endl;
}
void Detectanddraw (mat& img, cascadeclassifier& Cascade,
cascadeclassifier& Nestedcascade,
Double scale, bool tryflip);
String Cascadename;
String Nestedcascadename;
int main (int argc, const char** argv)
{
Videocapture capture;
Mat frame, image;
String InputName;
BOOL Tryflip;
Cascadeclassifier Cascade, Nestedcascade;
Double scale;
Cv::commandlineparser parser (argc, argv,
"{Help h| |}"
"{cascade|haarcascade_frontalface_alt.xml|}"
"{nested-cascade|haarcascade_eye_tree_eyeglasses.xml|}"
"{scale|1|} {try-flip| |} {@filename |lena.jpg|} "
);
if (Parser.has ("Help"))
{
Help ();
return 0;
}
Cascadename = parser.get<string> ("cascade");
Nestedcascadename = parser.get<string> ("Nested-cascade");
Scale = parser.get<double> ("scale");
if (Scale < 1)
scale = 1;
Tryflip = Parser.has ("Try-flip");
InputName = parser.get<string> ("@filename");
if (!parser.check ())
{
Parser.printerrors ();
return 0;
}
if (!nestedcascade.load (nestedcascadename))
Cerr << "warning:could not load classifier cascade for nested objects" << Endl;
if (!cascade.load (cascadename))
{
Cerr << "error:could not load classifier cascade" << Endl;
Help ();
return-1;
}
if (Inputname.empty () | | | (IsDigit (inputname[0]) && inputname.size () = = 1))
{
int c = Inputname.empty ()? 0:inputname[0]-' 0 ';
if (!capture.open (c))
cout << "Capture from camera #" << C << "didn-t work" << Endl;
}
else if (inputname.size ())
{
Image = Imread (InputName, 1);
if (Image.empty ())
{
if (!capture.open (InputName))
cout << "Could not read" << inputname << Endl;
}
}
Else
{
Image = Imread (".. /data/lena.jpg ", 1);
if (Image.empty ()) cout << "couldn" t read. /data/lena.jpg "<< Endl;
}
if (capture.isopened ())
{
cout << "Video capturing has been started ..." << Endl;
for (;;)
{
Capture >> frame;
if (Frame.empty ())
Break
Mat frame1 = Frame.clone ();
Detectanddraw (FRAME1, Cascade, Nestedcascade, scale, tryflip);
int c = Waitkey (10);
if (c = = | | c = = ' Q ' | | c = = ' Q ')
Break
}
}
Else
{
cout << "detecting face (s) in" << InputName << Endl;
if (!image.empty ())
{
Detectanddraw (image, Cascade, Nestedcascade, scale, tryflip);
Waitkey (0);
}
else if (!inputname.empty ())
{
/* Assume it is a text file containing the
List of the image filenames to being processed-one per line */
file* f = fopen (Inputname.c_str (), "RT");
if (f)
{
Char buf[1000 + 1];
while (Fgets (buf, +, F))
{
int len = (int) strlen (BUF), C;
while (len > 0 && isspace (buf[len-1]))
len--;
Buf[len] = ' + ';
cout << "File" << buf << Endl;
Image = Imread (buf, 1);
if (!image.empty ())
{
Detectanddraw (image, Cascade, Nestedcascade, scale, tryflip);
c = Waitkey (0);
if (c = = | | c = = ' Q ' | | c = = ' Q ')
Break
}
Else
{
Cerr << "Aw snap, couldn ' t read image" << buf << Endl;
}
}
Fclose (f);
}
}
}
return 0;
}
void Detectanddraw (mat& img, cascadeclassifier& Cascade,
cascadeclassifier& Nestedcascade,
Double scale, BOOL tryflip)
{
Double T = 0;
Vector<rect> faces, Faces2;
Const static Scalar colors[] =
{
Scalar (255, 0, 0),
Scalar (255, 128, 0),
Scalar (255, 255, 0),
Scalar (0, 255, 0),
Scalar (0, 128, 255),
Scalar (0, 255, 255),
Scalar (0, 0, 255),
Scalar (255, 0, 255)
};
Mat Gray, Smallimg;
Cvtcolor (IMG, Gray, Color_bgr2gray);
Double FX = 1/scale;
Resize (Gray, Smallimg, Size (), FX, FX, Inter_linear);
Equalizehist (smallimg, smallimg);
T = (double) cvgettickcount ();
Cascade.detectmultiscale (Smallimg, Faces,
1.1, 2, 0
//| Cascade_find_biggest_object
//| Cascade_do_rough_search
| Cascade_scale_image,
Size (30, 30));
if (tryflip)
{
Flip (smallimg, smallimg, 1);
Cascade.detectmultiscale (smallimg, Faces2,
1.1, 2, 0
//| Cascade_find_biggest_object
//| Cascade_do_rough_search
| Cascade_scale_image,
Size (30, 30));
for (Vector<rect>::const_iterator r = Faces2.begin (); R! = Faces2.end (); r++)
{
Faces.push_back (Rect ( Smallimg.cols-r->x-r->width, R->y, R->width, r->height));
}
}
T = (double) cvgettickcount ()-T;
printf ("Detection time =%g ms\n", t/(double) cvgettickfrequency () *1000.));
for (size_t i = 0; i < faces.size (); i++)
{
Rect r = faces[i];
Mat Smallimgroi;
Vector<rect> nestedobjects;
Point Center;
Scalar color = colors[i% 8];
int radius;
Double aspect_ratio = (double) r.width/r.height;
if (0.75 < Aspect_ratio && Aspect_ratio < 1.3)
{
Center.x = Cvround ((r.x + r.width*0.5) *scale);
Center.y = Cvround ((r.y + r.height*0.5) *scale);
Radius = Cvround ((r.width + r.height) *0.25*scale);
Circle (IMG, center, RADIUS, color, 3, 8, 0);
}
Else
Rectangle (img, Cvpoint (Cvround (R.x*scale), Cvround (R.y*scale)),
Cvpoint (Cvround ((r.x + r.width-1) *scale), Cvround ((R.y + r.height-1) *scale),
Color, 3, 8, 0);
if (Nestedcascade.empty ())
Continue
Smallimgroi = Smallimg (R);
Nestedcascade.detectmultiscale (Smallimgroi, Nestedobjects,
1.1, 2, 0
//| Cascade_find_biggest_object
//| Cascade_do_rough_search
//| Cascade_do_canny_pruning
| Cascade_scale_image,
Size (30, 30));
for (size_t j = 0; J < Nestedobjects.size (); j + +)
{
Rect nr = Nestedobjects[j];
Center.x = Cvround ((r.x + nr.x + nr.width*0.5) *scale);
Center.y = Cvround ((r.y + nr.y + nr.height*0.5) *scale);
Radius = Cvround ((nr.width + nr.height) *0.25*scale);
Circle (IMG, center, RADIUS, color, 3, 8, 0);
}
}
Imshow ("Result", IMG);
}
Results:
OpenCV Human Face Detection Demo--facedetect