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.
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 GRT_BEGIN_NAMESPACE
24 
25 FeatureExtraction::StringFeatureExtractionMap* FeatureExtraction::stringFeatureExtractionMap = NULL;
26 UINT FeatureExtraction::numFeatureExtractionInstances = 0;
27 
28 FeatureExtraction* FeatureExtraction::createInstanceFromString( const std::string &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  inputType = DATA_TYPE_VECTOR;
48  outputType = DATA_TYPE_VECTOR;
49  numFeatureExtractionInstances++;
50  infoLog.setProceedingText("[FeatureExtraction]");
51  warningLog.setProceedingText("[WARNING FeatureExtraction]");
52  errorLog.setProceedingText("[ERROR FeatureExtraction]");
53 }
54 
56  if( --numFeatureExtractionInstances == 0 ){
57  delete stringFeatureExtractionMap;
58  stringFeatureExtractionMap = NULL;
59  }
60 }
61 
62 bool FeatureExtraction::copyBaseVariables(const FeatureExtraction *featureExtractionModule){
63 
64  if( featureExtractionModule == NULL ){
65  errorLog << "copyBaseVariables(const FeatureExtraction *featureExtractionModule) - featureExtractionModule pointer is NULL!" << std::endl;
66  return false;
67  }
68 
69  if( !this->copyMLBaseVariables( featureExtractionModule ) ){
70  return false;
71  }
72 
73  this->featureExtractionType = featureExtractionModule->featureExtractionType;
74  this->initialized = featureExtractionModule->initialized;
75  this->featureDataReady = featureExtractionModule->featureDataReady;
76  this->numInputDimensions = featureExtractionModule->numInputDimensions;
77  this->numOutputDimensions = featureExtractionModule->numOutputDimensions;
78  this->featureVector = featureExtractionModule->featureVector;
79  this->featureMatrix = featureExtractionModule->featureMatrix;
80  this->debugLog = featureExtractionModule->debugLog;
81  this->errorLog = featureExtractionModule->errorLog;
82  this->warningLog = featureExtractionModule->warningLog;
83 
84  return true;
85 }
86 
88 
89  if( numOutputDimensions == 0 ){
90  errorLog << "init() - Failed to init module, the number of output dimensions is zero!" << std::endl;
91  initialized = false;
92  return false;
93  }
94 
95  //Flag that the feature data has not been computed yet
96  featureDataReady = false;
97 
98  //Resize the feature vector
99  featureVector.resize(numOutputDimensions,0);
100 
101  //Flag the module has been initialized
102  initialized = true;
103 
104  return true;
105 }
106 
108 
109  //Clear the base class
110  MLBase::clear();
111 
112  initialized = false;
113  featureDataReady = false;
114  featureVector.clear();
115 
116  return true;
117 }
118 
120 
121  if( !file.is_open() ){
122  errorLog << "saveFeatureExtractionSettingsToFile(fstream &file) - The file is not open!" << std::endl;
123  return false;
124  }
125 
126  if( !MLBase::saveBaseSettingsToFile( file ) ) return false;
127 
128  file << "Initialized: " << initialized << std::endl;
129 
130  return true;
131 }
132 
134 
135  if( !file.is_open() ){
136  errorLog << "loadFeatureExtractionSettingsFromFile(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  return false;
143  }
144 
145  std::string word;
146 
147  //Load if the filter has been initialized
148  file >> word;
149  if( word != "Initialized:" ){
150  errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - Failed to read Initialized header!" << std::endl;
151  clear();
152  return false;
153  }
154  file >> initialized;
155 
156  //If the module has been initalized then call the init function to setup the feature data vector
157  if( initialized ){
158  return init();
159  }
160 
161  return true;
162 }
163 
165  return featureExtractionType;
166 }
167 
169  return numInputDimensions;
170 }
171 
173  return numOutputDimensions;
174 }
175 
177  return initialized;
178 }
179 
181  return featureDataReady;
182 }
183 
185  return featureVector;
186 }
187 
189  return featureMatrix;
190 }
191 
192 GRT_END_NAMESPACE
193 
This is the main base class that all GRT Feature Extraction algorithms should inherit from...
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:370
UINT getNumInputDimensions() const
std::map< std::string, FeatureExtraction *(*)() > StringFeatureExtractionMap
bool saveFeatureExtractionSettingsToFile(std::fstream &file) const
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
bool getFeatureDataReady() const
std::string getFeatureExtractionType() const
FeatureExtraction * createNewInstance() const
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:49
const VectorFloat & getFeatureVector() const
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:393
const MatrixFloat & getFeatureMatrix() const
virtual bool clear()
Definition: MLBase.cpp:126
bool loadFeatureExtractionSettingsFromFile(std::fstream &file)
UINT getNumOutputDimensions() const
bool copyBaseVariables(const FeatureExtraction *featureExtractionModule)
bool getInitialized() const