GestureRecognitionToolkit  Version: 0.2.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 #define GRT_DLL_EXPORTS
22 #include "FeatureExtraction.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 FeatureExtraction::StringFeatureExtractionMap* FeatureExtraction::stringFeatureExtractionMap = NULL;
27 UINT FeatureExtraction::numFeatureExtractionInstances = 0;
28 
29 FeatureExtraction* FeatureExtraction::createInstanceFromString( const std::string &featureExtractionType){
30 
31  StringFeatureExtractionMap::iterator iter = getMap()->find( featureExtractionType );
32  if( iter == getMap()->end() ){
33  return NULL;
34  }
35  return iter->second();
36 }
37 
39  return createInstanceFromString(featureExtractionType);
40 }
41 
43  featureExtractionType = "NOT_SET";
44  initialized = false;
45  featureDataReady = false;
46  numInputDimensions = 0;
47  numOutputDimensions = 0;
48  inputType = DATA_TYPE_VECTOR;
49  outputType = DATA_TYPE_VECTOR;
50  numFeatureExtractionInstances++;
51  infoLog.setProceedingText("[FeatureExtraction]");
52  warningLog.setProceedingText("[WARNING FeatureExtraction]");
53  errorLog.setProceedingText("[ERROR FeatureExtraction]");
54 }
55 
57  if( --numFeatureExtractionInstances == 0 ){
58  delete stringFeatureExtractionMap;
59  stringFeatureExtractionMap = NULL;
60  }
61 }
62 
63 bool FeatureExtraction::copyBaseVariables(const FeatureExtraction *featureExtractionModule){
64 
65  if( featureExtractionModule == NULL ){
66  errorLog << "copyBaseVariables(const FeatureExtraction *featureExtractionModule) - featureExtractionModule pointer is NULL!" << std::endl;
67  return false;
68  }
69 
70  if( !this->copyMLBaseVariables( featureExtractionModule ) ){
71  return false;
72  }
73 
74  this->featureExtractionType = featureExtractionModule->featureExtractionType;
75  this->initialized = featureExtractionModule->initialized;
76  this->featureDataReady = featureExtractionModule->featureDataReady;
77  this->numInputDimensions = featureExtractionModule->numInputDimensions;
78  this->numOutputDimensions = featureExtractionModule->numOutputDimensions;
79  this->featureVector = featureExtractionModule->featureVector;
80  this->featureMatrix = featureExtractionModule->featureMatrix;
81  this->debugLog = featureExtractionModule->debugLog;
82  this->errorLog = featureExtractionModule->errorLog;
83  this->warningLog = featureExtractionModule->warningLog;
84 
85  return true;
86 }
87 
89 
90  if( numOutputDimensions == 0 ){
91  errorLog << "init() - Failed to init module, the number of output dimensions is zero!" << std::endl;
92  initialized = false;
93  return false;
94  }
95 
96  //Flag that the feature data has not been computed yet
97  featureDataReady = false;
98 
99  //Resize the feature vector
100  featureVector.resize(numOutputDimensions,0);
101 
102  //Flag the module has been initialized
103  initialized = true;
104 
105  return true;
106 }
107 
109 
110  //Clear the base class
111  MLBase::clear();
112 
113  initialized = false;
114  featureDataReady = false;
115  featureVector.clear();
116 
117  return true;
118 }
119 
121 
122  if( !file.is_open() ){
123  errorLog << "saveFeatureExtractionSettingsToFile(fstream &file) - The file is not open!" << std::endl;
124  return false;
125  }
126 
127  if( !MLBase::saveBaseSettingsToFile( file ) ) return false;
128 
129  file << "Initialized: " << initialized << std::endl;
130 
131  return true;
132 }
133 
135 
136  if( !file.is_open() ){
137  errorLog << "loadFeatureExtractionSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
138  return false;
139  }
140 
141  //Try and load the base settings from the file
142  if( !MLBase::loadBaseSettingsFromFile( file ) ){
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 feature data vector
158  if( initialized ){
159  return init();
160  }
161 
162  return true;
163 }
164 
166  return featureExtractionType;
167 }
168 
170  return numInputDimensions;
171 }
172 
174  return numOutputDimensions;
175 }
176 
178  return initialized;
179 }
180 
182  return featureDataReady;
183 }
184 
186  return featureVector;
187 }
188 
190  return featureMatrix;
191 }
192 
193 GRT_END_NAMESPACE
194 
This is the main base class that all GRT Feature Extraction algorithms should inherit from...
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:375
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:50
const VectorFloat & getFeatureVector() const
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:398
const MatrixFloat & getFeatureMatrix() const
virtual bool clear()
Definition: MLBase.cpp:127
bool loadFeatureExtractionSettingsFromFile(std::fstream &file)
UINT getNumOutputDimensions() const
bool copyBaseVariables(const FeatureExtraction *featureExtractionModule)
bool getInitialized() const