Пример #1
0
        public KinectManager()
        {
            try
            {
                _context = new Context(@"..\..\Data\openniconfig.xml");
                _depth_generator = _context.FindExistingNode(NodeType.Depth) as DepthGenerator;
                if (_depth_generator == null)
                    throw new Exception(@"Error in Data\openniconfig.xml - No depth node found.");

                _user_generator = new UserGenerator(_context);
                _skeleton_caps = _user_generator.SkeletonCapability;
                _pose_detect_caps = _user_generator.PoseDetectionCapability;
                _calibration_pose = _skeleton_caps.CalibrationPose;

                // event handler for detection
                _user_generator.NewUser += (_user_generator_NewUser);
                _user_generator.LostUser += (_user_generator_LostUser);
                _pose_detect_caps.PoseDetected += (_pose_detect_caps_PoseDetected);
                _skeleton_caps.CalibrationEnd += (_skeleton_caps_CalibrationEnd);

                _skeleton_caps.SetSkeletonProfile(SkeletonProfile.All);

                // initialize joints
                _joints = new Dictionary<int, Dictionary<SkeletonJoint, SkeletonJointPosition>>();
                _joint_orientation = new Dictionary<int, Dictionary<SkeletonJoint, SkeletonJointOrientation>>();

                // start generating data
                _user_generator.StartGenerating();

            }catch(Exception ex)
            {
                Console.WriteLine("Error initializing OpenNi.");
                Console.WriteLine(ex.Message);
            }

            // update timer for the depth image
            DispatcherTimer dispatcher_timer = new DispatcherTimer();
            dispatcher_timer.Tick += new EventHandler(dispatcher_timer_Tick);
            dispatcher_timer.Interval = new TimeSpan(0, 0, 0, 0, 10); // update every 10 ms
            dispatcher_timer.Start();
            Console.WriteLine("Finished loading");
        }
Пример #2
0
        public UserController(Context context, string calibrationPath)
        {
            this.Context = context;
            this.CalibrationPath = calibrationPath;
            this.Users = new Dictionary<int, KinectUser>();
            _UserLock = new object();
            _SkeletonLock = new object();

            _UserGenerator = new UserGenerator(Context);
            _SkeletonCapability = _UserGenerator.SkeletonCapability;
            _PoseDetectionCapability = _UserGenerator.PoseDetectionCapability;

            _CalibrationPose = _SkeletonCapability.CalibrationPose;

            _UserGenerator.NewUser += new EventHandler<NewUserEventArgs>(_UserGenerator_NewUser);
            _UserGenerator.LostUser += new EventHandler<UserLostEventArgs>(_UserGenerator_LostUser);
            _PoseDetectionCapability.PoseDetected += new EventHandler<PoseDetectedEventArgs>(PoseDetectionCapability_PoseDetected);
            _SkeletonCapability.CalibrationComplete += new EventHandler<CalibrationProgressEventArgs>(_SkeletonCapability_CalibrationComplete);

            _SkeletonCapability.SetSkeletonProfile(SkeletonProfile.All);
            _UserGenerator.StartGenerating();
        }
        // 初期化
        private void xnInitialize()
        {
            // コンテキストの初期化
              ScriptNode scriptNode;
              context = Context.CreateFromXmlFile( CONFIG_XML_PATH, out scriptNode );

              // 鏡モード(反転)にしない
              context.GlobalMirror = false;

              // イメージジェネレータの作成
              image = context.FindExistingNode(NodeType.Image) as ImageGenerator;
              if (image == null) {
            throw new Exception(context.GlobalErrorState);
              }

              // デプスジェネレータの作成
              depth = context.FindExistingNode(NodeType.Depth) as DepthGenerator;
              if (depth == null) {
            throw new Exception(context.GlobalErrorState);
              }

              // デプスの座標をイメージに合わせる
              depth.AlternativeViewpointCapability.SetViewpoint(image);

              // ユーザージェネレータの作成
              user = context.FindExistingNode(NodeType.User) as UserGenerator;
              if (depth == null) {
            throw new Exception(context.GlobalErrorState);
              }

              // ユーザー検出機能をサポートしているか確認
              if (!user.IsCapabilitySupported("User::Skeleton")) {
            throw new Exception("ユーザー検出をサポートしていません");
              }

              // ユーザー認識のコールバックを登録
              user.NewUser += new EventHandler<NewUserEventArgs>(user_NewUser);
              user.LostUser += new EventHandler<UserLostEventArgs>(user_LostUser);

              //キャリブレーションにポーズが必要か確認
              skelton = user.SkeletonCapability;
              if (skelton.DoesNeedPoseForCalibration) {
            // ポーズ検出のサポートチェック
            if (!user.IsCapabilitySupported("User::PoseDetection")) {
              throw new Exception("ユーザー検出をサポートしていません");
            }

            // キャリブレーションポーズの取得
            pose = skelton.CalibrationPose;

            // ポーズ検出のコールバックを登録
            PoseDetectionCapability poseDetect = user.PoseDetectionCapability;
            poseDetect.PoseDetected += new EventHandler<PoseDetectedEventArgs>(poseDetect_PoseDetected);
            poseDetect.OutOfPose += new EventHandler<OutOfPoseEventArgs>( poseDetect_OutOfPose );
              }

              // キャリブレーションのコールバックを登録
              skelton.CalibrationStart += new EventHandler<CalibrationStartEventArgs>(skelton_CalibrationStart);
              skelton.CalibrationComplete += new EventHandler<CalibrationProgressEventArgs>( skelton_CalibrationComplete );

              // すべてをトラッキングする
              skelton.SetSkeletonProfile(SkeletonProfile.All);

              // ジェスチャーの検出開始
              context.StartGeneratingAll();
        }
Пример #4
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();
            }
        }
Пример #5
0
        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            if (FContextChanged)
            {
                if (FContextIn.PluginIO.IsConnected)
                {
                    if (FContextIn[0] != null)
                    {
                        try
                        {
                            FUserGenerator = new UserGenerator(FContextIn[0]);
                            FSkeletonCapability = FUserGenerator.SkeletonCapability;
                            FPoseDetectionCapability = FUserGenerator.PoseDetectionCapability;
                            FCalibPose = FSkeletonCapability.CalibrationPose;

                            FUserGenerator.NewUser += userGenerator_NewUser;
                            FUserGenerator.LostUser += userGenerator_LostUser;
                            FPoseDetectionCapability.PoseDetected += poseDetectionCapability_PoseDetected;
                            FSkeletonCapability.CalibrationComplete += skeletonCapbility_CalibrationComplete;

                            FSkeletonCapability.SetSkeletonProfile(FSkeletonProfileIn[0]);
                            FJoints = new Dictionary<int, Dictionary<SkeletonJoint, SkeletonJointTransformation>>();

                            FUserGenerator.StartGenerating();

                            FContextChanged = false;
                        }
                        catch (Exception ex)
                        {
                            FLogger.Log(ex);
                        }
                    }
                }
                else
                {
                    CleanUp();
                    FContextChanged = false;
                }
            }

            if ( (FUserGenerator != null) && (FUserIdIn != null) )
            {
                if (FEnabledIn.IsChanged)
                    if (FEnabledIn[0])
                        FUserGenerator.StartGenerating();
                    else
                        FUserGenerator.StopGenerating();

                if (FUserGenerator.IsDataNew)
                {
                    if (FSkeletonProfileIn.IsChanged)
                        FSkeletonCapability.SetSkeletonProfile(FSkeletonProfileIn[0]);

                    if (FSmoothingIn.IsChanged)
                        FSkeletonCapability.SetSmoothing(FSmoothingIn[0]);

                    FUserIdOut.SliceCount = 0;
                    FStatusOut.SliceCount = 0;

                    FJointPositionOut.SliceCount = 0;
                    FJointOrientationXOut.SliceCount = 0;
                    FJointOrientationYOut.SliceCount = 0;
                    FJointOrientationZOut.SliceCount = 0;

                    if (FUserGenerator.NumberOfUsers > 0)
                    {
                        //get all Users and sort them
                        int[] users = FUserGenerator.GetUsers();

                        FUserIdOut.SliceCount = users.Length;
                        FStatusOut.SliceCount = users.Length;

                        Array.Sort(users);
                        for (int i = 0; i<users.Length; i++) {

                            int userId = users[i];
                            bool userIsTracked = FSkeletonCapability.IsTracking(userId);
                            bool userIsCalibrated = FSkeletonCapability.IsCalibrated(userId);
                            bool userIsCalibrating = FSkeletonCapability.IsCalibrating(userId);

                            if(userIsTracked) FStatusOut[i] = "Tracking user ";
                            else if(userIsCalibrating) FStatusOut[i] = "Calibrating user ";
                            else FStatusOut[i] = "Looking for pose on user ";
                            FStatusOut[i] += userId.ToString();
                            FUserIdOut[i] = userId;

                            //if((userId != FUserIdIn[0]) && (!userIsTracked)) FSkeletonCapability.StopTracking(userId);

                            if(FUserIdIn.IsChanged && (userId != FUserIdIn[0]))
                            {
                                //if(!userIsTracked) FSkeletonCapability.StartTracking(userId);

                            }

                        } //for

                        if(FSkeletonCapability.IsTracking(FUserIdIn[0]))
                        {

                            int binSize = FJointIn.SliceCount;
                            FJointPositionOut.SliceCount = binSize;
                            FJointOrientationXOut.SliceCount = FJointOrientationYOut.SliceCount = FJointOrientationZOut.SliceCount = binSize;

                            for (int i = 0; i < binSize; i++)
                            {
                                var j = GetJoint(FUserIdIn[0], FJointIn[i]);

                                var p = j.Position.Position;
                                if (j.Position.Confidence >= FConfidenceIn[0])
                                    FJointPositionOut[i] = new Vector3D(p.X, p.Y, p.Z) / 1000;

                                var o = j.Orientation;
                                if (o.Confidence > FConfidenceIn[0])
                                {
                                    FJointOrientationXOut[i] = new Vector3D(o.X1, o.Y1, o.Z1);
                                    FJointOrientationYOut[i] = new Vector3D(o.X2, o.Y2, o.Z2);
                                    FJointOrientationZOut[i] = new Vector3D(o.X3, o.Y3, o.Z3);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                FUserIdOut.SliceCount = 0;
                FStatusOut.SliceCount = 0;
                FJointPositionOut.SliceCount = 0;
                FJointOrientationXOut.SliceCount = 0;
                FJointOrientationYOut.SliceCount = 0;
                FJointOrientationZOut.SliceCount = 0;
            }
        }
Пример #6
0
    void Start()
    {
        InitializeCharacter ();
        context = new Context (XML_SETUP_FILE);
        sessionManager = new SessionManager(context, "Wave", "RaiseHand");
        depthGenerator = context.FindExistingNode(NodeType.Depth) as DepthGenerator;
        handsGenerator = context.FindExistingNode(NodeType.Hands) as HandsGenerator;

        //image = context.FindExistingNode(NodeType.Image) as ImageGenerator;
        //gesture = context.FindExistingNode(NodeType.Gesture) as GestureGenerator;
        if (depthGenerator == null) throw new Exception("Viewer must have a depth node!");
        //if (gesture == null) throw new Exception("Viewer must have a gesture node!");
        //if (image == null) throw new Exception("Viewer must have a image node!");

        userGenerator = new UserGenerator (context);
        skeletonCapability = userGenerator.SkeletonCapability;
        poseDetectionCapability = userGenerator.PoseDetectionCapability;
        calibPose = skeletonCapability.CalibrationPose;
        //gestureGenerator = new GestureGenerator(context);

        foreach (String s in poseDetectionCapability.GetAllAvailablePoses())
            Debug.LogWarning("available pose found: " +s);

        //foreach (String s in gestureGenerator.EnumerateAllGestures())
        //	Debug.LogWarning("available gesture found: " + s);

        //gestureGenerator.GestureRecognized += HandleGestureGestureRecognized;
        //gesture.AddGesture("Click");
        //gestureGenerator.AddGesture("RaiseHand"); //seems buggy
        //gestureGenerator.AddGesture("Wave");

        swipeDetector = new SwipeDetector();
        //steadyDetector = new SteadyDetector();//(3, 1f);

        //swipeDetector.SwipeLeft += HandleSwipeDetectorSwipeLeft;

        //Session Managment
        sessionManager.SessionStart += HandleSessionManagerSessionStart;
        sessionManager.SessionEnd += HandleSessionManagerSessionEnd;

        //User Generator
        userGenerator.NewUser += HandleUserGeneratorNewUser;
        userGenerator.LostUser += HandleUserGeneratorLostUser;
        poseDetectionCapability.PoseDetected += HandlePoseDetectionCapabilityPoseDetected;
        skeletonCapability.CalibrationEnd += HandleSkeletonCapabilityCalibrationEnd;

        skeletonCapability.SetSkeletonProfile (SkeletonProfile.All);
        skeletonCapability.SetSmoothing(.5f); // give us some smooothing

        //Start generating
        userGenerator.StartGenerating();
        //handsGenerator.MirrorCapability.SetMirror(true); // TODO: should this be true?
        handsGenerator.StartGenerating();
        //gestureGenerator.StartGenerating();

        //slider selector
        //selectableSlider2D = new SelectableSlider2D(Screen.width, Screen.height);
        //selectableSlider2D.ValueChange += HandleSelectableSlider2DValueChange;
        //selectableSlider2D.ItemHover += HandleSelectableSlider2DItemHover;
        //selectableSlider2D.ItemSelect += HandleSelectableSlider2DItemSelect;

        pointDenoiser = new PointDenoiser();
        pointDenoiser.AddListener(swipeDetector);
        //pointDenoiser.AddListener(steadyDetector);
        //pointDenoiser.AddListener(selectableSlider2D);

        //flowRouter = new FlowRouter();
        //flowRouter.ActiveListener = pointDenoiser;

        sessionManager.AddListener(pointDenoiser);

        MapOutputMode mapMode = depthGenerator.MapOutputMode;

        // Init depth & label map related stuff
        usersMapSize = mapMode.XRes * mapMode.YRes;
        //usersLabelTexture = new Texture2D(mapMode.XRes, mapMode.YRes); //nonPOT slow
        usersLabelTexture = new Texture2D(1024,512);
        //speed up by using power of two and then setpixel() with blockwidth (640) and blockheight (480)
        usersMapColors = new Color[usersMapSize];
        usersMapRect = new Rect(Screen.width - usersLabelTexture.width / 2, Screen.height - usersLabelTexture.height / 2, usersLabelTexture.width / 2, usersLabelTexture.height / 2);
        usersLabelMap = new short[usersMapSize];
        usersDepthMap = new short[usersMapSize];
        usersHistogramMap = new float[5000];

        this.shouldRun = true;

        if (NiteInitializingEvent != null) //notify others that we're done initializing
            NiteInitializingEvent(this, EventArgs.Empty);
    }
        public MainWindow()
        {
            InitializeComponent();

            trackingUser = false;
            trackingUserId = 0;
            stop = false;

            console = new Console();
            console.Show();
            console.Top = 10;
            console.Left = 10;

            prompt = new UserPrompt();
            prompt.Show();
            prompt.Top = 10;
            prompt.Left = 550;

            this.Top = 250;
            this.Left = 550;

            context = new Context(CONFIG_FILE);
            imageGenerator = new ImageGenerator(context);
            userGenerator = new UserGenerator(context);

            if (DRAW_SKELETON)
            {
                depthGenerator = new DepthGenerator(context);
                depthBitmap = new WriteableBitmap(640, 480, DPI_X, DPI_Y, PixelFormats.Rgb24, null);
                depthData = new DepthMetaData();
                Histogram = new int[depthGenerator.DeviceMaxDepth];
                skeletonDraw = new SkeletonDraw();
            }

            poseDetectionCapability = userGenerator.PoseDetectionCapability;
            skeletonCapability = userGenerator.SkeletonCapability;

            imageBitmap = new WriteableBitmap(640, 480, DPI_X, DPI_Y, PixelFormats.Rgb24, null);
            imageData = new ImageMetaData();

            Device.SetUp();

            if (File.Exists(SpatialController.CALIBRATION_DATA_FILE))
            {
                spatialController = new SpatialController(ControllerStartup.FromFile, userGenerator);
            }
            else
            {
                spatialController = new SpatialController(ControllerStartup.Calibrate, userGenerator);
            }

            spatialController.RecalibrateCommand += new RecalibrateEventHandler(RecalibrateCommand);

            userGenerator.NewUser += NewUser;
            userGenerator.LostUser += LostUser;

            skeletonCapability.CalibrationStart += CalibrationStart;
            skeletonCapability.CalibrationEnd += CalibrationEnd;
            skeletonCapability.SetSkeletonProfile(SkeletonProfile.All);
            poseDetectionCapability.PoseDetected += PoseDetected;
            poseDetectionCapability.PoseEnded += PoseEnded;

            kinectDataThread = new Thread(new ThreadStart(ReadKinectData));
            kinectDataThread.IsBackground = true;

            checkGesturesThread = new Thread(new ThreadStart(CheckGestures));
            checkGesturesThread.IsBackground = true;

            imageTimer = new DispatcherTimer();
            imageTimer.Tick += new EventHandler(ImageTick);
            imageTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);

            kinectDataThread.Start();
            checkGesturesThread.Start();
            imageTimer.Start();

            UserPrompt.Write("Finished loading window.");
            Console.Write("Finished loading window");

            UserPrompt.Write("Please assume the Psi pose and hold it until you see a skeleton overlaid"
                    + " on the streaming video.");
        }