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