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.
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 //Define the string that will be used to identify the object
27 std::string EnvelopeExtractor::id = "EnvelopeExtractor";
28 std::string EnvelopeExtractor::getId() { return EnvelopeExtractor::id; }
29 
30 //Register your module with the FeatureExtraction base class
32 
33 EnvelopeExtractor::EnvelopeExtractor( const UINT bufferSize,const UINT numDimensions ) : FeatureExtraction( EnvelopeExtractor::getId() )
34 {
35  this->bufferSize = 0;
36 
37  if( bufferSize > 0 && numDimensions > 0 ){
38  init( bufferSize, numDimensions );
39  }
40 }
41 
43 {
44  //Invoke the equals operator to copy the data from the rhs instance to this instance
45  *this = rhs;
46 }
47 
49 }
50 
52 
53  if(this!=&rhs){
54  //Here you should copy any class variables from the rhs instance to this instance
55  this->bufferSize = rhs.bufferSize;
56  this->buffer = rhs.buffer;
57 
58  //Copy the base variables
60  }
61  return *this;
62 }
63 
64 bool EnvelopeExtractor::deepCopyFrom(const FeatureExtraction *featureExtraction){
65 
66  if( featureExtraction == NULL ) return false;
67 
68  if( this->getId() == featureExtraction->getId() ){
69 
70  //Cast the feature extraction pointer to a pointer to your custom feature extraction module
71  //Then invoke the equals operator
72  *this = *dynamic_cast<const EnvelopeExtractor*>(featureExtraction);
73 
74  return true;
75  }
76 
77  errorLog << "deepCopyFrom(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
78 
79  return false;
80 }
81 
83 
84  if( !initialized ) return false;
85 
86  //Add the value to the buffer
87  buffer.push_back( inputVector );
88 
89  UINT count = buffer.getNumValuesInBuffer();
90  for(UINT j=0; j<numInputDimensions; j++){
91  featureVector[j] = 0;
92  for(UINT i=0; i<count; i++){
93  featureVector[j] += SQR( buffer[i][j] );
94  }
95  featureVector[j] = sqrt( featureVector[j] / count );
96  }
97 
98  //Flag that the feature data is ready
99  featureDataReady = true;
100 
101  return true;
102 }
103 
105  return true;
106 }
107 
108 bool EnvelopeExtractor::save( std::fstream &file ) const{
109 
110  if( !file.is_open() ){
111  errorLog << "save(fstream &file) - The file is not open!" << std::endl;
112  return false;
113  }
114 
115  //First, you should add a header (with no spaces) e.g.
116  file << "ENVELOPE_EXTRACTOR_FILE_V1.0" << std::endl;
117 
118  //Second, you should save the base feature extraction settings to the file
120  errorLog << "save(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
121  return false;
122  }
123 
124  file << "BufferSize: " << bufferSize << std::endl;
125 
126  return true;
127 }
128 
129 bool EnvelopeExtractor::load( std::fstream &file ){
130 
131  clear();
132 
133  if( !file.is_open() ){
134  errorLog << "load(fstream &file) - The file is not open!" << std::endl;
135  return false;
136  }
137 
138  std::string word;
139 
140  //First, you should read and validate the header
141  file >> word;
142  if( word != "ENVELOPE_EXTRACTOR_FILE_V1.0" ){
143  errorLog << "load(fstream &file) - Invalid file format!" << std::endl;
144  return false;
145  }
146 
147  //Second, you should load the base feature extraction settings to the file
149  errorLog << "loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
150  return false;
151  }
152 
153  //Load the buffer size header
154  file >> word;
155  if( word != "BufferSize:" ){
156  errorLog << "load(fstream &file) - Failed to read BufferSize header!" << std::endl;
157  return false;
158  }
159  file >> bufferSize;
160 
161  return init( bufferSize, numInputDimensions );
162 }
163 
164 bool EnvelopeExtractor::init( const UINT bufferSize, const UINT numDimensions ){
165 
166  clear();
167 
168  if( bufferSize == 0 || numDimensions == 0 ) return false;
169 
170  this->bufferSize = bufferSize;
171  numInputDimensions = numDimensions;
172  numOutputDimensions = numDimensions;
173  buffer.resize( bufferSize, VectorFloat(numDimensions,0) );
174 
175  //Call the feature extraction base class init function to setup the feature extraction buffers
176  return FeatureExtraction::init();
177 }
178 
179 GRT_END_NAMESPACE
bool push_back(const T &value)
std::string getId() const
Definition: GRTBase.cpp:85
virtual bool deepCopyFrom(const FeatureExtraction *featureExtraction)
bool saveFeatureExtractionSettingsToFile(std::fstream &file) const
virtual bool save(std::fstream &file) const
EnvelopeExtractor & operator=(const EnvelopeExtractor &rhs)
unsigned int getNumValuesInBuffer() const
virtual bool computeFeatures(const VectorFloat &inputVector)
virtual bool load(std::fstream &file)
bool loadFeatureExtractionSettingsFromFile(std::fstream &file)
static std::string getId()
EnvelopeExtractor(const UINT bufferSize=100, const UINT numDimensions=1)
bool copyBaseVariables(const FeatureExtraction *featureExtractionModule)
virtual bool clear() override
bool resize(const unsigned int newBufferSize)