GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
ClassLabelChangeFilter.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 
21 #define GRT_DLL_EXPORTS
22 #include "ClassLabelChangeFilter.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 //Define the string that will be used to identify the object
27 const std::string ClassLabelChangeFilter::id = "ClassLabelChangeFilter";
28 std::string ClassLabelChangeFilter::getId() { return ClassLabelChangeFilter::id; }
29 
30 //Register the ClassLabelChangeFilter module with the PostProcessing base class
32 
34 {
35  postProcessingInputMode = INPUT_MODE_PREDICTED_CLASS_LABEL;
36  postProcessingOutputMode = OUTPUT_MODE_PREDICTED_CLASS_LABEL;
37  init();
38 }
39 
41 {
42  postProcessingInputMode = INPUT_MODE_PREDICTED_CLASS_LABEL;
43  postProcessingOutputMode = OUTPUT_MODE_PREDICTED_CLASS_LABEL;
44 
45  //Copy the ClassLabelChangeFilter values
46  this->filteredClassLabel = rhs.filteredClassLabel;
47  this->labelChanged = rhs.labelChanged;
48 
49  //Clone the post processing base variables
51 }
52 
54 
55 }
56 
58 
59  if( this != &rhs ){
60  //Copy the ClassLabelChangeFilter values
61  this->filteredClassLabel = rhs.filteredClassLabel;
62  this->labelChanged = rhs.labelChanged;
63 
64  //Clone the post processing base variables
66  }
67  return *this;
68 }
69 
71 
72  if( postProcessing == NULL ) return false;
73 
74  if( this->getId() == postProcessing->getId() ){
75 
76  const ClassLabelChangeFilter *ptr = dynamic_cast<const ClassLabelChangeFilter*>(postProcessing);
77 
78  //Clone the ClassLabelChangeFilter values
79  this->filteredClassLabel = ptr->filteredClassLabel;
80  this->labelChanged = ptr->labelChanged;
81 
82  //Clone the post processing base variables
83  copyBaseVariables( postProcessing );
84  return true;
85  }
86  return false;
87 }
88 
90 
91  if( !initialized ){
92  errorLog << "process(const VectorDouble &inputVector) - Not initialized!" << std::endl;
93  return false;
94  }
95 
96  if( inputVector.getSize() != numInputDimensions ){
97  errorLog << "process(const VectorDouble &inputVector) - The size of the inputVector (" << inputVector.getSize() << ") does not match that of the filter (" << numInputDimensions << ")!" << std::endl;
98  return false;
99  }
100 
101  //Use only the first value (as that is the predicted class label)
102  processedData[0] = filter( (UINT)inputVector[0] );
103  return true;
104 }
105 
107  filteredClassLabel = 0;
108  labelChanged = false;
109  processedData.clear();
110  processedData.resize(1,0);
111  return true;
112 }
113 
115 
116  initialized = false;
117 
118  reset();
119  numInputDimensions = 1;
120  numOutputDimensions = 1;
121  initialized = true;
122  return true;
123 }
124 
125 UINT ClassLabelChangeFilter::filter(const UINT predictedClassLabel){
126 
127  labelChanged = false;
128 
129  if( predictedClassLabel != filteredClassLabel ){
130  filteredClassLabel = predictedClassLabel;
131  labelChanged = true;
132  return filteredClassLabel;
133  }
134 
135  return GRT_DEFAULT_NULL_CLASS_LABEL;
136 }
137 
138 bool ClassLabelChangeFilter::save( std::fstream &file ) const {
139 
140  if( !file.is_open() ){
141  errorLog << "save(fstream &file) - The file is not open!" << std::endl;
142  return false;
143  }
144 
145  file << "GRT_CLASS_LABEL_CHANGE_FILTER_FILE_V1.0" << std::endl;
146  file << "NumInputDimensions: " << numInputDimensions << std::endl;
147  file << "NumOutputDimensions: " << numOutputDimensions << std::endl;
148 
149  return true;
150 }
151 
152 bool ClassLabelChangeFilter::load( std::fstream &file ){
153 
154  if( !file.is_open() ){
155  errorLog << "load(fstream &file) - The file is not open!" << std::endl;
156  return false;
157  }
158 
159  std::string word;
160 
161  //Load the header
162  file >> word;
163 
164  if( word != "GRT_CLASS_LABEL_CHANGE_FILTER_FILE_V1.0" ){
165  errorLog << "load(fstream &file) - Invalid file format!" << std::endl;
166  return false;
167  }
168 
169  file >> word;
170  if( word != "NumInputDimensions:" ){
171  errorLog << "load(fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
172  return false;
173  }
174  file >> numInputDimensions;
175 
176  //Load the number of output dimensions
177  file >> word;
178  if( word != "NumOutputDimensions:" ){
179  errorLog << "load(fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
180  return false;
181  }
182  file >> numOutputDimensions;
183 
184  //Init the classLabelTimeoutFilter module to ensure everything is initialized correctly
185  return init();
186 }
187 
189  return labelChanged;
190 }
191 
192 GRT_END_NAMESPACE
virtual bool deepCopyFrom(const PostProcessing *postProcessing) override
std::string getId() const
Definition: GRTBase.cpp:85
ClassLabelChangeFilter & operator=(const ClassLabelChangeFilter &rhs)
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
UINT getSize() const
Definition: Vector.h:201
This is the main base class that all GRT PostProcessing algorithms should inherit from...
virtual bool process(const VectorDouble &inputVector) override
UINT filter(const UINT predictedClassLabel)
virtual bool load(std::fstream &file) override
bool copyBaseVariables(const PostProcessing *postProcessingModule)
virtual bool reset() override
virtual bool save(std::fstream &file) const override
The Class Label Change Filter signals when the predicted output of a classifier changes. For instance, if the output stream of a classifier was {1,1,1,1,2,2,2,2,3,3}, then the output of the filter would be {1,0,0,0,2,0,0,0,3,0}. This module is useful if you want to debounce a gesture and only care about when the gesture label changes.