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