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
| #include<opencv2/objdetect/objdetect.hpp> #include<opencv2/highgui/highgui.hpp> #include<opencv2/imgproc/imgproc.hpp>
using namespace cv; using namespace std;
CascadeClassifier faceCascade;
int main() { VideoCapture cap(0); if (!cap.isOpened()) { return -1; } Mat frame; Mat gray; bool stop = false;
faceCascade.load("E:\\VS2015\\myProject\\ConsoleApplication1\\ConsoleApplication1\\haarcascades\\haarcascade_frontalface_alt2.xml");
while (!stop) { cap >> frame;
vector<Rect> faces(0);
cvtColor(frame, gray, CV_BGR2GRAY); equalizeHist(gray, gray);
faceCascade.detectMultiScale(gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
if (faces.size()>0) { for (int i = 0; i<faces.size(); i++) { rectangle(frame, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(0, 255, 0), 1, 8); } }
imshow("face", frame); if (waitKey(50) >= 0) stop = true; }
}
|