38 using Trakers.Communication; |
38 using Trakers.Communication; |
39 using System.IO; |
39 using System.IO; |
40 using Trakers.Tracking.Gestures; |
40 using Trakers.Tracking.Gestures; |
41 using Trakers.Tracking.Events; |
41 using Trakers.Tracking.Events; |
42 using System.Configuration; |
42 using System.Configuration; |
43 using Kinect.Toolbox; |
43 using System.Resources; |
|
44 using System.Reflection; |
44 |
45 |
45 namespace Trakers.Tracking |
46 namespace Trakers.Tracking |
46 { |
47 { |
47 //Il s'agit des fonctions permettant d'appeler les fonctions des événements Main droite/gauche entre/quitte le champ. |
48 //Il s'agit des fonctions permettant d'appeler les fonctions des événements Main droite/gauche entre/quitte le champ. |
48 public delegate void LeftHandTrackedHandler(object o, LeftHandTrackedEventArgs e); |
49 public delegate void LeftHandTrackedHandler(object o, LeftHandTrackedEventArgs e); |
49 public delegate void RightHandTrackedHandler(object o, RightHandTrackedEventArgs e); |
50 public delegate void RightHandTrackedHandler(object o, RightHandTrackedEventArgs e); |
50 public delegate void LeftHandQuitHandler(object o, LeftHandQuitEventArgs e); |
51 public delegate void LeftHandQuitHandler(object o, LeftHandQuitEventArgs e); |
51 public delegate void RightHandQuitHandler(object o, RightHandQuitEventArgs e); |
52 public delegate void RightHandQuitHandler(object o, RightHandQuitEventArgs e); |
52 //Il s'agit des fonctions permettant d'appeler les fonctions des événements Swipe left/right/up/down. |
53 //Il s'agit de la fonction permettant d'appeler les fonctions des événements Swipe left/right/up/down. |
53 public delegate void SwipeHandler(object o, SwipeEventArgs e); |
54 public delegate void SwipeHandler(object o, SwipeEventArgs e); |
|
55 //Il s'agit de la fonction permettant d'appeler les fonctions des événements Push/Pull. |
|
56 public delegate void PushHandler(object o, PushEventArgs e); |
|
57 //Il s'agit de la fonction permettant d'appeler les fonctions des événements Jump. |
|
58 public delegate void JumpHandler(object o, JumpEventArgs e); |
54 |
59 |
55 public class KinectMain |
60 public class KinectMain |
56 { |
61 { |
|
62 //Gestionnaire de ressources. |
|
63 private ResourceManager rm; |
57 //Fenêtre de debug. |
64 //Fenêtre de debug. |
58 private Debug.DebugWindow debug; |
65 private Debug.DebugWindow debug; |
59 //Squelettes (Il y en a 6 par défaut). |
66 //Squelettes (Il y en a 6 par défaut). |
60 private Skeleton[] skeletons; |
67 private Skeleton[] skeletons; |
61 //Caméra infrarouge (sensor) de la Kinect. |
68 //Caméra infrarouge (sensor) de la Kinect. |
62 private KinectSensor kinectSensor; |
69 private KinectSensor kinectSensor; |
63 |
70 |
64 //Détecteur de gestes. |
71 //Détecteur de swipes. |
65 private SwipeDetector swipeDetector; |
72 private SwipeDetector swipeDetector; |
|
73 //Détecteur de pushes. |
|
74 private PushDetector pushDetector; |
|
75 //Détecteur de jumps. |
|
76 private JumpDetector jumpDetector; |
66 |
77 |
67 //Distances min/max délimitant le champ de recherche. |
78 //Distances min/max délimitant le champ de recherche. |
68 private float minDistHands; |
79 private float minDistHands; |
69 private float maxDistHands; |
80 private float maxDistHands; |
|
81 |
|
82 //Temps de rafraichissement pour le timer (Détection de gesture dans le serveur TUIO). |
|
83 private int timerElapsing; |
70 |
84 |
71 //Serveur TUIO pour la connexion du Middleware vers le Front Atelier. |
85 //Serveur TUIO pour la connexion du Middleware vers le Front Atelier. |
72 private Server server; |
86 private Server server; |
73 |
87 |
74 //Les événements des mains pour la recherche. |
88 //Les événements des mains pour la recherche. |
75 public static event LeftHandTrackedHandler LeftHandTrackedEvent; |
89 public static event LeftHandTrackedHandler LeftHandTrackedEvent; |
76 public static event RightHandTrackedHandler RightHandTrackedEvent; |
90 public static event RightHandTrackedHandler RightHandTrackedEvent; |
77 public static event LeftHandQuitHandler LeftHandQuitEvent; |
91 public static event LeftHandQuitHandler LeftHandQuitEvent; |
78 public static event RightHandQuitHandler RightHandQuitEvent; |
92 public static event RightHandQuitHandler RightHandQuitEvent; |
79 //Les événements swipe. |
93 //L'événement swipe. |
80 public static event SwipeHandler SwipeEvent; |
94 public static event SwipeHandler SwipeEvent; |
|
95 //L'événement push. |
|
96 public static event PushHandler PushEvent; |
|
97 //L'événement jump. |
|
98 public static event JumpHandler JumpEvent; |
81 |
99 |
82 //Voici les ID des noeuds d'un squelette. |
100 //Voici les ID des noeuds d'un squelette. |
83 private int hipCenterID = 0, spineID = 1, shoulderCenterID = 2, headID = 3; |
101 public int hipCenterID = 0, spineID = 1, shoulderCenterID = 2, headID = 3; |
84 private int shoulderLeftID = 4, elbowLeftID = 5, wristLeftID = 6, handLeftID = 7; |
102 public int shoulderLeftID = 4, elbowLeftID = 5, wristLeftID = 6, handLeftID = 7; |
85 private int shoulderRightID = 8, elbowRightID = 9, wristRightID = 10, handRightID = 11; |
103 public int shoulderRightID = 8, elbowRightID = 9, wristRightID = 10, handRightID = 11; |
86 private int hipLeftID = 12, kneeLeftID = 13, ankleLeftID = 14, footLeftID = 15; |
104 public int hipLeftID = 12, kneeLeftID = 13, ankleLeftID = 14, footLeftID = 15; |
87 private int hipRightID = 16, kneeRightID = 17, ankleRightID = 18, footRightID = 19; |
105 public int hipRightID = 16, kneeRightID = 17, ankleRightID = 18, footRightID = 19; |
|
106 |
|
107 private string connexionHost; |
|
108 private int connexionPort; |
88 |
109 |
89 /* |
110 /* |
90 * Initialisation de la classe principale. |
111 * Initialisation de la classe principale. |
91 * Affiche l'écran de debug dans lequel on voit la distance à la Kinect, |
112 * Affiche l'écran de debug dans lequel on voit la distance à la Kinect, |
92 * les mains détectées et le squelette de l'utilisateur. |
113 * les mains détectées et le squelette de l'utilisateur. |
93 */ |
114 */ |
94 public KinectMain() |
115 public KinectMain() |
95 { |
116 { |
|
117 //Si on n'a pas fait appel au gestionnaire de ressources avant, on le fait là. |
|
118 if(rm == null) |
|
119 rm = new ResourceManager("Trakers.Properties.resources", Assembly.GetExecutingAssembly()); |
96 //On crée la fenêtre de debug. |
120 //On crée la fenêtre de debug. |
97 debug = new Debug.DebugWindow(this); |
121 debug = new Debug.DebugWindow(this); |
98 |
122 |
99 //On crée le détecteur de gestes. |
123 //On crée les détecteurs de gestes. |
100 swipeDetector = new SwipeDetector(); |
124 swipeDetector = new SwipeDetector(debug); |
|
125 pushDetector = new PushDetector(debug); |
|
126 jumpDetector = new JumpDetector(debug); |
101 |
127 |
102 //On tente de charger les paramètres du fichier params.ini. |
128 //On tente de charger les paramètres du fichier params.ini. |
103 //Si on n'y arrive pas, on affiche une erreur et on charge les paramètres par défaut. |
129 //Si on n'y arrive pas, on affiche une erreur et on charge les paramètres par défaut. |
104 if (!loadParameters()) |
130 if (!loadParameters()) |
105 { |
131 { |
106 debug.ExceptionLbl.Content = "Impossible de charger les paramètres. Paramètres par défaut."; |
132 debug.ExceptionLbl.Content = rm.GetString("loadParametersFail"); |
107 //Distances de détection des mains par défaut pour la recherche (ici de 1m à 2m de la Kinect). |
133 //Distances de détection des mains par défaut pour la recherche (ici de 1m à 2m de la Kinect). |
108 minDistHands = (float)1.0; |
134 minDistHands = (float)1.0; |
109 maxDistHands = (float)1.5; |
135 maxDistHands = (float)1.5; |
110 } |
136 connexionHost = "127.0.0.1"; |
111 else if (maxDistHands <= 0 || minDistHands <= 0 || maxDistHands > 4 || minDistHands > 4 || minDistHands >= maxDistHands) |
137 connexionPort = 80; |
112 { |
138 timerElapsing = 1000; |
113 debug.ExceptionLbl.Content = "Paramètres incorrects. Paramètres par défaut."; |
|
114 //Distances de détection des mains par défaut pour la recherche (ici de 1m à 2m de la Kinect). |
|
115 minDistHands = (float)1.0; |
|
116 maxDistHands = (float)1.5; |
|
117 } |
139 } |
118 |
140 |
119 //On affiche la fenêtre de debug. |
141 //On affiche la fenêtre de debug. |
120 try |
142 try |
121 { |
143 { |
122 debug.ShowDialog(); |
144 debug.ShowDialog(); |
123 } |
145 } |
124 catch(Exception){} |
146 catch(Exception){} |
|
147 } |
|
148 |
|
149 /* |
|
150 * Initialisation de la classe principale avec comme argument le gestionnaire de ressources. |
|
151 */ |
|
152 public KinectMain(ResourceManager _rm) : this() |
|
153 { |
|
154 rm = _rm; |
125 } |
155 } |
126 |
156 |
127 /* |
157 /* |
128 * Initialisation du sensor de la Kinect. |
158 * Initialisation du sensor de la Kinect. |
129 */ |
159 */ |
141 kinectSensor.SkeletonStream.Enable(); |
171 kinectSensor.SkeletonStream.Enable(); |
142 |
172 |
143 //Quand le Middleware reçoit des trames de la Kinect, on va dans cette fonction. |
173 //Quand le Middleware reçoit des trames de la Kinect, on va dans cette fonction. |
144 kinectSensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(AllFramesReady); |
174 kinectSensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(AllFramesReady); |
145 |
175 |
|
176 //On applique des paramètres d'ajustement pour le squelette. |
|
177 TransformSmoothParameters parameters = new TransformSmoothParameters(); |
|
178 parameters.Smoothing = 0.2f; |
|
179 parameters.Correction = 0.8f; |
|
180 parameters.Prediction = 0.0f; |
|
181 parameters.JitterRadius = 0.5f; |
|
182 parameters.MaxDeviationRadius = 0.5f; |
|
183 kinectSensor.SkeletonStream.Enable(parameters); |
146 //On démarre la Kinect. |
184 //On démarre la Kinect. |
147 kinectSensor.Start(); |
185 kinectSensor.Start(); |
148 debug.ExceptionLbl.Content = ""; |
186 debug.ExceptionLbl.Content = ""; |
149 } |
187 } |
150 catch (System.Exception) |
188 catch (System.Exception) |
151 { |
189 { |
152 debug.ExceptionLbl.Content = "Kinect non connectée."; |
190 debug.ExceptionLbl.Content = rm.GetString("KinectNotConnected"); |
153 } |
191 } |
154 |
192 |
155 //Pour les événements main gauche/droite entre dans/quitte le champ, on a 4 listeners. |
193 //Pour les événements main gauche/droite entre dans/quitte le champ, on a 4 listeners. |
156 //Fonction appelée lorsque la main gauche entre dans le champ de recherche. |
194 //Fonction appelée lorsque la main gauche entre dans le champ de recherche. |
157 LeftHandTrackedListener leftHandTrackedListener = new LeftHandTrackedListener(); |
195 LeftHandTrackedListener leftHandTrackedListener = new LeftHandTrackedListener(); |
289 Joint shoulderRight = getJoint(first, shoulderRightID), elbowRight = getJoint(first, elbowRightID), wristRight = getJoint(first, wristRightID), handRight = getJoint(first, handRightID); |
335 Joint shoulderRight = getJoint(first, shoulderRightID), elbowRight = getJoint(first, elbowRightID), wristRight = getJoint(first, wristRightID), handRight = getJoint(first, handRightID); |
290 Joint hipLeft = getJoint(first, hipLeftID), kneeLeft = getJoint(first, kneeLeftID), ankleLeft = getJoint(first, ankleLeftID), footLeft = getJoint(first, footLeftID); |
336 Joint hipLeft = getJoint(first, hipLeftID), kneeLeft = getJoint(first, kneeLeftID), ankleLeft = getJoint(first, ankleLeftID), footLeft = getJoint(first, footLeftID); |
291 Joint hipRight = getJoint(first, hipRightID), kneeRight = getJoint(first, kneeRightID), ankleRight = getJoint(first, ankleRightID), footRight = getJoint(first, footRightID); |
337 Joint hipRight = getJoint(first, hipRightID), kneeRight = getJoint(first, kneeRightID), ankleRight = getJoint(first, ankleRightID), footRight = getJoint(first, footRightID); |
292 |
338 |
293 //On construit l'historique des postures. |
339 //On construit l'historique des postures. |
294 /*List<Joint> joints = new List<Joint>(); |
340 List<Joint> joints = new List<Joint>(); |
295 joints.Clear(); |
341 joints.Clear(); |
296 joints.Add(footRight); |
342 joints.Insert(hipCenterID, hipCenter); |
297 joints.Add(ankleRight); |
343 joints.Insert(spineID, spine); |
298 joints.Add(kneeRight); |
344 joints.Insert(shoulderCenterID, shoulderCenter); |
299 joints.Add(hipRight); |
345 joints.Insert(headID, head); |
300 joints.Add(footLeft); |
346 joints.Insert(shoulderLeftID, shoulderLeft); |
301 joints.Add(ankleLeft); |
347 joints.Insert(elbowLeftID, elbowLeft); |
302 joints.Add(kneeLeft); |
348 joints.Insert(wristLeftID, wristLeft); |
303 joints.Add(hipLeft); |
349 joints.Insert(handLeftID, handLeft); |
304 joints.Add(handRight); |
350 joints.Insert(shoulderRightID, shoulderRight); |
305 joints.Add(wristRight); |
351 joints.Insert(elbowRightID, elbowRight); |
306 joints.Add(elbowRight); |
352 joints.Insert(wristRightID, wristRight); |
307 joints.Add(shoulderRight); |
353 joints.Insert(handRightID, handRight); |
308 joints.Add(handLeft); |
354 joints.Insert(hipLeftID, hipLeft); |
309 joints.Add(wristLeft); |
355 joints.Insert(kneeLeftID, kneeLeft); |
310 joints.Add(elbowLeft); |
356 joints.Insert(ankleLeftID, ankleLeft); |
311 joints.Add(shoulderLeft); |
357 joints.Insert(footLeftID, footLeft); |
312 joints.Add(head); |
358 joints.Insert(hipRightID, hipRight); |
313 joints.Add(shoulderCenter); |
359 joints.Insert(kneeRightID, kneeRight); |
314 joints.Add(spine); |
360 joints.Insert(ankleRightID, ankleRight); |
315 joints.Add(hipCenter); |
361 joints.Insert(footRightID, footRight); |
316 swipeDetector.UpdateSkeletonHistory(joints);*/ |
362 GestureDetector.UpdateSkeletonHistory(joints); |
317 |
363 |
318 SkeletonDisplayManager sdm = new SkeletonDisplayManager(kinectSensor, debug.DebugCanvas); |
|
319 //sdm.Draw(); |
|
320 |
|
321 //On obtient sa distance à la Kinect. |
364 //On obtient sa distance à la Kinect. |
322 float distance = first.Position.Z; |
365 float distance = first.Position.Z; |
323 |
366 |
324 //On affiche la distance dans le debug. |
367 //On affiche la distance dans le debug. |
325 debug.showDistance(distance); |
368 debug.showDistance(distance); |
354 { |
397 { |
355 SwipeEventArgs swipeEvent = new SwipeEventArgs(debug, server, SwipeDetector.Direction.LEFT); |
398 SwipeEventArgs swipeEvent = new SwipeEventArgs(debug, server, SwipeDetector.Direction.LEFT); |
356 OnSwipeEvent(swipeEvent); |
399 OnSwipeEvent(swipeEvent); |
357 } |
400 } |
358 |
401 |
|
402 //Si l'utilisateur effectue un swipe right. |
|
403 if (swipeDetector.CheckForSwipeRight()) |
|
404 { |
|
405 SwipeEventArgs swipeEvent = new SwipeEventArgs(debug, server, SwipeDetector.Direction.RIGHT); |
|
406 OnSwipeEvent(swipeEvent); |
|
407 } |
|
408 |
|
409 //Enum sur la main qui effectue le geste. |
|
410 PushDetector.Hand handPush; |
|
411 //Si l'utilisateur effectue un push. |
|
412 if ((handPush = pushDetector.CheckForPush()) != PushDetector.Hand.NONE) |
|
413 { |
|
414 PushEventArgs pushEvent = new PushEventArgs(debug, server, PushDetector.Direction.PUSH, handPush); |
|
415 OnPushEvent(pushEvent); |
|
416 } |
|
417 //Si l'utilisateur effectue un pull. |
|
418 if ((handPush = pushDetector.CheckForPull()) != PushDetector.Hand.NONE) |
|
419 { |
|
420 PushEventArgs pushEvent = new PushEventArgs(debug, server, PushDetector.Direction.PULL, handPush); |
|
421 OnPushEvent(pushEvent); |
|
422 } |
|
423 |
|
424 //Si l'utilisateur effectue un saut. |
|
425 /*if (jumpDetector.CheckForJump()) |
|
426 { |
|
427 JumpEventArgs jumpEvent = new JumpEventArgs(debug, server); |
|
428 OnJumpEvent(jumpEvent); |
|
429 }*/ |
359 |
430 |
360 //Dessine le squelette dans le debug. |
431 //Dessine le squelette dans le debug. |
361 //debug.drawJoints(first.Joints, first); |
432 debug.drawJoints(first.Joints, first); |
362 //debug.showSkeleton(hipCenter, spine, shoulderCenter, head, shoulderLeft, elbowLeft, wristLeft, handLeft, shoulderRight, elbowRight, wristRight, handRight, hipLeft, kneeLeft, ankleLeft, footLeft, hipRight, kneeRight, ankleRight, footRight); |
433 debug.showSkeleton(hipCenter, spine, shoulderCenter, head, shoulderLeft, elbowLeft, wristLeft, handLeft, shoulderRight, elbowRight, wristRight, handRight, hipLeft, kneeLeft, ankleLeft, footLeft, hipRight, kneeRight, ankleRight, footRight); |
363 } |
434 } |
364 } |
435 } |
365 |
436 |
366 /* |
437 /* |
367 * Change l'échelle des coordonnées d'un noeud pour qu'en X et Y il corresponde à la résolution et en Z à la distance à la Kinect. |
438 * Change l'échelle des coordonnées d'un noeud pour qu'en X et Y il corresponde à la résolution et en Z à la distance à la Kinect. |
415 if (SwipeEvent != null) |
486 if (SwipeEvent != null) |
416 SwipeEvent(new object(), e); |
487 SwipeEvent(new object(), e); |
417 } |
488 } |
418 |
489 |
419 /* |
490 /* |
420 * Initialise l'événement et fait appel aux fonctions du listener quand l'utilisateur effectue un swipe up. |
491 * Initialise l'événement et fait appel aux fonctions du listener quand l'utilisateur effectue un push. |
421 */ |
492 */ |
422 /*public static void OnSwipeUpEvent(SwipeUpEventArgs e) |
493 public static void OnPushEvent(PushEventArgs e) |
423 { |
494 { |
424 if (SwipeUpEvent != null) |
495 if (PushEvent != null) |
425 SwipeUpEvent(new object(), e); |
496 PushEvent(new object(), e); |
426 }*/ |
497 } |
427 |
498 |
428 /* |
499 /* |
429 * Initialise l'événement et fait appel aux fonctions du listener quand l'utilisateur effectue un swipe down. |
500 * Initialise l'événement et fait appel aux fonctions du listener quand l'utilisateur effectue un saut. |
430 */ |
501 */ |
431 /*public static void OnSwipeDownEvent(SwipeDownEventArgs e) |
502 public static void OnJumpEvent(JumpEventArgs e) |
432 { |
503 { |
433 if (SwipeDownEvent != null) |
504 if (JumpEvent != null) |
434 SwipeDownEvent(new object(), e); |
505 JumpEvent(new object(), e); |
435 }*/ |
506 } |
436 |
507 |
437 /* |
508 /* |
438 * Méthode de chargement des paramètres (position du champ de recherche...). |
509 * Méthode de chargement des paramètres (position du champ de recherche...). |
439 */ |
510 */ |
440 public bool loadParameters() |
511 public bool loadParameters() |
441 { |
512 { |
442 try |
513 try |
443 { |
514 { |
444 minDistHands = float.Parse(ConfigurationManager.AppSettings["searchMinDistance"]); |
515 minDistHands = (float)double.Parse(ConfigurationManager.AppSettings["searchMinDistance"]); |
445 maxDistHands = float.Parse(ConfigurationManager.AppSettings["searchMaxDistance"]); |
516 maxDistHands = (float)double.Parse(ConfigurationManager.AppSettings["searchMaxDistance"]); |
|
517 connexionHost = ConfigurationManager.AppSettings["connexionHost"]; |
|
518 connexionPort = int.Parse(ConfigurationManager.AppSettings["connexionPort"]); |
|
519 hipCenterID = int.Parse(ConfigurationManager.AppSettings["hipCenterID"]); |
|
520 spineID = int.Parse(ConfigurationManager.AppSettings["spineID"]); |
|
521 shoulderCenterID = int.Parse(ConfigurationManager.AppSettings["shoulderCenterID"]); |
|
522 headID = int.Parse(ConfigurationManager.AppSettings["headID"]); |
|
523 shoulderLeftID = int.Parse(ConfigurationManager.AppSettings["shoulderLeftID"]); |
|
524 elbowLeftID = int.Parse(ConfigurationManager.AppSettings["elbowLeftID"]); |
|
525 wristLeftID = int.Parse(ConfigurationManager.AppSettings["wristLeftID"]); |
|
526 handLeftID = int.Parse(ConfigurationManager.AppSettings["handLeftID"]); |
|
527 shoulderRightID = int.Parse(ConfigurationManager.AppSettings["shoulderRightID"]); |
|
528 elbowRightID = int.Parse(ConfigurationManager.AppSettings["elbowRightID"]); |
|
529 wristRightID = int.Parse(ConfigurationManager.AppSettings["wristRightID"]); |
|
530 handRightID = int.Parse(ConfigurationManager.AppSettings["handRightID"]); |
|
531 hipLeftID = int.Parse(ConfigurationManager.AppSettings["hipLeftID"]); |
|
532 kneeLeftID = int.Parse(ConfigurationManager.AppSettings["kneeLeftID"]); |
|
533 ankleLeftID = int.Parse(ConfigurationManager.AppSettings["ankleLeftID"]); |
|
534 footLeftID = int.Parse(ConfigurationManager.AppSettings["footLeftID"]); |
|
535 hipRightID = int.Parse(ConfigurationManager.AppSettings["hipRightID"]); |
|
536 kneeRightID = int.Parse(ConfigurationManager.AppSettings["kneeRightID"]); |
|
537 ankleRightID = int.Parse(ConfigurationManager.AppSettings["ankleRightID"]); |
|
538 footRightID = int.Parse(ConfigurationManager.AppSettings["footRightID"]); |
|
539 timerElapsing = int.Parse(ConfigurationManager.AppSettings["timerElapsing"]); |
446 } |
540 } |
447 catch (Exception) |
541 catch (Exception) |
448 { |
542 { |
449 return false; |
543 return false; |
450 } |
544 } |
451 |
545 |
|
546 if (maxDistHands <= 0 || minDistHands <= 0 || maxDistHands > 4 || minDistHands > 4 || minDistHands >= maxDistHands) |
|
547 { |
|
548 debug.ExceptionLbl.Content = rm.GetString("loadParametersIncorrect"); |
|
549 return false; |
|
550 } |
452 return true; |
551 return true; |
453 } |
552 } |
454 } |
553 } |
455 } |
554 } |