示例#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();
                }
            }
        }
        private ExerciseScore Validate(ConvertedBody bodyLive, ConvertedBody bodyJSON)
        {
            // Create new score
            ExerciseScore exerciseScore = new ExerciseScore()
            {
                Check = true, Score = 4
            };

            // Loop through all joint mappings from exercise
            foreach (Map.Mappings mapping in exercise.ExerciseRecording.JointMappings)
            {
                // Loop through all joints in Dict
                foreach (var item in Map.DictMappings[mapping])
                {
                    // Get current joints out of dict
                    JointType currentType = (JointType)item.Key;
                    JointType targetType  = (JointType)item.Value;

                    // Get angle difference between live and recorded date
                    double angleDifference = 180 - Math.Abs(Math.Abs(bodyJSON.JointResults[currentType].Angle - bodyLive.JointResults[currentType].Angle) - 180);

                    // Check if difference is bigger then the smallest margin
                    if (angleDifference <= (Math.Abs(margin) * -1) || angleDifference >= margin)
                    {
                        // Set check on false if one is outside smallest margin
                        exerciseScore.Check = false;
                    }

                    // Find right score for difference
                    if (angleDifference > (Math.Abs(margin) * -1) && angleDifference < margin)
                    {
                        if (!(exerciseScore.Score < 4))
                        {
                            exerciseScore.Score = 4;
                        }
                    }
                    else if (angleDifference > (Math.Abs(middleMargin) * -1) && angleDifference < middleMargin)
                    {
                        if (!(exerciseScore.Score < 3))
                        {
                            exerciseScore.Score = 3;
                        }
                    }
                    else if (angleDifference > (Math.Abs(errorMargin) * -1) && angleDifference < errorMargin)
                    {
                        if (!(exerciseScore.Score < 2))
                        {
                            exerciseScore.Score = 2;
                        }
                    }
                    else
                    {
                        if (!(exerciseScore.Score < 1))
                        {
                            exerciseScore.Score = 1;
                        }
                    }
                }
            }

            // Add score to exerciseScores
            exerciseScores.Add(exerciseScore);

            return(exerciseScore);
        }
        public ExerciseScore Check(ConvertedBody bodyLive)
        {
            // Get body from current frame
            ConvertedBody bodyJSON = exercise.ExerciseRecording.ConvertedBodies[frame];

            // Check move
            ExerciseScore exerciseScore;

            exerciseScore = Validate(bodyLive, bodyJSON);

            // Draw red body with current frame
            exampleBodyDrawer.DrawSkeleton(bodyJSON.CheckJoints);

            switch (state)
            {
            case ValidatorState.NotStarted:

                // Wait for user to start
                if (frame == 0 && !exerciseScore.Check)
                {
                    return(exerciseScore);
                }

                state = ValidatorState.Checking;

                return(exerciseScore);

            case ValidatorState.WaitingForNext:

                // Wait for user to start
                if (frame == 0 && !exerciseScore.Check)
                {
                    return(exerciseScore);
                }

                state = ValidatorState.Checking;

                return(exerciseScore);

            case ValidatorState.Checking:
                if (exerciseScore.Check)
                {
                    // Check completed
                    frame++;
                    latestValidatedCheck = DateTime.Now;
                }

                // Find end of exercise
                if (frame == exercise.ExerciseRecording.ConvertedBodies.Count - 1)
                {
                    frame = 0;
                    current++;

                    UnityEngine.Debug.Log(current + "  -  " + exercise.Amount);

                    if (current == exercise.Amount)
                    {
                        state = ValidatorState.Done;
                    }
                    else
                    {
                        state = ValidatorState.WaitingForNext;
                    }

                    return(exerciseScore);
                }

                // Check if user missed checks for more then 1 second and cancel current exercise
                UnityEngine.Debug.Log(latestValidatedCheck < DateTime.Now.AddSeconds(-5));
                if (latestValidatedCheck < DateTime.Now.AddSeconds(-5))
                {
                    state = ValidatorState.WaitingForNext;
                    frame = 0;
                    return(exerciseScore);
                }

                UnityEngine.Debug.Log(frame);

                state = ValidatorState.Checking;

                return(exerciseScore);

            case ValidatorState.Done:
                return(null);

            default:
                return(null);
            }
        }
 public ExerciseScore Check(ConvertedBody bodyLive)
 {
     return(exerciseValidator.Check(bodyLive));
 }