Exemplo n.º 1
0
 public static UserTracker Create(Device device = null)
 {
     IntPtr deviceHandle = IntPtr.Zero;
     if (device != null && device.isValid)
         deviceHandle = device.Handle;
     IntPtr handle;
     NiTE.throwIfError(UserTracker_create(out handle, deviceHandle));
     UserTracker ut = new UserTracker(handle);
     ut.handler_events = UserTracker_RegisterListener(handle, ut.internal_listener);
     return ut;
 }
Exemplo n.º 2
0
        void uTracker_onNewData(UserTracker uTracker)
        {
            if (!uTracker.isValid)
                return;
            using (UserTrackerFrameRef frame = uTracker.readFrame())
            {
                if (!frame.isValid)
                    return;
                lock (image)
                {
                    if (image.Width != frame.UserMap.FrameSize.Width || image.Height != frame.UserMap.FrameSize.Height)
                        image = new Bitmap(frame.UserMap.FrameSize.Width, frame.UserMap.FrameSize.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    using (Graphics g = Graphics.FromImage(image))
                    {
                        g.FillRectangle(Brushes.Black, new Rectangle(new Point(0, 0), image.Size));
                        foreach (UserData user in frame.Users)
                        {
                            if (user.isNew && user.isVisible)
                                uTracker.StartSkeletonTracking(user.UserId);
                            if (user.isVisible && user.Skeleton.State == Skeleton.SkeletonState.TRACKED)
                            {
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_HAND, SkeletonJoint.JointType.RIGHT_ELBOW);
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.LEFT_HAND, SkeletonJoint.JointType.LEFT_ELBOW);

                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_ELBOW, SkeletonJoint.JointType.RIGHT_SHOULDER);
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.LEFT_ELBOW, SkeletonJoint.JointType.LEFT_SHOULDER);

                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_FOOT, SkeletonJoint.JointType.RIGHT_KNEE);
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.LEFT_FOOT, SkeletonJoint.JointType.LEFT_KNEE);

                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_KNEE, SkeletonJoint.JointType.RIGHT_HIP);
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.LEFT_KNEE, SkeletonJoint.JointType.LEFT_HIP);

                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_SHOULDER, SkeletonJoint.JointType.LEFT_SHOULDER);
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_HIP, SkeletonJoint.JointType.LEFT_HIP);

                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_SHOULDER, SkeletonJoint.JointType.RIGHT_HIP);
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.LEFT_SHOULDER, SkeletonJoint.JointType.LEFT_HIP);

                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.HEAD, SkeletonJoint.JointType.NECK);
                            }
                        }
                        g.Save();
                    }
                }
                this.Invoke(new MethodInvoker(delegate()
                {
                    fps = ((1000000 / (frame.Timestamp - lastTime)) + (fps * 4)) / 5;
                    lastTime = frame.Timestamp;
                    this.Text = "Frame #" + frame.FrameIndex.ToString() + " - Time: " + frame.Timestamp.ToString() + " - FPS: " + fps.ToString();
                    pb_preview.Image = image.Clone(new Rectangle(new Point(0, 0), image.Size), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                }));
            }
        }
Exemplo n.º 3
0
        public static UserTracker Create(Device device = null)
        {
            IntPtr deviceHandle = IntPtr.Zero;

            if (device != null && device.isValid)
            {
                deviceHandle = device.Handle;
            }
            IntPtr handle;

            NiTE.throwIfError(UserTracker_create(out handle, deviceHandle));
            UserTracker ut = new UserTracker(handle);

            ut.handler_events = UserTracker_RegisterListener(handle, ut.internal_listener);
            return(ut);
        }
Exemplo n.º 4
0
        private void button1_Click(object sender, EventArgs e)
        {
            uTracker = UserTracker.Create();
            btn_start.Enabled = false;
            uTracker.onNewData += new UserTracker.UserTrackerListener(uTracker_onNewData);

            //  FIXED Jun 2013
            ///* Because of incompatibility between current version of OpenNI and NiTE,
            // * we can't use event based reading. So we put our sample in a loop.
            // * You can copy OpenNI.dll from version 2.0 to solve this problem.
            // * Then you can uncomment above line of code and comment below ones.
            // */
            //while (this.IsHandleCreated)
            //{
            //    uTracker_onNewData(uTracker);
            //    Application.DoEvents();
            //}
        }
Exemplo n.º 5
0
        void uTracker_onNewData(UserTracker uTracker)
        {
            if (!uTracker.isValid)
                return;
            using (UserTrackerFrameRef frame = uTracker.readFrame())
            {
                if (!frame.isValid)
                    return;
                UserMap um = frame.UserMap;
                FillImageFromUserMap(frame.UserMap);

                UserData[] ud = frame.Users;
                using (Graphics g = Graphics.FromImage(image))
                {
                    foreach (UserData user in frame.Users)
                    {
                        if (user.CenterOfMass.Z > 0)
                        {
                            Point p = new Point();
                            PointF pf = uTracker.ConvertJointCoordinatesToDepth(user.CenterOfMass);
                            p.X = (int)pf.X - 5;
                            p.Y = (int)pf.Y - 5;
                            g.DrawEllipse(new Pen(Brushes.White, 5), new Rectangle(p, new Size(5, 5)));
                            g.DrawString("Center Of Mass", SystemFonts.DefaultFont, Brushes.White, p.X - 40, p.Y - 20);
                        }
                    }
                    g.Save();
                }
                this.Invoke(new MethodInvoker(delegate()
                {
                    fps = ((1000000 / (frame.Timestamp - lastTime)) + (fps * 4)) / 5;
                    lastTime = frame.Timestamp;
                    this.Text = "Frame #" + frame.FrameIndex.ToString() + " - Time: " + frame.Timestamp.ToString() + " - FPS: " + fps.ToString();
                    pb_preview.Image = image.Clone(new Rectangle(new Point(0,0), image.Size), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                }));
                
            }
        }
Exemplo n.º 6
0
        // ReSharper disable once ParameterHidesMember
        private void UserTrackerOnNewData(UserTracker userTracker)
        {
            if (!userTracker.IsValid)
            {
                return;
            }

            UserTrackerFrameRef frame = userTracker.ReadFrame();

            if (frame == null || !frame.IsValid)
            {
                return;
            }

            this.FillImageFromUserMap(frame.UserMap);

            using (Graphics g = Graphics.FromImage(this.image))
            {
                foreach (UserData user in frame.Users)
                {
                    if (user.CenterOfMass.Z > 0)
                    {
                        Point p = new Point();
                        PointF pf = userTracker.ConvertJointCoordinatesToDepth(user.CenterOfMass);
                        p.X = (int)pf.X - 5;
                        p.Y = (int)pf.Y - 5;
                        g.DrawEllipse(new Pen(Brushes.White, 5), new Rectangle(p, new Size(5, 5)));
                        g.DrawString("Center Of Mass", SystemFonts.DefaultFont, Brushes.White, p.X - 40, p.Y - 20);
                    }
                }

                g.Save();
            }

            this.Invoke(
                new MethodInvoker(
                    delegate
                        {
                            this.fps = ((1000000 / (frame.Timestamp - this.lastTime)) + (this.fps * 4)) / 5;
                            this.lastTime = frame.Timestamp;
                            this.Text = string.Format(
                                "Frame #{0} - Time: {1} - FPS: {2}",
                                frame.FrameIndex,
                                frame.Timestamp,
                                this.fps);
                            this.pb_preview.Image = this.image.Clone(
                                new Rectangle(new Point(0, 0), this.image.Size),
                                PixelFormat.Format24bppRgb);
                            frame.Release();
                        }));
        }
Exemplo n.º 7
0
 public SkeletonDrawer(UserTracker userTracker)
 {
     this.userTracker = userTracker;
 }
Exemplo n.º 8
0
        // ReSharper disable once ParameterHidesMember
        private void UserTrackerOnNewData(UserTracker userTracker)
        {
            if (!userTracker.IsValid)
            {
                return;
            }

            UserTrackerFrameRef frame = userTracker.ReadFrame();

            if (frame == null || !frame.IsValid)
            {
                return;
            }

            lock (this.image)
            {
                if (this.image.Width != frame.UserMap.FrameSize.Width
                    || this.image.Height != frame.UserMap.FrameSize.Height)
                {
                    this.image = new Bitmap(
                        frame.UserMap.FrameSize.Width,
                        frame.UserMap.FrameSize.Height,
                        PixelFormat.Format24bppRgb);
                }

                using (Graphics g = Graphics.FromImage(this.image))
                {
                    g.FillRectangle(Brushes.Black, new Rectangle(new Point(0, 0), this.image.Size));
                    foreach (UserData user in frame.Users)
                    {
                        if (user.IsNew && user.IsVisible)
                        {
                            userTracker.StartSkeletonTracking(user.UserId);
                        }

                        if (user.IsVisible && user.Skeleton.State == Skeleton.SkeletonState.Tracked)
                        {
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightHand,
                                SkeletonJoint.JointType.RightElbow);
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.LeftHand,
                                SkeletonJoint.JointType.LeftElbow);

                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightElbow,
                                SkeletonJoint.JointType.RightShoulder);
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.LeftElbow,
                                SkeletonJoint.JointType.LeftShoulder);

                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightFoot,
                                SkeletonJoint.JointType.RightKnee);
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.LeftFoot,
                                SkeletonJoint.JointType.LeftKnee);

                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightKnee,
                                SkeletonJoint.JointType.RightHip);
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.LeftKnee,
                                SkeletonJoint.JointType.LeftHip);

                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightShoulder,
                                SkeletonJoint.JointType.LeftShoulder);
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightHip,
                                SkeletonJoint.JointType.LeftHip);

                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightShoulder,
                                SkeletonJoint.JointType.RightHip);
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.LeftShoulder,
                                SkeletonJoint.JointType.LeftHip);

                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.Head,
                                SkeletonJoint.JointType.Neck);
                        }
                    }

                    g.Save();
                }
            }

            this.Invoke(
                new MethodInvoker(
                    delegate
                        {
                            this.fps = ((1000000 / (frame.Timestamp - this.lastTime)) + (this.fps * 4)) / 5;
                            this.lastTime = frame.Timestamp;
                            this.Text = string.Format(
                                "Frame #{0} - Time: {1} - FPS: {2}",
                                frame.FrameIndex,
                                frame.Timestamp,
                                this.fps);
                            this.pb_preview.Image = this.image.Clone(
                                new Rectangle(new Point(0, 0), this.image.Size),
                                PixelFormat.Format24bppRgb);
                            frame.Release();
                        }));
        }
Exemplo n.º 9
0
        private bool Start()
        {
            RegisterFilter();
            if (this.isIdle && this.broadcaster.HasServer())
            {
                MessageBox.Show(
                    @"Only one server is allowed.",
                    @"Multi-Server",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return false;
            }
            bool isSameDevice = this.currentDevice != null && this.currentDevice.IsValid
                                && this.currentDevice.DeviceInfo.Uri == Settings.Default.DeviceURI;
            bool isSameSensor = isSameDevice && this.currentSensor != null && this.currentSensor.IsValid
                                && this.currentSensor.SensorInfo.GetSensorType()
                                == (Device.SensorType)Settings.Default.CameraType;
            if (!isSameDevice)
            {
                if (Settings.Default.DeviceURI == string.Empty)
                {
                    this.currentDevice = null;
                    MessageBox.Show(
                        @"Please select a device to open and then click Apply.",
                        @"Device Open",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);
                    return false;
                }
            }
            if (!isSameSensor)
            {
                if (Settings.Default.CameraType == -1)
                {
                    this.currentDevice = null;
                    MessageBox.Show(
                        @"Please select a sensor to open and then click Apply.",
                        @"Sensor Create",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);
                    return false;
                }
            }
            if (!isSameDevice)
            {
                try
                {
                    this.currentDevice = Device.Open(Settings.Default.DeviceURI);
                }
                catch (Exception ex)
                {
                    this.currentDevice = null;
                    MessageBox.Show(
                        string.Format("Can not open selected Device. {0}", ex.Message),
                        @"Device Open",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                    return false;
                }
            }
            if (!isSameSensor)
            {
                try
                {
                    this.currentSensor =
                        this.currentDevice.CreateVideoStream((Device.SensorType)Settings.Default.CameraType);
                    this.currentSensor.OnNewFrame += this.CurrentSensorOnNewFrame;
                }
                catch (Exception ex)
                {
                    this.currentSensor = null;
                    MessageBox.Show(
                        string.Format("Can not open selected Sensor from selected Device. {0}", ex.Message),
                        @"Sensor Create",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                    return false;
                }
            }
            else
            {
                this.currentSensor.Stop();
            }
            VideoMode[] vmodes = this.currentSensor.SensorInfo.GetSupportedVideoModes().ToArray();
            VideoMode selectedVideoMode = null;
            switch (this.currentSensor.SensorInfo.GetSensorType())
            {
                case Device.SensorType.Color:
                    this.renderOptions = VideoFrameRef.CopyBitmapOptions.Force24BitRgb;
                    if (Settings.Default.Color_HD)
                    {
                        foreach (VideoMode vm in vmodes)
                        {
                            if (vm.Resolution.Width == 1280
                                && (vm.Resolution.Height == 960 || vm.Resolution.Height == 1024))
                            {
                                if ((selectedVideoMode == null
                                     || (selectedVideoMode.Fps < vm.Fps
                                         && vm.DataPixelFormat < selectedVideoMode.DataPixelFormat))
                                    && vm.DataPixelFormat != VideoMode.PixelFormat.Jpeg
                                    && vm.DataPixelFormat != VideoMode.PixelFormat.Yuv422)
                                {
                                    selectedVideoMode = vm;
                                }
                            }
                        }
                        this.isHd = selectedVideoMode != null;
                        if (!this.isHd)
                        {
                            MessageBox.Show(
                                @"This device doesn't support ~1.3MP resolution.",
                                @"HD Resolution",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                        }
                    }
                    if (selectedVideoMode == null)
                    {
                        foreach (VideoMode vm in vmodes)
                        {
                            if (vm.Resolution == new Size(640, 480))
                            {
                                if ((selectedVideoMode == null
                                     || (selectedVideoMode.Fps < vm.Fps
                                         && vm.DataPixelFormat < selectedVideoMode.DataPixelFormat))
                                    && vm.DataPixelFormat != VideoMode.PixelFormat.Jpeg
                                    && vm.DataPixelFormat != VideoMode.PixelFormat.Yuv422)
                                {
                                    selectedVideoMode = vm;
                                }
                            }
                        }
                    }
                    break;
                case Device.SensorType.Depth:
                    this.renderOptions = VideoFrameRef.CopyBitmapOptions.Force24BitRgb
                                         | VideoFrameRef.CopyBitmapOptions.DepthFillShadow;
                    if (Settings.Default.Depth_Fill)
                    {
                        if (this.cb_mirror.Enabled && this.cb_mirror.Checked)
                        {
                            this.renderOptions |= VideoFrameRef.CopyBitmapOptions.DepthFillRigthBlack;
                        }
                        else
                        {
                            this.renderOptions |= VideoFrameRef.CopyBitmapOptions.DepthFillLeftBlack;
                        }
                    }
                    if (Settings.Default.Depth_Invert)
                    {
                        this.renderOptions |= VideoFrameRef.CopyBitmapOptions.DepthInvert;
                    }
                    if (Settings.Default.Depth_Histogram)
                    {
                        this.renderOptions |= VideoFrameRef.CopyBitmapOptions.DepthHistogramEqualize;
                    }
                    foreach (VideoMode vm in vmodes)
                    {
                        if (vm.Resolution == new Size(640, 480))
                        {
                            if ((selectedVideoMode == null || selectedVideoMode.Fps < vm.Fps)
                                && (vm.DataPixelFormat == VideoMode.PixelFormat.Depth1Mm
                                    || vm.DataPixelFormat == VideoMode.PixelFormat.Depth100Um))
                            {
                                selectedVideoMode = vm;
                            }
                        }
                    }
                    break;
                case Device.SensorType.Ir:
                    this.renderOptions = VideoFrameRef.CopyBitmapOptions.Force24BitRgb;
                    foreach (VideoMode vm in vmodes)
                    {
                        if (vm.Resolution == new Size(640, 480))
                        {
                            if ((selectedVideoMode == null
                                 || (selectedVideoMode.Fps < vm.Fps
                                     && vm.DataPixelFormat < selectedVideoMode.DataPixelFormat))
                                && vm.DataPixelFormat != VideoMode.PixelFormat.Jpeg
                                && vm.DataPixelFormat != VideoMode.PixelFormat.Yuv422)
                            {
                                selectedVideoMode = vm;
                            }
                        }
                    }
                    break;
            }

            if (selectedVideoMode != null)
            {
                try
                {
                    if (this.currentSensor.VideoMode.Fps != selectedVideoMode.Fps
                        || this.currentSensor.VideoMode.DataPixelFormat != selectedVideoMode.DataPixelFormat
                        || this.currentSensor.VideoMode.Resolution != selectedVideoMode.Resolution)
                    {
                        this.currentSensor.VideoMode = selectedVideoMode;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(
                        string.Format("Can not set active video mode to {0}. {1}", selectedVideoMode, ex.Message),
                        @"Sensor Config",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                    return false;
                }
            }
            else
            {
                MessageBox.Show(
                    @"No acceptable video mode found.",
                    @"Sensor Config",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return false;
            }
            this.softMirror = Settings.Default.Mirroring;
            if (Settings.Default.SmartCam)
            {
                try
                {
                    if (!isSameDevice
                        || (this.uTracker == null || this.hTracker == null || !this.uTracker.IsValid
                            || !this.hTracker.IsValid))
                    {
                        this.uTracker = UserTracker.Create(this.currentDevice);
                        this.hTracker = HandTracker.Create(this.currentDevice);
                        this.hTracker.StartGestureDetection(GestureData.GestureType.HandRaise);
                        this.hTracker.OnNewData += this.NiTeOnNewData;
                    }
                }
                catch (Exception)
                {
                }
            }
            if (!HandleError(this.currentSensor.Start()))
            {
                this.Stop(false);
                return false;
            }
            this.btn_stopstart.Text = @"Stop Streaming";
            this.isIdle = false;
            this.notify.Visible = true;
            return true;
        }
Exemplo n.º 10
0
        private void SkeletonSensor_OnNewData(UserTracker userTracker)
        {
            if (!userTracker.IsValid)
                return;

            ulong currentTimeTicks = 0;
            Skeleton validSkeleton = null;
            using (UserTrackerFrameRef frame = userTracker.ReadFrame())
            {
                if (frame == null || !frame.IsValid)
                    return;

                foreach (UserData user in frame.Users)
                {
                    if (user.IsNew && user.IsVisible)
                    {
                        userTracker.StartSkeletonTracking(user.UserId);
                    }
                    if (user.IsVisible && user.Skeleton.State == Skeleton.SkeletonState.Tracked)
                    {
                        validSkeleton = user.Skeleton;
                        break;
                    }
                }
                currentTimeTicks = frame.Timestamp;
            }

            if (validSkeleton != null && IsEngineStart)
            {
                double deltaTimeTicks = (currentTimeTicks - lastUpdate);
                if (lastUpdate == 0)
                    deltaTimeTicks = 0;
                lastUpdate = currentTimeTicks;
                RunFatigueEngine(validSkeleton, deltaTimeTicks / 1000000);
            }

            Dispatcher.Invoke((Action)delegate
            {
                iKinectCapture.Source = kinectSensor.RawImageSource;
                if (validSkeleton != null)
                    iSkeleton.Source = DrawSkeleton(validSkeleton, Brushes.Transparent);
                else
                    iSkeleton.Source = null;
            });
        }