#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay(Mat frame);
/** Global variables */
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
/** @function main */
int main(int argc, const char** argv)
{
CommandLineParser parser(argc, argv,
"{help h||}"
"{face_cascade|C:/Users/离子键/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml|Path to face cascade.}"
"{eyes_cascade|C:/Users/离子键/opencv/sources/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml|Path to eyes cascade.}"
"{camera|0|Camera device number.}");
parser.about("\nThis program demonstrates using the cv::CascadeClassifier class to detect objects (Face + eyes) in a video stream.\n"
"You can use Haar or LBP features.\n\n");
parser.printMessage();
String face_cascade_name = parser.get("face_cascade");
String eyes_cascade_name = parser.get("eyes_cascade");
//-- 1. Load the cascades
if (!face_cascade.load(face_cascade_name))
{
cout <<"--(!)Error loading face cascade\n";
return -1;
};
if (!eyes_cascade.load(eyes_cascade_name))
{
cout <<"--(!)Error loading eyes cascade\n";
return -1;
};
int camera_device &#61; parser.get("camera");
VideoCapture capture;
//-- 2. Read the video stream
capture.open(camera_device);
if (!capture.isOpened())
{
cout <<"--(!)Error opening video capture\n";
return -1;
}
Mat frame;
while (capture.read(frame))
{
if (frame.empty())
{
cout <<"--(!) No captured frame -- Break!\n";
break;
}
//-- 3. Apply the classifier to the frame
detectAndDisplay(frame);
if (waitKey(10) &#61;&#61; 27)
{
break; // escape
}
}
return 0;
}
/** &#64;function detectAndDisplay */
void detectAndDisplay(Mat frame)
{
Mat frame_gray;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
//-- Detect faces
std::vector faces;
face_cascade.detectMultiScale(frame_gray, faces);
for (size_t i &#61; 0; i {
Point center(faces[i].x &#43; faces[i].width / 2, faces[i].y &#43; faces[i].height / 2);
ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4);
Mat faceROI &#61; frame_gray(faces[i]);
//-- In each face, detect eyes
std::vector eyes;
eyes_cascade.detectMultiScale(faceROI, eyes);
for (size_t j &#61; 0; j {
Point eye_center(faces[i].x &#43; eyes[j].x &#43; eyes[j].width / 2, faces[i].y &#43; eyes[j].y &#43; eyes[j].height / 2);
int radius &#61; cvRound((eyes[j].width &#43; eyes[j].height) * 0.25);
circle(frame, eye_center, radius, Scalar(255, 0, 0), 4);
}
}
//-- Show what you got
imshow("Capture - Face detection", frame);
}
OpenCV3编程入门---人脸识别示例&#xff1a;objectDetection报错“Error loading face cascade”_Burette_Lee的博客-CSDN博客