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.
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 #define GRT_DLL_EXPORTS
22 #include "PreProcessing.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 PreProcessing::StringPreProcessingMap* PreProcessing::stringPreProcessingMap = NULL;
27 UINT PreProcessing::numPreProcessingInstances = 0;
28 
29 PreProcessing* PreProcessing::createNewInstance() const { return create(); }
30 PreProcessing* PreProcessing::createInstanceFromString(const std::string &id) { return create(id); }
31 
32 PreProcessing* PreProcessing::create( const std::string &id ){
33 
34  StringPreProcessingMap::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 PreProcessing::PreProcessing( const std::string &id ) : MLBase( id, MLBase::PRE_PROCSSING )
46 {
47  initialized = false;
48  numInputDimensions = 0;
49  numOutputDimensions = 0;
50  numPreProcessingInstances++;
51 }
52 
54  if( --numPreProcessingInstances == 0 ){
55  delete stringPreProcessingMap;
56  stringPreProcessingMap = NULL;
57  }
58 }
59 
60 bool PreProcessing::copyBaseVariables(const PreProcessing *preProcessingModule){
61 
62  if( preProcessingModule == NULL ){
63  errorLog << "copyBaseVariables(const PreProcessing *preProcessingModule) - preProcessingModule pointer is NULL!" << std::endl;
64  return false;
65  }
66 
67  if( !this->copyMLBaseVariables( preProcessingModule ) ){
68  return false;
69  }
70 
71  this->initialized = preProcessingModule->initialized;
72  this->numInputDimensions = preProcessingModule->numInputDimensions;
73  this->numOutputDimensions = preProcessingModule->numOutputDimensions;
74  this->processedData = preProcessingModule->processedData;
75  this->debugLog = preProcessingModule->debugLog;
76  this->errorLog = preProcessingModule->errorLog;
77  this->warningLog = preProcessingModule->warningLog;
78 
79  return true;
80 }
81 
83 
84  //Reset the processed data vector
85  if( processedData.getSize() > 0 )
86  fill(processedData.begin(),processedData.end(),0);
87 
88  return true;
89 }
90 
92  initialized = false;
93  numInputDimensions = 0;
94  numOutputDimensions = 0;
95  processedData.clear();
96  return true;
97 }
98 
100 
101  if( numOutputDimensions == 0 ){
102  errorLog << "init() - Failed to init module, the number of output dimensions is zero!" << std::endl;
103  initialized = false;
104  return false;
105  }
106 
107  //Setup the output vector
108  processedData.resize( numOutputDimensions );
109 
110  //Flag the module has been initialized
111  initialized = true;
112 
113  return true;
114 }
115 
116 bool PreProcessing::savePreProcessingSettingsToFile(std::fstream &file) const{
117 
118  if( !file.is_open() ){
119  errorLog << "savePreProcessingSettingsToFile(fstream &file) - The file is not open!" << std::endl;
120  return false;
121  }
122 
123  if( !MLBase::saveBaseSettingsToFile( file ) ){
124  errorLog << "savePreProcessingSettingsToFile(fstream &file) - Failed to save base settings to file!" << std::endl;
125  return false;
126  }
127 
128  file << "Initialized: " << initialized << std::endl;
129 
130  return true;
131 }
132 
134 
135  if( !file.is_open() ){
136  errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
137  return false;
138  }
139 
140  //Try and load the base settings from the file
141  if( !MLBase::loadBaseSettingsFromFile( file ) ){
142  errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - Failed to load base settings from file!" << std::endl;
143  return false;
144  }
145 
146  std::string word;
147 
148  //Load if the filter has been initialized
149  file >> word;
150  if( word != "Initialized:" ){
151  errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - Failed to read Initialized header!" << std::endl;
152  clear();
153  return false;
154  }
155  file >> initialized;
156 
157  //If the module has been initalized then call the init function to setup the processed data vector
158  if( initialized ){
159  return init();
160  }
161 
162  return true;
163 }
164 
165 std::string PreProcessing::getPreProcessingType() const{
166  return getId();
167 }
168 
170  return initialized;
171 }
172 
174  return processedData;
175 }
176 
177 GRT_END_NAMESPACE
virtual bool clear() override
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:435
virtual ~PreProcessing(void)
std::string getId() const
Definition: GRTBase.cpp:85
bool savePreProcessingSettingsToFile(std::fstream &file) const
bool loadPreProcessingSettingsFromFile(std::fstream &file)
PreProcessing(const std::string &id="")
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
UINT getSize() const
Definition: Vector.h:201
virtual bool reset() override
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:62
PreProcessing * create() const
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:458
VectorFloat getProcessedData() const
bool copyBaseVariables(const PreProcessing *preProcessingModule)
This is the main base class that all GRT PreProcessing algorithms should inherit from.
This is the main base class that all GRT machine learning algorithms should inherit from...
Definition: MLBase.h:72
std::map< std::string, PreProcessing *(*)() > StringPreProcessingMap
bool getInitialized() const