GestureRecognitionToolkit  Version: 0.1.0
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
ClassLabelTimeoutFilter.cpp
1 /*
2  GRT MIT License
3  Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5  Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6  and associated documentation files (the "Software"), to deal in the Software without restriction,
7  including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9  subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in all copies or substantial
12  portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
22 
23 GRT_BEGIN_NAMESPACE
24 
25 //Register the ClassLabelTimeoutFilter module with the PostProcessing base class
26 RegisterPostProcessingModule< ClassLabelTimeoutFilter > ClassLabelTimeoutFilter::registerModule("ClassLabelTimeoutFilter");
27 
28 ClassLabelTimeoutFilter::ClassLabelTimeoutFilter(unsigned long timeoutDuration,UINT filterMode){
29  classType = "ClassLabelTimeoutFilter";
30  postProcessingType = classType;
31  postProcessingInputMode = INPUT_MODE_PREDICTED_CLASS_LABEL;
32  postProcessingOutputMode = OUTPUT_MODE_PREDICTED_CLASS_LABEL;
33  debugLog.setProceedingText("[DEBUG ClassLabelTimeoutFilter]");
34  errorLog.setProceedingText("[ERROR ClassLabelTimeoutFilter]");
35  warningLog.setProceedingText("[WARNING ClassLabelTimeoutFilter]");
36  init(timeoutDuration,filterMode);
37 }
38 
40 
41  classType = "ClassLabelTimeoutFilter";
42  postProcessingType = classType;
43  postProcessingInputMode = INPUT_MODE_PREDICTED_CLASS_LABEL;
44  postProcessingOutputMode = OUTPUT_MODE_PREDICTED_CLASS_LABEL;
45  debugLog.setProceedingText("[DEBUG ClassLabelTimeoutFilter]");
46  errorLog.setProceedingText("[ERROR ClassLabelTimeoutFilter]");
47  warningLog.setProceedingText("[WARNING ClassLabelTimeoutFilter]");
48 
49  //Copy the classLabelTimeoutFilter values
50  this->filteredClassLabel = rhs.filteredClassLabel;
51  this->filterMode = rhs.filterMode;
52  this->timeoutDuration = rhs.timeoutDuration;
53  this->classLabelTimers = rhs.classLabelTimers;
54 
55  //Clone the post processing base variables
57 }
58 
60 
61 }
62 
64 
65  if( this != &rhs ){
66  //Copy the classLabelTimeoutFilter values
67  this->filteredClassLabel = rhs.filteredClassLabel;
68  this->filterMode = rhs.filterMode;
69  this->timeoutDuration = rhs.timeoutDuration;
70  this->classLabelTimers = rhs.classLabelTimers;
71 
72  //Clone the post processing base variables
74  }
75  return *this;
76 }
77 
79 
80  if( postProcessing == NULL ) return false;
81 
82  if( this->getPostProcessingType() == postProcessing->getPostProcessingType() ){
83 
84  ClassLabelTimeoutFilter *ptr = (ClassLabelTimeoutFilter*)postProcessing;
85 
86  //Clone the classLabelTimeoutFilter values
87  this->filteredClassLabel = ptr->filterMode;
88  this->filterMode = ptr->filterMode;
89  this->timeoutDuration = ptr->timeoutDuration;
90  this->classLabelTimers = ptr->classLabelTimers;
91 
92  //Clone the post processing base variables
93  return copyBaseVariables( postProcessing );
94  }
95  return false;
96 }
97 
99 
100 #ifdef GRT_SAFE_CHECKING
101  if( !initialized ){
102  errorLog << "process(const VectorDouble &inputVector) - Not initialized!" << std::endl;
103  return false;
104  }
105 
106  if( inputVector.getSize() != numInputDimensions ){
107  errorLog << "process(const VectorDouble &inputVector) - The size of the inputVector (" << inputVector.getSize() << ") does not match that of the filter (" << numInputDimensions << ")!" << std::endl;
108  return false;
109  }
110 #endif
111 
112  //Use only the first value (as that is the predicted class label)
113  processedData[0] = filter( (UINT)inputVector[0] );
114  return true;
115 }
116 
118  filteredClassLabel = 0;
119  classLabelTimers.clear();
120  processedData.clear();
121  processedData.resize(1,0);
122  return true;
123 }
124 
125 bool ClassLabelTimeoutFilter::init(unsigned long timeoutDuration,UINT filterMode){
126 
127  initialized = false;
128 
129  if( filterMode != ALL_CLASS_LABELS && filterMode != INDEPENDENT_CLASS_LABELS ){
130  errorLog << "init(double timeoutDuration,UINT filterMode) - Unkown filter mode!" << std::endl;
131  return false;
132  }
133 
134  this->timeoutDuration = timeoutDuration;
135  this->filterMode = filterMode;
136  numInputDimensions = 1;
137  numOutputDimensions = 1;
138  initialized = reset();
139  return true;
140 }
141 
142 UINT ClassLabelTimeoutFilter::filter(UINT predictedClassLabel){
143 
144  //If we get the NULL class and there are no active timers running then we do not need to do anything
145  if( predictedClassLabel == 0 && classLabelTimers.size() == 0 ){
146  filteredClassLabel = predictedClassLabel;
147  return filteredClassLabel;
148  }
149 
150  bool matchFound = false;
152 
153  switch( filterMode ){
154  case ALL_CLASS_LABELS:
155 
156  //Have we seen any class label yet, if not then just start the timer and return the current class label
157  if( classLabelTimers.size() == 0 ){
158  filteredClassLabel = predictedClassLabel;
159  classLabelTimers.push_back( ClassLabelAndTimer(predictedClassLabel,timeoutDuration) );
160  }else{
161  //Otherwise check to see if the timer has timed-out
162  if( classLabelTimers[0].timerReached() ){
163  //Clear the timer
164  classLabelTimers.clear();
165 
166  //Check if the current predictedClassLabel is a valid gesture, if so then recursively call this function
167  //to start a new filter
168  filteredClassLabel = 0;
169  if( predictedClassLabel ){
170  filteredClassLabel = filter( predictedClassLabel );
171  }
172 
173  }else filteredClassLabel = 0;
174  }
175 
176  break;
177  case INDEPENDENT_CLASS_LABELS:
178 
179  //Search the classLabelTimers buffer to find a matching class label
180  if( classLabelTimers.size() > 0 ){
181  iter = classLabelTimers.begin();
182 
183  while( iter != classLabelTimers.end() ){
184  if( iter->getClassLabel() == predictedClassLabel ){
185  //Check to see if the timer for this class has elapsed
186  if( iter->timerReached() ){
187  //Reset the timer for this label
188  iter->set(predictedClassLabel,timeoutDuration);
189 
190  //Signal that a match was found
191  matchFound = true;
192  filteredClassLabel = predictedClassLabel;
193  break;
194  }else filteredClassLabel = 0;
195 
196  //Update the iterator
197  iter++;
198  }else{
199  if( iter->timerReached() ){
200  //Erase the current timer from the buffer
201  iter = classLabelTimers.erase( iter );
202  }else iter++;
203  }
204  }
205 
206  }
207 
208  //If a match has not been found then create a new timer
209  if( !matchFound ){
210  classLabelTimers.push_back( ClassLabelAndTimer(predictedClassLabel,timeoutDuration) );
211  filteredClassLabel = predictedClassLabel;
212  }
213 
214  break;
215  }
216 
217  return filteredClassLabel;
218 }
219 
221 
222  for(UINT i=0; i<classLabelTimers.getSize(); i++){
223  if( classLabelTimers[i].timerReached() ){
224  return true;
225  }
226  }
227 
228  return false;
229 }
230 
231 bool ClassLabelTimeoutFilter::saveModelToFile( std::string filename ) const{
232 
233  if( !initialized ){
234  errorLog << "saveModelToFile(string filename) - The ClassLabelTimeoutFilter has not been initialized" << std::endl;
235  return false;
236  }
237 
238  std::fstream file;
239  file.open(filename.c_str(), std::ios::out);
240 
241  if( !saveModelToFile( file ) ){
242  file.close();
243  return false;
244  }
245 
246  file.close();
247 
248  return true;
249 }
250 
251 bool ClassLabelTimeoutFilter::saveModelToFile( std::fstream &file ) const{
252 
253  if( !file.is_open() ){
254  errorLog << "saveModelToFile(fstream &file) - The file is not open!" << std::endl;
255  return false;
256  }
257 
258  file << "GRT_CLASS_LABEL_TIMEOUT_FILTER_FILE_V1.0" << std::endl;
259  file << "NumInputDimensions: " << numInputDimensions << std::endl;
260  file << "NumOutputDimensions: " << numOutputDimensions << std::endl;
261  file << "FilterMode: " << filterMode << std::endl;
262  file << "TimeoutDuration: " << timeoutDuration << std::endl;
263 
264  return true;
265 }
266 
267 bool ClassLabelTimeoutFilter::loadModelFromFile( std::string filename ){
268 
269  std::fstream file;
270  file.open(filename.c_str(), std::ios::in);
271 
272  if( !loadModelFromFile( file ) ){
273  file.close();
274  initialized = false;
275  return false;
276  }
277 
278  file.close();
279 
280  return true;
281 }
282 
284 
285  if( !file.is_open() ){
286  errorLog << "loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
287  return false;
288  }
289 
290  std::string word;
291 
292  //Load the header
293  file >> word;
294 
295  if( word != "GRT_CLASS_LABEL_TIMEOUT_FILTER_FILE_V1.0" ){
296  errorLog << "loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
297  return false;
298  }
299 
300  file >> word;
301  if( word != "NumInputDimensions:" ){
302  errorLog << "loadModelFromFile(fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
303  return false;
304  }
305  file >> numInputDimensions;
306 
307  //Load the number of output dimensions
308  file >> word;
309  if( word != "NumOutputDimensions:" ){
310  errorLog << "loadModelFromFile(fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
311  return false;
312  }
313  file >> numOutputDimensions;
314 
315  //Load the filterMode
316  file >> word;
317  if( word != "FilterMode:" ){
318  errorLog << "loadModelFromFile(fstream &file) - Failed to read FilterMode header!" << std::endl;
319  return false;
320  }
321  file >> filterMode;
322 
323  file >> word;
324  if( word != "TimeoutDuration:" ){
325  errorLog << "loadModelFromFile(fstream &file) - Failed to read TimeoutDuration header!" << std::endl;
326  return false;
327  }
328  file >> timeoutDuration;
329 
330  //Init the classLabelTimeoutFilter module to ensure everything is initialized correctly
331  return init(timeoutDuration,filterMode);
332 }
333 
334 bool ClassLabelTimeoutFilter::setTimeoutDuration(unsigned long timeoutDuration){
335  this->timeoutDuration = timeoutDuration;
336  if( initialized ){
337  return reset();
338  }
339  return true;
340 }
341 
343  if( filterMode != ALL_CLASS_LABELS && filterMode != INDEPENDENT_CLASS_LABELS ) return false;
344  this->filterMode = filterMode;
345  if( initialized ){
346  return reset();
347  }
348  return true;
349 }
350 
351 GRT_END_NAMESPACE
ClassLabelTimeoutFilter(unsigned long timeoutDuration=1000, UINT filterMode=ALL_CLASS_LABELS)
The Class Label Timeout Filter is a useful post-processing module which debounces a gesture (i...
virtual bool process(const VectorDouble &inputVector)
UINT filter(UINT predictedClassLabel)
std::string getPostProcessingType() const
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
unsigned int getSize() const
Definition: Vector.h:193
bool copyBaseVariables(const PostProcessing *postProcessingModule)
bool setTimeoutDuration(unsigned long timeoutDuration)
virtual bool loadModelFromFile(std::string filename)
bool init()
ClassLabelTimeoutFilter & operator=(const ClassLabelTimeoutFilter &rhs)
Definition: Vector.h:41
virtual bool saveModelToFile(std::string filename) const
bool setFilterMode(UINT filterMode)
virtual bool deepCopyFrom(const PostProcessing *postProcessing)