source: level2/competencies/src/FaceDetection.cpp @ 157

Revision 157, 4.8 KB checked in by ginevra, 10 years ago (diff)
Line 
1// AUTHOR: Ginevra Castellano
2// Queen Mary University of London
3// DATE: 10/2009
4// VERSION: 1.0
5
6// Copyright (C) 2009 Ginevra Castellano
7
8// This file is part of the FaceTracking program
9
10// FaceTracking is free software: you can redistribute it and/or modify
11// it under the terms of the GNU Lesser General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18// GNU Lesser General Public License for more details.
19
20// You should have received a copy of the GNU Lesser General Public License
21// along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23// FaceTracking uses the OpenCV library
24// Copyright (C) 2000-2006, Intel Corporation, all rights reserved.
25// Third party copyrights are property of their respective owners.
26// See OpenCV_license.txt, in the program folder, for details.
27
28// FaceTracking uses the Camshift wrapper program (see camshift_wrapper.cpp and camshift_wrapper.h):
29// Copyright (c) 2007, Robin Hewitt (http://www.robin-hewitt.com)
30// See License.txt, in the Camshift wrapper folder, for details.
31
32
33
34#include "cv.h"
35#include "highgui.h"
36
37#include <stdio.h>
38
39#include "FaceDetection.h"
40
41
42FaceDetection::FaceDetection()
43{
44    this->storage = NULL; // Create memory for calculations
45        this->cascade = NULL; // Create a new Haar classifier
46        this->cascade_lateral = NULL; // Create a new Haar classifier
47       
48        this->cascade_name = "haarcascade_frontalface_alt.xml"; // Create a string that contains the cascade name
49        this->cascade_name_lateral = "haarcascade_profileface.xml"; // Create a string that contains the cascade name
50
51        this->rect = NULL;
52}
53
54
55FaceDetection::~FaceDetection()
56{
57        if(this->cascade) cvReleaseHaarClassifierCascade(&(this->cascade));
58        if(this->cascade_lateral) cvReleaseHaarClassifierCascade(&(this->cascade_lateral));
59        if(this->storage) cvReleaseMemStorage(&(this->storage));
60}
61
62
63void FaceDetection::InitFaceDetection(void)
64{
65        this->cascade_name = "C:/Program Files/OpenCV/data/haarcascades/haarcascade_frontalface_alt.xml";
66        this->cascade_name_lateral = "C:/Program Files/OpenCV/data/haarcascades/haarcascade_profileface.xml";
67       
68        this->storage = cvCreateMemStorage(0); // Allocate the memory storage
69
70        // Load the HaarClassifierCascade
71    this->cascade = (CvHaarClassifierCascade*)cvLoad(this->cascade_name, 0, 0, 0 );
72        this->cascade_lateral = (CvHaarClassifierCascade*)cvLoad(this->cascade_name_lateral, 0, 0, 0 );
73
74        // Check whether the cascade has loaded successfully, otherwise report and error and quit
75    if(!this->cascade)
76    {
77     fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
78     fprintf( stderr,
79     "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
80    }
81
82         if(!this->cascade_lateral)
83    {
84     fprintf( stderr, "ERROR: Could not load classifier cascade lateral\n" );
85     fprintf( stderr,
86     "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
87        }
88}
89
90
91CvRect *FaceDetection::detectFace(IplImage *src)
92{
93    int minSize = src->width / 4; // Size of the smallest face to search for
94
95        // Clear the memory storage which was used before
96        cvClearMemStorage(this->storage);
97
98        // If the cascade is loaded, then:
99    if(this->cascade && this->cascade_lateral)
100    {
101     double t = (double)cvGetTickCount();
102
103         // There can be more than one face in an image; detect faces and store them in a growable sequence
104       
105         CvSeq* faces = cvHaarDetectObjects(src, this->cascade, this->storage,
106                                            1.1, 3, CV_HAAR_FIND_BIGGEST_OBJECT,
107                                            cvSize(minSize, minSize));
108
109     t = (double)cvGetTickCount() - t;
110
111     printf( "\nDetection time = %gms\n", t/((double)cvGetTickFrequency()*1000.));
112
113         if(faces->total < 1)  // If no face is detected using the frontal classifier, use the lateral Haar classifier
114
115         faces = cvHaarDetectObjects(src, this->cascade_lateral, this->storage,
116                                            1.1, 3, CV_HAAR_FIND_BIGGEST_OBJECT,
117                                            cvSize(minSize, minSize));
118               
119         
120         if(faces)
121         {
122                // If one or more faces are detected, return the first one
123                 if (faces->total > 0)
124                 {
125                         printf("\nDetected a face");
126                         this->rect = (CvRect*)cvGetSeqElem(faces, 0);
127                 }
128                 //otherwise, reset the face rectangle to be NULL
129                 else
130                 {
131                         printf("\nDidn't detect a face");
132                         this->rect = NULL;
133                 }
134         }
135         else this->rect = NULL;
136
137         return this->rect;
138    }
139}
Note: See TracBrowser for help on using the repository browser.