1 | // facedet.c - by Robin Hewitt, 2007 |
---|
2 | // http://www.cognotics.com/opencv/downloads/camshift_wrapper |
---|
3 | // This is free software. See License.txt, in the download |
---|
4 | // package, for details. |
---|
5 | // |
---|
6 | |
---|
7 | #include "cv.h" |
---|
8 | #include <stdio.h> |
---|
9 | #include "facedet.h" |
---|
10 | |
---|
11 | |
---|
12 | // File-level variables |
---|
13 | CvHaarClassifierCascade * pCascade = 0; // the face detector |
---|
14 | CvMemStorage * pStorage = 0; // memory for detector to use |
---|
15 | CvSeq * pFaceRectSeq; // memory-access interface |
---|
16 | |
---|
17 | |
---|
18 | ////////////////////////////////// |
---|
19 | // initFaceDet() |
---|
20 | // |
---|
21 | int initFaceDet(const char * haarCascadePath) |
---|
22 | { |
---|
23 | if( !(pStorage = cvCreateMemStorage(0)) ) |
---|
24 | { |
---|
25 | fprintf(stderr, "Can\'t allocate memory for face detection\n"); |
---|
26 | return 0; |
---|
27 | } |
---|
28 | |
---|
29 | pCascade = (CvHaarClassifierCascade *)cvLoad( haarCascadePath, 0, 0, 0 ); |
---|
30 | if( !pCascade ) |
---|
31 | { |
---|
32 | fprintf(stderr, "Can\'t load Haar classifier cascade from\n" |
---|
33 | " %s\n" |
---|
34 | "Please check that this is the correct path\n", |
---|
35 | haarCascadePath); |
---|
36 | return 0; |
---|
37 | } |
---|
38 | |
---|
39 | return 1; |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | ////////////////////////////////// |
---|
44 | // closeFaceDet() |
---|
45 | // |
---|
46 | void closeFaceDet() |
---|
47 | { |
---|
48 | if(pCascade) cvReleaseHaarClassifierCascade(&pCascade); |
---|
49 | if(pStorage) cvReleaseMemStorage(&pStorage); |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | ////////////////////////////////// |
---|
54 | // detectFace() |
---|
55 | // |
---|
56 | CvRect * detectFace(IplImage * pImg) |
---|
57 | { |
---|
58 | CvRect* r = 0; |
---|
59 | |
---|
60 | // detect faces in image |
---|
61 | int minFaceSize = pImg->width / 5; |
---|
62 | pFaceRectSeq = cvHaarDetectObjects |
---|
63 | (pImg, pCascade, pStorage, |
---|
64 | 1.1, // increase search scale by 10% each pass |
---|
65 | 4, // require six neighbors |
---|
66 | CV_HAAR_DO_CANNY_PRUNING, // skip regions unlikely to contain a face |
---|
67 | cvSize(minFaceSize, minFaceSize)); |
---|
68 | |
---|
69 | // if one or more faces are detected, return the first one |
---|
70 | if( pFaceRectSeq && pFaceRectSeq->total ) |
---|
71 | r = (CvRect*)cvGetSeqElem(pFaceRectSeq, 0); |
---|
72 | |
---|
73 | return r; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | |
---|