|
15
|
1 |
/* |
|
|
2 |
* This file is part of the TraKERS\Middleware package. |
|
|
3 |
* |
|
|
4 |
* (c) IRI <http://www.iri.centrepompidou.fr/> |
|
|
5 |
* |
|
27
|
6 |
* For the full copyright and license information, please view the LICENSE |
|
15
|
7 |
* file that was distributed with this source code. |
|
|
8 |
*/ |
|
|
9 |
|
|
|
10 |
/* |
|
|
11 |
* Projet : TraKERS |
|
|
12 |
* Module : MIDDLEWARE |
|
|
13 |
* Sous-Module : Tracking/Gestures |
|
|
14 |
* Classe : CircleDetector |
|
|
15 |
* |
|
|
16 |
* Auteur : alexandre.bastien@iri.centrepompidou.fr |
|
|
17 |
* |
|
|
18 |
* Fonctionnalités : Permet de détecter si l'utilisateur a effectué un cercle, en se basant sur |
|
|
19 |
* des règles appliquées à la positions des noeuds dans le temps. |
|
|
20 |
*/ |
|
|
21 |
|
|
|
22 |
using System; |
|
|
23 |
using System.Collections.Generic; |
|
|
24 |
using System.Linq; |
|
|
25 |
using System.Text; |
|
|
26 |
using Microsoft.Kinect; |
|
|
27 |
using System.Windows.Media.Media3D; |
|
|
28 |
using System.Drawing; |
|
17
|
29 |
using Trakers.Debug; |
|
15
|
30 |
|
|
17
|
31 |
namespace Trakers.Tracking.Gestures |
|
15
|
32 |
{ |
|
|
33 |
public class CircleDetector : GestureDetector |
|
|
34 |
{ |
|
17
|
35 |
public CircleDetector(DebugWindow _debug) : base(_debug) |
|
15
|
36 |
{ |
|
|
37 |
gesturePeriod = (float)1; |
|
|
38 |
indexesPerSecond = 30; |
|
|
39 |
indexesToCheck = (int)(gesturePeriod * indexesPerSecond); |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
/* |
|
|
43 |
* Lit les noeuds de l'historique du squelette afin de détecter un cercle. |
|
|
44 |
* Règles : |
|
|
45 |
* Se fait avec une main. |
|
|
46 |
* Chaque point est à la même distance du barycentre. |
|
17
|
47 |
* On regarde pour la main gauche. |
|
15
|
48 |
*/ |
|
17
|
49 |
public bool CheckForLeftCircle() |
|
15
|
50 |
{ |
|
17
|
51 |
//Indique si la main gauche a décrit un cercle. |
|
|
52 |
bool leftHandDoCircle = true; |
|
|
53 |
|
|
15
|
54 |
//Crée un historique de squelette local, puisque l'historique est mis à jour toutes les ~1/30 s. |
|
|
55 |
List<List<Joint>> localHistory = new List<List<Joint>>(history); |
|
|
56 |
|
|
|
57 |
//Si il n'y a pas assez de positions dans l'historique local pour vérifier le geste. |
|
|
58 |
if (localHistory.Count < indexesToCheck + 1) |
|
|
59 |
return false; |
|
|
60 |
|
|
|
61 |
//La distance de référence est ici la distance entre le milieu du dos et le milieu des épaules. |
|
|
62 |
refDistance = Math.Abs(localHistory[0][(int)JointType.Spine].Position.Y - localHistory[0][(int)JointType.ShoulderCenter].Position.Y); |
|
|
63 |
//On commence la position pour les indexesToCheck dernières postures (celle à l'index 0 étant la dernière). |
|
17
|
64 |
SkeletonPoint startPointLeft = localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandRight].Position; |
|
|
65 |
|
|
15
|
66 |
//Barycentres pour les mains. |
|
|
67 |
PointF leftBarycenter = new PointF(0, 0); |
|
|
68 |
//Distances moyennes des points aux barycentres. |
|
|
69 |
float averageDistToLeftBarycenter = 0; |
|
17
|
70 |
|
|
15
|
71 |
//Index du point de départ dans la détection. |
|
|
72 |
int beginIndex = localHistory.Count - indexesToCheck; |
|
|
73 |
|
|
|
74 |
//Calcul du barycentre de la main gauche. |
|
|
75 |
for (int i = beginIndex; i > 0; i--) |
|
|
76 |
{ |
|
|
77 |
leftBarycenter.X += localHistory[i][(int)JointType.HandLeft].Position.X; |
|
|
78 |
leftBarycenter.Y += localHistory[i][(int)JointType.HandLeft].Position.Y; |
|
|
79 |
} |
|
|
80 |
leftBarycenter.X /= indexesToCheck; |
|
|
81 |
leftBarycenter.Y /= indexesToCheck; |
|
|
82 |
|
|
|
83 |
//Estimation de la distance moyenne d'un point au barycentre gauche. |
|
|
84 |
for (int i = beginIndex; i > 0; i--) |
|
|
85 |
{ |
|
|
86 |
float ptX = localHistory[i][(int)JointType.HandLeft].Position.X; |
|
|
87 |
float ptY = localHistory[i][(int)JointType.HandLeft].Position.Y; |
|
|
88 |
averageDistToLeftBarycenter += (float)Distance2D(ptX, leftBarycenter.X, ptY, leftBarycenter.Y); |
|
|
89 |
} |
|
|
90 |
averageDistToLeftBarycenter /= indexesToCheck; |
|
|
91 |
|
|
17
|
92 |
//Pour les points, on suit l'algorithme. |
|
|
93 |
|
|
|
94 |
//Si la distance moyenne de chaque point de la main gauche au barycentre gauche est trop faible |
|
|
95 |
//Alors la main gauche n'a pas décrit de cercle. |
|
|
96 |
if (averageDistToLeftBarycenter < refDistance / 2) |
|
|
97 |
leftHandDoCircle = false; |
|
|
98 |
|
|
|
99 |
//Indique si on a atteint le point d'arrivée pour la main gauche. |
|
|
100 |
bool endLeftReached = false; |
|
|
101 |
|
|
|
102 |
if(leftHandDoCircle) |
|
|
103 |
for (int i = localHistory.Count - indexesToCheck + 1; i < localHistory.Count; i++) |
|
|
104 |
{ |
|
|
105 |
//Si la distance d'un point de la main gauche excède à la distance moyenne au barycentre gauche avec une erreur N |
|
|
106 |
//OU si le point de départ de la main gauche est plus éloigné du point d'arrivée de la main gauche de N. |
|
|
107 |
//Alors la main gauche n'a pas décrit de cercle. |
|
|
108 |
float X = localHistory[i][(int)JointType.HandLeft].Position.X; |
|
|
109 |
float Y = localHistory[i][(int)JointType.HandLeft].Position.Y; |
|
|
110 |
|
|
|
111 |
//Si un point est proche du point de départ. |
|
|
112 |
if(Distance2D(X, Y, startPointLeft.X, startPointLeft.Y) < refDistance / 5 && X != startPointLeft.X && Y != startPointLeft.Y) |
|
|
113 |
endLeftReached = true; |
|
|
114 |
|
|
|
115 |
if (Math.Abs((double)Distance2D(X, Y, leftBarycenter.X, leftBarycenter.Y) - averageDistToLeftBarycenter) > refDistance / 5) |
|
|
116 |
{ |
|
|
117 |
leftHandDoCircle = false; |
|
|
118 |
break; |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
//Si la main gauche a atteint une position proche de son point de départ. |
|
|
122 |
if (endLeftReached) |
|
|
123 |
{ |
|
|
124 |
//S'il y a trop peu de points |
|
|
125 |
if (i - (localHistory.Count - indexesToCheck + 1) < (indexesToCheck / 2)) |
|
|
126 |
leftHandDoCircle = false; |
|
|
127 |
break; |
|
|
128 |
} |
|
|
129 |
} |
|
|
130 |
|
|
|
131 |
//On supprime l'historique local. |
|
|
132 |
localHistory.Clear(); |
|
|
133 |
//Si on est arrivé jusqu'ici, toutes les conditions pour un swipe left ont été remplies. |
|
|
134 |
return leftHandDoCircle; |
|
|
135 |
} |
|
|
136 |
|
|
|
137 |
/* |
|
|
138 |
* Lit les noeuds de l'historique du squelette afin de détecter un cercle. |
|
|
139 |
* Règles : |
|
|
140 |
* Se fait avec une main. |
|
|
141 |
* Chaque point est à la même distance du barycentre. |
|
|
142 |
* On regarde pour la main droite. |
|
|
143 |
*/ |
|
|
144 |
public bool CheckForRightCircle() |
|
|
145 |
{ |
|
|
146 |
//Indique si la main droite a décrit un cercle. |
|
|
147 |
bool rightHandDoCircle = true; |
|
|
148 |
|
|
|
149 |
//Crée un historique de squelette local, puisque l'historique est mis à jour toutes les ~1/30 s. |
|
|
150 |
List<List<Joint>> localHistory = new List<List<Joint>>(history); |
|
|
151 |
|
|
|
152 |
//if (Math.Abs(Math.Abs(localHistory[0][(int)JointType.HandLeft].Position.X - localHistory[0][(int)JointType.HandRight].Position.X) - refDistance) < 10) |
|
|
153 |
// Console.Out.WriteLine("REF"); |
|
|
154 |
|
|
|
155 |
//Si il n'y a pas assez de positions dans l'historique local pour vérifier le geste. |
|
|
156 |
if (localHistory.Count < indexesToCheck + 1) |
|
|
157 |
return false; |
|
|
158 |
|
|
|
159 |
//La distance de référence est ici la distance entre le milieu du dos et le milieu des épaules. |
|
|
160 |
refDistance = Math.Abs(localHistory[0][(int)JointType.Spine].Position.Y - localHistory[0][(int)JointType.ShoulderCenter].Position.Y); |
|
|
161 |
//On commence la position pour les indexesToCheck dernières postures (celle à l'index 0 étant la dernière). |
|
|
162 |
SkeletonPoint startPointRight = localHistory[localHistory.Count - indexesToCheck][(int)JointType.HandRight].Position; |
|
|
163 |
|
|
|
164 |
//Barycentres pour la main droite. |
|
|
165 |
PointF rightBarycenter = new PointF(0, 0); |
|
|
166 |
//Distances moyennes des points aux barycentres. |
|
|
167 |
float averageDistToRightBarycenter = 0; |
|
|
168 |
|
|
|
169 |
//Index du point de départ dans la détection. |
|
|
170 |
int beginIndex = localHistory.Count - indexesToCheck; |
|
|
171 |
|
|
|
172 |
//Index du point d'arrivée. |
|
|
173 |
int endRightIndex = 0; |
|
|
174 |
|
|
|
175 |
//On cherche le point d'arrivée. |
|
|
176 |
/*for (int i = beginIndex; i > 0; i--) |
|
|
177 |
{ |
|
|
178 |
float X = localHistory[i][(int)JointType.HandRight].Position.X; |
|
|
179 |
float Y = localHistory[i][(int)JointType.HandRight].Position.Y; |
|
|
180 |
|
|
|
181 |
//Si un point est proche du point de départ. |
|
|
182 |
if (Distance2D(X, Y, startPointRight.X, startPointRight.Y) < refDistance / 2 && X != startPointRight.X && Y != startPointRight.Y) |
|
|
183 |
{ |
|
|
184 |
Console.Out.WriteLine("REACHED"); |
|
|
185 |
endRightIndex = i; |
|
|
186 |
break; |
|
|
187 |
} |
|
|
188 |
}*/ |
|
|
189 |
|
|
|
190 |
/*//S'il n'y a pas assez de points. |
|
|
191 |
if (beginIndex - endRightIndex < indexesToCheck) |
|
|
192 |
return false;*/ |
|
|
193 |
|
|
|
194 |
//Calcul du barycentre de la main droite. |
|
|
195 |
for (int i = localHistory.Count - indexesToCheck + 1; i < localHistory.Count; i++) |
|
|
196 |
{ |
|
|
197 |
rightBarycenter.X += localHistory[i][(int)JointType.HandRight].Position.X; |
|
|
198 |
rightBarycenter.Y += localHistory[i][(int)JointType.HandRight].Position.Y; |
|
|
199 |
} |
|
|
200 |
rightBarycenter.X /= indexesToCheck; |
|
|
201 |
rightBarycenter.Y /= indexesToCheck; |
|
|
202 |
|
|
15
|
203 |
//Estimation de la distance moyenne d'un point au barycentre droit. |
|
17
|
204 |
for (int i = localHistory.Count - indexesToCheck + 1; i < localHistory.Count; i++) |
|
15
|
205 |
{ |
|
|
206 |
float ptX = localHistory[i][(int)JointType.HandRight].Position.X; |
|
|
207 |
float ptY = localHistory[i][(int)JointType.HandRight].Position.Y; |
|
|
208 |
averageDistToRightBarycenter += (float)Distance2D(ptX, rightBarycenter.X, ptY, rightBarycenter.Y); |
|
|
209 |
} |
|
|
210 |
averageDistToRightBarycenter /= indexesToCheck; |
|
|
211 |
|
|
17
|
212 |
//Pour les points, on suit l'algorithme. |
|
|
213 |
|
|
|
214 |
//Si la distance moyenne de chaque point de la main droite au barycentre droit est trop faible |
|
|
215 |
//Alors la main droite n'a pas décrit de cercle. |
|
|
216 |
if (averageDistToRightBarycenter < refDistance / 2) |
|
|
217 |
rightHandDoCircle = false; |
|
|
218 |
|
|
|
219 |
float globalPercent = 0; |
|
|
220 |
|
|
|
221 |
if (rightHandDoCircle) |
|
|
222 |
for (int i = localHistory.Count - indexesToCheck + 1; i < localHistory.Count; i++) |
|
|
223 |
{ |
|
|
224 |
//Si la distance moyenne de chaque point de la main droite au barycentre droit est trop faible |
|
|
225 |
//OU si la distance d'un point de la main droite excède à la distance moyenne au barycentre droit avec une erreur N |
|
|
226 |
//OU si le point de départ de la main gauche est plus éloigné du point d'arrivée de la main gauche de N. |
|
|
227 |
//Alors la main droite n'a pas décrit de cercle. |
|
15
|
228 |
|
|
17
|
229 |
float X = localHistory[i][(int)JointType.HandRight].Position.X; |
|
|
230 |
float Y = localHistory[i][(int)JointType.HandRight].Position.Y; |
|
|
231 |
float R = averageDistToRightBarycenter, r = (float)Math.Abs((double)Distance2D(X, Y, rightBarycenter.X, rightBarycenter.Y)); |
|
|
232 |
float percent = 0; |
|
|
233 |
|
|
|
234 |
if (r < R) |
|
|
235 |
percent = 100 * r / R; |
|
|
236 |
else |
|
|
237 |
percent = 100 * R / r; |
|
|
238 |
|
|
|
239 |
globalPercent += percent; |
|
15
|
240 |
|
|
17
|
241 |
/*if (Math.Abs((double)Distance2D(X, Y, rightBarycenter.X, rightBarycenter.Y) - averageDistToRightBarycenter) > refDistance / 5) |
|
|
242 |
{ |
|
|
243 |
//Console.Out.WriteLine("FAIL"); |
|
|
244 |
return false; |
|
|
245 |
}*/ |
|
|
246 |
} |
|
|
247 |
|
|
|
248 |
float res = ((float)globalPercent / indexesToCheck); |
|
|
249 |
|
|
|
250 |
Console.Out.WriteLine("p:" + ((float)globalPercent / indexesToCheck)); |
|
|
251 |
|
|
|
252 |
if (res >= 50) |
|
|
253 |
Console.In.Read(); |
|
15
|
254 |
|
|
|
255 |
//On supprime l'historique local. |
|
|
256 |
localHistory.Clear(); |
|
|
257 |
//Si on est arrivé jusqu'ici, toutes les conditions pour un swipe left ont été remplies. |
|
|
258 |
return true; |
|
|
259 |
} |
|
|
260 |
|
|
|
261 |
public double Distance2D(float x1, float x2, float y1, float y2) |
|
|
262 |
{ |
|
|
263 |
return Math.Sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)); |
|
|
264 |
} |
|
|
265 |
} |
|
|
266 |
} |