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