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.
FeatureExtraction.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 "FeatureExtraction.h"
22 
23 namespace GRT{
24 
25 FeatureExtraction::StringFeatureExtractionMap* FeatureExtraction::stringFeatureExtractionMap = NULL;
26 UINT FeatureExtraction::numFeatureExtractionInstances = 0;
27 
28 FeatureExtraction* FeatureExtraction::createInstanceFromString(string const &featureExtractionType){
29 
30  StringFeatureExtractionMap::iterator iter = getMap()->find( featureExtractionType );
31  if( iter == getMap()->end() ){
32  return NULL;
33  }
34  return iter->second();
35 }
36 
38  return createInstanceFromString(featureExtractionType);
39 }
40 
42  featureExtractionType = "NOT_SET";
43  initialized = false;
44  featureDataReady = false;
45  numInputDimensions = 0;
46  numOutputDimensions = 0;
47  numFeatureExtractionInstances++;
48  infoLog.setProceedingText("[FeatureExtraction]");
49  warningLog.setProceedingText("[WARNING FeatureExtraction]");
50  errorLog.setProceedingText("[ERROR FeatureExtraction]");
51 }
52 
54  if( --numFeatureExtractionInstances == 0 ){
55  delete stringFeatureExtractionMap;
56  stringFeatureExtractionMap = NULL;
57  }
58 }
59 
60 bool FeatureExtraction::copyBaseVariables(const FeatureExtraction *featureExtractionModule){
61 
62  if( featureExtractionModule == NULL ){
63  errorLog << "copyBaseVariables(const FeatureExtraction *featureExtractionModule) - featureExtractionModule pointer is NULL!" << endl;
64  return false;
65  }
66 
67  if( !this->copyGRTBaseVariables( featureExtractionModule ) ){
68  return false;
69  }
70 
71  this->featureExtractionType = featureExtractionModule->featureExtractionType;
72  this->initialized = featureExtractionModule->initialized;
73  this->featureDataReady = featureExtractionModule->featureDataReady;
74  this->numInputDimensions = featureExtractionModule->numInputDimensions;
75  this->numOutputDimensions = featureExtractionModule->numOutputDimensions;
76  this->featureVector = featureExtractionModule->featureVector;
77  this->debugLog = featureExtractionModule->debugLog;
78  this->errorLog = featureExtractionModule->errorLog;
79  this->warningLog = featureExtractionModule->warningLog;
80 
81  return true;
82 }
83 
85 
86  if( numOutputDimensions == 0 ){
87  errorLog << "init() - Failed to init module, the number of output dimensions is zero!" << endl;
88  initialized = false;
89  return false;
90  }
91 
92  //Flag that the feature data has not been computed yet
93  featureDataReady = false;
94 
95  //Resize the feature vector
96  featureVector.resize(numOutputDimensions,0);
97 
98  //Flag the module has been initialized
99  initialized = true;
100 
101  return true;
102 }
103 
105 
106  //Clear the base class
107  MLBase::clear();
108 
109  initialized = false;
110  featureDataReady = false;
111  featureVector.clear();
112 
113  return true;
114 }
115 
117 
118  if( !file.is_open() ){
119  errorLog << "saveFeatureExtractionSettingsToFile(fstream &file) - The file is not open!" << endl;
120  return false;
121  }
122 
123  if( !MLBase::saveBaseSettingsToFile( file ) ) return false;
124 
125  file << "Initialized: " << initialized << endl;
126 
127  return true;
128 }
129 
131 
132  if( !file.is_open() ){
133  errorLog << "loadFeatureExtractionSettingsFromFile(fstream &file) - The file is not open!" << endl;
134  return false;
135  }
136 
137  //Try and load the base settings from the file
138  if( !MLBase::loadBaseSettingsFromFile( file ) ){
139  return false;
140  }
141 
142  string word;
143 
144  //Load if the filter has been initialized
145  file >> word;
146  if( word != "Initialized:" ){
147  errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - Failed to read Initialized header!" << endl;
148  clear();
149  return false;
150  }
151  file >> initialized;
152 
153  //If the module has been initalized then call the init function to setup the feature data vector
154  if( initialized ){
155  return init();
156  }
157 
158  return true;
159 }
160 
162  return featureExtractionType;
163 }
164 
166  return numInputDimensions;
167 }
168 
170  return numOutputDimensions;
171 }
172 
174  return initialized;
175 }
176 
178  return featureDataReady;
179 }
180 
182  return featureVector;
183 }
184 
185 
186 } //End of namespace GRT
187 
This is the main base class that all GRT Feature Extraction algorithms should inherit from...
virtual bool clear()
Definition: MLBase.cpp:118
std::map< string, FeatureExtraction *(*)() > StringFeatureExtractionMap
bool loadBaseSettingsFromFile(fstream &file)
Definition: MLBase.cpp:357
Definition: AdaBoost.cpp:25
bool saveBaseSettingsToFile(fstream &file) const
Definition: MLBase.cpp:334
UINT getNumOutputDimensions() const
VectorDouble getFeatureVector() const
UINT getNumInputDimensions() const
string getFeatureExtractionType() const
bool copyGRTBaseVariables(const GRTBase *GRTBase)
Definition: GRTBase.cpp:32
bool loadFeatureExtractionSettingsFromFile(fstream &file)
bool saveFeatureExtractionSettingsToFile(fstream &file) const
FeatureExtraction * createNewInstance() const
bool copyBaseVariables(const FeatureExtraction *featureExtractionModule)