0% found this document useful (0 votes)
65 views3 pages

#Include #Include "CV.H" #Include "Highgui.h"

This C++ code uses OpenCV to detect faces in real-time video from a webcam. It loads a pre-trained Haar cascade classifier to detect faces, initializes the webcam, and enters a loop to continuously capture frames, detect faces in each frame using the classifier, and display the video output with red boxes drawn around any detected faces. It releases resources before ending.

Uploaded by

Avriel Desu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views3 pages

#Include #Include "CV.H" #Include "Highgui.h"

This C++ code uses OpenCV to detect faces in real-time video from a webcam. It loads a pre-trained Haar cascade classifier to detect faces, initializes the webcam, and enters a loop to continuously capture frames, detect faces in each frame using the classifier, and display the video output with red boxes drawn around any detected faces. It releases resources before ending.

Uploaded by

Avriel Desu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Listing 1: 

OpenCV Face Detection

1. /**
2.  * Display video from webcam and detect faces
3.  */
4. #include <stdio.h>
5. #include "cv.h"
6. #include "highgui.h"
7.  
8. CvHaarClassifierCascade *cascade;
9. CvMemStorage            *storage;
10.  
11. void detectFaces( IplImage *img );
12.  
13. int main( int argc, char** argv )
14. {
15.     CvCapture *capture;
16.     IplImage  *frame;
17.     int       key;
18.     char      *filename = "haarcascade_frontalface_alt.xml";
19.  
20.     /* load the classifier
21.        note that I put the file in the same directory with
22.        this code */
23.     cascade = ( CvHaarClassifierCascade* )cvLoad( filename, 0, 0, 0 );
24.  
25.     /* setup memory buffer; needed by the face detector */
26.     storage = cvCreateMemStorage( 0 );
27.  
28.     /* initialize camera */
29.     capture = cvCaptureFromCAM( 0 );
30.  
31.     /* always check */
32.     assert( cascade && storage && capture );
33.  
34.     /* create a window */
35.     cvNamedWindow( "video", 1 );
36.  
37.     while( key != 'q' ) {
38.         /* get a frame */
39.         frame = cvQueryFrame( capture );
40.  
41.         /* always check */
42.         if( !frame ) break;
43.  
44.         /* 'fix' frame */
45.         cvFlip( frame, frame, -1 );
46.         frame->origin = 0;
47.  
48.         /* detect faces and display video */
49.         detectFaces( frame );
50.  
51.         /* quit if user press 'q' */
52.         key = cvWaitKey( 10 );
53.     }
54.  
55.     /* free memory */
56.     cvReleaseCapture( &capture );
57.     cvDestroyWindow( "video" );
58.     cvReleaseHaarClassifierCascade( &cascade );
59.     cvReleaseMemStorage( &storage );
60.  
61.     return 0;
62. }
63.  
64. void detectFaces( IplImage *img )
65. {
66.     int i;
67.  
68.     /* detect faces */
69.     CvSeq *faces = cvHaarDetectObjects(
70.             img,
71.             cascade,
72.             storage,
73.             1.1,
74.             3,
75.             0 /*CV_HAAR_DO_CANNY_PRUNNING*/,
76.             cvSize( 40, 40 ) );
77.  
78.     /* for each face found, draw a red box */
79.     for( i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) {
80.         CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );
81.         cvRectangle( img,
82.                      cvPoint( r->x, r->y ),
83.                      cvPoint( r->x + r->width, r->y + r->height ),
84.                      CV_RGB( 255, 0, 0 ), 1, 8, 0 );
85.     }
86.  
87.     /* display video */
88.     cvShowImage( "video", img );
89. }
90.  

You might also like