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.
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 #include "PostProcessing.h"
22 
23 GRT_BEGIN_NAMESPACE
24 
25 PostProcessing::StringPostProcessingMap* PostProcessing::stringPostProcessingMap = NULL;
26 UINT PostProcessing::numPostProcessingInstances = 0;
27 
28 PostProcessing* PostProcessing::createInstanceFromString(std::string const &postProcessingType){
29 
30  StringPostProcessingMap::iterator iter = getMap()->find( postProcessingType );
31  if( iter == getMap()->end() ){
32  return NULL;
33  }
34  return iter->second();
35 }
36 
38  postProcessingType = "NOT_SET";
39  postProcessingInputMode = INPUT_MODE_NOT_SET;
40  postProcessingOutputMode = OUTPUT_MODE_NOT_SET;
41  initialized = false;
42  numInputDimensions = 0;
43  numOutputDimensions = 0;
44  numPostProcessingInstances++;
45 }
46 
48  if( --numPostProcessingInstances == 0 ){
49  delete stringPostProcessingMap;
50  stringPostProcessingMap = NULL;
51  }
52 }
53 
54 bool PostProcessing::copyBaseVariables(const PostProcessing *postProcessingModule){
55 
56  if( postProcessingModule == NULL ){
57  errorLog << "copyBaseVariables(const PostProcessing *postProcessingModule) - postProcessingModule pointer is NULL!" << std::endl;
58  return false;
59  }
60 
61  if( !this->copyMLBaseVariables( postProcessingModule ) ){
62  return false;
63  }
64 
65  this->postProcessingType = postProcessingModule->postProcessingType;
66  this->postProcessingInputMode = postProcessingModule->postProcessingInputMode;
67  this->postProcessingOutputMode = postProcessingModule->postProcessingOutputMode;
68  this->initialized = postProcessingModule->initialized;
69  this->numInputDimensions = postProcessingModule->numInputDimensions;
70  this->numOutputDimensions = postProcessingModule->numOutputDimensions;
71  this->processedData = postProcessingModule->processedData;
72  this->debugLog = postProcessingModule->debugLog;
73  this->errorLog = postProcessingModule->errorLog;
74  this->warningLog = postProcessingModule->warningLog;
75  return true;
76 }
77 
79 
80  if( numOutputDimensions == 0 ){
81  errorLog << "init() - Failed to init module, the number of output dimensions is zero!" << std::endl;
82  initialized = false;
83  return false;
84  }
85 
86  //Setup the output vector
87  processedData.resize( numOutputDimensions );
88 
89  //Flag the module has been initialized
90  initialized = true;
91 
92  return true;
93 }
94 
95 bool PostProcessing::saveModelToFile(std::string filename) const{
96 
97  std::fstream file;
98  file.open(filename.c_str(), std::ios::out);
99 
100  if( !saveModelToFile( file ) ){
101  return false;
102  }
103 
104  file.close();
105 
106  return true;
107 }
108 
109 bool PostProcessing::loadModelFromFile(std::string filename){
110 
111  std::fstream file;
112  file.open(filename.c_str(), std::ios::in);
113 
114  if( !loadModelFromFile( file ) ){
115  return false;
116  }
117 
118  //Close the file
119  file.close();
120 
121  return true;
122 }
123 
125 
126  if( !file.is_open() ){
127  errorLog << "savePostProcessingSettingsToFile(fstream &file) - The file is not open!" << std::endl;
128  return false;
129  }
130 
131  if( !MLBase::saveBaseSettingsToFile( file ) ) return false;
132 
133  file << "Initialized: " << initialized << std::endl;
134 
135  return true;
136 }
137 
139 
140  if( !file.is_open() ){
141  errorLog << "loadPostProcessingSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
142  return false;
143  }
144 
145  //Try and load the base settings from the file
146  if( !MLBase::loadBaseSettingsFromFile( file ) ){
147  return false;
148  }
149 
150  std::string word;
151 
152  //Load if the filter has been initialized
153  file >> word;
154  if( word != "Initialized:" ){
155  errorLog << "loadPostProcessingSettingsFromFile(fstream &file) - Failed to read Initialized header!" << std::endl;
156  clear();
157  return false;
158  }
159  file >> initialized;
160 
161  //If the module has been initalized then call the init function to setup the processed data vector
162  if( initialized ){
163  return init();
164  }
165 
166  return true;
167 }
168 
170  return createInstanceFromString(postProcessingType);
171 }
172 
174  return postProcessingType;
175 }
176 
178  return postProcessingInputMode;
179 }
180 
182  return postProcessingOutputMode;
183 }
184 
186  return numInputDimensions;
187 }
188 
190  return numOutputDimensions;
191 }
192 
194  return initialized;
195 }
196 
198  return postProcessingInputMode==INPUT_MODE_PREDICTED_CLASS_LABEL;
199 }
200 
202  return postProcessingInputMode==INPUT_MODE_CLASS_LIKELIHOODS;
203 }
204 
206  return postProcessingOutputMode==OUTPUT_MODE_PREDICTED_CLASS_LABEL;
207 }
208 
210  return postProcessingOutputMode==OUTPUT_MODE_CLASS_LIKELIHOODS;
211 }
212 
214  return processedData;
215 }
216 
217 GRT_END_NAMESPACE
218 
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:370
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:49
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:393
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:126
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