source: level2/competencies/DistanceCheck/src/distanceCheck.cpp @ 233

Revision 170, 4.9 KB checked in by ginevra, 11 years ago (diff)
Line 
1// AUTHOR: Ginevra Castellano
2// Queen Mary University of London
3// DATE: 03/2009
4// VERSION: 1.0
5
6// Copyright (C) 2009 Ginevra Castellano
7
8// This file is part of the DistanceCheck program.
9
10// DistanceCheck 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// DistanceCheck is based on the face detection code provided by OpenCV.
24// OpenCV: 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
29// This class includes functions that allow one to predict whether the user is staying still, approaching the camera or
30// withdrawing by comparing the area of the face bounding box in the current frame with the values of the area in
31// a temporal window of size N preceding it
32
33#include "distanceCheck.h"
34
35
36DistanceCheck::DistanceCheck(int windowSize)
37{
38        this->windowSize = windowSize;
39        if (this->windowSize < 0) this->windowSize = 1;
40}
41
42DistanceCheck::~DistanceCheck()
43{
44        this->faceAreaVals.clear();
45
46}
47
48double DistanceCheck::getAverageVal(int startValIndex, int endValIndex)
49{
50        int valsSize = this->faceAreaVals.size();    // Calculate the size of faceAreaVals (the vector that stores the areas)
51        double average = 0.0;
52        int sumArea = 0;
53
54        int j= 0;
55        for (j = startValIndex; j < endValIndex+1; j++) 
56        {
57                sumArea += this->faceAreaVals[j];     // Sum of the areas
58        }
59        int num = (endValIndex - startValIndex) + 1;
60        average = (double)((double)sumArea/(double)num);  // Calculate the average area
61        return average;
62}
63
64double DistanceCheck::getAverageValInWindow(void)
65{
66        int valsSize = this->faceAreaVals.size();
67        if (valsSize <= this->windowSize) return 0.0; 
68        int currentIndex = valsSize - 1;
69        double average = 0.0;
70        average = this->getAverageVal(currentIndex - this->windowSize, currentIndex - 1); // Calculate the average area over a temporal window of size "windowSize" preceding the current frame
71        printf("\nAverage is %lf", average);
72        return average;
73}
74
75void DistanceCheck::addFaceVal(int val)
76{
77        this->faceAreaVals.push_back(val);  // Store the values of the area in a vector
78}
79
80void DistanceCheck::setFaceVal(int index, int val)
81{
82        if ((index > this->faceAreaVals.size() -1) || (index < 0)) return; 
83        this->faceAreaVals[index] = val;
84}
85
86int DistanceCheck::getNumElements(void) 
87{
88        return this->faceAreaVals.size();
89}
90
91
92// Compare the area of the face bounding box in the current frame with the average value of the area over a set
93// temporal window preceding it
94
95int DistanceCheck::getMovementType(void)
96{
97
98    int valsSize = this->faceAreaVals.size();
99        int currentIndex = valsSize - 1;
100        double average = this->getAverageValInWindow();
101
102        if (valsSize <= this->windowSize) return UNDEFINED;
103
104        double percent = 0.1;  // Percentage of increase/decrease of the area of the face bounding box
105    double extra = faceAreaVals[currentIndex - this->windowSize] * percent;  // Increase/decrease of the area of the face bounding box in the first frame of the window
106    double newBiggerArea = faceAreaVals[currentIndex - this->windowSize] + extra; // Area in the first frame of the window increased of a certain percentage
107    double newSmallerArea = faceAreaVals[currentIndex - this->windowSize] - extra;  // Area in the first frame of the window decreased of a certain percentage
108
109        // If the area of the face bounding box in the current frame is not bigger than the area of the starting frame of the window
110        // increased of a certain percentage and is not smaller than the area at the starting frame of the window decreased of a
111        // of a certain percentage the user is staying still
112
113        if (faceAreaVals[this->faceAreaVals.size() - 1] <= newBiggerArea &&
114                faceAreaVals[this->faceAreaVals.size() - 1]   >= newSmallerArea)
115        {
116                return STAYING_STILL;
117        }
118   
119        // If the area of the face bounding box in the current frame is bigger than the average area over the preceding temporal window
120        // the user is approaching the camera
121       
122        else if (this->faceAreaVals[this->faceAreaVals.size() - 1] > average)
123        {
124                return APPROACHING;
125        }
126       
127        // If the area of the face bounding box in the current frame is smaller than the average area over the preceding temporal window
128        // the user is withdrawing
129       
130        else
131        {
132                return WITHDRAWING;
133        }
134}
135
Note: See TracBrowser for help on using the repository browser.