|
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. |
|
5
|
54 |
refDistance = Math.Abs(localHistory[0][(int)JointType.Spine].Position.Y - localHistory[0][(int)JointType.ShoulderCenter].Position.Y); |
|
4
|
55 |
//On commence la position pour les indexesToCheck dernières postures (celle à l'index 0 étant la dernière). |
|
5
|
56 |
SkeletonPoint startPointLeft = localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandLeft].Position; |
|
|
57 |
SkeletonPoint startPointRight = localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandRight].Position; |
|
4
|
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 |
//Si la position Y de la main est plus haute que la tête |
|
|
66 |
//OU si la position Y de la main est plus basse que la hanche |
|
|
67 |
//OU si la nouvelle position Z de la main est moins profonde que la précédente |
|
|
68 |
//OU si la nouvelle position X de la main est plus éloignée de la distance N par rapport à la première position X |
|
|
69 |
//OU si la nouvelle position Y de la main est plus éloignée de la distance N par rapport à la première position Y |
|
|
70 |
//Alors la main en question ne fait pas de push. |
|
5
|
71 |
if (localHistory[i][(int)JointType.HandLeft].Position.Y < localHistory[i][(int)JointType.Head].Position.Y || |
|
|
72 |
localHistory[i][(int)JointType.HandLeft].Position.Y > localHistory[i][(int)JointType.HipCenter].Position.Y || |
|
|
73 |
localHistory[i][(int)JointType.HandLeft].Position.Z > localHistory[i - 1][(int)JointType.HandLeft].Position.Z || |
|
|
74 |
Math.Abs(localHistory[i][(int)JointType.HandLeft].Position.X - startPointLeft.X) > refDistance / 5 || |
|
|
75 |
Math.Abs(localHistory[i][(int)JointType.HandLeft].Position.Y - startPointLeft.Y) > refDistance / 5) |
|
4
|
76 |
leftHandOK = false; |
|
5
|
77 |
if (localHistory[i][(int)JointType.HandRight].Position.Y < localHistory[i][(int)JointType.Head].Position.Y || |
|
|
78 |
localHistory[i][(int)JointType.HandRight].Position.Y > localHistory[i][(int)JointType.HipCenter].Position.Y || |
|
|
79 |
localHistory[i][(int)JointType.HandRight].Position.Z > localHistory[i - 1][(int)JointType.HandRight].Position.Z || |
|
|
80 |
Math.Abs(localHistory[i][(int)JointType.HandRight].Position.X - startPointRight.X) > refDistance / 5 || |
|
|
81 |
Math.Abs(localHistory[i][(int)JointType.HandRight].Position.Y - startPointRight.Y) > refDistance / 5) |
|
4
|
82 |
rightHandOK = false; |
|
|
83 |
|
|
|
84 |
if (!leftHandOK && !rightHandOK) |
|
|
85 |
return Hand.NONE; |
|
|
86 |
} |
|
|
87 |
|
|
|
88 |
//Si la distance en Z du geste a été plus courte que la distance N |
|
|
89 |
//Alors on retourne faux. |
|
5
|
90 |
if (Math.Abs(localHistory[localHistory.Count - 1][(int)JointType.HandLeft].Position.Z - localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandLeft].Position.Z) * 100 < 20) |
|
4
|
91 |
leftHandOK = false; |
|
5
|
92 |
if (Math.Abs(localHistory[localHistory.Count - 1][(int)JointType.HandRight].Position.Z - localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandRight].Position.Z) * 100 < 20) |
|
4
|
93 |
rightHandOK = false; |
|
|
94 |
|
|
|
95 |
//Si la dernière position de la main droite/gauche est sur le côté gauche/droit du corps |
|
|
96 |
//OU si la première position calculée de la main droite/gauche est sur le côté gauche/droit du corps |
|
|
97 |
//Alors on retourne faux. |
|
5
|
98 |
if (localHistory[localHistory.Count - 1][(int)JointType.HandLeft].Position.X > localHistory[localHistory.Count - 1][(int)JointType.HipCenter].Position.X || |
|
|
99 |
localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandLeft].Position.X > localHistory[localHistory.Count - 1][(int)JointType.HipCenter].Position.X) |
|
4
|
100 |
leftHandOK = false; |
|
5
|
101 |
if (localHistory[localHistory.Count - 1][(int)JointType.HandRight].Position.X < localHistory[localHistory.Count - 1][(int)JointType.HipCenter].Position.X || |
|
|
102 |
localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandRight].Position.X < localHistory[localHistory.Count - 1][(int)JointType.HipCenter].Position.X) |
|
4
|
103 |
rightHandOK = false; |
|
|
104 |
|
|
|
105 |
if (!leftHandOK && !rightHandOK) |
|
|
106 |
return Hand.NONE; |
|
|
107 |
|
|
|
108 |
//On supprime l'historique local. |
|
|
109 |
localHistory.Clear(); |
|
|
110 |
|
|
|
111 |
debug.ExceptionLbl.Background = System.Windows.Media.Brushes.White; |
|
|
112 |
//Si on est arrivé jusqu'ici, toutes les conditions pour un push ont été remplies. |
|
5
|
113 |
|
|
4
|
114 |
if (leftHandOK && rightHandOK) |
|
|
115 |
return Hand.BOTH; |
|
|
116 |
else if (leftHandOK) |
|
|
117 |
return Hand.LEFT; |
|
|
118 |
else if (rightHandOK) |
|
|
119 |
return Hand.RIGHT; |
|
|
120 |
|
|
|
121 |
return Hand.NONE; |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
/* |
|
|
125 |
* Lit les noeuds de l'historique du squelette afin de détecter un Pull. |
|
|
126 |
* Règles : |
|
|
127 |
* Se fait avec une main. |
|
|
128 |
* Chaque nouvelle position de la main doit être moins profonde que la précédente. |
|
|
129 |
* Chaque nouvelle position de la main ne doit pas dévier trop de l'axe perpendiculaire au plan (X, Y). |
|
|
130 |
* Le geste doit mesurer en profondeur une certaine distance. |
|
|
131 |
*/ |
|
|
132 |
public Hand CheckForPull() |
|
|
133 |
{ |
|
|
134 |
//Crée un historique de squelette local, puisque l'historique est mis à jour toutes les ~1/30 s. |
|
|
135 |
List<List<Joint>> localHistory = new List<List<Joint>>(history); |
|
|
136 |
|
|
|
137 |
//Si il n'y a pas assez de positions dans l'historique local pour vérifier le geste. |
|
|
138 |
if (localHistory.Count < indexesToCheck + 1) |
|
|
139 |
return Hand.NONE; |
|
|
140 |
|
|
|
141 |
//La distance de référence est ici la distance entre le milieu du dos et le milieu des épaules. |
|
5
|
142 |
refDistance = Math.Abs(localHistory[0][(int)JointType.Spine].Position.Y - localHistory[0][(int)JointType.ShoulderCenter].Position.Y); |
|
4
|
143 |
//On commence la position pour les indexesToCheck dernières postures (celle à l'index 0 étant la dernière). |
|
5
|
144 |
SkeletonPoint startPointLeft = localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandLeft].Position; |
|
|
145 |
SkeletonPoint startPointRight = localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandRight].Position; |
|
4
|
146 |
|
|
|
147 |
//Booléens indiquant si le mouvement serait valide pour la main gauche ou droite. |
|
|
148 |
bool leftHandOK = true, rightHandOK = true; |
|
|
149 |
|
|
|
150 |
//De la position p1 à pn, on suit l'algorithme. |
|
|
151 |
for (int i = localHistory.Count - indexesToCheck + 1; i < localHistory.Count; i++) |
|
|
152 |
{ |
|
|
153 |
//Si la position Y de la main est plus haute que la tête |
|
|
154 |
//OU si la position Y de la main est plus basse que la hanche |
|
|
155 |
//OU si la nouvelle position Z de la main est plus profonde que la précédente |
|
|
156 |
//OU si la nouvelle position X de la main est plus éloignée de la distance N par rapport à la première position X |
|
|
157 |
//OU si la nouvelle position Y de la main est plus éloignée de la distance N par rapport à la première position Y |
|
|
158 |
//Alors la main en question ne fait pas de push. |
|
5
|
159 |
if (localHistory[i][(int)JointType.HandLeft].Position.Y < localHistory[i][(int)JointType.Head].Position.Y || |
|
|
160 |
localHistory[i][(int)JointType.HandLeft].Position.Y > localHistory[i][(int)JointType.HipCenter].Position.Y || |
|
|
161 |
localHistory[i][(int)JointType.HandLeft].Position.Z < localHistory[i - 1][(int)JointType.HandLeft].Position.Z || |
|
|
162 |
Math.Abs(localHistory[i][(int)JointType.HandLeft].Position.X - startPointLeft.X) > refDistance / 5 || |
|
|
163 |
Math.Abs(localHistory[i][(int)JointType.HandLeft].Position.Y - startPointLeft.Y) > refDistance / 5) |
|
4
|
164 |
leftHandOK = false; |
|
5
|
165 |
if (localHistory[i][(int)JointType.HandRight].Position.Y < localHistory[i][(int)JointType.Head].Position.Y || |
|
|
166 |
localHistory[i][(int)JointType.HandRight].Position.Y > localHistory[i][(int)JointType.HipCenter].Position.Y || |
|
|
167 |
localHistory[i][(int)JointType.HandRight].Position.Z < localHistory[i - 1][(int)JointType.HandRight].Position.Z || |
|
|
168 |
Math.Abs(localHistory[i][(int)JointType.HandRight].Position.X - startPointRight.X) > refDistance / 5 || |
|
|
169 |
Math.Abs(localHistory[i][(int)JointType.HandRight].Position.Y - startPointRight.Y) > refDistance / 5) |
|
4
|
170 |
rightHandOK = false; |
|
|
171 |
|
|
|
172 |
if (!leftHandOK && !rightHandOK) |
|
|
173 |
return Hand.NONE; |
|
|
174 |
} |
|
|
175 |
|
|
|
176 |
//Si la distance en Z du geste a été plus courte que la distance N |
|
|
177 |
//Alors on retourne faux. |
|
5
|
178 |
if (Math.Abs(localHistory[localHistory.Count - 1][(int)JointType.HandLeft].Position.Z - localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandLeft].Position.Z) * 100 < 20) |
|
4
|
179 |
leftHandOK = false; |
|
5
|
180 |
if (Math.Abs(localHistory[localHistory.Count - 1][(int)JointType.HandRight].Position.Z - localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandRight].Position.Z) * 100 < 20) |
|
4
|
181 |
rightHandOK = false; |
|
|
182 |
|
|
|
183 |
//Si la dernière position de la main droite/gauche est sur le côté gauche/droit du corps |
|
|
184 |
//OU si la première position calculée de la main droite/gauche est sur le côté gauche/droit du corps |
|
|
185 |
//Alors on retourne faux. |
|
5
|
186 |
if (localHistory[localHistory.Count - 1][(int)JointType.HandLeft].Position.X > localHistory[localHistory.Count - 1][(int)JointType.HipCenter].Position.X || |
|
|
187 |
localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandLeft].Position.X > localHistory[localHistory.Count - 1][(int)JointType.HipCenter].Position.X) |
|
4
|
188 |
leftHandOK = false; |
|
5
|
189 |
if (localHistory[localHistory.Count - 1][(int)JointType.HandRight].Position.X < localHistory[localHistory.Count - 1][(int)JointType.HipCenter].Position.X || |
|
|
190 |
localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandRight].Position.X < localHistory[localHistory.Count - 1][(int)JointType.HipCenter].Position.X) |
|
4
|
191 |
rightHandOK = false; |
|
|
192 |
|
|
|
193 |
if (!leftHandOK && !rightHandOK) |
|
|
194 |
return Hand.NONE; |
|
|
195 |
|
|
|
196 |
//On supprime l'historique local. |
|
|
197 |
localHistory.Clear(); |
|
|
198 |
|
|
|
199 |
debug.ExceptionLbl.Background = System.Windows.Media.Brushes.Black; |
|
5
|
200 |
|
|
4
|
201 |
//Si on est arrivé jusqu'ici, toutes les conditions pour un push ont été remplies. |
|
|
202 |
if (leftHandOK && rightHandOK) |
|
|
203 |
return Hand.BOTH; |
|
|
204 |
else if (leftHandOK) |
|
|
205 |
return Hand.LEFT; |
|
|
206 |
else if (rightHandOK) |
|
|
207 |
return Hand.RIGHT; |
|
|
208 |
|
|
|
209 |
return Hand.NONE; |
|
|
210 |
} |
|
|
211 |
} |
|
|
212 |
} |