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.
MovementDetector.cpp
1 
2 #include "MovementDetector.h"
3 
4 GRT_BEGIN_NAMESPACE
5 
6 MovementDetector::MovementDetector( const UINT numDimensions, const Float upperThreshold, const Float lowerThreshold, const Float gamma, const UINT searchTimeout ) {
7  classType = "MovementDetector";
8  infoLog.setProceedingText("[MovementDetector]");
9  debugLog.setProceedingText("[DEBUG MovementDetector]");
10  errorLog.setProceedingText("[ERROR MovementDetector]");
11  trainingLog.setProceedingText("[TRAINING MovementDetector]");
12  warningLog.setProceedingText("[WARNING MovementDetector]");
13 
14  this->numInputDimensions = numDimensions;
15  this->numOutputDimensions = 1;
16  this->upperThreshold = upperThreshold;
17  this->lowerThreshold = lowerThreshold;
18  this->gamma = gamma;
19  this->searchTimeout = searchTimeout;
20  this->trained = true;
21 
22  reset();
23 }
24 
25 MovementDetector::~MovementDetector(){
26 
27 }
28 
30 
31  movementDetected = false;
32  noMovementDetected = false;
33 
34  if( !trained ){
35  errorLog << "predict_(VectorFloat &input) - AdaBoost Model Not Trained!" << std::endl;
36  return false;
37  }
38 
39  if( input.size() != numInputDimensions ){
40  errorLog << "predict_(VectorFloat &input) - The size of the input vector (" << input.size() << ") does not match the num features in the model (" << numInputDimensions << std::endl;
41  return false;
42  }
43 
44  //Compute the movement index, unless we are in the first sample
45  Float x = 0;
46  if( !firstSample ){
47  for(UINT n=0; n<numInputDimensions; n++){
48  x += SQR( input[n] - lastSample[n] );
49  }
50  movementIndex = (movementIndex*gamma) + sqrt( x );
51  }
52 
53  //Flag that this is not the first sample and store the input for the next prediction
54  firstSample = false;
55  lastSample = input;
56 
57  switch( state ){
58  case SEARCHING_FOR_MOVEMENT:
59  if( movementIndex >= upperThreshold ){
60  movementDetected = true;
61  state = SEARCHING_FOR_NO_MOVEMENT;
62  }
63  break;
64  case SEARCHING_FOR_NO_MOVEMENT:
65  if( movementIndex < lowerThreshold ){
66  noMovementDetected = true;
67  state = SEARCH_TIMEOUT;
68  searchTimer.start();
69  }
70  break;
71  case SEARCH_TIMEOUT:
72  // searchTimeout is cast because of a C4018 warning on visual (signed/unsigned incompatibility)
73  if( searchTimer.getMilliSeconds() >= (signed long)searchTimeout ){
74  state = SEARCH_TIMEOUT;
75  searchTimer.stop();
76  }
77  break;
78  }
79 
80 
81  return true;
82 }
83 
85 
86  trained = false;
87  lastSample.clear();
88 
89  reset();
90 
91  return true;
92 }
93 
95 
96  state = SEARCH_TIMEOUT;
97  searchTimer.start();
98  firstSample = true;
99  movementDetected = false;
100  noMovementDetected = false;
101  movementIndex = 0;
102 
103  return true;
104 }
105 
106 bool MovementDetector::saveModelToFile( std::fstream &file ) const{
107 
108  //Write the header info
109  file << "GRT_MOVEMENT_DETECTOR_MODEL_FILE_V1.0\n";
110 
111  //Write the base settings to the file
112  if( !MLBase::saveBaseSettingsToFile(file) ){
113  errorLog <<"saveModelToFile(fstream &file) - Failed to save ML base settings to file!" << std::endl;
114  return false;
115  }
116 
117  //Write the detector variables
118  file << "SearchTimeout: " << searchTimeout << std::endl;
119  file << "UpperThreshold: " << upperThreshold << std::endl;
120  file << "LowerThreshold: " << lowerThreshold << std::endl;
121  file << "Gamma: " << gamma << std::endl;
122 
123  return true;
124 }
125 
126 bool MovementDetector::loadModelFromFile( std::fstream &file ){
127 
128  clear();
129 
130  if(!file.is_open())
131  {
132  errorLog << "loadModelFromFile(string filename) - Could not open file to load model!" << std::endl;
133  return false;
134  }
135 
136  std::string word;
137  file >> word;
138 
139  //Write the header info
140  if( word != "GRT_MOVEMENT_DETECTOR_MODEL_FILE_V1.0" ){
141  errorLog <<"loadModelFromFile(fstream &file) - Failed to read file header!" << std::endl;
142  return false;
143  }
144 
145  //Load the base settings from the file
147  errorLog << "loadModelFromFile(string filename) - Failed to load base settings from file!" << std::endl;
148  return false;
149  }
150 
151  file >> word;
152  if( word != "SearchTimeout:" ){
153  errorLog <<"loadModelFromFile(fstream &file) - Failed to read SearchTimeout header!" << std::endl;
154  return false;
155  }
156  file >> searchTimeout;
157 
158  file >> word;
159  if( word != "UpperThreshold:" ){
160  errorLog <<"loadModelFromFile(fstream &file) - Failed to read UpperThreshold header!" << std::endl;
161  return false;
162  }
163  file >> upperThreshold;
164 
165  file >> word;
166  if( word != "LowerThreshold:" ){
167  errorLog <<"loadModelFromFile(fstream &file) - Failed to read LowerThreshold header!" << std::endl;
168  return false;
169  }
170  file >> lowerThreshold;
171 
172  file >> word;
173  if( word != "Gamma:" ){
174  errorLog <<"loadModelFromFile(fstream &file) - Failed to read Gamma header!" << std::endl;
175  return false;
176  }
177  file >> gamma;
178 
179  return true;
180 }
181 
182 Float MovementDetector::getUpperThreshold() const {
183  return upperThreshold;
184 }
185 
186 Float MovementDetector::getLowerThreshold() const {
187  return lowerThreshold;
188 }
189 
190 Float MovementDetector::getMovementIndex() const {
191  return movementIndex;
192 }
193 
194 Float MovementDetector::getGamma() const {
195  return gamma;
196 }
197 
198 bool MovementDetector::getMovementDetected() const {
199  return movementDetected;
200 }
201 
202 bool MovementDetector::getNoMovementDetect() const {
203  return noMovementDetected;
204 }
205 
206 UINT MovementDetector::getState() const {
207  return state;
208 }
209 
210 UINT MovementDetector::getSearchTimeout() const {
211  return searchTimeout;
212 }
213 
214 bool MovementDetector::setUpperThreshold(const Float upperThreshold) {
215  this->upperThreshold = upperThreshold;
216  return true;
217 }
218 
219 bool MovementDetector::setLowerThreshold(const Float lowerThreshold) {
220  this->lowerThreshold = lowerThreshold;
221  return true;
222 }
223 
224 bool MovementDetector::setGamma(const Float gamma) {
225  this->gamma = gamma;
226  return true;
227 }
228 
229 bool MovementDetector::setSearchTimeout(const UINT searchTimeout){
230  this->searchTimeout = searchTimeout;
231  return true;
232 }
233 
234 GRT_END_NAMESPACE
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:370
signed long getMilliSeconds()
Definition: Timer.h:117
virtual bool saveModelToFile(std::fstream &file) const
virtual bool clear()
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:393
bool start()
Definition: Timer.h:64
virtual bool predict_(VectorFloat &input)
virtual bool reset()
bool stop()
Definition: Timer.h:105
virtual bool loadModelFromFile(std::fstream &file)
This class implements a simple movement detection algorithm. This can be used to detect periods of 'l...