Exemplo n.º 1
0
        void FixedUpdate()
        {
            if (_reader != null)
            {
                var frame = _reader.AcquireLatestFrame();

                if (frame != null)
                {
                    IList <Body> _bodies = new Body[frame.BodyFrameSource.BodyCount];

                    frame.GetAndRefreshBodyData(_bodies);

                    // Display only first active body
                    for (int i = 0; i < frame.BodyFrameSource.BodyCount; i++)
                    {
                        if (_bodies[i] != null)
                        {
                            if (_bodies[i].IsTracked)
                            {
                                if (_bodies[i].HandLeftState == HandState.Closed)
                                {
                                    leftHandClosed         = true;
                                    leftHandStateText.text = "Closed";
                                }
                                else //if (_bodies[i].HandLeftState == HandState.Open)
                                {
                                    leftHandClosed         = false;
                                    leftHandStateText.text = "Open";
                                }
                                if (_bodies[i].HandRightState == HandState.Closed)
                                {
                                    rightHandClosed         = true;
                                    rightHandStateText.text = "Closed";
                                }
                                else //if (_bodies[i].HandRightState == HandState.Open)
                                {
                                    rightHandClosed         = false;
                                    rightHandStateText.text = "Open";
                                }

                                bodyDrawer.DrawSkeleton(_bodies[i].Joints);

                                if (state == PractiseState.KinectChecking)
                                {
                                    ConvertedBody convertedBody = exerciseService.Convert(_bodies[i],
                                                                                          jsonExercise
                                                                                          .ExerciseRecording
                                                                                          .JointMappings);

                                    ProgressText.text = exerciseService.Progression();

                                    ExerciseScore score = exerciseService.Check(convertedBody);

                                    if (exerciseService.State() == ExerciseValidator.ValidatorState.Checking)
                                    {
                                        // Check body and add score to list
                                        exerciseResultScores.Add(score);

                                        // Add body to list for recording
                                        exerciseResultRecording.Add(convertedBody);
                                    }

                                    if (exerciseService.State() == ExerciseValidator.ValidatorState.Done)
                                    {
                                        CompletedOverlay.SetActive(true);
                                        TimerText.text = "";

                                        int endScore = 0;
                                        int total    = 0;


                                        for (int j = 0; j < exerciseResultScores.Count; j++)
                                        {
                                            total += exerciseResultScores[j].Score;
                                        }

                                        endScore = (int)Math.Round(((float)total / (float)exerciseResultScores.Count) * 25);

                                        string exerciseResultRecordingCompressed = Convert.ToBase64String(Gzip.Compress(JsonConvert.SerializeObject(exerciseResultRecording)));

                                        JObject resultJson = new JObject(
                                            new JProperty("duration", 0),
                                            new JProperty("score", endScore),
                                            new JProperty("exercisePlanning_ID", hrs.currentPlanningId),
                                            new JProperty("result", exerciseResultRecordingCompressed));

                                        ScoreText.text = baseScoreText + endScore + "%";

                                        StartCoroutine(
                                            requestService.Post("/exerciseresult", resultJson.ToString(), success =>
                                        {
                                            Debug.Log(success);
                                        },
                                                                error =>
                                        {
                                            Debug.Log(error);
                                        }
                                                                ));

                                        state = PractiseState.KinectDone;
                                    }
                                }

                                // Exit after first tracked body is found
                                break;
                            }
                        }
                    }

                    // Disable untracked body
                    for (int i = 0; i < frame.BodyFrameSource.BodyCount; i++)
                    {
                        if (!_bodies[i].IsTracked && bodyDrawer.Tracked)
                        {
                            //bodyDrawer.Untracked();
                        }
                    }

                    // Clear frame to get a new one
                    frame.Dispose();
                }
            }
        }
        void FixedUpdate()
        {
            if (_reader != null)
            {
                switch (state)
                {
                case RecordState.KinectDisplaying:
                case RecordState.KinectRecording:
                    var kframe = _reader.AcquireLatestFrame();

                    if (kframe != null)
                    {
                        IList <Body> _bodies = new Body[kframe.BodyFrameSource.BodyCount];

                        kframe.GetAndRefreshBodyData(_bodies);

                        // Display only first active body
                        for (int i = 0; i < kframe.BodyFrameSource.BodyCount; i++)
                        {
                            if (_bodies[i] != null)
                            {
                                if (_bodies[i].IsTracked)
                                {
                                    if (_bodies[i].HandLeftState == HandState.Closed)
                                    {
                                        leftHandClosed         = true;
                                        leftHandStateText.text = "Closed";
                                    }
                                    else     //if (_bodies[i].HandLeftState == HandState.Open)
                                    {
                                        leftHandClosed         = false;
                                        leftHandStateText.text = "Open";
                                    }
                                    if (_bodies[i].HandRightState == HandState.Closed)
                                    {
                                        rightHandClosed         = true;
                                        rightHandStateText.text = "Closed";
                                    }
                                    else     //if (_bodies[i].HandRightState == HandState.Open)
                                    {
                                        rightHandClosed         = false;
                                        rightHandStateText.text = "Open";
                                    }

                                    //Debug.Log("tracked : " + i);
                                    //skeletonDrawers[i].DrawSkeleton(_bodies[i]);
                                    bodyDrawer.DrawSkeleton(_bodies[i].Joints);
                                    if (state == RecordState.KinectRecording)
                                    {
                                        exerciseConvertedBodies.Add(exerciseService.Convert(_bodies[i], homeRevalSession
                                                                                            .CurrentRecording
                                                                                            .ExerciseRecording
                                                                                            .JointMappings));
                                    }

                                    // Exit after first tracked body is found
                                    break;
                                }
                            }
                        }

                        // Disable untracked body
                        for (int i = 0; i < kframe.BodyFrameSource.BodyCount; i++)
                        {
                            if (!_bodies[i].IsTracked && bodyDrawer.Tracked)
                            {
                                //bodyDrawer.Untracked();
                            }
                        }

                        // Clear frame to get a new one
                        kframe.Dispose();
                    }

                    break;

                case RecordState.ReplayPlaying:

                    bodyDrawer.DrawSkeleton(homeRevalSession.CurrentRecording.ExerciseRecording.ConvertedBodies[frame].CheckJoints);
                    if (timer.ElapsedMilliseconds > 33)
                    {
                        frame++;

                        if (frame >= homeRevalSession.CurrentRecording.ExerciseRecording.ConvertedBodies.Count - 1)
                        {
                            frame = 0;
                            state = RecordState.ReplayPaused;

                            // Update view
                            pauseReplayButton.SetActive(false);
                            playReplayButton.SetActive(true);
                        }


                        timer.Reset();
                        timer.Start();

                        // Update view
                        frameText.text     = "frame " + (frame + 1).ToString() + "/" + (homeRevalSession.CurrentRecording.ExerciseRecording.ConvertedBodies.Count);
                        replaySlider.value = frame;
                    }

                    break;

                case RecordState.ReplayPaused:
                    bodyDrawer.DrawSkeleton(homeRevalSession.CurrentRecording.ExerciseRecording.ConvertedBodies[frame].CheckJoints);
                    break;
                }
            }
        }