GestureRecognitionToolkit  Version: 0.2.0
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
PostProcessing.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 "PostProcessing.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 PostProcessing::StringPostProcessingMap* PostProcessing::stringPostProcessingMap = NULL;
27 UINT PostProcessing::numPostProcessingInstances = 0;
28 
29 PostProcessing* PostProcessing::createInstanceFromString(std::string const &postProcessingType){
30 
31  StringPostProcessingMap::iterator iter = getMap()->find( postProcessingType );
32  if( iter == getMap()->end() ){
33  return NULL;
34  }
35  return iter->second();
36 }
37 
39  postProcessingType = "NOT_SET";
40  postProcessingInputMode = INPUT_MODE_NOT_SET;
41  postProcessingOutputMode = OUTPUT_MODE_NOT_SET;
42  initialized = false;
43  numInputDimensions = 0;
44  numOutputDimensions = 0;
45  numPostProcessingInstances++;
46 }
47 
49  if( --numPostProcessingInstances == 0 ){
50  delete stringPostProcessingMap;
51  stringPostProcessingMap = NULL;
52  }
53 }
54 
55 bool PostProcessing::copyBaseVariables(const PostProcessing *postProcessingModule){
56 
57  if( postProcessingModule == NULL ){
58  errorLog << "copyBaseVariables(const PostProcessing *postProcessingModule) - postProcessingModule pointer is NULL!" << std::endl;
59  return false;
60  }
61 
62  if( !this->copyMLBaseVariables( postProcessingModule ) ){
63  return false;
64  }
65 
66  this->postProcessingType = postProcessingModule->postProcessingType;
67  this->postProcessingInputMode = postProcessingModule->postProcessingInputMode;
68  this->postProcessingOutputMode = postProcessingModule->postProcessingOutputMode;
69  this->initialized = postProcessingModule->initialized;
70  this->numInputDimensions = postProcessingModule->numInputDimensions;
71  this->numOutputDimensions = postProcessingModule->numOutputDimensions;
72  this->processedData = postProcessingModule->processedData;
73  this->debugLog = postProcessingModule->debugLog;
74  this->errorLog = postProcessingModule->errorLog;
75  this->warningLog = postProcessingModule->warningLog;
76  return true;
77 }
78 
80 
81  if( numOutputDimensions == 0 ){
82  errorLog << "init() - Failed to init module, the number of output dimensions is zero!" << std::endl;
83  initialized = false;
84  return false;
85  }
86 
87  //Setup the output vector
88  processedData.resize( numOutputDimensions );
89 
90  //Flag the module has been initialized
91  initialized = true;
92 
93  return true;
94 }
95 
96 bool PostProcessing::saveModelToFile(std::string filename) const{
97 
98  std::fstream file;
99  file.open(filename.c_str(), std::ios::out);
100 
101  if( !saveModelToFile( file ) ){
102  return false;
103  }
104 
105  file.close();
106 
107  return true;
108 }
109 
110 bool PostProcessing::loadModelFromFile(std::string filename){
111 
112  std::fstream file;
113  file.open(filename.c_str(), std::ios::in);
114 
115  if( !loadModelFromFile( file ) ){
116  return false;
117  }
118 
119  //Close the file
120  file.close();
121 
122  return true;
123 }
124 
126 
127  if( !file.is_open() ){
128  errorLog << "savePostProcessingSettingsToFile(fstream &file) - The file is not open!" << std::endl;
129  return false;
130  }
131 
132  if( !MLBase::saveBaseSettingsToFile( file ) ) return false;
133 
134  file << "Initialized: " << initialized << std::endl;
135 
136  return true;
137 }
138 
140 
141  if( !file.is_open() ){
142  errorLog << "loadPostProcessingSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
143  return false;
144  }
145 
146  //Try and load the base settings from the file
147  if( !MLBase::loadBaseSettingsFromFile( file ) ){
148  return false;
149  }
150 
151  std::string word;
152 
153  //Load if the filter has been initialized
154  file >> word;
155  if( word != "Initialized:" ){
156  errorLog << "loadPostProcessingSettingsFromFile(fstream &file) - Failed to read Initialized header!" << std::endl;
157  clear();
158  return false;
159  }
160  file >> initialized;
161 
162  //If the module has been initalized then call the init function to setup the processed data vector
163  if( initialized ){
164  return init();
165  }
166 
167  return true;
168 }
169 
171  return createInstanceFromString(postProcessingType);
172 }
173 
175  return postProcessingType;
176 }
177 
179  return postProcessingInputMode;
180 }
181 
183  return postProcessingOutputMode;
184 }
185 
187  return numInputDimensions;
188 }
189 
191  return numOutputDimensions;
192 }
193 
195  return initialized;
196 }
197 
199  return postProcessingInputMode==INPUT_MODE_PREDICTED_CLASS_LABEL;
200 }
201 
203  return postProcessingInputMode==INPUT_MODE_CLASS_LIKELIHOODS;
204 }
205 
207  return postProcessingOutputMode==OUTPUT_MODE_PREDICTED_CLASS_LABEL;
208 }
209 
211  return postProcessingOutputMode==OUTPUT_MODE_CLASS_LIKELIHOODS;
212 }
213 
215  return processedData;
216 }
217 
218 GRT_END_NAMESPACE
219 
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:375
virtual ~PostProcessing(void)
std::string getPostProcessingType() const
bool getIsPostProcessingInputModeClassLikelihoods() const
bool savePostProcessingSettingsToFile(std::fstream &file) const
bool getInitialized() const
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
PostProcessing(void)
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:50
UINT getPostProcessingInputMode() const
bool getIsPostProcessingOutputModePredictedClassLabel() const
virtual bool loadModelFromFile(std::string filename)
UINT getPostProcessingOutputMode() const
bool getIsPostProcessingInputModePredictedClassLabel() const
bool copyBaseVariables(const PostProcessing *postProcessingModule)
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:398
VectorFloat getProcessedData() const
UINT getNumOutputDimensions() const
This is the main base class that all GRT PostProcessing algorithms should inherit from...
virtual bool clear()
Definition: MLBase.cpp:127
bool getIsPostProcessingOutputModeClassLikelihoods() const
bool init()
static PostProcessing * createInstanceFromString(std::string const &postProcessingType)
UINT getNumInputDimensions() const
bool loadPostProcessingSettingsFromFile(std::fstream &file)
std::map< std::string, PostProcessing *(*)() > StringPostProcessingMap
virtual bool saveModelToFile(std::string filename) const
PostProcessing * createNewInstance() const