Facial and eye, nose, and mouth detection programs based on opencv Harr cascade classifier

Source: Internet
Author: User
# Include <opencv2/CORE/core. HPP> # include <opencv2/highgui // highgui. HPP> # include <opencv2/objdetect. HPP >#include <string >#include <vector> using namespace STD; int main () {CV: cascadeclassifier mfacedetector; CV: cascadeclassifier meyedetector; CV: cascadeclassifier executor; CV: cascadeclassifier mnosedetector; // load four face feature classifier files. You can find if (mfacedetector from the installation directory of opencv. empty () mfacedetect Or. load ("haarcascade_frontalface_default.xml"); If (meyedetector. empty () meyedetector. load ("haarcascade_mcs_eyepair_big.xml"); If (mnosedetector. empty () mnosedetector. load ("haarcascade_mcs_nose.xml"); If (mmouthdetector. empty () mmouthdetector. load ("haarcascade_mcs_mouth.xml"); // open the video file // CV: videocapture capture ("bike. avi "); // 0 open default camera CV: videocapture capture (0); // check whether the video is No open if (! Capture. isopened () return 1; // get the frame rate double rate = capture. get (cv_cap_prop_fps); bool stop (false); CV: mat frame; // current video frame CV: mat melabimage; // backup frame image CV :: namedwindow ("extracted frame"); // interval between two frames int delay = 1000/rate; // loop playback of all frames while (! Stop) {// read the next frame if (! Capture. read (FRAME) break; frame. copyto (melabimage); // face detection // scaling factor float scalefactor = 3.0f; vector <CV: rect> facevec; mfacedetector. detectmultiscale (frame, facevec, scalefactor); int I, j; for (I = 0; I <facevec. size (); I ++) {CV: rectangle (melabimage, facevec [I], cv_rgb (255, 0), 2); CV :: mat face = frame (facevec [I]); // detects the eye vector <CV: rect> eyevec; meyedetector. detectmultiscale (face, eyevec); For (j = 0; j <eyevec. size (); j ++) {CV: rect = eyevec [J]; rect. X + = facevec [I]. x; rect. Y + = facevec [I]. y; CV: rectangle (melabimage, rect, cv_rgb (0,255, 0), 2);} // detect the nose vector <CV: rect> nosevec; mnosedetector. detectmultiscale (face, nosevec, 3); For (j = 0; j <nosevec. size (); j ++) {CV: rect = nosevec [J]; rect. X + = facevec [I]. x; rect. Y + = facevec [I]. y; CV: rectangle (melabimage, rect, cv_rgb (255,), 2);} // detect the mouth vector <CV: rect> mouthvec; CV :: rect halfrect = facevec [I]; halfrect. height/= 2; halfrect. Y + = halfrect. height; CV: mat halfface = frame (halfrect); mmouthdetector. detectmultiscale (halfface, mouthvec, 3); For (j = 0; j <mouthvec. size (); j ++) {CV: rect = mouthvec [J]; rect. X + = halfrect. x; rect. Y + = halfrect. y; CV: rectangle (melabimage, rect, cv_rgb (255,255,255), 2) ;}// display the image CV: imshow ("extracted frame ", melabimage); // press any key to stop the video. // If (CV: waitkey (Delay)> = 0) // stop = true; CV: waitkey (20 );} // disable the video file capture. release (); 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.