Beispiel #1
0
        void msfr_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            try
            {
                MultiSourceFrame msf = e.FrameReference.AcquireFrame();

                if (msf != null)
                {
                    using (BodyFrame bodyFrame = msf.BodyFrameReference.AcquireFrame())
                        using (ColorFrame colorFrame = msf.ColorFrameReference.AcquireFrame())
                            using (DepthFrame irFrame = msf.DepthFrameReference.AcquireFrame())
                            {
                                if (bodyFrame != null && colorFrame != null && irFrame != null)
                                {
                                    bodies = new Body[bodyFrame.BodyFrameSource.BodyCount];
                                    FrameDescription framedesc = colorFrame.FrameDescription;
                                    using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer())
                                    {
                                        colorBitmap.Lock();
                                        if ((framedesc.Width == colorBitmap.PixelWidth) && (framedesc.Height == colorBitmap.PixelHeight))
                                        {
                                            colorFrame.CopyConvertedFrameDataToIntPtr(colorBitmap.BackBuffer, (uint)(framedesc.Width * framedesc.Height * 4), ColorImageFormat.Bgra);
                                            colorBitmap.AddDirtyRect(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight));
                                        }
                                        colorBitmap.Unlock();

                                        bodyFrame.GetAndRefreshBodyData(bodies);
                                        canvas.Children.Clear();

                                        foreach (var body in bodies)
                                        {
                                            if (body != null)
                                            {
                                                if (body.IsTracked)
                                                {
                                                    //DrawJoint(body);

                                                    foreach (var item in body.Joints)
                                                    {
                                                        JointType jointType = item.Key;
                                                        Joint     joint     = item.Value;

                                                        if (flag)
                                                        {
                                                            if (joint.TrackingState == TrackingState.Tracked)
                                                            {
                                                                CameraSpacePoint camersp = joint.Position;
                                                                ColorSpacePoint  csp     = this.kinectRegion.KinectSensor.CoordinateMapper.MapCameraPointToColorSpace(camersp);

                                                                //this part draws a line between the waistline
                                                                if (joint.JointType == JointType.HipLeft)
                                                                {
                                                                    Point point1 = new Point(csp.X - 140, csp.Y);
                                                                    foreach (Joint joint2 in body.Joints.Values)
                                                                    {
                                                                        if (joint2.JointType == JointType.HipRight)
                                                                        {
                                                                            CameraSpacePoint camers = joint2.Position;
                                                                            ColorSpacePoint  cs     = this.kinectRegion.KinectSensor.CoordinateMapper.MapCameraPointToColorSpace(camers);
                                                                            Point            point2 = new Point(cs.X + 140, cs.Y);
                                                                            DrawLine(point1, point2);
                                                                            DrawTextWidth("Your waist is large about " + BodyWidth.Width(body) + " centimeters", cs, 410, 70);
                                                                            DrawPoint(point1, point2);
                                                                        }
                                                                    }
                                                                }

                                                                //this part draws a line between the head and the foot for the height
                                                                if (joint.JointType == JointType.Head)
                                                                {
                                                                    Point point3 = new Point(csp.X + 500, csp.Y);
                                                                    foreach (Joint joint3 in body.Joints.Values)
                                                                    {
                                                                        if (joint3.JointType == JointType.FootLeft)
                                                                        {
                                                                            if ((joint3.TrackingState != TrackingState.Inferred))
                                                                            {
                                                                                CameraSpacePoint camer  = joint3.Position;
                                                                                ColorSpacePoint  c      = this.kinectRegion.KinectSensor.CoordinateMapper.MapCameraPointToColorSpace(camer);
                                                                                Point            point4 = new Point(csp.X + 500, c.Y);
                                                                                if (IsPointForDrawing(point3))
                                                                                {
                                                                                    if (IsPointForDrawing(point4))
                                                                                    {
                                                                                        DrawLine(point3, point4);
                                                                                        DrawTextHeight("Your height is about " + BodyHeight.Height(body) + " meters", csp, 320, 70);
                                                                                        DrawPoint(point3, point4);
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        //this part puts some buttons on screen
                                                        if (joint.JointType == JointType.SpineMid)
                                                        {
                                                            SetTables();

                                                            DepthSpacePoint space2 = this.kinectRegion.KinectSensor.CoordinateMapper.MapCameraPointToDepthSpace(joint.Position);
                                                            ColorSpacePoint space  = this.kinectRegion.KinectSensor.CoordinateMapper.MapCameraPointToColorSpace(joint.Position);
                                                            Point           center = new Point(space.X - 75, space.Y);

                                                            foreach (Joint joints in body.Joints.Values)
                                                            {
                                                                if (joints.JointType == JointType.ShoulderLeft)
                                                                {
                                                                    DepthSpacePoint depth_point = this.kinectRegion.KinectSensor.CoordinateMapper.MapCameraPointToDepthSpace(joints.Position);
                                                                    ColorSpacePoint color_point = this.kinectRegion.KinectSensor.CoordinateMapper.MapCameraPointToColorSpace(joints.Position);

                                                                    float distance = (float)Math.Round((joints.Position.Z), 2);

                                                                    ShowMessageAboutDistance(distance, color_point);

                                                                    Point[] verticies = new Point[MAX];

                                                                    //DrawText("Your distance from the sensor is " + distance, color_point);

                                                                    //TODO probabilmente è da cambiare la variabile all'interno
                                                                    for (int i = 0; i < bodyFrame.BodyCount; i++)
                                                                    {
                                                                        if (distance < 1.5)
                                                                        {
                                                                            verticies = Polygon.CalculateVertices(buttons_array[i].Count, 300, 18, center);
                                                                            ReplaceButtons(100, 100, center, buttons_array[i], verticies);
                                                                        }
                                                                        if ((distance < 2.5) && (distance > 1.5))
                                                                        {
                                                                            verticies = Polygon.CalculateVertices(buttons_array[i].Count, 250, 18, center);
                                                                            ReplaceButtons(80, 85, center, buttons_array[i], verticies);
                                                                        }

                                                                        if ((distance < 3.5) && (distance > 2.5))
                                                                        {
                                                                            verticies = Polygon.CalculateVertices(buttons_array[i].Count, 200, 18, center);
                                                                            ReplaceButtons(60, 70, center, buttons_array[i], verticies);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                }
            }
            finally
            {
                //if (msfr != null) msfr.Dispose();
            }
        }
Beispiel #2
0
        void msfr_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            try
            {
                MultiSourceFrame msf = e.FrameReference.AcquireFrame();
                if (msf != null)
                {
                    using (BodyFrame bodyFrame = msf.BodyFrameReference.AcquireFrame())
                    {
                        using (ColorFrame colorFrame = msf.ColorFrameReference.AcquireFrame())
                        {
                            using (DepthFrame irFrame = msf.DepthFrameReference.AcquireFrame())
                            {
                                if (bodyFrame != null && colorFrame != null)
                                {
                                    FrameDescription framedesc = colorFrame.FrameDescription;
                                    using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer())
                                    {
                                        colorBitmap.Lock();
                                        if ((framedesc.Width == colorBitmap.PixelWidth) && (framedesc.Height == colorBitmap.PixelHeight))
                                        {
                                            colorFrame.CopyConvertedFrameDataToIntPtr(colorBitmap.BackBuffer, (uint)(framedesc.Width * framedesc.Height * 4), ColorImageFormat.Bgra);
                                            colorBitmap.AddDirtyRect(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight));
                                        }
                                        colorBitmap.Unlock();
                                        bodyFrame.GetAndRefreshBodyData(bodies);
                                        canvas.Children.Clear();
                                        foreach (Body body in bodies)
                                        {
                                            if (body != null)
                                            {
                                                if (body.IsTracked)
                                                {
                                                    //DrawJoint(body);

                                                    foreach (var item in body.Joints)
                                                    {
                                                        JointType jointType = item.Key;
                                                        Joint     joint     = item.Value;

                                                        if (joint.TrackingState == TrackingState.Tracked)
                                                        {
                                                            CameraSpacePoint camersp = joint.Position;
                                                            ColorSpacePoint  csp     = this.kinectRegion.KinectSensor.CoordinateMapper.MapCameraPointToColorSpace(camersp);
                                                            if (joint.JointType == JointType.Head)
                                                            {
                                                                DrawText("Your height is about " + BodyHeight.Height(body) + " meters", csp);
                                                            }

                                                            //this part draws a line between the waistline
                                                            if (joint.JointType == JointType.HipLeft)
                                                            {
                                                                Point point1 = new Point(csp.X - 140, csp.Y);
                                                                foreach (Joint joint2 in body.Joints.Values)
                                                                {
                                                                    if (joint2.JointType == JointType.HipRight)
                                                                    {
                                                                        CameraSpacePoint camers = joint2.Position;
                                                                        ColorSpacePoint  cs     = this.kinectRegion.KinectSensor.CoordinateMapper.MapCameraPointToColorSpace(camers);
                                                                        Point            point2 = new Point(cs.X + 140, cs.Y);
                                                                        DrawLine(point1, point2);
                                                                        DrawText("Your waist is large about " + BodyWidth.Width(body) + " centimeters", cs);
                                                                        DrawPoint(point1, point2);
                                                                    }
                                                                }
                                                            }

                                                            if (joint.JointType == JointType.SpineMid)
                                                            {
                                                                ColorSpacePoint space     = this.kinectRegion.KinectSensor.CoordinateMapper.MapCameraPointToColorSpace(joint.Position);
                                                                Point           center    = new Point(space.X, space.Y);
                                                                Point[]         verticies = new Point[MAX];


                                                                verticies = Polygon.CalculateVertices(buttons.Count, 300, -10, center);

                                                                int i = 0;
                                                                foreach (Button button in buttons)
                                                                {
                                                                    Canvas.SetLeft(button, verticies[i].X);
                                                                    Canvas.SetTop(button, verticies[i].Y);
                                                                    i++;
                                                                }

                                                                verticies = null;

                                                                choices.Clear();
                                                            }

                                                            //while this part draw a line for the heigth
                                                            if (joint.JointType == JointType.Head)
                                                            {
                                                                Point point3 = new Point(csp.X + 250, csp.Y);
                                                                foreach (Joint joint3 in body.Joints.Values)
                                                                {
                                                                    if (joint3.JointType == JointType.FootLeft)
                                                                    {
                                                                        if (joint3.TrackingState == TrackingState.Tracked)
                                                                        {
                                                                            CameraSpacePoint camer  = joint3.Position;
                                                                            ColorSpacePoint  c      = this.kinectRegion.KinectSensor.CoordinateMapper.MapCameraPointToColorSpace(camer);
                                                                            Point            point4 = new Point(csp.X + 250, c.Y);
                                                                            if (IsPointForDrawing(point3))
                                                                            {
                                                                                if (IsPointForDrawing(point4))
                                                                                {
                                                                                    DrawLine(point3, point4);
                                                                                    DrawPoint(point3, point4);
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }

                                                            /*
                                                             *                                                          //this part tries to associate the hand with a button
                                                             *                                                          if (joint.JointType == (JointType.HandLeft))
                                                             *                                                          {
                                                             *                                                              if (isHandOver(joint, buttons))
                                                             *                                                              {
                                                             *                                                                  button1.Visibility = Visibility.Visible;
                                                             *
                                                             *                                                              }
                                                             *                                                          }
                                                             *
                                                             *                                                          if (joint.JointType == (JointType.HandRight))
                                                             *                                                          {
                                                             *                                                              if (isHandOver(joint, buttons))
                                                             *                                                              {
                                                             *                                                                  button1.Visibility = Visibility.Visible;
                                                             *                                                              }
                                                             *                                                          } */
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                //this.kinectRegion.KinectSensor.Close();
            }
        }