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.
PreProcessing.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 "PreProcessing.h"
22 
23 GRT_BEGIN_NAMESPACE
24 
25 PreProcessing::StringPreProcessingMap* PreProcessing::stringPreProcessingMap = NULL;
26 UINT PreProcessing::numPreProcessingInstances = 0;
27 
28 PreProcessing* PreProcessing::createInstanceFromString( const std::string &preProcessingType ){
29 
30  StringPreProcessingMap::iterator iter = getMap()->find( preProcessingType );
31  if( iter == getMap()->end() ){
32  return NULL;
33  }
34  return iter->second();
35 }
36 
38  preProcessingType = "NOT_SET";
39  initialized = false;
40  numInputDimensions = 0;
41  numOutputDimensions = 0;
42  numPreProcessingInstances++;
43 }
44 
46  if( --numPreProcessingInstances == 0 ){
47  delete stringPreProcessingMap;
48  stringPreProcessingMap = NULL;
49  }
50 }
51 
52 bool PreProcessing::copyBaseVariables(const PreProcessing *preProcessingModule){
53 
54  if( preProcessingModule == NULL ){
55  errorLog << "copyBaseVariables(const PreProcessing *preProcessingModule) - preProcessingModule pointer is NULL!" << std::endl;
56  return false;
57  }
58 
59  if( !this->copyMLBaseVariables( preProcessingModule ) ){
60  return false;
61  }
62 
63  this->preProcessingType = preProcessingModule->preProcessingType;
64  this->initialized = preProcessingModule->initialized;
65  this->numInputDimensions = preProcessingModule->numInputDimensions;
66  this->numOutputDimensions = preProcessingModule->numOutputDimensions;
67  this->processedData = preProcessingModule->processedData;
68  this->debugLog = preProcessingModule->debugLog;
69  this->errorLog = preProcessingModule->errorLog;
70  this->warningLog = preProcessingModule->warningLog;
71 
72  return true;
73 }
74 
76 
77  //Reset the processed data vector
78  if( processedData.size() > 0 )
79  fill(processedData.begin(),processedData.end(),0);
80 
81  return true;
82 }
83 
85  initialized = false;
86  numInputDimensions = 0;
87  numOutputDimensions = 0;
88  processedData.clear();
89  return true;
90 }
91 
93 
94  if( numOutputDimensions == 0 ){
95  errorLog << "init() - Failed to init module, the number of output dimensions is zero!" << std::endl;
96  initialized = false;
97  return false;
98  }
99 
100  //Setup the output vector
101  processedData.resize( numOutputDimensions );
102 
103  //Flag the module has been initialized
104  initialized = true;
105 
106  return true;
107 }
108 
109 bool PreProcessing::saveModelToFile( std::string filename ) const{
110 
111  std::fstream file;
112  file.open(filename.c_str(), std::ios::out);
113 
114  if( !saveModelToFile( file ) ){
115  return false;
116  }
117 
118  file.close();
119 
120  return true;
121 }
122 
123 bool PreProcessing::loadModelFromFile( std::string filename ){
124 
125  std::fstream file;
126  file.open(filename.c_str(), std::ios::in);
127 
128  if( !loadModelFromFile( file ) ){
129  return false;
130  }
131 
132  //Close the file
133  file.close();
134 
135  return true;
136 }
137 
138 bool PreProcessing::savePreProcessingSettingsToFile(std::fstream &file) const{
139 
140  if( !file.is_open() ){
141  errorLog << "savePreProcessingSettingsToFile(fstream &file) - The file is not open!" << std::endl;
142  return false;
143  }
144 
145  if( !MLBase::saveBaseSettingsToFile( file ) ){
146  errorLog << "savePreProcessingSettingsToFile(fstream &file) - Failed to save base settings to file!" << std::endl;
147  return false;
148  }
149 
150  file << "Initialized: " << initialized << std::endl;
151 
152  return true;
153 }
154 
156 
157  if( !file.is_open() ){
158  errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
159  return false;
160  }
161 
162  //Try and load the base settings from the file
163  if( !MLBase::loadBaseSettingsFromFile( file ) ){
164  errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - Failed to load base settings from file!" << std::endl;
165  return false;
166  }
167 
168  std::string word;
169 
170  //Load if the filter has been initialized
171  file >> word;
172  if( word != "Initialized:" ){
173  errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - Failed to read Initialized header!" << std::endl;
174  clear();
175  return false;
176  }
177  file >> initialized;
178 
179  //If the module has been initalized then call the init function to setup the processed data vector
180  if( initialized ){
181  return init();
182  }
183 
184  return true;
185 }
186 
188  return createInstanceFromString(preProcessingType);
189 }
190 
192  return preProcessingType;
193 }
194 
196  return numInputDimensions;
197 }
198 
200  return numOutputDimensions;
201 }
202 
204  return initialized;
205 }
206 
208  return processedData;
209 }
210 
211 GRT_END_NAMESPACE
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:370
virtual ~PreProcessing(void)
UINT getNumInputDimensions() const
UINT getNumOutputDimensions() const
bool savePreProcessingSettingsToFile(std::fstream &file) const
bool loadPreProcessingSettingsFromFile(std::fstream &file)
PreProcessing * createNewInstance() const
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
static PreProcessing * createInstanceFromString(std::string const &preProcessingType)
virtual bool reset()
virtual bool loadModelFromFile(std::string filename)
std::string getPreProcessingType() const
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:49
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:393
virtual bool saveModelToFile(std::string filename) const
VectorFloat getProcessedData() const
bool copyBaseVariables(const PreProcessing *preProcessingModule)
virtual bool clear()
This is the main base class that all GRT PreProcessing algorithms should inherit from.
std::map< std::string, PreProcessing *(*)() > StringPreProcessingMap
bool getInitialized() const