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(); } }
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); }