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.
EnvelopeExtractor.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 "EnvelopeExtractor.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 //Register your module with the FeatureExtraction base class
27 RegisterFeatureExtractionModule< EnvelopeExtractor > EnvelopeExtractor::registerModule("EnvelopeExtractor");
28 
29 EnvelopeExtractor::EnvelopeExtractor( const UINT bufferSize,const UINT numDimensions ){
30 
31  classType = "EnvelopeExtractor";
32  featureExtractionType = classType;
33  this->bufferSize = 0;
34 
35  debugLog.setProceedingText("[DEBUG EnvelopeExtractor]");
36  errorLog.setProceedingText("[ERROR EnvelopeExtractor]");
37  warningLog.setProceedingText("[WARNING EnvelopeExtractor]");
38 
39  if( bufferSize > 0 && numDimensions > 0 ){
40  init( bufferSize, numDimensions );
41  }
42 }
43 
45 
46  classType = "EnvelopeExtractor";
47  featureExtractionType = classType;
48 
49  debugLog.setProceedingText("[DEBUG EnvelopeExtractor]");
50  errorLog.setProceedingText("[ERROR EnvelopeExtractor]");
51  warningLog.setProceedingText("[WARNING EnvelopeExtractor]");
52 
53  //Invoke the equals operator to copy the data from the rhs instance to this instance
54  *this = rhs;
55 }
56 
58  //Here you should add any specific code to cleanup your custom feature extraction module if needed
59 }
60 
62 
63  if(this!=&rhs){
64  //Here you should copy any class variables from the rhs instance to this instance
65  this->bufferSize = rhs.bufferSize;
66  this->buffer = rhs.buffer;
67 
68  //Copy the base variables
70  }
71  return *this;
72 }
73 
74 bool EnvelopeExtractor::deepCopyFrom(const FeatureExtraction *featureExtraction){
75 
76  if( featureExtraction == NULL ) return false;
77 
78  if( this->getFeatureExtractionType() == featureExtraction->getFeatureExtractionType() ){
79 
80  //Cast the feature extraction pointer to a pointer to your custom feature extraction module
81  //Then invoke the equals operator
82  *this = *(EnvelopeExtractor*)featureExtraction;
83 
84  return true;
85  }
86 
87  errorLog << "clone(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
88 
89  return false;
90 }
91 
93 
94  if( !initialized ) return false;
95 
96  //Add the value to the buffer
97  buffer.push_back( inputVector );
98 
99  UINT count = buffer.getNumValuesInBuffer();
100  for(UINT j=0; j<numInputDimensions; j++){
101  featureVector[j] = 0;
102  for(UINT i=0; i<count; i++){
103  featureVector[j] += SQR( buffer[i][j] );
104  }
105  featureVector[j] = sqrt( featureVector[j] / count );
106  }
107 
108  //Flag that the feature data is ready
109  featureDataReady = true;
110 
111  return true;
112 }
113 
115  return true;
116 }
117 
118 bool EnvelopeExtractor::saveModelToFile( std::string filename ) const{
119 
120  std::fstream file;
121  file.open(filename.c_str(), std::ios::out);
122 
123  if( !saveModelToFile( file ) ){
124  return false;
125  }
126 
127  file.close();
128 
129  return true;
130 }
131 
132 bool EnvelopeExtractor::loadModelFromFile( std::string filename ){
133 
134  std::fstream file;
135  file.open(filename.c_str(), std::ios::in);
136 
137  if( !loadModelFromFile( file ) ){
138  return false;
139  }
140 
141  //Close the file
142  file.close();
143 
144  return true;
145 }
146 
147 bool EnvelopeExtractor::saveModelToFile( std::fstream &file ) const{
148 
149  if( !file.is_open() ){
150  errorLog << "saveModelToFile(fstream &file) - The file is not open!" << std::endl;
151  return false;
152  }
153 
154  //First, you should add a header (with no spaces) e.g.
155  file << "ENVELOPE_EXTRACTOR_FILE_V1.0" << std::endl;
156 
157  //Second, you should save the base feature extraction settings to the file
159  errorLog << "saveModelToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
160  return false;
161  }
162 
163  file << "BufferSize: " << bufferSize << std::endl;
164 
165  return true;
166 }
167 
168 bool EnvelopeExtractor::loadModelFromFile( std::fstream &file ){
169 
170  clear();
171 
172  if( !file.is_open() ){
173  errorLog << "loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
174  return false;
175  }
176 
177  std::string word;
178  UINT numLayers = 0;
179  UINT numRows = 0;
180  UINT numCols = 0;
181 
182  //First, you should read and validate the header
183  file >> word;
184  if( word != "ENVELOPE_EXTRACTOR_FILE_V1.0" ){
185  errorLog << "loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
186  return false;
187  }
188 
189  //Second, you should load the base feature extraction settings to the file
191  errorLog << "loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
192  return false;
193  }
194 
195  //Load the buffer size header
196  file >> word;
197  if( word != "BufferSize:" ){
198  errorLog << "loadModelFromFile(fstream &file) - Failed to read BufferSize header!" << std::endl;
199  return false;
200  }
201  file >> bufferSize;
202 
203  return init( bufferSize, numInputDimensions );
204 }
205 
206 bool EnvelopeExtractor::init( const UINT bufferSize, const UINT numDimensions ){
207 
208  clear();
209 
210  if( bufferSize == 0 || numDimensions == 0 ) return false;
211 
212  this->bufferSize = bufferSize;
213  numInputDimensions = numDimensions;
214  numOutputDimensions = numDimensions;
215  buffer.resize( bufferSize, VectorFloat(numDimensions,0) );
216 
217  //Call the feature extraction base class init function to setup the feature extraction buffers
218  return FeatureExtraction::init();
219 }
220 
221 GRT_END_NAMESPACE
bool push_back(const T &value)
virtual bool deepCopyFrom(const FeatureExtraction *featureExtraction)
virtual bool loadModelFromFile(std::string filename)
bool saveFeatureExtractionSettingsToFile(std::fstream &file) const
EnvelopeExtractor & operator=(const EnvelopeExtractor &rhs)
std::string getFeatureExtractionType() const
unsigned int getNumValuesInBuffer() const
virtual bool computeFeatures(const VectorFloat &inputVector)
bool loadFeatureExtractionSettingsFromFile(std::fstream &file)
EnvelopeExtractor(const UINT bufferSize=100, const UINT numDimensions=1)
bool copyBaseVariables(const FeatureExtraction *featureExtractionModule)
bool resize(const unsigned int newBufferSize)
virtual bool saveModelToFile(std::string filename) const