Ejemplo n.º 1
0
        public void Start()
        {
            context = SynchronizationContext.Current;

            CancellationToken token = cancellationTokenSource.Token;

            Task.Factory.StartNew(() =>
            {
                foreach (ReplaySkeletonFrame frame in frames)
                {
                    Thread.Sleep(TimeSpan.FromMilliseconds(frame.TimeStamp));

                    if (token.IsCancellationRequested)
                    {
                        return;
                    }

                    ReplaySkeletonFrame closure = frame;
                    context.Send(state =>
                    {
                        if (SkeletonFrameReady != null)
                        {
                            SkeletonFrameReady(this, new ReplaySkeletonFrameReadyEventArgs {
                                SkeletonFrame = closure
                            });
                        }
                    }, null);
                }
            }, token);
        }
        public void Draw(ReplaySkeletonFrame frame)
        {
            rootCanvas.Children.Clear();
            foreach (Skeleton skeleton in frame.Skeletons)
            {
                if (skeleton.TrackingState != SkeletonTrackingState.Tracked)
                    continue;

                Plot(JointType.HandLeft, skeleton.Joints);
                Trace(JointType.HandLeft, JointType.WristLeft, skeleton.Joints);
                Plot(JointType.WristLeft, skeleton.Joints);
                Trace(JointType.WristLeft, JointType.ElbowLeft, skeleton.Joints);
                Plot(JointType.ElbowLeft, skeleton.Joints);
                Trace(JointType.ElbowLeft, JointType.ShoulderLeft, skeleton.Joints);
                Plot(JointType.ShoulderLeft, skeleton.Joints);
                Trace(JointType.ShoulderLeft, JointType.ShoulderCenter, skeleton.Joints);
                Plot(JointType.ShoulderCenter, skeleton.Joints);

                Trace(JointType.ShoulderCenter, JointType.Head, skeleton.Joints);

                Plot(JointType.Head, JointType.ShoulderCenter, skeleton.Joints);

                Trace(JointType.ShoulderCenter, JointType.ShoulderRight, skeleton.Joints);
                Plot(JointType.ShoulderRight, skeleton.Joints);
                Trace(JointType.ShoulderRight, JointType.ElbowRight, skeleton.Joints);
                Plot(JointType.ElbowRight, skeleton.Joints);
                Trace(JointType.ElbowRight, JointType.WristRight, skeleton.Joints);
                Plot(JointType.WristRight, skeleton.Joints);
                Trace(JointType.WristRight, JointType.HandRight, skeleton.Joints);
                Plot(JointType.HandRight, skeleton.Joints);

                Trace(JointType.ShoulderCenter, JointType.Spine, skeleton.Joints);
                Plot(JointType.Spine, skeleton.Joints);
                Trace(JointType.Spine, JointType.HipCenter, skeleton.Joints);
                Plot(JointType.HipCenter, skeleton.Joints);

                Trace(JointType.HipCenter, JointType.HipLeft, skeleton.Joints);
                Plot(JointType.HipLeft, skeleton.Joints);
                Trace(JointType.HipLeft, JointType.KneeLeft, skeleton.Joints);
                Plot(JointType.KneeLeft, skeleton.Joints);
                Trace(JointType.KneeLeft, JointType.AnkleLeft, skeleton.Joints);
                Plot(JointType.AnkleLeft, skeleton.Joints);
                Trace(JointType.AnkleLeft, JointType.FootLeft, skeleton.Joints);
                Plot(JointType.FootLeft, skeleton.Joints);

                Trace(JointType.HipCenter, JointType.HipRight, skeleton.Joints);
                Plot(JointType.HipRight, skeleton.Joints);
                Trace(JointType.HipRight, JointType.KneeRight, skeleton.Joints);
                Plot(JointType.KneeRight, skeleton.Joints);
                Trace(JointType.KneeRight, JointType.AnkleRight, skeleton.Joints);
                Plot(JointType.AnkleRight, skeleton.Joints);
                Trace(JointType.AnkleRight, JointType.FootRight, skeleton.Joints);
                Plot(JointType.FootRight, skeleton.Joints);
            }
        }
Ejemplo n.º 3
0
        public SkeletonReplay(Stream stream)
        {
            BinaryReader reader = new BinaryReader(stream);

            int frameNumber = 0;

            while (reader.BaseStream.Position != reader.BaseStream.Length)
            {
                ReplaySkeletonFrame frame = new ReplaySkeletonFrame(reader, frameNumber++);

                frames.Add(frame);
            }

            reader.Dispose();
            stream.Dispose();
        }
Ejemplo n.º 4
0
        public SkeletonReplay(Stream stream)
        {
            BinaryReader reader = new BinaryReader(stream);

            int frameNumber = 0;

            while (reader.BaseStream.Position != reader.BaseStream.Length)
            {
                ReplaySkeletonFrame frame = new ReplaySkeletonFrame(reader, frameNumber++);

                frames.Add(frame);
            }

            reader.Dispose();
            stream.Dispose();
        }
Ejemplo n.º 5
0
        void ProcessFrame(ReplaySkeletonFrame frame)
        {
            Dictionary<int, string> stabilities = new Dictionary<int, string>();
            foreach (var skeleton in frame.Skeletons)
            {
                if (skeleton.TrackingState != SkeletonTrackingState.Tracked)
                    continue;

                barycenterHelper.Add(skeleton.Position.ToVector3(), skeleton.TrackingID);

                stabilities.Add(skeleton.TrackingID, barycenterHelper.IsStable(skeleton.TrackingID) ? "Stable" : "Unstable");
                if (!barycenterHelper.IsStable(skeleton.TrackingID))
                    continue;

                if (recordNextFrameForPosture)
                {
                    recordNextFrameForPosture = false;
                    templatePostureDetector.AddTemplate(skeleton);
                }

                foreach (Joint joint in skeleton.Joints)
                {
                    if (joint.Position.W < 0.8f || joint.TrackingState != JointTrackingState.Tracked)
                        continue;

                    if (joint.ID == JointID.HandRight)
                    {
                        swipeGestureRecognizer.Add(joint.Position, kinectRuntime.SkeletonEngine);
                        circleGestureRecognizer.Add(joint.Position, kinectRuntime.SkeletonEngine);
                    }
                }

                algorithmicPostureRecognizer.TrackPostures(skeleton);
                templatePostureDetector.TrackPostures(skeleton);
            }

            skeletonDisplayManager.Draw(frame);

            stabilitiesList.ItemsSource = stabilities;

            currentPosture.Text = "Current posture: " + algorithmicPostureRecognizer.CurrentPosture.ToString();
        }
Ejemplo n.º 6
0
        void ReplayFrame(ReplayDepthImageFrame df, ReplayColorImageFrame cf,
        ReplaySkeletonFrame sf)
        {
            if (df != null) {
            labelFrameNumber.Content = df.FrameNumber.ToString();
            if (gtReplayer != null) {
              var data = gtReplayer.GetDataFrame(df.FrameNumber);
              if (data != null) {
            UpdateGroundTruthDisplay(data);
              }
            }
              }
              colorManager.Update(cf, !displayDebug);
              depthManager.Update(df);
              UpdateSkeletonDisplay(sf);
              if (handTracker != null && recogEngine != null) {
            var result = handTracker.Update(depthManager.PixelData, colorManager.PixelData,
            SkeletonUtil.FirstTrackedSkeleton(sf.Skeletons));
            var gesture = recogEngine.Update(result, viewHog);

            lock (inputServer)
              inputServer.Send(gesture);

            textGestureEvent.Text = gesture;
            fpsCounter.LogFPS();
            UpdateDisplay(result);
              }
        }
Ejemplo n.º 7
0
 public ReplayAllFrames()
 {
     SkeletonFrame   = new ReplaySkeletonFrame();
     DepthImageFrame = new ReplayDepthImageFrame();
     ColorImageFrame = new ReplayColorImageFrame();
 }
Ejemplo n.º 8
0
 public ReplayAllFrames()
 {
     SkeletonFrame  = new ReplaySkeletonFrame();
       DepthImageFrame = new ReplayDepthImageFrame();
       ColorImageFrame = new ReplayColorImageFrame();
 }
Ejemplo n.º 9
0
        private void ProcessFrame(ReplaySkeletonFrame frame)
        {
            foreach (var skeleton in frame.Skeletons)
            {
                if (skeleton.TrackingState != SkeletonTrackingState.Tracked)
                    continue;

                foreach (Joint joint in skeleton.Joints)
                {
                    if (joint.TrackingState != JointTrackingState.Tracked)
                        continue;

                    if (joint.JointType == JointType.HandRight)
                    {
                        this.listener.AddSwipe(joint.Position, this.kinect);
                    }
                    else if (joint.JointType == JointType.HandLeft)
                    {
                        this.listener.AddCircle(joint.Position, this.kinect);
                    }
                }

                this.listener.TrackPosture(skeleton);
            }
        }
Ejemplo n.º 10
0
        void ProcessFrame(ReplaySkeletonFrame frame)
        {
            Dictionary<int, string> stabilities = new Dictionary<int, string>();
            foreach (var skeleton in frame.Skeletons)
            {
                if (skeleton.TrackingState != SkeletonTrackingState.Tracked)
                    continue;

                //if (eyeTracker == null)
                //    eyeTracker = new EyeTracker(kinectSensor);

                //eyeTracker.Track(skeleton);

                contextTracker.Add(skeleton.Position.ToVector3(), skeleton.TrackingId);
                stabilities.Add(skeleton.TrackingId, contextTracker.IsStableRelativeToCurrentSpeed(skeleton.TrackingId) ? "Stable" : "Non stable");
                if (!contextTracker.IsStableRelativeToCurrentSpeed(skeleton.TrackingId))
                    continue;

                //if (eyeTracker.IsLookingToSensor.HasValue && eyeTracker.IsLookingToSensor == false)
                //    continue;

                foreach (Joint joint in skeleton.Joints)
                {
                    if (joint.TrackingState != JointTrackingState.Tracked)
                        continue;

                    if (joint.JointType == JointType.HandRight)
                    {
                        circleGestureRecognizer.Add(joint.Position, kinectSensor);
                    }
                    else if (joint.JointType == JointType.HandLeft)
                    {
                        swipeGestureRecognizer.Add(joint.Position, kinectSensor);
                        if (controlMouse.IsChecked == true)
                            MouseController.Current.SetHandPosition(kinectSensor, joint, skeleton);
                    }
                }

                algorithmicPostureRecognizer.TrackPostures(skeleton);
                templatePostureDetector.TrackPostures(skeleton);

                if (recordNextFrameForPosture)
                {
                    templatePostureDetector.AddTemplate(skeleton);
                    recordNextFrameForPosture = false;
                }
            }

            skeletonDisplayManager.Draw(frame.Skeletons, seatedMode.IsChecked == true);

            stabilitiesList.ItemsSource = stabilities;
        }
Ejemplo n.º 11
0
        void ProcessFrame(ReplaySkeletonFrame frame)
        {
            Dictionary<int, string> stabilities = new Dictionary<int, string>();
            foreach (var skeleton in frame.Skeletons)
            {
                if (skeleton.TrackingState != SkeletonTrackingState.Tracked)
                    continue;

                barycenterHelper.Add(skeleton.Position.ToVector3(), skeleton.TrackingId);
                if (!barycenterHelper.IsStable(skeleton.TrackingId))
                    return;

                foreach (Joint joint in skeleton.Joints)
                {
                    if (joint.TrackingState != JointTrackingState.Tracked)
                        continue;

                    if (joint.JointType == JointType.HandRight)
                    {
                        swipeGestureRecognizer.Add(joint.Position, kinectSensor);
                        circleDetector.Add(joint.Position, kinectSensor);
                    }
                }

                algorithmicPostureRecognizer.TrackPostures(skeleton);
                templatePostureDetector.TrackPostures(skeleton);

                if (recordNextFrameForPosture)
                {
                    templatePostureDetector.AddTemplate(skeleton);
                    recordNextFrameForPosture = false;
                }
            }

            skeletonDisplayManager.Draw(frame);

            stabilitiesList.ItemsSource = stabilities;

            currentPosture.Text = "Current posture: " + algorithmicPostureRecognizer.CurrentPosture;
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Determines if the frame is stable, if so process it
 /// </summary>
 /// <param name="frame"></param>
 void ProcessFrame(ReplaySkeletonFrame frame)
 {
     Dictionary<int, string> stabilities = new Dictionary<int, string>();
     foreach (var skeleton in frame.Skeletons)
     {
         if (skeleton.TrackingState != SkeletonTrackingState.Tracked)
             continue;
         _gestureDetector.Add(skeleton, _kinectSensor);
         barycenterHelper.Add(skeleton.Position.ToVector3(), skeleton.TrackingId);
         if (!barycenterHelper.IsStable(skeleton.TrackingId))
             return;
     }
 
 }
Ejemplo n.º 13
0
        void ProcessFrame(ReplaySkeletonFrame frame)
        {
            Dictionary<int, string> stabilities = new Dictionary<int, string>();
            foreach (var skeleton in frame.Skeletons)
            {
                if (skeleton.TrackingState != SkeletonTrackingState.Tracked)
                    continue;

                barycenterHelper.Add(skeleton.Position.ToVector3(), skeleton.TrackingId);
                if (!barycenterHelper.IsStable(skeleton.TrackingId))
                    return;
                _gestureSuite.Add(skeleton, _kinectSensor);
            }

            skeletonDisplayManager.Draw(frame);

            stabilitiesList.ItemsSource = stabilities;

          
        }
        void ProcessFrame(ReplaySkeletonFrame frame)
        {
            foreach(var skeleton in frame.Skeletons)
            {
                if(skeleton.TrackingState != SkeletonTrackingState.Tracked)
                    continue;

                barycenterHelper.Add(skeleton.Position.ToVector3(), skeleton.TrackingId);

                if(!barycenterHelper.IsStable(skeleton.TrackingId))
                    return;

                foreach(Joint joint in skeleton.Joints)
                {
                    if(joint.TrackingState != JointTrackingState.Tracked)
                        continue;

                    int positionInit = villes.getPositionElement("paris");

                     if (joint.JointType == JointType.HandRight)
                     {
                        sonDebutDeJeu.LoadedBehavior = MediaState.Play;
                        int positionVilleDansTableau = villes.getPositionElement();
                        swipeGestureReconnaissance.Add(joint.Position, kinectSensor);
                        hand.SetPosition(joint);

                        if (btn1.Check(hand))
                        {
                            Console.WriteLine("la main est dans la zone de droite et sur le bouton principal");

                            if (swipefronttoback)
                            {
                                btn1.BackgroundColor = Brushes.Chocolate;
                                btn1.SetPosition(joint);

                            }

                        }

                        if (swipebacktofront)
                        {
                           // zone.BackgroundColor = Brushes.CadetBlue;
                            zone.Opacity = 1;
                            zone.Text = btn1.Text;
                            zone.TextColor = Brushes.Black;
                            BoutonPositionInitial();

                            lesZonesDepots.verifierDoublonsOnMap(zone);
                            if (lesZonesDepots.reponse().Equals("Maintenant que les villes sont placées. Vous pouvez demander la correction"))
                            {
                                sonToutesLesVillesPlacees.Position = new TimeSpan(0);
                                sonToutesLesVillesPlacees.LoadedBehavior = MediaState.Play;
                            }
                            swipefronttoback = false;
                            swipebacktofront = false;
                        }

                        if (ZonePoubelle.Check(hand))
                        {
                            BoutonPositionInitial();
                            swipefronttoback = false;

                        }
                        if (zoneDefilementBoutonVille.Check(hand))//si la main est dans la zone de droite avec les boutons
                        {

                            if (swipebottom)
                            {
                                sonVent.Position = new TimeSpan(0);
                                sonVent.LoadedBehavior = MediaState.Play;

                                suivant.Text = btn1.Text;
                                btn1.Text = precedent.Text;
                                precedent.Text = villes.recupererNomVille(btn1.Text, "next");
                                informationMiageVille();
                                Console.WriteLine("main droite defilement vers le bas");

                                swipebottom = false;
                            }

                        }

                    }

                     if (joint.JointType == JointType.HandLeft)
                     {
                         mainGauche = true;
                         jointMainGauche = joint;
                         Console.WriteLine("main gauche trouvée");

                     }
                     if (joint.JointType == JointType.Head)
                     {
                         tete = true;
                         jointTete = joint;
                         Console.WriteLine("tete trouvée");
                     }
                     Console.WriteLine("coucou");
                     zone = GetZone();
                     if (tete && mainGauche && zone!= null && zone.Check(hand) && btn1.Check(hand))
                     {
                         Console.WriteLine("Main + tete trouvée");
                         if (jointMainGauche.Position.Y > jointTete.Position.Y)
                         {
                             BoutonPositionInitial();
                             Console.WriteLine("Main gauche au dessus de tete");

                             //zone.BackgroundColor = Brushes.CadetBlue;
                             zone.Opacity = 1;
                             zone.Text = btn1.Text;
                             swipefronttoback = false;
                             zone.TextColor = Brushes.Black;

                            lesZonesDepots.verifierDoublonsOnMap(zone);
                            if (lesZonesDepots.reponse().Equals("Maintenant que les villes sont placées. Vous pouvez demander la correction"))
                            {
                                sonToutesLesVillesPlacees.Position = new TimeSpan(0);
                                sonToutesLesVillesPlacees.LoadedBehavior = MediaState.Play;
                            }
                         }
                         tete = false;
                         mainGauche = false;
                    }

                }

             }
        }
Ejemplo n.º 15
0
    void UpdateSkeletonDisplay(ReplaySkeletonFrame frame) {
      if (frame.Skeletons == null)
        return;
      Dictionary<int, string> stabilities = new Dictionary<int, string>();
      foreach (var skeleton in frame.Skeletons) {
        if (skeleton.TrackingState != SkeletonTrackingState.Tracked)
          continue;

        contextTracker.Add(skeleton.Position.ToVector3(), skeleton.TrackingId);
        stabilities.Add(skeleton.TrackingId,
            contextTracker.IsStableRelativeToCurrentSpeed(skeleton.TrackingId) ?
                                                          "Stable" : "Non stable");
      }

      try {
        if (viewSkeleton && skeletonDisplayManager != null) {
          skeletonDisplayManager.Draw(frame.Skeletons, false,
                                      HandInputParams.ColorImageFormat);
        } else {
          skeletonCanvas.Children.Clear();
        }
      } catch (Exception e) {
        Log.Error(e.Message);
      }
    }
        // you can add a new path to the internal learning machine by first calling StartRecordTemplate() method. By doing that, any following calls to Add
        // will save the joint position but will also add the position to a new recorded path. The path will be closed, packed, and integrated into the learning 
        // machine with a call to EndRecordTemplate() method.

        //ProcessFrame method is called when a skeleton frame is ready. It is then responsible for feeding the gesture and posture detector classes 
        //after checking the stability with the context tracker
        void ProcessFrame(ReplaySkeletonFrame frame)
        {
            Dictionary<int, string> stabilities = new Dictionary<int, string>();
            standStartTime = DateTime.Now;
            foreach (var skeleton in frame.Skeletons)
            {
                if (skeleton.TrackingState != SkeletonTrackingState.Tracked)
                    continue;
                // Stability?
                contextTracker.Add(skeleton.Position.ToVector3(), skeleton.TrackingId);
                stabilities.Add(skeleton.TrackingId, contextTracker.IsStableRelativeToCurrentSpeed(skeleton.TrackingId) ? "Stable" : "Non stable");
                if (!contextTracker.IsStableRelativeToCurrentSpeed(skeleton.TrackingId))
                    continue;

                Joint leftKneePosition = skeleton.Joints.Where(j => j.JointType == JointType.KneeLeft).First();//.Position.ToVector3();
                Joint rightKneePosition = skeleton.Joints.Where(j => j.JointType == JointType.KneeRight).First();//.Position.ToVector3();
                Joint centerHipPosition = skeleton.Joints.Where(j => j.JointType == JointType.HipCenter).First();//.Position.ToVector3();
                Joint rightHipPosition = skeleton.Joints.Where(j => j.JointType == JointType.HipRight).First();//.Position.ToVector3();
                Joint leftHipPosition = skeleton.Joints.Where(j => j.JointType == JointType.HipLeft).First();//.Position.ToVector3();
                Joint rightFootPosition = skeleton.Joints.Where(j => j.JointType == JointType.FootRight).First();//.Position.ToVector3();
                Joint leftFootPosition = skeleton.Joints.Where(j => j.JointType == JointType.FootLeft).First();//.Position.ToVector3();
                Joint spinePosition = skeleton.Joints.Where(j => j.JointType == JointType.Spine).First();
                
                // ALGORITHMIC AUTOMATON BASED RECOGNITION  (based on supposition that user is initially "GoingToStand" when appears in camera at first time.
                currentAngle = AngleBetweenTwoVectors(leftHipPosition, leftKneePosition, leftKneePosition, leftFootPosition);
                if (contextTracker.IsBasicallySitting(skeleton) && contextTracker.IsSitting(skeleton)) {
                    if (currentPosture != "Sitting" && currentPosture == "GoingToSit")
                    {
                        currentPosture = "Sitting"; 
                        sittingDuration = (float)(sitStartTime.Subtract(DateTime.Now)).Duration().TotalSeconds; 
                        System.Console.WriteLine("*********** SITTING *********************left knee degree: " + currentAngle + " and duration: " + sittingDuration);
                        OnGestureDetected("Sit");
                        totalSittingDuration += sittingDuration;
                        nSits++;
                    }                       
                }
                else{
                    if (contextTracker.IsStanding(skeleton))
                    {
                        if (currentPosture != "Standing" && currentPosture == "GoingToStand")
                        {
                            currentPosture = "Standing";
                            standingDuration = (float)(standStartTime.Subtract(DateTime.Now)).Duration().TotalSeconds; 
                            System.Console.WriteLine("*********** STANDING *********************left knee degree: " + currentAngle + " and duration: " + standingDuration);
                            OnGestureDetected("Stand");
                            totalStandingDuration += standingDuration;
                            nStands++;
                        }                            
                    }       
                    else
                        if (contextTracker.IsGoingToSit(skeleton, previousAngle, currentAngle))
                        {
                            if (currentPosture != "GoingToSit" && currentPosture == "Standing")
                            {
                                //System.Console.WriteLine("*********** GOING TO SIT *********************left knee degree: " + currentAngle);
                                currentPosture = "GoingToSit";
                                sitStartTime = DateTime.Now;
                            }                                
                        }
                        else {
                            if (contextTracker.IsGoingToStand(skeleton, previousAngle, currentAngle))
                            {
                                if (currentPosture != "GoingToStand" && currentPosture == "Sitting")
                                {
                                    //System.Console.WriteLine("*********** GOING TO STAND *********************left knee degree: " + currentAngle);
                                    currentPosture = "GoingToStand";
                                    standStartTime = DateTime.Now;
                                }                                    
                            }   
                        }
                }
                previousAngle = currentAngle;

                //System.Console.WriteLine("--- HEAD POSITION " + skeleton.Joints[JointType.Head].Position.X);
                // right limit of X axis vision: --- HEAD POSITION -0,8555053, -0,987416. Left: 0.7

                // Start/Stop Logging ExerciseSession with TinHead sound:

                // Is right hand touching head? (furthermore, the person should be standing straight and with the head in the center of the field vision.
                handHeadDistance = jointDistance(skeleton.Joints[JointType.Head], skeleton.Joints[JointType.HandRight]);
                if (handHeadDistance < 0.4f && oldHandHeadDistance > 0.4f && currentPosture != "Standing" && skeleton.Joints[JointType.Head].Position.X < 0.65 && skeleton.Joints[JointType.Head].Position.X > -0.65)  // If touching head
                {             
                    if (!sessionStarted)
                    { // Start session
                        dingPlayer.Play(); // Play tin-head sound
                        sessionStarted = true;
                        sitStandSessionStartDateTime = DateTime.Now;
                        standStartTime = DateTime.Now;
                        sitStartTime = DateTime.Now;
                        username = "******";  //TODO: recognize user
                        clearHistory();
                    }
                    else
                    { // Stop session and save session in M3 Smart Space
                        dingPlayer.Play(); // Play tin-head sound
                        dingPlayer.Play(); // Play tin-head sound
                        sessionStarted = false;
                        sitStandSessionDuration = (float)(DateTime.Now - sitStandSessionStartDateTime).Duration().TotalSeconds;
                        avgNSits = 5; // TODO: calculate history avgNSits by parsing SPARQLResults
                        avgNSitsPerSession = nSits / avgNSits; // TODO: add property to Person and GHI 
                        avgSecondsToSit = totalSittingDuration / nSits;
                        avgSecondsToStand = totalStandingDuration / nStands;

                        //Graph as Arraylist of string[4]={s,p,o,o_type}   
                        //Each RDF triple is represented using a string[4] datatype, where:
                        //string[0] = subject
                        //string[1] = predicate
                        //string[2] = object
                        //string[3] = object type ["uri"|"literal"]
                        // Insert example
                        //string[] cuatriple = new string[4] { pr + "SitStandSession" + DateTime.Now.ToString(), "a", pr + "SitStandSession", "uri" };
                        //string[] cuatriple2 = new string[4] { pr + "Natalia", pr + "executesSitStandSession", (pr + "SitStandSession" + DateTime.Now.ToString()), "literal" };

                        if (nSits > 0)
                        {
                            SitStandExerciseSession session = new SitStandExerciseSession(("SitStandSession" + username + DateTime.Now.ToString()), username, sitStandSessionStartDateTime, sitStandSessionDuration, nSits, avgSecondsToSit, avgSecondsToStand);
                            sessions.Add(session);

                            foreach (SitStandExerciseSession sitSession in sessions)
                            {
                                string[] cuatriple = new string[4] { pr + session.SessionName, "a", pr + "SitStandSession", "uri" };
                                string[] cuatriple2 = new string[4] { pr + session.Username, pr + "executesSitStandSession", pr + session.SessionName, "uri" };
                                string[] cuatriple3 = new string[4] { pr + session.SessionName, pr + "hasStartDateTime", DateTimeToOWLDateTimeStr(session.StartDateTime), "literal" };
                                string[] cuatriple4 = new string[4] { pr + session.SessionName, pr + "hasDuration", FloatToOWLFloatStr(session.Duration), "literal" };
                                string[] cuatriple5 = new string[4] { pr + session.SessionName, pr + "consistsOfNSits", session.NSits.ToString(), "literal" };
                                string[] cuatriple6 = new string[4] { pr + session.SessionName, pr + "tookAvgSecondsToSit", FloatToOWLFloatStr(session.AvgSecondsToSit), "literal" };
                                string[] cuatriple7 = new string[4] { pr + session.SessionName, pr + "tookAvgSecondsToStand", FloatToOWLFloatStr(session.AvgSecondsToStand), "literal" };

                                ArrayList triple = new ArrayList();
                                triple.Add(cuatriple);
                                ArrayList triple2 = new ArrayList();
                                triple2.Add(cuatriple2);
                                ArrayList triple3 = new ArrayList();
                                triple3.Add(cuatriple3);
                                ArrayList triple4 = new ArrayList();
                                triple4.Add(cuatriple4);
                                ArrayList triple5 = new ArrayList();
                                triple5.Add(cuatriple5);
                                ArrayList triple6 = new ArrayList();
                                triple6.Add(cuatriple6);
                                ArrayList triple7 = new ArrayList();
                                triple7.Add(cuatriple7);
                                if (connectedToSmartSpace)  // SAVE IN M3 SIB 
                                {
                                    System.Console.WriteLine("--- Inserting " + M3.insert(triple));
                                    System.Console.WriteLine("--- Inserting " + M3.insert(triple2));
                                    System.Console.WriteLine("--- Inserting " + M3.insert(triple3));
                                    System.Console.WriteLine("--- Inserting " + M3.insert(triple4));
                                    System.Console.WriteLine("--- Inserting " + M3.insert(triple5));
                                    System.Console.WriteLine("--- Inserting " + M3.insert(triple6));
                                    System.Console.WriteLine("--- Inserting " + M3.insert(triple7));

                                    }
                                else
                                {
                                    // Writes RDF triples to file to pull to a SIB from scratch. TO-DO: method to load triples from file
                                    File.AppendAllText(Path.Combine(Environment.CurrentDirectory, @"data\KinectSessionsDB.txt"), "<" + cuatriple[0] + ", " + cuatriple[1] + ", " + cuatriple[2] + ">\n"); // <-- use \\ not \ in path
                                    File.AppendAllText(Path.Combine(Environment.CurrentDirectory, @"data\KinectSessionsDB.txt"), "<" + cuatriple2[0] + ", " + cuatriple2[1] + ", " + cuatriple2[2] + ">\n");
                                    File.AppendAllText(Path.Combine(Environment.CurrentDirectory, @"data\KinectSessionsDB.txt"), "<" + cuatriple3[0] + ", " + cuatriple3[1] + ", " + cuatriple3[2] + ">\n");
                                    File.AppendAllText(Path.Combine(Environment.CurrentDirectory, @"data\KinectSessionsDB.txt"), "<" + cuatriple4[0] + ", " + cuatriple4[1] + ", " + cuatriple4[2] + ">\n");
                                    File.AppendAllText(Path.Combine(Environment.CurrentDirectory, @"data\KinectSessionsDB.txt"), "<" + cuatriple5[0] + ", " + cuatriple5[1] + ", " + cuatriple5[2] + ">\n");
                                    File.AppendAllText(Path.Combine(Environment.CurrentDirectory, @"data\KinectSessionsDB.txt"), "<" + cuatriple6[0] + ", " + cuatriple6[1] + ", " + cuatriple6[2] + ">\n");
                                    File.AppendAllText(Path.Combine(Environment.CurrentDirectory, @"data\KinectSessionsDB.txt"), "<" + cuatriple7[0] + ", " + cuatriple7[1] + ", " + cuatriple7[2] + ">\n");
                                
                                }
                            }
                            totalSittingDuration = 0;
                            totalStandingDuration = 0;
                            nSits = 0;
                            nStands = 0;
                        }
                    }
                }
                oldHandHeadDistance = handHeadDistance;


                //var leftShoulderPosition = skeleton.Joints.Where(j => j.JointType == JointType.ShoulderLeft).First().Position.ToVector3();
                //var rightShoulderPosition = skeleton.Joints.Where(j => j.JointType == JointType.ShoulderRight).First().Position.ToVector3();                        
                //if (contextTracker.AreHipsTowardsSensor(skeleton)) //FRONT EXERCISES
                //    System.Console.WriteLine("Shoulders Z distance: "+ Math.Abs(leftShoulderPosition.Z - rightShoulderPosition.Z));
                //else if(contextTracker.AreHipsInProfileTowardsSensor(skeleton)) //PROFILE EXERCISES
                //    System.Console.WriteLine("Shoulders Z distance: "+ Math.Abs(leftShoulderPosition.Z - rightShoulderPosition.Z));

                //ANGLE-BASED PATTERN RECOGNITION  
                //TODO: complete with more angles for gesture key angles
                //System.Console.WriteLine("************ANGLE GROIN *******: "+ AngleBetweenTwoVectors(leftKneePosition, centerHipPosition, centerHipPosition, rightKneePosition));
                //System.Console.WriteLine("************ANGLE LEFT hip position: *******: " + leftHipPosition.Position.X + " " + leftHipPosition.Position.Y + " " + leftHipPosition.Position.Z);
                //System.Console.WriteLine("************ANGLE LEFT knee position: *******: " + leftKneePosition.Position.X + " " + leftKneePosition.Position.Y + " " + leftKneePosition.Position.Z);
                //System.Console.WriteLine("************ANGLE LEFT foot position: *******: " + leftFootPosition.Position.X + " " + leftFootPosition.Position.Y + " " + leftFootPosition.Position.Z);
                //System.Console.WriteLine("************ANGLE LEFT WAIST *******: " + AngleBetweenTwoVectors(spinePosition, centerHipPosition, centerHipPosition, leftKneePosition));
                //System.Console.WriteLine("************ANGLE KNEE *********************: " + AngleBetweenTwoVectors(leftHipPosition, leftKneePosition, leftKneePosition, leftFootPosition)); 

                //leftHipAbductionGestureRecognizer.AddAngle(AngleBetweenTwoVectors(leftKneePosition, centerHipPosition, centerHipPosition, rightKneePosition), kinectSensor);
                //rightHipAbductionGestureRecognizer.AddAngle(AngleBetweenTwoVectors(leftKneePosition, centerHipPosition, centerHipPosition, rightKneePosition), kinectSensor);

                //leftHipExtensionGestureRecognizer.AddAngle(AngleBetweenTwoVectors(leftKneePosition, centerHipPosition, centerHipPosition, rightKneePosition), kinectSensor);
                //rightHipExtensionGestureRecognizer.AddAngle(AngleBetweenTwoVectors(leftKneePosition, centerHipPosition, centerHipPosition, rightKneePosition), kinectSensor);

                //leftKneeExtensionGestureRecognizer.AddAngle(AngleBetweenTwoVectors(leftHipPosition, leftKneePosition, leftKneePosition, leftFootPosition), kinectSensor);
                //rightKneeExtensionGestureRecognizer.AddAngle(AngleBetweenTwoVectors(rightHipPosition, rightKneePosition, rightKneePosition, rightFootPosition), kinectSensor);

                sitGestureRecognizer.AddAngle(AngleBetweenTwoVectors(leftHipPosition, leftKneePosition, leftKneePosition, leftFootPosition), kinectSensor);
                standGestureRecognizer.AddAngle(AngleBetweenTwoVectors(leftHipPosition, leftKneePosition, leftKneePosition, leftFootPosition), kinectSensor);
                
                foreach (Joint joint in skeleton.Joints)
                {
                    if (joint.TrackingState != JointTrackingState.Tracked)
                        continue;

                                        
                    //PATH-BASED PATTERN RECOGNITION
                    if (joint.JointType == JointType.HandRight)
                    {
                        swipeGestureRecognizer.Add(joint.Position, kinectSensor);
                        circleGestureRecognizer.Add(joint.Position, kinectSensor);
                    }

                    //var leftShoulderPosition = skeleton.Joints.Where(j => j.JointType == JointType.ShoulderLeft).First().Position.ToVector3();
                    //var rightShoulderPosition = skeleton.Joints.Where(j => j.JointType == JointType.ShoulderRight).First().Position.ToVector3();                        
                    //if (contextTracker.AreHipsTowardsSensor(skeleton)) //FRONT EXERCISES
                    //    System.Console.WriteLine("Shoulders Z distance: "+ Math.Abs(leftShoulderPosition.Z - rightShoulderPosition.Z));
                    //else if(contextTracker.AreHipsInProfileTowardsSensor(skeleton)) //PROFILE EXERCISES
                    //    System.Console.WriteLine("Shoulders Z distance: "+ Math.Abs(leftShoulderPosition.Z - rightShoulderPosition.Z));
                            
                    //CONDITION FILTERS TO BE USED FOR TRAINING/RECOGNITION: sitting/basicallyStanding, shoulders/hips front/profile  TODO: to test
                    
                    if (joint.JointType == JointType.FootLeft)  // LEFT FOOT EXERCISES
                    {
                        //if (contextTracker.IsShouldersTowardsSensor(skeleton) && contextTracker.IsStanding(skeleton)) // FRONT EXERCISES  -using 2 filters
                        //{
                            //System.Console.WriteLine("**********************SKELETON FRONT *******");
                            leftHipAbductionGestureRecognizer.Add(joint.Position, kinectSensor);
                        //}
                        //else if (contextTracker.AreShouldersInProfileTowardsSensor(skeleton) && contextTracker.IsStanding(skeleton))
                        //{ // PROFILE EXERCISES
                            //System.Console.WriteLine("**********************SKELETON PROFILE*******");
                            leftHipExtensionGestureRecognizer.Add(joint.Position, kinectSensor);                            
                        //}
                        //else if (contextTracker.IsBasicallySitting(skeleton) && contextTracker.IsSitting(skeleton)) // SITTING EXERCISES
                            leftKneeExtensionGestureRecognizer.Add(joint.Position, kinectSensor);
                    }
                    if (joint.JointType == JointType.FootRight)  // RIGHT FOOT EXERCISES
                    {
                        //if (contextTracker.IsShouldersTowardsSensor(skeleton) && contextTracker.IsStanding(skeleton)) // FRONT EXERCISES
                        //{
                            rightHipAbductionGestureRecognizer.Add(joint.Position, kinectSensor);
                        //}
                        //else if (contextTracker.AreShouldersInProfileTowardsSensor(skeleton) && contextTracker.IsStanding(skeleton))
                        //{ // PROFILE EXERCISES
                            rightHipExtensionGestureRecognizer.Add(joint.Position, kinectSensor);
                        //}
                        //else if (contextTracker.IsBasicallySitting(skeleton) && contextTracker.IsSitting(skeleton))  // SITTING EXERCISES
                            rightKneeExtensionGestureRecognizer.Add(joint.Position, kinectSensor);
                    }
                    // SIT-STAND EXERCISES  -> 2 points to detect 1 gesture does not work, use similar to SitStandGestureDetector for hybrid (algorithmic and template-based) detector

                    //if (contextTracker.isSitting(skeleton)) //FRONT EXERCISES
                    //    System.Console.WriteLine("********************** SITTING! *******");
                    //else if (contextTracker.isStanding(skeleton))
                    //    System.Console.WriteLine("*********************STANDING******");
                    //else if (contextTracker.isBasicallySitting(skeleton))
                    //    System.Console.WriteLine("*********************BASICALLY SITTING******"); 
                    
                    if (joint.JointType == JointType.Head)  // SIT-STAND EXERCISES
                    {
                        //if (contextTracker.AreShouldersInProfileTowardsSensor(skeleton)) //PROFILE EXERCISES
                        //{
                            sitGestureRecognizer.Add(joint.Position, kinectSensor);
                            standGestureRecognizer.Add(joint.Position, kinectSensor);
                        //}                        
                    }              
                }
                algorithmicPostureRecognizer.TrackPostures(skeleton);
                templatePostureDetector.TrackPostures(skeleton);
                //The recording of a posture is based on the recordNextFrameForPosture boolean. When it is true, 
                //the ProcessFrame method record the current posture in the TemplatedPostureDectector object:
                if (recordNextFrameForPosture)
                {
                    templatePostureDetector.AddTemplate(skeleton);
                    recordNextFrameForPosture = false;
                }
                Skeleton[] list = new Skeleton[1] { skeleton };
                skeletonDisplayManager.Draw(list, false);
            }
            //skeletonDisplayManager.Draw(frame, false); //, false);
            stabilitiesList.ItemsSource = stabilities;
        }
Ejemplo n.º 17
0
        void ProcessFrame(ReplaySkeletonFrame frame)
        {
            Dictionary<int, string> stabilities = new Dictionary<int, string>();
            foreach (var skeleton in frame.Skeletons)
            {
                StringBuilder strBuilder = new StringBuilder();

                if (skeleton.TrackingState != SkeletonTrackingState.Tracked)
                    continue;

                //timer for recording and wait
                if (!timerWait.IsRunning && (timerRec.Elapsed.Seconds > 2 || !timerRec.IsRunning))
                {
                    timerRec.Stop();
                    timerRec.Reset();
                    //Console.WriteLine("Please wait while we process the gesture......");
                    //strBuilder.Append("Please wait while we process the gesture......\n");
                    ProcessGesture();
                    timerWait.Reset();
                    timerWait.Start();
                }
                if (timerWait.Elapsed.Seconds >= 5 && !timerRec.IsRunning)
                {
                    //Console.WriteLine("Gesture recorded, NEXT GESTURE!");
                    //strBuilder.Append("Gesture recorded, NEXT GESTURE!\n");
                    timerWait.Stop();
                    timerRec.Start();
                }

                /*contextTracker.Add(skeleton.Position.ToVector3(), skeleton.TrackingId);
                stabilities.Add(skeleton.TrackingId, contextTracker.IsStableRelativeToCurrentSpeed(skeleton.TrackingId) ? "Stable" : "Non stable");
                if (!contextTracker.IsStableRelativeToCurrentSpeed(skeleton.TrackingId))
                    continue;
                */
                bool touchedLeft = false;
                bool touchedRight = false;
                foreach (Joint joint in skeleton.Joints)
                {
                    if (status == RecordingStatus.STOP)
                        break;

                    if (!timerRec.IsRunning) {
                        continue;
                    }

                    if (m_useGestureLocked && (System.DateTime.Now >= m_timeToEndAt))
                    {
                        strBuilder.Append("Unlocking");
                        m_useGestureLocked = false;
                        break;
                    }
                    else if (m_useGestureLocked)
                    {
                        strBuilder.Append("Locked - " + m_gestureLockMessage);
                        break;
                    }

                    if (joint.TrackingState != JointTrackingState.Tracked)
                        continue;
                    //strBuilder.Append(joint.JointType.ToString());

                    if (status == RecordingStatus.RECORD)
                    {

                        if (joint.JointType.Equals(JointType.HandRight))
                        {
                            Console.WriteLine(String.Format("Right [{0}, {1}, {2}]", joint.Position.X, joint.Position.Y, joint.Position.Z));
                            strBuilder.Append(String.Format("\nRight [{0}, {1}, {2}]", joint.Position.X, joint.Position.Y, joint.Position.Z));
                            rightList.Add(new CoordinateContainer(joint.Position.X, joint.Position.Y));
                        }
                        else if (joint.JointType.Equals(JointType.HandLeft))
                        {
                            Console.WriteLine(String.Format("Left [{0}, {1}, {2}]", joint.Position.X, joint.Position.Y, joint.Position.Z));
                            strBuilder.Append(String.Format("\nLeft [{0}, {1}, {2}]", joint.Position.X, joint.Position.Y, joint.Position.Z));
                            leftList.Add(new CoordinateContainer(joint.Position.X, joint.Position.Y));
                        }
                    }
                    else if (status == RecordingStatus.USE) {
                        double cur_pos = joint.Position.Y;
                        if (joint.JointType.Equals(JointType.HandRight)) {
                            //Console.WriteLine(String.Format("Using right: [{0}, {1}]", joint.Position.X, joint.Position.Y));
                            //strBuilder.Append(String.Format("\nUsing right: [{0}, {1}]", joint.Position.X, joint.Position.Y));
                            //assume gesture array have only 6 elements
                            int rightIndex = 0;
                            foreach (CoordinateContainer container in gesture1_right)
                            {
                                touchedRight |= IsWithinRange(joint.Position.Y, container.getY()) || IsWithinRange(joint.Position.X, container.getX());
                                if (touchedRight)
                                {
                                    m_rightIndex = rightIndex;
                                    break;
                                }
                                rightIndex++;
                            }
                            if (touchedRight)
                            {
                                if (m_rightIndex != -1)
                                {
                                    Console.WriteLine("Right Hand gesture detected " + gesture1_right[m_rightIndex].name);
                                }
                                //strBuilder.Append("\nRight Hand gesture detected");
                            }
                        }
                        else if (joint.JointType.Equals(JointType.HandLeft)) {
                            //Console.WriteLine(String.Format("Using left: [{0}, {1}]", joint.Position.X, joint.Position.Y));
                            //strBuilder.Append(String.Format("\nUsing left: [{0}, {1}]", joint.Position.X, joint.Position.Y));
                            //assume gesture array have only 6 elements
                            int leftIndex = 0;
                            foreach (CoordinateContainer container in gesture1_left)
                            {
                                touchedLeft |= IsWithinRange(joint.Position.Y, container.getY()) || IsWithinRange(joint.Position.X, container.getX());
                                if (touchedLeft) {
                                    m_leftIndex = leftIndex;
                                    break;
                                }
                                leftIndex++;
                            }
                            if (touchedLeft)
                            {
                                if (m_leftIndex != -1)
                                {
                                    Console.WriteLine("Left Hand gesture detected " + gesture1_left[m_leftIndex].name);
                                }
                                //strBuilder.Append("\nLeft Hand gesture detected");
                            }
                        }

                        //if a gesture was discovered, lock for time buffer
                        if (m_rightIndex != -1 && m_leftIndex != -1)
                        {
                            if (m_rightIndex == m_leftIndex)
                            {
                                m_timeToEndAt = System.DateTime.Now.AddSeconds(k_secondsToBuffer);
                                m_useGestureLocked = true;
                                m_gestureLockMessage = strBuilder.ToString();
                                strBuilder.Append("\n" + gesture1_right[m_leftIndex].name);
                                Console.WriteLine(gesture1_right[m_leftIndex].name);
                                m_codeString = m_codeString + gesture1_right[m_leftIndex].name + " ";
                                codeView.Content = m_codeString;
                                m_leftIndex = -1;
                                m_rightIndex = -1;
                                touchedRight = false;
                                touchedLeft = false;
                            }
                        }
                    }
                }

                //MessageBox.Show(strBuilder.ToString());

                //this is the place to draw sitting position or not
                skeletonDisplayManager.Draw(frame.Skeletons, false);
                Output.Text = strBuilder.ToString();
                //we only care about 1 skeleton
                break;
            }
        }