예제 #1
0
파일: Tracker.cs 프로젝트: apaganelli/mBESS
        void ITracker.Tracker(Body body)
        {
            Util.TrackingState newState = moviment.Tracker(body);

            if (newState == Util.TrackingState.Identified && CurrentState != Util.TrackingState.Identified)
            {
                CallEvent(MovimentIdentified);
            }

            if (newState == Util.TrackingState.Ongoing && (newState == Util.TrackingState.Ongoing || CurrentState == Util.TrackingState.NotIdentified))
            {
                CallEvent(MovimentOngoing);
            }

            CurrentState = newState;
        }
예제 #2
0
파일: Tracker.cs 프로젝트: apaganelli/mBESS
 public Tracker()
 {
     CurrentState = Util.TrackingState.NotIdentified;
     moviment     = Activator.CreateInstance <T>();
 }
예제 #3
0
        /// <summary>
        /// Updates the body array with new information from the sensor
        /// Should be called whenever a new BodyFrameArrivedEvent occurs
        /// </summary>
        /// <param name="bodies">Array of bodies to update</param>
        public void UpdateBodyFrame(Body[] bodies)
        {
            if (bodies != null)
            {
                using (DrawingContext dc = this.drawingGroup.Open())
                {
                    // Draw a transparent background to set the render size
                    dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));
                    int penIndex = 0;

                    foreach (Body body in bodies)
                    {
                        Pen drawPen = this.bodyColors[penIndex++];

                        if (body.IsTracked)
                        {
                            _faceSource.TrackingId = body.TrackingId;
                            IReadOnlyDictionary <JointType, Joint> joints = body.Joints;

                            // Calculate Center of Mass using segment method.
                            CameraSpacePoint CoM = _CoM.CalculateCoM(joints);

                            if (CoM.Z < 0)
                            {
                                CoM.Z = InferredZPositionClamp;
                            }

                            DepthSpacePoint Location_CoM = coordinateMapper.MapCameraPointToDepthSpace(CoM);
                            Point           CoM_Point    = new Point(Location_CoM.X, Location_CoM.Y);

                            // UpdateJointPosition(joints);

                            // convert the joint points to depth (display) space
                            Dictionary <JointType, Point> jointPoints = new Dictionary <JointType, Point>();

                            foreach (JointType jointType in joints.Keys)
                            {
                                // sometimes the depth(Z) of an inferred joint may show as negative
                                // clamp down to 0.1f to prevent coordinatemapper from returning (-Infinity, -Infinity)
                                CameraSpacePoint position = joints[jointType].Position;

                                if (position.Z < 0)
                                {
                                    position.Z = InferredZPositionClamp;
                                }

                                DepthSpacePoint depthSpacePoint = this.coordinateMapper.MapCameraPointToDepthSpace(position);
                                jointPoints[jointType] = new Point(depthSpacePoint.X, depthSpacePoint.Y);
                            }


                            // Pose calibration, recording positions to get a reference of them when they are theoretically stable.
                            if (RecordDoubleStance)
                            {
                                if (_dsCounter < ApplicationViewModel.MaxFramesReference)
                                {
                                    _app.DSFL_X.Add(joints[JointType.FootLeft].Position.X);
                                    _app.DSFL_Y.Add(joints[JointType.FootLeft].Position.Y);
                                    _app.DSFL_Z.Add(joints[JointType.FootLeft].Position.Z);

                                    _app.DSFR_X.Add(joints[JointType.FootRight].Position.X);
                                    _app.DSFR_Y.Add(joints[JointType.FootRight].Position.Y);
                                    _app.DSFR_Z.Add(joints[JointType.FootRight].Position.Z);

                                    _app.DSHL_X.Add(joints[JointType.HandLeft].Position.X);
                                    _app.DSHL_Y.Add(joints[JointType.HandLeft].Position.Y);
                                    _app.DSHL_Z.Add(joints[JointType.HandLeft].Position.Z);

                                    _app.DSHR_X.Add(joints[JointType.HandRight].Position.X);
                                    _app.DSHR_Y.Add(joints[JointType.HandRight].Position.Y);
                                    _app.DSHR_Z.Add(joints[JointType.HandRight].Position.Z);

                                    _app.DSHE_X.Add(joints[JointType.Head].Position.X);
                                    _app.DSHE_Y.Add(joints[JointType.Head].Position.Y);
                                    _app.DSHE_Z.Add(joints[JointType.Head].Position.Z);

                                    _app.DSSM_X.Add(joints[JointType.SpineMid].Position.X);
                                    _app.DSSM_Y.Add(joints[JointType.SpineMid].Position.Y);
                                    _app.DSSM_Z.Add(joints[JointType.SpineMid].Position.Z);

                                    _app.DSSB_X.Add(joints[JointType.SpineBase].Position.X);
                                    _app.DSSB_Y.Add(joints[JointType.SpineBase].Position.Y);
                                    _app.DSSB_Z.Add(joints[JointType.SpineBase].Position.Z);

                                    _dsCounter++;
                                    // Adds 50 ms to counter.
                                    _app.PoseCalibrationCounter += 50;
                                }
                            }
                            else
                            {
                                if (_dsCounter > 0)
                                {
                                    poseDoubleStance.CalculatedStandardJoints();
                                }

                                _dsCounter = 0;
                            }

                            // Checks if user is in the right pose (balanced) or unbalanced
                            if (ExecuteDoubleStanceTest)
                            {
                                Util.TrackingState trackingState = poseDoubleStance.Tracker(body);

                                _frameNumId++;
                                _skipFrames--;          // delay after counting an error to be back to position without counting new errors.

                                switch (trackingState)
                                {
                                case Util.TrackingState.Identified:                 // the test is over
                                    ExecuteDoubleStanceTest = false;
                                    ((DoubleCalibrationViewModel)_app.CurrentPageViewModel).StatusText = "Finished sucessfully";
                                    _dsPoseErrorCounter = 0;
                                    break;

                                case Util.TrackingState.NotIdentified:              // a pose error
                                    ((DoubleCalibrationViewModel)_app.CurrentPageViewModel).StatusText = "Double stance pose with error. # of errors: " + ((DoubleCalibrationViewModel)_app.CurrentPageViewModel).TotalDoubleStanceError;

                                    if (poseDoubleStance.NOK_Type != _lastNOK_Type)
                                    {
                                        if (_skipFrames < 0)
                                        {
                                            _dsPoseErrorCounter++;
                                        }
                                    }

                                    if (_dsPoseErrorCounter > _NumFramesThresholdError)
                                    {
                                        _dsPoseErrorCounter = 0;
                                        _lastNOK_Type       = poseDoubleStance.NOK_Type;
                                        ((DoubleCalibrationViewModel)_app.CurrentPageViewModel).TotalDoubleStanceError++;

                                        if (_lastNOK_Type > 7)     // foot displacement in general causes hand and trunk displacement, give 1 second to return to position.
                                        {
                                            _skipFrames = 30;
                                        }
                                    }

                                    break;

                                case Util.TrackingState.Ongoing:                    // it is okay, keep going.
                                    ((DoubleCalibrationViewModel)_app.CurrentPageViewModel).StatusText = "Double stance pose is okay. # errors: " + ((DoubleCalibrationViewModel)_app.CurrentPageViewModel).TotalDoubleStanceError;
                                    _dsPoseErrorCounter = 0;
                                    _lastNOK_Type       = 0;
                                    _skipFrames         = 0;
                                    break;
                                }
                            }   // end execute double stance test - analyze posture.


                            // Text to show eyes status on drawing space.
                            string faceText = "EYES: ";

                            // Only analyze eyes if it has a tracked faced.
                            if (_faceResult != null)
                            {
                                var eyeLeftClosed  = _faceResult.FaceProperties[FaceProperty.LeftEyeClosed];
                                var eyeRightClosed = _faceResult.FaceProperties[FaceProperty.RightEyeClosed];

                                if (eyeLeftClosed == DetectionResult.No || eyeRightClosed == DetectionResult.No)
                                {
                                    if (eyeLeftClosed == DetectionResult.No)
                                    {
                                        faceText += "LEFT ";
                                    }
                                    if (eyeRightClosed == DetectionResult.No)
                                    {
                                        faceText += "RIGHT ";
                                    }
                                    faceText += "OPEN";

                                    if (_addEyeError)
                                    {
                                        // Only update error and status messages if test is on.
                                        if (ExecuteDoubleStanceTest)
                                        {
                                            if (_dsEyesErrorCounter > _NumFramesThresholdError)
                                            {
                                                ((DoubleCalibrationViewModel)_app.CurrentPageViewModel).TotalDoubleStanceError++;
                                                ((DoubleCalibrationViewModel)_app.CurrentPageViewModel).StatusText = "Eye(s) opened error";
                                                _dsEyesErrorCounter = 0;

                                                _addEyeError = false;
                                            }
                                            else
                                            {
                                                _dsEyesErrorCounter++;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    faceText           += "CLOSED";
                                    _dsEyesErrorCounter = 0;

                                    if (!_addEyeError)
                                    {
                                        _addEyeError = true;
                                    }
                                }

                                Point textLocation = new Point(10, 10);

                                dc.DrawText(new FormattedText(
                                                faceText,
                                                CultureInfo.GetCultureInfo("en-us"),
                                                FlowDirection.LeftToRight,
                                                new Typeface("Georgia"),
                                                30,                                    // Font size
                                                Brushes.White),
                                            textLocation);
                            }

                            this.DrawBody(joints, jointPoints, dc, drawPen, true);
                            this.DrawHand(body.HandLeftState, jointPoints[JointType.HandLeft], dc);
                            this.DrawHand(body.HandRightState, jointPoints[JointType.HandRight], dc);
                            dc.DrawEllipse(Brushes.White, null, CoM_Point, CoMSize, CoMSize);
                        }
                    }

                    // prevent drawing outside of our render area
                    this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));
                }
            }
        }