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.
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 std::string FeatureExtraction::getFeatureExtractionType() const { return MLBase::getId(); } //Legacy
30 FeatureExtraction* FeatureExtraction::createInstanceFromString( const std::string &id ){ return create(id); }
31 FeatureExtraction* FeatureExtraction::createNewInstance() const{ return create(); }
32 
33 FeatureExtraction* FeatureExtraction::create( const std::string &id ){
34 
35  StringFeatureExtractionMap::iterator iter = getMap()->find( id );
36  if( iter == getMap()->end() ){
37  return NULL;
38  }
39  return iter->second();
40 }
41 
43  return create( MLBase::getId() );
44 }
45 
46 FeatureExtraction::FeatureExtraction( const std::string id ) : MLBase( id, MLBase::FEATURE_EXTRACTION )
47 {
48  initialized = false;
49  featureDataReady = false;
50  numInputDimensions = 0;
51  numOutputDimensions = 0;
52  inputType = DATA_TYPE_VECTOR;
53  outputType = DATA_TYPE_VECTOR;
54  numFeatureExtractionInstances++;
55 }
56 
58  if( --numFeatureExtractionInstances == 0 ){
59  delete stringFeatureExtractionMap;
60  stringFeatureExtractionMap = NULL;
61  }
62 }
63 
64 bool FeatureExtraction::copyBaseVariables(const FeatureExtraction *featureExtractionModule){
65 
66  if( featureExtractionModule == NULL ){
67  errorLog << "copyBaseVariables(const FeatureExtraction *featureExtractionModule) - featureExtractionModule pointer is NULL!" << std::endl;
68  return false;
69  }
70 
71  if( !this->copyMLBaseVariables( featureExtractionModule ) ){
72  return false;
73  }
74 
75  this->featureExtractionType = featureExtractionModule->featureExtractionType;
76  this->initialized = featureExtractionModule->initialized;
77  this->featureDataReady = featureExtractionModule->featureDataReady;
78  this->numInputDimensions = featureExtractionModule->numInputDimensions;
79  this->numOutputDimensions = featureExtractionModule->numOutputDimensions;
80  this->featureVector = featureExtractionModule->featureVector;
81  this->featureMatrix = featureExtractionModule->featureMatrix;
82 
83  return true;
84 }
85 
87 
88  if( numOutputDimensions == 0 ){
89  errorLog << "init() - Failed to init module, the number of output dimensions is zero!" << std::endl;
90  initialized = false;
91  return false;
92  }
93 
94  //Flag that the feature data has not been computed yet
95  featureDataReady = false;
96 
97  //Resize the feature vector
98  featureVector.resize(numOutputDimensions,0);
99 
100  //Flag the module has been initialized
101  initialized = true;
102 
103  return true;
104 }
105 
107 
108  //Clear the base class
109  MLBase::clear();
110 
111  initialized = false;
112  featureDataReady = false;
113  featureVector.clear();
114 
115  return true;
116 }
117 
119 
120  if( !file.is_open() ){
121  errorLog << "saveFeatureExtractionSettingsToFile(fstream &file) - The file is not open!" << std::endl;
122  return false;
123  }
124 
125  if( !MLBase::saveBaseSettingsToFile( file ) ) return false;
126 
127  file << "Initialized: " << initialized << std::endl;
128 
129  return true;
130 }
131 
133 
134  if( !file.is_open() ){
135  errorLog << "loadFeatureExtractionSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
136  return false;
137  }
138 
139  //Try and load the base settings from the file
140  if( !MLBase::loadBaseSettingsFromFile( file ) ){
141  return false;
142  }
143 
144  std::string word;
145 
146  //Load if the filter has been initialized
147  file >> word;
148  if( word != "Initialized:" ){
149  errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - Failed to read Initialized header!" << std::endl;
150  clear();
151  return false;
152  }
153  file >> initialized;
154 
155  //If the module has been initalized then call the init function to setup the feature data vector
156  if( initialized ){
157  return init();
158  }
159 
160  return true;
161 }
162 
164  return initialized;
165 }
166 
168  return featureDataReady;
169 }
170 
172  return featureVector;
173 }
174 
176  return featureMatrix;
177 }
178 
179 GRT_END_NAMESPACE
180 
This is the main base class that all GRT Feature Extraction algorithms should inherit from...
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:435
std::string getId() const
Definition: GRTBase.cpp:85
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
FeatureExtraction * create() const
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:62
const VectorFloat & getFeatureVector() const
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:458
const MatrixFloat & getFeatureMatrix() const
virtual bool clear()
Definition: MLBase.cpp:149
bool loadFeatureExtractionSettingsFromFile(std::fstream &file)
bool copyBaseVariables(const FeatureExtraction *featureExtractionModule)
virtual bool clear() override
This is the main base class that all GRT machine learning algorithms should inherit from...
Definition: MLBase.h:72
FeatureExtraction(const std::string id="")
bool getInitialized() const