&1 问题来源
在运行官网的facedetect这个demo的时候,总是不会出来result的图形,电脑右下角提示的错误是“显示器驱动程序已停止响应,而且已恢复 windows 8(R)”。
&2 前期处理
- 修改代码,各种代码上的调试都尝试过,demo运行失败了;
- 百度上的禁用视觉效果方案,即修改电脑的主题为“windows 经典”主题,demo运行失败了;
- 百度上的重新安装显卡驱动方案,即重新装集成网卡驱动,导致显示器黑屏,倒腾了一天才整回来,失败;
&3 成功解决
首先,打开注册表,找到HKEY_LOCAL_MACHINE,在SYSTEM中的CurrentControlSet中的Control的GrphicsDrivers上面点击右键,新建QEORD(64位)值(Q),数值名称为:TdrDelay,数值数据为:8,基数不用改变,选择十六进制即可。
然后,在你的项目编译文件夹内加入四个文件,haarcascade_eye_tree_eyeglasses.xml和haarcascade_frontalface_alt.xml、opencv_ffmpeg310_64.dll及opencv_world310d.dll;
在opencv的环境配置中,(前面有博文介绍),去掉可执行文件目录,去掉附加依赖项的opencv_world310.lib,至此,所有的环境配置方面已经完成。
&4 demo的代码和运行结果
注意:在opencv安装文件夹sourcessamplescpp中的文件facedetect.cpp即是源代码。
1 #include "opencv2/objdetect.hpp" 2 #include "opencv2/highgui.hpp" 3 #include "opencv2/imgproc.hpp" 4 #include <iostream> 5 6 using namespace std; 7 using namespace cv; 8 9 static void help() 10 11 { 12 cout << " This program demonstrates the cascade recognizer. Now you can use Haar or LBP features. " 13 "This classifier can recognize many kinds of rigid objects, once the appropriate classifier is trained. " 14 "It's most known use is for faces. " 15 "Usage: " 16 "./facedetect [--cascade=<cascade_path> this is the primary trained classifier such as frontal face] " 17 " [--nested-cascade[=nested_cascade_path this an optional secondary classifier such as eyes]] " 18 " [--scale=<image scale greater or equal to 1, try 1.3 for example>] " 19 " [--try-flip] " 20 " [filename|camera_index] " 21 "see facedetect.cmd for one call: " 22 "./facedetect --cascade="../../data/haarcascades/haarcascade_frontalface_alt.xml" --nested-cascade="../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml" --scale=1.3 " 23 "During execution: Hit any key to quit. " 24 " Using OpenCV version " << CV_VERSION << " " << endl; 25 } 26 27 void detectAndDraw(Mat& img, CascadeClassifier& cascade, 28 CascadeClassifier& nestedCascade, 29 double scale, bool tryflip); 30 31 string cascadeName; 32 string nestedCascadeName; 33 34 int main(int argc, const char** argv) 35 { 36 VideoCapture capture; 37 Mat frame, image; 38 string inputName; 39 bool tryflip; 40 CascadeClassifier cascade, nestedCascade; 41 double scale; 42 43 cv::CommandLineParser parser(argc, argv, 44 "{help h||}" 45 "{cascade|haarcascade_frontalface_alt.xml|}" 46 "{nested-cascade|haarcascade_eye_tree_eyeglasses.xml|}" 47 "{scale|1|}{try-flip||}{@filename|lena.jpg|}" 48 ); 49 50 51 if (parser.has("help")) 52 { 53 help(); 54 return 0; 55 } 56 cascadeName = parser.get<string>("cascade"); 57 nestedCascadeName = parser.get<string>("nested-cascade"); 58 scale = parser.get<double>("scale"); 59 if (scale < 1) 60 scale = 1; 61 tryflip = parser.has("try-flip"); 62 inputName = parser.get<string>("@filename"); 63 if (!parser.check()) 64 { 65 parser.printErrors(); 66 return 0; 67 } 68 if (!nestedCascade.load(nestedCascadeName)) 69 cerr << "WARNING: Could not load classifier cascade for nested objects" << endl; 70 if (!cascade.load(cascadeName)) 71 { 72 cerr << "ERROR: Could not load classifier cascade" << endl; 73 help(); 74 return -1; 75 } 76 if (inputName.empty() || (isdigit(inputName[0]) && inputName.size() == 1)) 77 { 78 int c = inputName.empty() ? 0 : inputName[0] - '0'; 79 if (!capture.open(c)) 80 cout << "Capture from camera #" << c << " didn't work" << endl; 81 } 82 else if (inputName.size()) 83 { 84 image = imread(inputName, 1); 85 if (image.empty()) 86 { 87 if (!capture.open(inputName)) 88 cout << "Could not read " << inputName << endl; 89 } 90 } 91 else 92 { 93 image = imread("../data/lena.jpg", 1); 94 if (image.empty()) cout << "Couldn't read ../data/lena.jpg" << endl; 95 } 96 97 if (capture.isOpened()) 98 { 99 cout << "Video capturing has been started ..." << endl; 100 101 for (;;) 102 { 103 capture >> frame; 104 if (frame.empty()) 105 break; 106 107 Mat frame1 = frame.clone(); 108 detectAndDraw(frame1, cascade, nestedCascade, scale, tryflip); 109 110 int c = waitKey(10); 111 if (c == 27 || c == 'q' || c == 'Q') 112 break; 113 } 114 } 115 else 116 { 117 cout << "Detecting face(s) in " << inputName << endl; 118 if (!image.empty()) 119 { 120 detectAndDraw(image, cascade, nestedCascade, scale, tryflip); 121 waitKey(0); 122 } 123 else if (!inputName.empty()) 124 { 125 /* assume it is a text file containing the 126 list of the image filenames to be processed - one per line */ 127 FILE* f = fopen(inputName.c_str(), "rt"); 128 if (f) 129 { 130 char buf[1000 + 1]; 131 while (fgets(buf, 1000, f)) 132 { 133 int len = (int)strlen(buf), c; 134 while (len > 0 && isspace(buf[len - 1])) 135 len--; 136 buf[len] = '