|
4
|
1 |
/* |
|
|
2 |
* Projet : TraKERS |
|
|
3 |
* Module : MIDDLEWARE |
|
|
4 |
* Sous-Module : Tracking/Gestures |
|
|
5 |
* Classe : PushDetector |
|
|
6 |
* |
|
|
7 |
* Auteur : alexandre.bastien@iri.centrepompidou.fr |
|
|
8 |
* |
|
|
9 |
* Fonctionnalités : Permet de détecter si l'utilisateur a effectué un Push, en se basant sur |
|
|
10 |
* des règles appliquées à la positions des noeuds dans le temps. |
|
|
11 |
*/ |
|
|
12 |
|
|
|
13 |
using System; |
|
|
14 |
using System.Collections.Generic; |
|
|
15 |
using System.Linq; |
|
|
16 |
using System.Text; |
|
|
17 |
using Microsoft.Kinect; |
|
|
18 |
|
|
|
19 |
namespace Trakers.Tracking.Gestures |
|
|
20 |
{ |
|
|
21 |
public class PushDetector : GestureDetector |
|
|
22 |
{ |
|
|
23 |
public enum Direction { PUSH, PULL }; |
|
|
24 |
public enum Hand { LEFT, RIGHT, BOTH, NONE }; |
|
|
25 |
|
|
|
26 |
Debug.DebugWindow debug; |
|
|
27 |
|
|
|
28 |
public PushDetector(Debug.DebugWindow _d) : base() |
|
|
29 |
{ |
|
|
30 |
debug = _d; |
|
|
31 |
gesturePeriod = (float)0.3; |
|
|
32 |
indexesPerSecond = 30; |
|
|
33 |
indexesToCheck = (int)(gesturePeriod * indexesPerSecond); |
|
|
34 |
} |
|
|
35 |
|
|
|
36 |
/* |
|
|
37 |
* Lit les noeuds de l'historique du squelette afin de détecter un Push. |
|
|
38 |
* Règles : |
|
|
39 |
* Se fait avec une main (gauche ou droite). |
|
|
40 |
* Chaque nouvelle position de la main doit être plus profonde que la précédente. |
|
|
41 |
* Chaque nouvelle position de la main ne doit pas dévier trop de l'axe perpendiculaire au plan (X, Y). |
|
|
42 |
* Le geste doit mesurer en profondeur une certaine distance. |
|
|
43 |
*/ |
|
|
44 |
public Hand CheckForPush() |
|
|
45 |
{ |
|
|
46 |
//Crée un historique de squelette local, puisque l'historique est mis à jour toutes les ~1/30 s. |
|
|
47 |
List<List<Joint>> localHistory = new List<List<Joint>>(history); |
|
|
48 |
|
|
|
49 |
//Si il n'y a pas assez de positions dans l'historique local pour vérifier le geste. |
|
|
50 |
if (localHistory.Count < indexesToCheck + 1) |
|
|
51 |
return Hand.NONE; |
|
|
52 |
|
|
|
53 |
//La distance de référence est ici la distance entre le milieu du dos et le milieu des épaules. |
|
|
54 |
refDistance = Math.Abs(localHistory[0][spineID].Position.Y - localHistory[0][shoulderCenterID].Position.Y); |
|
|
55 |
//On commence la position pour les indexesToCheck dernières postures (celle à l'index 0 étant la dernière). |
|
|
56 |
SkeletonPoint startPointLeft = localHistory[localHistory.Count - indexesToCheck][handLeftID].Position; |
|
|
57 |
SkeletonPoint startPointRight = localHistory[localHistory.Count - indexesToCheck][handRightID].Position; |
|
|
58 |
|
|
|
59 |
//Booléens indiquant si le mouvement serait valide pour la main gauche ou droite. |
|
|
60 |
bool leftHandOK = true, rightHandOK = true; |
|
|
61 |
|
|
|
62 |
//De la position p1 à pn, on suit l'algorithme. |
|
|
63 |
for (int i = localHistory.Count - indexesToCheck + 1; i < localHistory.Count; i++) |
|
|
64 |
{ |
|
|
65 |
if (localHistory[i][handRightID].Position.Y > localHistory[i][headID].Position.Y) |
|
|
66 |
debug.R1.Fill = System.Windows.Media.Brushes.Blue; |
|
|
67 |
else |
|
|
68 |
debug.R1.Fill = System.Windows.Media.Brushes.DarkGray; |
|
|
69 |
|
|
|
70 |
if (localHistory[i][handRightID].Position.Y < localHistory[i][hipCenterID].Position.Y) |
|
|
71 |
debug.R2.Fill = System.Windows.Media.Brushes.Blue; |
|
|
72 |
else |
|
|
73 |
debug.R2.Fill = System.Windows.Media.Brushes.DarkGray; |
|
|
74 |
|
|
|
75 |
if (localHistory[i][handRightID].Position.Z < localHistory[i - 1][handRightID].Position.Z) |
|
|
76 |
debug.R3.Fill = System.Windows.Media.Brushes.Blue; |
|
|
77 |
else |
|
|
78 |
debug.R3.Fill = System.Windows.Media.Brushes.DarkGray; |
|
|
79 |
|
|
|
80 |
//Console.Out.WriteLine(Math.Abs(localHistory[i][handRightID].Position.X - startPointRight.X) + " " + Math.Abs(localHistory[i][handRightID].Position.Y - startPointRight.Y) + " " + refDistance / 10); |
|
|
81 |
|
|
|
82 |
if (Math.Abs(localHistory[i][handRightID].Position.X - startPointRight.X) < refDistance / 5 && |
|
|
83 |
Math.Abs(localHistory[i][handRightID].Position.Y - startPointRight.Y) < refDistance / 5) |
|
|
84 |
debug.R4.Fill = System.Windows.Media.Brushes.Blue; |
|
|
85 |
else |
|
|
86 |
debug.R4.Fill = System.Windows.Media.Brushes.DarkGray; |
|
|
87 |
|
|
|
88 |
//Si la position Y de la main est plus haute que la tête |
|
|
89 |
//OU si la position Y de la main est plus basse que la hanche |
|
|
90 |
//OU si la nouvelle position Z de la main est moins profonde que la précédente |
|
|
91 |
//OU si la nouvelle position X de la main est plus éloignée de la distance N par rapport à la première position X |
|
|
92 |
//OU si la nouvelle position Y de la main est plus éloignée de la distance N par rapport à la première position Y |
|
|
93 |
//Alors la main en question ne fait pas de push. |
|
|
94 |
if (localHistory[i][handLeftID].Position.Y < localHistory[i][headID].Position.Y || |
|
|
95 |
localHistory[i][handLeftID].Position.Y > localHistory[i][hipCenterID].Position.Y || |
|
|
96 |
localHistory[i][handLeftID].Position.Z > localHistory[i - 1][handLeftID].Position.Z || |
|
|
97 |
Math.Abs(localHistory[i][handLeftID].Position.X - startPointLeft.X) > refDistance / 5 || |
|
|
98 |
Math.Abs(localHistory[i][handLeftID].Position.Y - startPointLeft.Y) > refDistance / 5) |
|
|
99 |
leftHandOK = false; |
|
|
100 |
if (localHistory[i][handRightID].Position.Y < localHistory[i][headID].Position.Y || |
|
|
101 |
localHistory[i][handRightID].Position.Y > localHistory[i][hipCenterID].Position.Y || |
|
|
102 |
localHistory[i][handRightID].Position.Z > localHistory[i - 1][handRightID].Position.Z || |
|
|
103 |
Math.Abs(localHistory[i][handRightID].Position.X - startPointRight.X) > refDistance / 5 || |
|
|
104 |
Math.Abs(localHistory[i][handRightID].Position.Y - startPointRight.Y) > refDistance / 5) |
|
|
105 |
rightHandOK = false; |
|
|
106 |
|
|
|
107 |
if (!leftHandOK && !rightHandOK) |
|
|
108 |
return Hand.NONE; |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
//Console.Out.WriteLine("OK"); |
|
|
112 |
|
|
|
113 |
if (Math.Abs(localHistory[localHistory.Count - 1][handRightID].Position.Z - localHistory[localHistory.Count - indexesToCheck][handRightID].Position.Z) * 100 > 20) |
|
|
114 |
debug.R5.Fill = System.Windows.Media.Brushes.Blue; |
|
|
115 |
else |
|
|
116 |
debug.R5.Fill = System.Windows.Media.Brushes.DarkGray; |
|
|
117 |
|
|
|
118 |
if (localHistory[localHistory.Count - 1][handRightID].Position.X > localHistory[localHistory.Count - 1][hipCenterID].Position.X || |
|
|
119 |
localHistory[localHistory.Count - indexesToCheck][handRightID].Position.X > localHistory[localHistory.Count - 1][hipCenterID].Position.X) |
|
|
120 |
debug.R6.Fill = System.Windows.Media.Brushes.Blue; |
|
|
121 |
else |
|
|
122 |
debug.R6.Fill = System.Windows.Media.Brushes.DarkGray; |
|
|
123 |
|
|
|
124 |
//Si la distance en Z du geste a été plus courte que la distance N |
|
|
125 |
//Alors on retourne faux. |
|
|
126 |
//float dist = (localHistory[localHistory.Count - 1][handRightID].Position.X - localHistory[localHistory.Count - indexesToCheck][handRightID].Position.X); |
|
|
127 |
|
|
|
128 |
//Console.WriteLine(Math.Abs(localHistory[0][handLeftID].Position.Z - localHistory[localHistory.Count - indexesToCheck][handLeftID].Position.Z) * 100 + " " + refDistance); |
|
|
129 |
|
|
|
130 |
if (Math.Abs(localHistory[localHistory.Count - 1][handLeftID].Position.Z - localHistory[localHistory.Count - indexesToCheck][handLeftID].Position.Z) * 100 < 20) |
|
|
131 |
leftHandOK = false; |
|
|
132 |
if (Math.Abs(localHistory[localHistory.Count - 1][handRightID].Position.Z - localHistory[localHistory.Count - indexesToCheck][handRightID].Position.Z) * 100 < 20) |
|
|
133 |
rightHandOK = false; |
|
|
134 |
|
|
|
135 |
/*if(rightHandOK || leftHandOK) |
|
|
136 |
Console.Out.WriteLine("000000000");*/ |
|
|
137 |
|
|
|
138 |
//Si la dernière position de la main droite/gauche est sur le côté gauche/droit du corps |
|
|
139 |
//OU si la première position calculée de la main droite/gauche est sur le côté gauche/droit du corps |
|
|
140 |
//Alors on retourne faux. |
|
|
141 |
if (localHistory[localHistory.Count - 1][handLeftID].Position.X > localHistory[localHistory.Count - 1][hipCenterID].Position.X || |
|
|
142 |
localHistory[localHistory.Count - indexesToCheck][handLeftID].Position.X > localHistory[localHistory.Count - 1][hipCenterID].Position.X) |
|
|
143 |
leftHandOK = false; |
|
|
144 |
if (localHistory[localHistory.Count - 1][handRightID].Position.X < localHistory[localHistory.Count - 1][hipCenterID].Position.X || |
|
|
145 |
localHistory[localHistory.Count - indexesToCheck][handRightID].Position.X < localHistory[localHistory.Count - 1][hipCenterID].Position.X) |
|
|
146 |
rightHandOK = false; |
|
|
147 |
|
|
|
148 |
/*if (rightHandOK || leftHandOK) |
|
|
149 |
Console.Out.WriteLine("11111111111");*/ |
|
|
150 |
|
|
|
151 |
if (!leftHandOK && !rightHandOK) |
|
|
152 |
return Hand.NONE; |
|
|
153 |
|
|
|
154 |
/*if (rightHandOK || leftHandOK) |
|
|
155 |
Console.Out.WriteLine("================");*/ |
|
|
156 |
|
|
|
157 |
//On supprime l'historique local. |
|
|
158 |
localHistory.Clear(); |
|
|
159 |
|
|
|
160 |
debug.ExceptionLbl.Background = System.Windows.Media.Brushes.White; |
|
|
161 |
//Console.WriteLine("PUSH"); |
|
|
162 |
//Console.Read(); |
|
|
163 |
|
|
|
164 |
//Si on est arrivé jusqu'ici, toutes les conditions pour un push ont été remplies. |
|
|
165 |
if (leftHandOK && rightHandOK) |
|
|
166 |
return Hand.BOTH; |
|
|
167 |
else if (leftHandOK) |
|
|
168 |
return Hand.LEFT; |
|
|
169 |
else if (rightHandOK) |
|
|
170 |
return Hand.RIGHT; |
|
|
171 |
|
|
|
172 |
return Hand.NONE; |
|
|
173 |
} |
|
|
174 |
|
|
|
175 |
/* |
|
|
176 |
* Lit les noeuds de l'historique du squelette afin de détecter un Pull. |
|
|
177 |
* Règles : |
|
|
178 |
* Se fait avec une main. |
|
|
179 |
* Chaque nouvelle position de la main doit être moins profonde que la précédente. |
|
|
180 |
* Chaque nouvelle position de la main ne doit pas dévier trop de l'axe perpendiculaire au plan (X, Y). |
|
|
181 |
* Le geste doit mesurer en profondeur une certaine distance. |
|
|
182 |
*/ |
|
|
183 |
public Hand CheckForPull() |
|
|
184 |
{ |
|
|
185 |
//Crée un historique de squelette local, puisque l'historique est mis à jour toutes les ~1/30 s. |
|
|
186 |
List<List<Joint>> localHistory = new List<List<Joint>>(history); |
|
|
187 |
|
|
|
188 |
//Si il n'y a pas assez de positions dans l'historique local pour vérifier le geste. |
|
|
189 |
if (localHistory.Count < indexesToCheck + 1) |
|
|
190 |
return Hand.NONE; |
|
|
191 |
|
|
|
192 |
//La distance de référence est ici la distance entre le milieu du dos et le milieu des épaules. |
|
|
193 |
refDistance = Math.Abs(localHistory[0][spineID].Position.Y - localHistory[0][shoulderCenterID].Position.Y); |
|
|
194 |
//On commence la position pour les indexesToCheck dernières postures (celle à l'index 0 étant la dernière). |
|
|
195 |
SkeletonPoint startPointLeft = localHistory[localHistory.Count - indexesToCheck][handLeftID].Position; |
|
|
196 |
SkeletonPoint startPointRight = localHistory[localHistory.Count - indexesToCheck][handRightID].Position; |
|
|
197 |
|
|
|
198 |
//Booléens indiquant si le mouvement serait valide pour la main gauche ou droite. |
|
|
199 |
bool leftHandOK = true, rightHandOK = true; |
|
|
200 |
|
|
|
201 |
//De la position p1 à pn, on suit l'algorithme. |
|
|
202 |
for (int i = localHistory.Count - indexesToCheck + 1; i < localHistory.Count; i++) |
|
|
203 |
{ |
|
|
204 |
//Si la position Y de la main est plus haute que la tête |
|
|
205 |
//OU si la position Y de la main est plus basse que la hanche |
|
|
206 |
//OU si la nouvelle position Z de la main est plus profonde que la précédente |
|
|
207 |
//OU si la nouvelle position X de la main est plus éloignée de la distance N par rapport à la première position X |
|
|
208 |
//OU si la nouvelle position Y de la main est plus éloignée de la distance N par rapport à la première position Y |
|
|
209 |
//Alors la main en question ne fait pas de push. |
|
|
210 |
if (localHistory[i][handLeftID].Position.Y < localHistory[i][headID].Position.Y || |
|
|
211 |
localHistory[i][handLeftID].Position.Y > localHistory[i][hipCenterID].Position.Y || |
|
|
212 |
localHistory[i][handLeftID].Position.Z < localHistory[i - 1][handLeftID].Position.Z || |
|
|
213 |
Math.Abs(localHistory[i][handLeftID].Position.X - startPointLeft.X) > refDistance / 5 || |
|
|
214 |
Math.Abs(localHistory[i][handLeftID].Position.Y - startPointLeft.Y) > refDistance / 5) |
|
|
215 |
leftHandOK = false; |
|
|
216 |
if (localHistory[i][handRightID].Position.Y < localHistory[i][headID].Position.Y || |
|
|
217 |
localHistory[i][handRightID].Position.Y > localHistory[i][hipCenterID].Position.Y || |
|
|
218 |
localHistory[i][handRightID].Position.Z < localHistory[i - 1][handRightID].Position.Z || |
|
|
219 |
Math.Abs(localHistory[i][handRightID].Position.X - startPointRight.X) > refDistance / 5 || |
|
|
220 |
Math.Abs(localHistory[i][handRightID].Position.Y - startPointRight.Y) > refDistance / 5) |
|
|
221 |
rightHandOK = false; |
|
|
222 |
|
|
|
223 |
if (!leftHandOK && !rightHandOK) |
|
|
224 |
return Hand.NONE; |
|
|
225 |
} |
|
|
226 |
|
|
|
227 |
//Console.Out.WriteLine("OK"); |
|
|
228 |
|
|
|
229 |
if (Math.Abs(localHistory[localHistory.Count - 1][handRightID].Position.Z - localHistory[localHistory.Count - indexesToCheck][handRightID].Position.Z) * 100 > 20) |
|
|
230 |
debug.R5.Fill = System.Windows.Media.Brushes.Blue; |
|
|
231 |
else |
|
|
232 |
debug.R5.Fill = System.Windows.Media.Brushes.DarkGray; |
|
|
233 |
|
|
|
234 |
if (localHistory[localHistory.Count - 1][handRightID].Position.X > localHistory[localHistory.Count - 1][hipCenterID].Position.X || |
|
|
235 |
localHistory[localHistory.Count - indexesToCheck][handRightID].Position.X > localHistory[localHistory.Count - 1][hipCenterID].Position.X) |
|
|
236 |
debug.R6.Fill = System.Windows.Media.Brushes.Blue; |
|
|
237 |
else |
|
|
238 |
debug.R6.Fill = System.Windows.Media.Brushes.DarkGray; |
|
|
239 |
|
|
|
240 |
//Si la distance en Z du geste a été plus courte que la distance N |
|
|
241 |
//Alors on retourne faux. |
|
|
242 |
//float dist = (localHistory[localHistory.Count - 1][handRightID].Position.X - localHistory[localHistory.Count - indexesToCheck][handRightID].Position.X); |
|
|
243 |
|
|
|
244 |
//Console.WriteLine(Math.Abs(localHistory[0][handLeftID].Position.Z - localHistory[localHistory.Count - indexesToCheck][handLeftID].Position.Z) * 100 + " " + refDistance); |
|
|
245 |
|
|
|
246 |
if (Math.Abs(localHistory[localHistory.Count - 1][handLeftID].Position.Z - localHistory[localHistory.Count - indexesToCheck][handLeftID].Position.Z) * 100 < 20) |
|
|
247 |
leftHandOK = false; |
|
|
248 |
if (Math.Abs(localHistory[localHistory.Count - 1][handRightID].Position.Z - localHistory[localHistory.Count - indexesToCheck][handRightID].Position.Z) * 100 < 20) |
|
|
249 |
rightHandOK = false; |
|
|
250 |
|
|
|
251 |
/*if (rightHandOK || leftHandOK) |
|
|
252 |
Console.Out.WriteLine("000000000");*/ |
|
|
253 |
|
|
|
254 |
//Si la dernière position de la main droite/gauche est sur le côté gauche/droit du corps |
|
|
255 |
//OU si la première position calculée de la main droite/gauche est sur le côté gauche/droit du corps |
|
|
256 |
//Alors on retourne faux. |
|
|
257 |
if (localHistory[localHistory.Count - 1][handLeftID].Position.X > localHistory[localHistory.Count - 1][hipCenterID].Position.X || |
|
|
258 |
localHistory[localHistory.Count - indexesToCheck][handLeftID].Position.X > localHistory[localHistory.Count - 1][hipCenterID].Position.X) |
|
|
259 |
leftHandOK = false; |
|
|
260 |
if (localHistory[localHistory.Count - 1][handRightID].Position.X < localHistory[localHistory.Count - 1][hipCenterID].Position.X || |
|
|
261 |
localHistory[localHistory.Count - indexesToCheck][handRightID].Position.X < localHistory[localHistory.Count - 1][hipCenterID].Position.X) |
|
|
262 |
rightHandOK = false; |
|
|
263 |
|
|
|
264 |
/*if (rightHandOK || leftHandOK) |
|
|
265 |
Console.Out.WriteLine("11111111111");*/ |
|
|
266 |
|
|
|
267 |
if (!leftHandOK && !rightHandOK) |
|
|
268 |
return Hand.NONE; |
|
|
269 |
|
|
|
270 |
/*if (rightHandOK || leftHandOK) |
|
|
271 |
Console.Out.WriteLine("================");*/ |
|
|
272 |
|
|
|
273 |
//On supprime l'historique local. |
|
|
274 |
localHistory.Clear(); |
|
|
275 |
|
|
|
276 |
debug.ExceptionLbl.Background = System.Windows.Media.Brushes.Black; |
|
|
277 |
//Console.WriteLine("PUSH"); |
|
|
278 |
//Console.Read(); |
|
|
279 |
|
|
|
280 |
//Si on est arrivé jusqu'ici, toutes les conditions pour un push ont été remplies. |
|
|
281 |
if (leftHandOK && rightHandOK) |
|
|
282 |
return Hand.BOTH; |
|
|
283 |
else if (leftHandOK) |
|
|
284 |
return Hand.LEFT; |
|
|
285 |
else if (rightHandOK) |
|
|
286 |
return Hand.RIGHT; |
|
|
287 |
|
|
|
288 |
return Hand.NONE; |
|
|
289 |
} |
|
|
290 |
} |
|
|
291 |
} |