OpenCV self-haar classifier for human face recognition

Source: Internet
Author: User

1, identify a picture of the number of faces, where the absolute path is used, can be modified by itself

#include <cstdio> #include <cstdlib> #include <Windows.h> #include <vector> #include <i
ostream> #include <string> #include <opencv2\opencv.hpp> using namespace std;

using namespace CV;
	int main (int argc, char* argv[]) {//Load Haar feature detection classifier cascadeclassifier cascade;

	Cascade.load ("D:\\Program files\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt2.xml");
	Load Image const String pstrimagename = "E:\\1.jpg";
	Mat srcimage = Imread (Pstrimagename, Cv_load_image_color);

	Mat Grayimage;

	 Cvtcolor (Srcimage, Grayimage, Cv_bgr2gray);
	
		Face recognition and tagging if (Cascade.empty ()) cout << "classifier not Loaded" << Endl;  Cvscalar facecirclecolors[] = {{0, 0, 255}}, {{0, 128, 255}}, {{0, 255, 255}}, {{0, 255, 0}

		}, {{255, 128, 0}}, {{255, 255, 0}}, {{255, 0, 0}}, {{255, 0, 255}};

		Vector<cv::rect> faces;
		DWORD Dwtimebegin, Dwtimeend; Dwtimebegin = GetTickCount ();
		Identification of Cascade.detectmultiscale (Grayimage, faces);

		Dwtimeend = GetTickCount (); cout << "face Count:" << faces.end ()-Faces.begin () << "Time to identify:" << dwtimeend-dwtimebegin << "
		Ms\n ";
		Mark int n = 0;
			for (Vector<cv::rect>::const_iterator i = Faces.begin (); I <faces.end (); i++, n++) {Point Center;
			int radius;
			Center.x = Cvround ((i->x + i->width * 0.5));
			Center.y = Cvround ((i->y + i->height * 0.5));
			Radius = Cvround ((i->width + i->height) * 0.25);
		Circle (srcimage, center, RADIUS, facecirclecolors[n% 8], 2);
	} imshow ("src", srcimage);

Waitkey (); }

2. Recognize faces from the video stream

#include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/
Imgproc.hpp "#include <iostream> #include <stdio.h> using namespace std;


using namespace CV;
/** global variable */Cascadeclassifier Face_cascade;
Cascadeclassifier Eyes_cascade; Cvscalar facecirclecolors[] = {{0, 0, 255}}, {{0, 128, 255}}, {{0, 255, 255}}, {{0, 255, 0}}, {{2

55, 128, 0}}, {{255, 255, 0}}, {{255, 0, 0}}, {{255, 0, 255}}; /** @ MAIN function */int main (int argc, const char** argv) {face_cascade.load ("D:\\Program Files\\opencv\\sources\\data\\haarc
	Ascades\\haarcascade_frontalface_alt.xml ");


	if (Face_cascade.empty ()) cout << "Face Error loading" << Endl;
	Videocapture Capture (0);
			
		Mat frame;
			
			while (1) {Capture >> frame;
						
				if (Frame.empty ()) cout << "error" << Endl;
				Vector<cv::rect> faces;
				Mat Grayimage; Cvtcolor (frame, grayimage, Cv_bgr2gray);

				Identification of Face_cascade.detectmultiscale (Grayimage, faces);
				Mark int n = 0;
					for (Vector<cv::rect>::const_iterator i = Faces.begin (); I!=faces.end (); i++, n++) {Point Center;
					int radius;
					Center.x = Cvround ((i->x + i->width * 0.5));
					Center.y = Cvround ((i->y + i->height * 0.5));
					Radius = Cvround ((i->width + i->height) * 0.25);
				Circle (frame, center, radius, facecirclecolors[n% 8], 2);
				} imshow ("video", frame);
		Waitkey (10);
} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.