middleware/src/Tracking/KinectMain.cs
changeset 13 50de8e8f44d7
parent 11 a1bf0d21022e
child 14 10d5199d9874
equal deleted inserted replaced
12:a446351f08c0 13:50de8e8f44d7
    18  * Fonctionnalités : Récupère les trames de données de la Kinect, les squelettes détectés via le SDK 1.0 de Microsoft.
    18  * Fonctionnalités : Récupère les trames de données de la Kinect, les squelettes détectés via le SDK 1.0 de Microsoft.
    19  * Interprète ces trames de façon à afficher le flux vidéo couleurs, et récupérer la distance de l'utilisateur et les
    19  * Interprète ces trames de façon à afficher le flux vidéo couleurs, et récupérer la distance de l'utilisateur et les
    20  * noeuds de son squelette. Lance des événements lorsque la main gauche/droite entre dans/quitte le champ.
    20  * noeuds de son squelette. Lance des événements lorsque la main gauche/droite entre dans/quitte le champ.
    21  * Envoie des données au sous-module de debug de manière a afficher un retour visuel sur la position de l'utilisateur,
    21  * Envoie des données au sous-module de debug de manière a afficher un retour visuel sur la position de l'utilisateur,
    22  * son squelette, la détection de ses mains.
    22  * son squelette, la détection de ses mains.
       
    23  * Découpe l'interaction avec le middleware en différents modes.
    23  */
    24  */
    24 
    25 
    25 using System;
    26 using System;
    26 using System.Collections.Generic;
    27 using System.Collections.Generic;
    27 using System.Linq;
    28 using System.Linq;
    65     public delegate void PushHandler(object o, PushEventArgs e);
    66     public delegate void PushHandler(object o, PushEventArgs e);
    66     //Il s'agit de la fonction permettant d'appeler les fonctions des événements Jump.
    67     //Il s'agit de la fonction permettant d'appeler les fonctions des événements Jump.
    67     public delegate void JumpHandler(object o, JumpEventArgs e);
    68     public delegate void JumpHandler(object o, JumpEventArgs e);
    68     //Il s'agit de la fonction permettant d'appeler les fonctions des événements de proximité.
    69     //Il s'agit de la fonction permettant d'appeler les fonctions des événements de proximité.
    69     public delegate void UserPositionHandler(object o, UserPositionEventArgs e);
    70     public delegate void UserPositionHandler(object o, UserPositionEventArgs e);
    70     //Il s'agit de la fonctoin permettant de notifier le serveur en cas de changement de mode.
       
    71     public delegate void ModChangeHandler(object o, ModChangeEventArgs e);
       
    72 
    71 
    73     public class KinectMain
    72     public class KinectMain
    74     {
    73     {
    75         //Gestionnaire de ressources.
    74         //Gestionnaire de ressources.
    76         private ResourceManager rm;
    75         private ResourceManager rm;
    95         private float maxDistHands;
    94         private float maxDistHands;
    96         private float minDist;
    95         private float minDist;
    97         private float maxDist;
    96         private float maxDist;
    98         private float zeroPoint;
    97         private float zeroPoint;
    99         private int imagesToShow;
    98         private int imagesToShow;
       
    99         private int takenPoints;
       
   100         private int directionChangeTresholdXY;
       
   101         private float directionChangeTresholdZ;
   100 
   102 
   101         //Temps de rafraichissement pour le timer (Détection de gesture dans le serveur TUIO).
   103         //Temps de rafraichissement pour le timer (Détection de gesture dans le serveur TUIO).
   102         private int timerElapsing;
   104         private int timerElapsing;
   103 
   105 
   104         //Serveur TUIO pour la connexion du Middleware vers le Front Atelier.
   106         //Serveur TUIO pour la connexion du Middleware vers le Front Atelier.
   105         private Server server;
   107         private Server server;
       
   108 
       
   109         //Gestionnaire de modes.
       
   110         private ModeManagement modeManagement;
   106 
   111 
   107         //Les événements des mains pour la recherche.
   112         //Les événements des mains pour la recherche.
   108         public static event LeftHandTrackedHandler LeftHandTrackedEvent;
   113         public static event LeftHandTrackedHandler LeftHandTrackedEvent;
   109         public static event RightHandTrackedHandler RightHandTrackedEvent;
   114         public static event RightHandTrackedHandler RightHandTrackedEvent;
   110         public static event LeftHandQuitHandler LeftHandQuitEvent;
   115         public static event LeftHandQuitHandler LeftHandQuitEvent;
   115         public static event PushHandler PushEvent;
   120         public static event PushHandler PushEvent;
   116         //L'événement jump.
   121         //L'événement jump.
   117         public static event JumpHandler JumpEvent;
   122         public static event JumpHandler JumpEvent;
   118         //L'événement l'utilisateur se déplace dans la zone de détection.
   123         //L'événement l'utilisateur se déplace dans la zone de détection.
   119         public static event UserPositionHandler UserPositionEvent;
   124         public static event UserPositionHandler UserPositionEvent;
   120         //L'événement on change de mode.
       
   121         public static event ModChangeHandler ModChangeEvent;
       
   122 
   125 
   123         private string connexionHost;
   126         private string connexionHost;
   124         private int connexionPort;
   127         private int connexionPort;
   125 
   128 
   126         /*
   129         /*
   146                 zeroPoint = 1.7f;
   149                 zeroPoint = 1.7f;
   147                 connexionHost = "127.0.0.1";
   150                 connexionHost = "127.0.0.1";
   148                 connexionPort = 80;
   151                 connexionPort = 80;
   149                 timerElapsing = 1000;
   152                 timerElapsing = 1000;
   150                 imagesToShow = 25;
   153                 imagesToShow = 25;
       
   154                 takenPoints = 10;
       
   155                 directionChangeTresholdXY = 10;
       
   156                 directionChangeTresholdZ = 0.01f;
   151             }
   157             }
   152 
   158 
   153             //On crée la fenêtre de debug.
   159             //On crée la fenêtre de debug.
   154             debug = new Debug.DebugWindow(this);
   160             debug = new Debug.DebugWindow(this);
   155 
   161 
   243 
   249 
   244             //Fonction appelée lorsque l'utilisateur se déplace dans la zone de détection.
   250             //Fonction appelée lorsque l'utilisateur se déplace dans la zone de détection.
   245             UserPositionListener userPositionListener = new UserPositionListener();
   251             UserPositionListener userPositionListener = new UserPositionListener();
   246             UserPositionEvent += new UserPositionHandler(userPositionListener.ShowOnScreen);
   252             UserPositionEvent += new UserPositionHandler(userPositionListener.ShowOnScreen);
   247 
   253 
   248             //Fonction appelée lorsqu'on change de mode.
       
   249             ModChangeListener modChangeListener = new ModChangeListener();
       
   250             ModChangeEvent += new ModChangeHandler(modChangeListener.ShowOnScreen);
       
   251 
       
   252             //On connecte le serveur à l'adresse locale sur le port 80.
   254             //On connecte le serveur à l'adresse locale sur le port 80.
   253             server = new Server(connexionHost, connexionPort, timerElapsing, debug);
   255             server = new Server(connexionHost, connexionPort, timerElapsing, debug);
       
   256 
       
   257             //On crée le gestionnaire de modes.
       
   258             modeManagement = new ModeManagement(this, server, debug);
   254         }
   259         }
   255 
   260 
   256         /*
   261         /*
   257         *  Fermeture du sensor de la Kinect.
   262         *  Fermeture du sensor de la Kinect.
   258         */
   263         */
   398                     OnLeftHandTrackedEvent(leftHandTrackedEvent);
   403                     OnLeftHandTrackedEvent(leftHandTrackedEvent);
   399                 }
   404                 }
   400                 //Si la main gauche quitte le champ, on lance l'événement approprié.
   405                 //Si la main gauche quitte le champ, on lance l'événement approprié.
   401                 else
   406                 else
   402                 {
   407                 {
   403                     LeftHandQuitEventArgs leftHandQuitEvent = new LeftHandQuitEventArgs(handLeft, handLeft.Position.Z, debug, server);
   408                     LeftHandQuitEventArgs leftHandQuitEvent = new LeftHandQuitEventArgs(debug, server);
   404                     OnLeftHandQuitEvent(leftHandQuitEvent);
   409                     OnLeftHandQuitEvent(leftHandQuitEvent);
   405                 }
   410                 }
   406                 //Si la main droite est dans le champ, on lance l'événement approprié.
   411                 //Si la main droite est dans le champ, on lance l'événement approprié.
   407                 if (handRight.Position.Z < maxDistHands && handRight.Position.Z > minDistHands)
   412                 if (handRight.Position.Z < maxDistHands && handRight.Position.Z > minDistHands)
   408                 {
   413                 {
   410                     OnRightHandTrackedEvent(rightHandTrackedEvent);
   415                     OnRightHandTrackedEvent(rightHandTrackedEvent);
   411                 }
   416                 }
   412                 //Si la main droite quitte le champ, on lance l'événement approprié.
   417                 //Si la main droite quitte le champ, on lance l'événement approprié.
   413                 else
   418                 else
   414                 {
   419                 {
   415                     RightHandQuitEventArgs rightHandQuitEvent = new RightHandQuitEventArgs(handRight, handRight.Position.Z, debug, server);
   420                     RightHandQuitEventArgs rightHandQuitEvent = new RightHandQuitEventArgs(debug, server);
   416                     OnRightHandQuitEvent(rightHandQuitEvent);
   421                     OnRightHandQuitEvent(rightHandQuitEvent);
   417                 }
   422                 }
   418 
   423 
   419                 //Si l'utilisateur effectue un swipe left.
   424                 //Si l'utilisateur effectue un swipe left.
   420                 if (swipeDetector.CheckForSwipeLeft())
   425                 if (swipeDetector.CheckForSwipeLeft())
   456                 //On traite le problème en plusieurs limites, on discrétise la zone.
   461                 //On traite le problème en plusieurs limites, on discrétise la zone.
   457                 if (first.TrackingState == SkeletonTrackingState.Tracked)
   462                 if (first.TrackingState == SkeletonTrackingState.Tracked)
   458                 {
   463                 {
   459                     float proximity = userPositionDetector.CalcProximity(first.Position.Z);
   464                     float proximity = userPositionDetector.CalcProximity(first.Position.Z);
   460                     int numberOfImages = userPositionDetector.ImagesToShow(proximity, imagesToShow);
   465                     int numberOfImages = userPositionDetector.ImagesToShow(proximity, imagesToShow);
       
   466 
       
   467                     modeManagement.DetectProximityBasedModes(proximity, numberOfImages);
   461 
   468 
   462                     if (proximity > 0f)
   469                     if (proximity > 0f)
   463                     {
   470                     {
   464                         UserPositionEventArgs userPositionEvent = new UserPositionEventArgs(debug, server, proximity, numberOfImages);
   471                         UserPositionEventArgs userPositionEvent = new UserPositionEventArgs(debug, server, proximity, numberOfImages);
   465                         OnUserPositionEvent(userPositionEvent);
   472                         OnUserPositionEvent(userPositionEvent);
   466                     }
   473                     }
   467                     else
   474                     else
   468                     {
   475                     {
   469                         debug.hideSkeleton();
   476                         debug.hideSkeleton();
       
   477                         LeftHandQuitEventArgs leftHandQuitEvent = new LeftHandQuitEventArgs(debug, server);
       
   478                         OnLeftHandQuitEvent(leftHandQuitEvent);
       
   479                         RightHandQuitEventArgs rightHandQuitEvent = new RightHandQuitEventArgs(debug, server);
       
   480                         OnRightHandQuitEvent(rightHandQuitEvent);
   470                     }
   481                     }
   471                 }
   482                 }
   472 
   483 
   473                 //Dessine le squelette dans le debug.
   484                 //Dessine le squelette dans le debug.
   474                 debug.drawJoints(first.Joints, first);
   485                 debug.drawJoints(first.Joints, first);
   475                 debug.showSkeleton(hipCenter, spine, shoulderCenter, head, shoulderLeft, elbowLeft, wristLeft, handLeft, shoulderRight, elbowRight, wristRight, handRight, hipLeft, kneeLeft, ankleLeft, footLeft, hipRight, kneeRight, ankleRight, footRight);
   486                 debug.showSkeleton(hipCenter, spine, shoulderCenter, head, shoulderLeft, elbowLeft, wristLeft, handLeft, shoulderRight, elbowRight, wristRight, handRight, hipLeft, kneeLeft, ankleLeft, footLeft, hipRight, kneeRight, ankleRight, footRight);
   476             }
   487             }
   477             else
   488             else
       
   489             {
   478                 debug.hideSkeleton();
   490                 debug.hideSkeleton();
       
   491                 LeftHandQuitEventArgs leftHandQuitEvent = new LeftHandQuitEventArgs(debug, server);
       
   492                 OnLeftHandQuitEvent(leftHandQuitEvent);
       
   493                 RightHandQuitEventArgs rightHandQuitEvent = new RightHandQuitEventArgs(debug, server);
       
   494                 OnRightHandQuitEvent(rightHandQuitEvent);
       
   495             }
   479         }
   496         }
   480 
   497 
   481         /*
   498         /*
   482         *  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.
   499         *  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.
   483         */
   500         */
   555         */
   572         */
   556         public static void OnUserPositionEvent(UserPositionEventArgs e)
   573         public static void OnUserPositionEvent(UserPositionEventArgs e)
   557         {
   574         {
   558             if (UserPositionEvent != null)
   575             if (UserPositionEvent != null)
   559                 UserPositionEvent(new object(), e);
   576                 UserPositionEvent(new object(), e);
   560         }
       
   561 
       
   562         /*
       
   563         *  Initialise l'événement et fait appel aux fonctions du listener quand on change de mode.
       
   564         */
       
   565         public static void OnModChangeEvent(ModChangeEventArgs e)
       
   566         {
       
   567             if (ModChangeEvent != null)
       
   568                 ModChangeEvent(new object(), e);
       
   569         }
   577         }
   570 
   578 
   571         /*
   579         /*
   572         *  Méthode de chargement des paramètres (position du champ de recherche...).
   580         *  Méthode de chargement des paramètres (position du champ de recherche...).
   573         */
   581         */
   582                 zeroPoint = (float)double.Parse(ConfigurationManager.AppSettings["zeroPoint"]);
   590                 zeroPoint = (float)double.Parse(ConfigurationManager.AppSettings["zeroPoint"]);
   583                 connexionHost = ConfigurationManager.AppSettings["connexionHost"];
   591                 connexionHost = ConfigurationManager.AppSettings["connexionHost"];
   584                 connexionPort = int.Parse(ConfigurationManager.AppSettings["connexionPort"]);
   592                 connexionPort = int.Parse(ConfigurationManager.AppSettings["connexionPort"]);
   585                 timerElapsing = int.Parse(ConfigurationManager.AppSettings["timerElapsing"]);
   593                 timerElapsing = int.Parse(ConfigurationManager.AppSettings["timerElapsing"]);
   586                 imagesToShow = int.Parse(ConfigurationManager.AppSettings["imagesToShow"]);
   594                 imagesToShow = int.Parse(ConfigurationManager.AppSettings["imagesToShow"]);
       
   595                 takenPoints = int.Parse(ConfigurationManager.AppSettings["takenPoints"]);
       
   596                 directionChangeTresholdXY = int.Parse(ConfigurationManager.AppSettings["directionChangeTresholdXY"]);
       
   597                 directionChangeTresholdZ = (float)double.Parse(ConfigurationManager.AppSettings["directionChangeTresholdZ"]);
   587             }
   598             }
   588             catch (Exception)
   599             catch (Exception)
   589             {
   600             {
   590                 return false;
   601                 return false;
   591             }
   602             }
   592 
   603 
   593             if (maxDistHands <= 0f || minDistHands <= 0f || maxDistHands > maxDist || minDistHands > maxDist ||
   604             if (maxDistHands <= 0f || minDistHands <= 0f || maxDistHands > maxDist || minDistHands > maxDist ||
   594                 minDistHands >= maxDistHands || zeroPoint < maxDistHands || minDistHands < minDist ||
   605                 minDistHands >= maxDistHands || zeroPoint < maxDistHands || minDistHands < minDist ||
   595                 zeroPoint >= maxDist || connexionPort < 0 || timerElapsing < 0 || imagesToShow < 1)
   606                 zeroPoint >= maxDist || connexionPort < 0 || timerElapsing < 0 || imagesToShow < 1 ||
       
   607                 takenPoints <= 0 || directionChangeTresholdXY < 0 || directionChangeTresholdZ < 0)
   596             {
   608             {
   597                 debug.ExceptionLbl.Content = rm.GetString("loadParametersIncorrect");
   609                 debug.ExceptionLbl.Content = rm.GetString("loadParametersIncorrect");
   598                 return false;
   610                 return false;
   599             }
   611             }
   600             return true;
   612             return true;
   604          * Met à jour les nouveaux paramètres dans la configuration.
   616          * Met à jour les nouveaux paramètres dans la configuration.
   605          */
   617          */
   606         public void updateParameters()
   618         public void updateParameters()
   607         {
   619         {
   608             userPositionDetector.setParams(minDist, maxDist, minDistHands, maxDistHands, zeroPoint);
   620             userPositionDetector.setParams(minDist, maxDist, minDistHands, maxDistHands, zeroPoint);
       
   621             //segmenter.setParams(takenPoints, directionChangeTresholdXY, directionChangeTresholdZ);
   609 
   622 
   610             //On récupère la config.
   623             //On récupère la config.
   611             Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
   624             Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
   612             //On met à jour.
   625             //On met à jour.
   613             config.AppSettings.Settings.Remove("searchMinDistance");
   626             config.AppSettings.Settings.Remove("searchMinDistance");
   626             config.AppSettings.Settings.Add("connexionPort", connexionPort.ToString());
   639             config.AppSettings.Settings.Add("connexionPort", connexionPort.ToString());
   627             config.AppSettings.Settings.Remove("timerElapsing");
   640             config.AppSettings.Settings.Remove("timerElapsing");
   628             config.AppSettings.Settings.Add("timerElapsing", timerElapsing.ToString());
   641             config.AppSettings.Settings.Add("timerElapsing", timerElapsing.ToString());
   629             config.AppSettings.Settings.Remove("imagesToShow");
   642             config.AppSettings.Settings.Remove("imagesToShow");
   630             config.AppSettings.Settings.Add("imagesToShow", imagesToShow.ToString());
   643             config.AppSettings.Settings.Add("imagesToShow", imagesToShow.ToString());
       
   644             config.AppSettings.Settings.Remove("takenPoints");
       
   645             config.AppSettings.Settings.Add("takenPoints", takenPoints.ToString());
       
   646             config.AppSettings.Settings.Remove("directionChangeTresholdXY");
       
   647             config.AppSettings.Settings.Add("directionChangeTresholdXY", directionChangeTresholdXY.ToString());
       
   648             config.AppSettings.Settings.Remove("directionChangeTresholdZ");
       
   649             config.AppSettings.Settings.Add("directionChangeTresholdZ", directionChangeTresholdZ.ToString());
   631 
   650 
   632             //Sauvegarde la configuration.
   651             //Sauvegarde la configuration.
   633             config.Save(ConfigurationSaveMode.Modified);
   652             config.Save(ConfigurationSaveMode.Modified);
   634             ConfigurationManager.RefreshSection("appSettings");
   653             ConfigurationManager.RefreshSection("appSettings");
   635         }
   654         }
   671         }
   690         }
   672         public void setImagesToShow(int _imagesToShow)
   691         public void setImagesToShow(int _imagesToShow)
   673         {
   692         {
   674             imagesToShow = _imagesToShow;
   693             imagesToShow = _imagesToShow;
   675         }
   694         }
       
   695         public void setTakenPoints(int _takenPoints)
       
   696         {
       
   697             takenPoints = _takenPoints;
       
   698         }
       
   699         public void setDirectionChangeTresholdXY(int _directionChangeTresholdXY)
       
   700         {
       
   701             directionChangeTresholdXY = _directionChangeTresholdXY;
       
   702         }
       
   703         public void setDirectionChangeTresholdZ(float _directionChangeTresholdZ)
       
   704         {
       
   705             directionChangeTresholdZ = _directionChangeTresholdZ;
       
   706         }
   676 
   707 
   677         public float getMinDistHands()
   708         public float getMinDistHands()
   678         {
   709         {
   679             return minDistHands;
   710             return minDistHands;
   680         }
   711         }
   707             return timerElapsing;
   738             return timerElapsing;
   708         }
   739         }
   709         public int getImagesToShow()
   740         public int getImagesToShow()
   710         {
   741         {
   711             return imagesToShow;
   742             return imagesToShow;
       
   743         }
       
   744         public int getTakenPoints()
       
   745         {
       
   746             return takenPoints;
       
   747         }
       
   748         public int getDirectionChangeTresholdXY()
       
   749         {
       
   750             return directionChangeTresholdXY;
       
   751         }
       
   752         public float getDirectionChangeTresholdZ()
       
   753         {
       
   754             return directionChangeTresholdZ;
   712         }
   755         }
   713     }
   756     }
   714 }
   757 }