CamAdventure
 All Classes Files Functions Macros Pages
facedetect.c
1 // Sample Application to demonstrate how Face detection can be done as a part of your source code.
2 
3 // Include header files
4 #include "cv.h"
5 #include "highgui.h"
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <math.h>
12 #include <float.h>
13 #include <limits.h>
14 #include <time.h>
15 #include <ctype.h>
16 
17 
18 // Create a string that contains the exact cascade name
19 const char* cascade_name =
20 "C:/Program Files/OpenCV/data/haarcascades/haarcascade_frontalface_alt.xml";
21 /* "haarcascade_profileface.xml";*/
22 
23 
24 // Function prototype for detecting and drawing an object from an image
25 void detect_and_draw( IplImage* image );
26 
27 // Main function, defines the entry point for the program.
28 int main( int argc, char** argv )
29 {
30 
31  //Ouverture flux camera
32  CvCapture* capture = cvCaptureFromCAM( 0 );
33 
34  if( !capture ){
35  printf("ERROR: capture is NULL \n" );
36  exit(EXIT_FAILURE);
37  }
38 
39 // Call the function to detect and draw the face positions
40 detect_and_draw(img);
41 
42 // Wait for user input before quitting the program
43 cvWaitKey();
44 
45 // Release the image
46 cvReleaseImage(&img);
47 
48 // Destroy the window previously created with filename: "result"
49 cvDestroyWindow("result");
50 
51 // return 0 to indicate successfull execution of the program
52 return 0;
53 }
54 
55 // Function to detect and draw any faces that is present in an image
56 void detect_and_draw( IplImage* img )
57 {
58 
59 // Create memory for calculations
60 static CvMemStorage* storage = 0;
61 
62 // Create a new Haar classifier
63 static CvHaarClassifierCascade* cascade = 0;
64 
65 int scale = 1;
66 
67 // Create a new image based on the input image
68 IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
69 
70 // Create two points to represent the face locations
71 CvPoint pt1, pt2;
72 int i;
73 
74 // Load the [[HaarClassifierCascade]]
75 cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
76 
77 // Check whether the cascade has loaded successfully. Else report and error and quit
78 if( !cascade )
79 {
80 fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
81 return;
82 }
83 
84 // Allocate the memory storage
85 storage = cvCreateMemStorage(0);
86 
87 // Create a new named window with title: result
88 cvNamedWindow( "result", 1 );
89 
90 // Clear the memory storage which was used before
91 cvClearMemStorage( storage );
92 
93 // Find whether the cascade is loaded, to find the faces. If yes, then:
94 if( cascade )
95 {
96 
97 // There can be more than one face in an image. So create a growable sequence of faces.
98 // Detect the objects and store them in the sequence
99 CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
100 1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
101 cvSize(40, 40) );
102 
103 // Loop the number of faces found.
104 for( i = 0; i < (faces ? faces->total : 0); i++ )
105 {
106 // Create a new rectangle for drawing the face
107 CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
108 
109 // Find the dimensions of the face,and scale it if necessary
110 pt1.x = r->x*scale;
111 pt2.x = (r->x+r->width)*scale;
112 pt1.y = r->y*scale;
113 pt2.y = (r->y+r->height)*scale;
114 
115 // Draw the rectangle in the input image
116 cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
117 }
118 }
119 
120 // Show the image in the window named "result"
121 cvShowImage( "result", img );
122 
123 // Release the temp image created.
124 cvReleaseImage( &temp );
125 }
int main(int argc, char *argv[])
Entrée du programme.
Definition: KirbyTrack.c:164