Exemplo n.º 1
0
        private void Window_Initialized(object sender, EventArgs e)
        {
            string configPath = Properties.Settings.Default.ONIConfig;

            Title += " - " + configPath;

            try
            {
                if (!File.Exists(configPath))
                    throw new ApplicationException("Config file '" + configPath + "' does not exist.");

                _labelDataBrush = Resources["labelData"] as ImageBrush;

                _skeletonDrawer = new SkeletonDrawer(depthGrid);
                _skeleton = new Dictionary<int, Dictionary<SkeletonJoint, SkeletonJointPosition>>();

                _sensor = new Sensor(configPath);

                Console.WriteLine("Sensor initialized successfully.");

                _skeletonCapability = _sensor.UserGenerator.SkeletonCapability;
                _skeletonCapability.SetSkeletonProfile(SkeletonProfile.All);
                _skeletonCapability.SetSmoothing(Properties.Settings.Default.SkeletonSmoothing);

                _sensor.GeneratorUpdate += delegate(object s, EventArgs args)
                {
                    Action action = () =>
                    {
                        _labelDataBrush.ImageSource = _sensor.LabelBitmap;

                        depthImage.Source = _sensor.DepthBitmap;
                        rgbImage.Source = _sensor.RGBBitmap;

                        if (Properties.Settings.Default.DisplaySkeleton && _skeleton.Count > 0)
                        {
                            var users = _skeleton.Keys;

                            Func<int, SkeletonJoint, SkeletonJointPosition> jointPosition = (int user, SkeletonJoint joint) =>
                            {
                                SkeletonJointPosition position = _skeletonCapability.GetSkeletonJointPosition(user, joint);

                                if (position.Position.Z == 0)
                                    position.Confidence = 0;
                                else
                                    position.Position = _sensor.DepthGenerator.ConvertRealWorldToProjective(position.Position);

                                return position;
                            };

                            foreach (int user in users)
                            {
                                _skeleton[user][SkeletonJoint.Head] = jointPosition(user, SkeletonJoint.Head);
                                _skeleton[user][SkeletonJoint.Neck] = jointPosition(user, SkeletonJoint.Neck);
                                _skeleton[user][SkeletonJoint.Torso] = jointPosition(user, SkeletonJoint.Torso);

                                _skeleton[user][SkeletonJoint.LeftShoulder] = jointPosition(user, SkeletonJoint.LeftShoulder);
                                _skeleton[user][SkeletonJoint.LeftElbow] = jointPosition(user, SkeletonJoint.LeftElbow);
                                _skeleton[user][SkeletonJoint.LeftHand] = jointPosition(user, SkeletonJoint.LeftHand);

                                _skeleton[user][SkeletonJoint.RightShoulder] = jointPosition(user, SkeletonJoint.RightShoulder);
                                _skeleton[user][SkeletonJoint.RightElbow] = jointPosition(user, SkeletonJoint.RightElbow);
                                _skeleton[user][SkeletonJoint.RightHand] = jointPosition(user, SkeletonJoint.RightHand);

                                _skeleton[user][SkeletonJoint.LeftHip] = jointPosition(user, SkeletonJoint.LeftHip);
                                _skeleton[user][SkeletonJoint.LeftKnee] = jointPosition(user, SkeletonJoint.LeftKnee);
                                _skeleton[user][SkeletonJoint.LeftFoot] = jointPosition(user, SkeletonJoint.LeftFoot);

                                _skeleton[user][SkeletonJoint.RightHip] = jointPosition(user, SkeletonJoint.RightHip);
                                _skeleton[user][SkeletonJoint.RightKnee] = jointPosition(user, SkeletonJoint.RightKnee);
                                _skeleton[user][SkeletonJoint.RightFoot] = jointPosition(user, SkeletonJoint.RightFoot);
                            }

                            _skeletonDrawer.Draw(_skeleton);
                        }
                    };

                    Dispatcher.Invoke(action);
                };

                _sensor.NewUser += delegate(object s, NewUserEventArgs args)
                {
                    Console.WriteLine("[" + args.ID + "] Found");

                    if(Properties.Settings.Default.DisplaySkeleton)
                        _skeletonCapability.RequestCalibration(args.ID, true);
                };

                _sensor.LostUser += delegate(object s, UserLostEventArgs args)
                {
                    Console.WriteLine("[" + args.ID + "] Lost");

                    _skeleton.Remove(args.ID);

                    Action action = () =>
                    {
                        _skeletonDrawer.UserLost(args.ID);
                    };

                    Dispatcher.Invoke(action);
                };

                _skeletonCapability.CalibrationComplete += delegate(object s, CalibrationProgressEventArgs args)
                {
                    Console.WriteLine("[" + args.ID + "] Calibration status: " + args.Status);

                    if (args.Status == CalibrationStatus.OK)
                    {
                        _skeletonCapability.StartTracking(args.ID);

                        Dictionary<SkeletonJoint, SkeletonJointPosition> jointPositions = new Dictionary<SkeletonJoint, SkeletonJointPosition>();

                        jointPositions.Add(SkeletonJoint.Head, new SkeletonJointPosition());
                        jointPositions.Add(SkeletonJoint.Neck, new SkeletonJointPosition());
                        jointPositions.Add(SkeletonJoint.Torso, new SkeletonJointPosition());

                        jointPositions.Add(SkeletonJoint.LeftShoulder, new SkeletonJointPosition());
                        jointPositions.Add(SkeletonJoint.LeftElbow, new SkeletonJointPosition());
                        jointPositions.Add(SkeletonJoint.LeftHand, new SkeletonJointPosition());

                        jointPositions.Add(SkeletonJoint.RightShoulder, new SkeletonJointPosition());
                        jointPositions.Add(SkeletonJoint.RightElbow, new SkeletonJointPosition());
                        jointPositions.Add(SkeletonJoint.RightHand, new SkeletonJointPosition());

                        jointPositions.Add(SkeletonJoint.LeftHip, new SkeletonJointPosition());
                        jointPositions.Add(SkeletonJoint.LeftKnee, new SkeletonJointPosition());
                        jointPositions.Add(SkeletonJoint.LeftFoot, new SkeletonJointPosition());

                        jointPositions.Add(SkeletonJoint.RightHip, new SkeletonJointPosition());
                        jointPositions.Add(SkeletonJoint.RightKnee, new SkeletonJointPosition());
                        jointPositions.Add(SkeletonJoint.RightFoot, new SkeletonJointPosition());

                        _skeleton.Add(args.ID, jointPositions);
                    }
                    else if (args.Status != CalibrationStatus.ManualAbort)
                        _skeletonCapability.RequestCalibration(args.ID, true);
                };
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
                MessageBox.Show(ex.Message, "Error while initializing OpenNI", MessageBoxButton.OK, MessageBoxImage.Error);
                Application.Current.Shutdown();
            }
        }
Exemplo n.º 2
0
            public BitmapGenerator(Sensor sensor)
            {
                if (sensor == null)
                    throw new ArgumentNullException();

                _lock = new object();
                _sensor = sensor;

                _depthData = new byte[sensor.ImageWidth * sensor.ImageHeight];
                _labelData = new byte[sensor.ImageWidth * sensor.ImageHeight];

                _depth = new WriteableBitmap(sensor.ImageWidth, sensor.ImageHeight, 96, 96, System.Windows.Media.PixelFormats.Gray8, null);
                _label = new WriteableBitmap(sensor.ImageWidth, sensor.ImageHeight, 96, 96, System.Windows.Media.PixelFormats.Gray8, null);
                _rgb = new WriteableBitmap(sensor.ImageWidth, sensor.ImageHeight, 96, 96, System.Windows.Media.PixelFormats.Rgb24, null);

                sensor.GeneratorUpdate += new EventHandler(delegate(object sender, EventArgs e)
                {
                    lock (_lock)
                    {
                        _depthValid = false;
                        _labelValid = false;
                        _rgbValid = false;
                    }
                });
            }