-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfacedet.c
69 lines (52 loc) · 1.96 KB
/
facedet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//Developed By Yash Shah
//facedet.c
#include "cv.h"
#include <stdio.h>
#include "facedet.h"
// File-level variables
map<string, CvHaarClassifierCascade *> pCascades; // the face detector
CvMemStorage * pStorage = 0; // memory for detector to use
CvSeq * pFaceRectSeq; // memory-access interface
//////////////////////////////////
// initFaceDet()
//
int initFaceDet(string haarCascadePath, map<string, string> cascadeMap)
{
if( !(pStorage = cvCreateMemStorage(0)) )
{
fprintf(stderr, "Can\'t allocate memory for face detection\n");
return 0;
}
pCascades["face"] = (CvHaarClassifierCascade *)cvLoad( (haarCascadePath + cascadeMap["face"]).c_str(), 0, 0, 0 );
pCascades["leftEye"] = (CvHaarClassifierCascade *)cvLoad( (haarCascadePath + cascadeMap["leftEye"]).c_str(), 0, 0, 0 );
pCascades["rightEye"] = (CvHaarClassifierCascade *)cvLoad( (haarCascadePath + cascadeMap["rightEye"]).c_str(), 0, 0, 0 );
pCascades["mouth"] = (CvHaarClassifierCascade *)cvLoad( (haarCascadePath + cascadeMap["mouth"]).c_str(), 0, 0, 0 );
return 1;
}
//////////////////////////////////
// closeFaceDet()
//
void closeFaceDet()
{
/* if(pCascade) cvReleaseHaarClassifierCascade(&pCascade);*/
if(pStorage) cvReleaseMemStorage(&pStorage);
}
//////////////////////////////////
// detectFace()
//
CvRect * detectObject(IplImage * pImg, string object)
{
CvRect* r = 0;
// detect faces in image
int minFaceSize = pImg->width / 5;
pFaceRectSeq = cvHaarDetectObjects
(pImg, pCascades[object], pStorage,
1.1, // increase search scale by 10% each pass
6, // require six neighbors
CV_HAAR_DO_CANNY_PRUNING, // skip regions unlikely to contain a face
cvSize(minFaceSize, minFaceSize));
// if one or more faces are detected, return the first one
if( pFaceRectSeq && pFaceRectSeq->total )
r = (CvRect*)cvGetSeqElem(pFaceRectSeq, 0);
return r;
}