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.
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::createNewInstance() const { return create(); }
30 PostProcessing* PostProcessing::createInstanceFromString(const std::string &id) { return create(id); }
31 
32 PostProcessing* PostProcessing::create(const std::string &id){
33 
34  StringPostProcessingMap::iterator iter = getMap()->find( id );
35  if( iter == getMap()->end() ){
36  return NULL;
37  }
38  return iter->second();
39 }
40 
42  return create( getId() );
43 }
44 
45 PostProcessing::PostProcessing( const std::string &id ) : MLBase( id, MLBase::POST_PROCESSING )
46 {
47  postProcessingInputMode = INPUT_MODE_NOT_SET;
48  postProcessingOutputMode = OUTPUT_MODE_NOT_SET;
49  initialized = false;
50  numInputDimensions = 0;
51  numOutputDimensions = 0;
52  numPostProcessingInstances++;
53 }
54 
56  if( --numPostProcessingInstances == 0 ){
57  delete stringPostProcessingMap;
58  stringPostProcessingMap = NULL;
59  }
60 }
61 
62 bool PostProcessing::copyBaseVariables(const PostProcessing *postProcessingModule){
63 
64  if( postProcessingModule == NULL ){
65  errorLog << "copyBaseVariables(const PostProcessing *postProcessingModule) - postProcessingModule pointer is NULL!" << std::endl;
66  return false;
67  }
68 
69  if( !this->copyMLBaseVariables( postProcessingModule ) ){
70  return false;
71  }
72 
73  this->postProcessingType = postProcessingModule->postProcessingType;
74  this->postProcessingInputMode = postProcessingModule->postProcessingInputMode;
75  this->postProcessingOutputMode = postProcessingModule->postProcessingOutputMode;
76  this->initialized = postProcessingModule->initialized;
77  this->numInputDimensions = postProcessingModule->numInputDimensions;
78  this->numOutputDimensions = postProcessingModule->numOutputDimensions;
79  this->processedData = postProcessingModule->processedData;
80  this->debugLog = postProcessingModule->debugLog;
81  this->errorLog = postProcessingModule->errorLog;
82  this->warningLog = postProcessingModule->warningLog;
83  return true;
84 }
85 
87 
88  if( numOutputDimensions == 0 ){
89  errorLog << "init() - Failed to init module, the number of output dimensions is zero!" << std::endl;
90  initialized = false;
91  return false;
92  }
93 
94  //Setup the output vector
95  processedData.resize( numOutputDimensions );
96 
97  //Flag the module has been initialized
98  initialized = true;
99 
100  return true;
101 }
102 
104 
105  if( !file.is_open() ){
106  errorLog << "savePostProcessingSettingsToFile(fstream &file) - The file is not open!" << std::endl;
107  return false;
108  }
109 
110  if( !MLBase::saveBaseSettingsToFile( file ) ) return false;
111 
112  file << "Initialized: " << initialized << std::endl;
113 
114  return true;
115 }
116 
118 
119  if( !file.is_open() ){
120  errorLog << "loadPostProcessingSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
121  return false;
122  }
123 
124  //Try and load the base settings from the file
125  if( !MLBase::loadBaseSettingsFromFile( file ) ){
126  return false;
127  }
128 
129  std::string word;
130 
131  //Load if the filter has been initialized
132  file >> word;
133  if( word != "Initialized:" ){
134  errorLog << "loadPostProcessingSettingsFromFile(fstream &file) - Failed to read Initialized header!" << std::endl;
135  clear();
136  return false;
137  }
138  file >> initialized;
139 
140  //If the module has been initalized then call the init function to setup the processed data vector
141  if( initialized ){
142  return init();
143  }
144 
145  return true;
146 }
147 
148 std::string PostProcessing::getPostProcessingType() const{
149  return postProcessingType;
150 }
151 
153  return postProcessingInputMode;
154 }
155 
157  return postProcessingOutputMode;
158 }
159 
161  return initialized;
162 }
163 
165  return postProcessingInputMode==INPUT_MODE_PREDICTED_CLASS_LABEL;
166 }
167 
169  return postProcessingInputMode==INPUT_MODE_CLASS_LIKELIHOODS;
170 }
171 
173  return postProcessingOutputMode==OUTPUT_MODE_PREDICTED_CLASS_LABEL;
174 }
175 
177  return postProcessingOutputMode==OUTPUT_MODE_CLASS_LIKELIHOODS;
178 }
179 
181  return processedData;
182 }
183 
184 GRT_END_NAMESPACE
185 
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:435
std::string getId() const
Definition: GRTBase.cpp:85
virtual ~PostProcessing(void)
bool getIsPostProcessingInputModeClassLikelihoods() const
bool savePostProcessingSettingsToFile(std::fstream &file) const
PostProcessing * create() const
bool getInitialized() const
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
This is the main base class that all GRT PostProcessing algorithms should inherit from...
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:62
UINT getPostProcessingInputMode() const
bool getIsPostProcessingOutputModePredictedClassLabel() const
UINT getPostProcessingOutputMode() const
bool getIsPostProcessingInputModePredictedClassLabel() const
bool copyBaseVariables(const PostProcessing *postProcessingModule)
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:458
VectorFloat getProcessedData() const
virtual bool clear()
Definition: MLBase.cpp:149
PostProcessing(const std::string &id="")
bool getIsPostProcessingOutputModeClassLikelihoods() const
bool init()
This is the main base class that all GRT machine learning algorithms should inherit from...
Definition: MLBase.h:72
bool loadPostProcessingSettingsFromFile(std::fstream &file)
std::map< std::string, PostProcessing *(*)() > StringPostProcessingMap