none
WPF High CPU usage - Kinect SDK 1.7 RRS feed

  • Question

  • I have one simple application for Kinect but it seems it consumes a lot of resources. It works normally for a 1-2 minutes and then lag becomes unbearable.

    This is my configuration:

    Intel Core i3 CPU M330 2.13 GHz

    4 GB RAM

    ATI Radeon HD 4570

    This is code for application window:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
    
        }
    
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
    
            this.WindowState = System.Windows.WindowState.Maximized;
        }
    
        private void StopKinect(KinectSensor sensor)
        {
            if (sensor != null)
            {
                if (sensor.IsRunning)
                {
                    //stop sensor 
                    sensor.Stop();
    
                    //stop audio if not null
                    if (sensor.AudioSource != null)
                    {
                        sensor.AudioSource.Stop();
                    }
    
    
                }
            }
        }
    
        private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
        {
            StopKinect(Generics.GlobalKinectSensorChooser.Kinect);
        }
    }

    This is code for main menu (first screen):

    public partial class MainMenu : Page
    {
        #region "Kinect"
        private KinectSensorChooser sensorChooser;
        #endregion
    
        public MainMenu()
        {
            this.InitializeComponent();
            if (Generics.GlobalKinectSensorChooser == null)
            {
                // initialize the sensor chooser and UI
                this.sensorChooser = new KinectSensorChooser();
                this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged;
                this.sensorChooserUi.KinectSensorChooser = this.sensorChooser;
                this.sensorChooser.Start();
                Generics.GlobalKinectSensorChooser = this.sensorChooser;
            }
            else
            {   // initialize the sensor chooser and UI 
                this.sensorChooser = new KinectSensorChooser();
                this.sensorChooser = Generics.GlobalKinectSensorChooser;
                this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged;
                this.sensorChooserUi.KinectSensorChooser = sensorChooser;
            }
    
            // Bind the sensor chooser's current sensor to the KinectRegion
            var regionSensorBinding = new Binding("Kinect") { Source = this.sensorChooser };
            BindingOperations.SetBinding(this.kinectRegion, KinectRegion.KinectSensorProperty, regionSensorBinding);
    
        }
    
        private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args)
        {
            bool error = false;
            if (args.OldSensor != null)
            {
                try
                {
                    args.OldSensor.DepthStream.Range = DepthRange.Default;
                    args.OldSensor.SkeletonStream.EnableTrackingInNearRange = false;
                    args.OldSensor.DepthStream.Disable();
                    args.OldSensor.SkeletonStream.Disable();
    
                    args.OldSensor.ColorStream.Disable();
                }
                catch (InvalidOperationException)
                {
                    // KinectSensor might enter an invalid state while enabling/disabling streams or stream features.
                    // E.g.: sensor might be abruptly unplugged.
                    error = true;
                }
            }
    
            if (args.NewSensor != null)
            {
                try
                {
                    args.NewSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                    args.NewSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                    args.NewSensor.SkeletonStream.Enable();
                }
                catch (InvalidOperationException)
                {
                    error = true;
                }
            }
    
            if (!error)
                kinectRegion.KinectSensor = args.NewSensor;
        }
    
        private void Screen1ButtonOnClick(object sender, RoutedEventArgs e)
        {
            this.sensorChooser.KinectChanged -= SensorChooserOnKinectChanged;
            (Application.Current.MainWindow.FindName("_mainFrame") as Frame).Source = new Uri("Test.xaml", UriKind.Relative);
        }     
    }

    And this code for screen1:

    public partial class Test : Page
    {
    #region "Kinect"
    private KinectSensorChooser sensorChooser;
    #endregion
    
    const float MaxDepthDistance = 4095; // max value returned
    const float MinDepthDistance = 850; // min value returned
    const float MaxDepthDistanceOffset = MaxDepthDistance - MinDepthDistance;
    
    public Test()
    {
    
    this.InitializeComponent();
    // initialize the sensor chooser and UI
    this.sensorChooser = new KinectSensorChooser();
    //Assign the sensor chooser with the sensor chooser from the mainwindow. 
    //We are reusing the sensorchoosing declared in the first window that can in contact with kinect
    this.sensorChooser = Generics.GlobalKinectSensorChooser;
    //subscribe to the sensorChooserOnKinectChanged event
    this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged;
    //Assign Kinect Sensorchooser to the sensorchooser we got from our static class
    this.sensorChooserUi.KinectSensorChooser = sensorChooser;
    // Bind the sensor chooser's current sensor to the KinectRegion
    var regionSensorBinding = new Binding("Kinect") { Source = this.sensorChooser };
    BindingOperations.SetBinding(this.kinectRegion, KinectRegion.KinectSensorProperty, regionSensorBinding);
    }
    
    private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args)
    {
    bool error = false;
    if (args.OldSensor != null)
    {
    try
    {
    args.OldSensor.DepthStream.Range = DepthRange.Default;
    args.OldSensor.SkeletonStream.EnableTrackingInNearRange = false;
    args.OldSensor.DepthStream.Disable();
    args.OldSensor.SkeletonStream.Disable();
    
    args.OldSensor.ColorStream.Disable();
    }
    catch (InvalidOperationException)
    {
    error = true;
    }
    }
    
    if (args.NewSensor != null)
    {
    try
    {
    args.NewSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
    args.NewSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
    args.NewSensor.SkeletonStream.Enable();
    
    }
    catch (InvalidOperationException)
    {
    error = true;
    }
    }
    
    if (!error)
    kinectRegion.KinectSensor = args.NewSensor;
    }
    
    
    private void MenuButtonOnClick(object sender, RoutedEventArgs e)
    {
    //Unsubscribe to the sensorchooser's event SensorChooseronkinectChanged
    this.sensorChooser.KinectChanged -= SensorChooserOnKinectChanged;
    (Application.Current.MainWindow.FindName("_mainFrame") as Frame).Source = new Uri("MainScreen.xaml", UriKind.Relative);
    }
    }
    

    There are a couple of more screens, I choosed this screen where I'm using KinectDepthViewer from WpfViewers. This screen has the worst lag, application is unusable almost instantly. Other screens where I have just buttons don't have a lag at the beginning but they get it after 2-3 minutes of usage.

    I hope someone has some advice. I tested it on a desktop PC which has a processor with 4 cores. The problem also appears on that configuration. In task manager I see that processor is constantly near 100%. Memory usage also quickly rises to 600-700 MB. I don't think this a normal situation for some simple navigation between screens. When I run more complex examples from Developer Toolkit Browser, I don't have any problems and there is no lag. It's weird that I can't use this simple example for more than a minute because lag becomes unbearable.

    Thank you!

    Wednesday, June 5, 2013 3:25 PM

Answers

All replies

  • While your system may meet a minimum hardware requirement you may want to use a system that meets the recommended requirements given the amount of depth/color/skeletal/audio processing you have listed there. 

    If you need to run on a system with those features, you will need to lower the resolution on the depth/color. Additionally, employing a way of processing every other frame may also provide some perf improvements. Not to mention, WPF will its own overhead for rendering any complex UI that you may have implemented.

    The Intel Core i3 is not a quad core
    http://ark.intel.com/products/47663/Intel-Core-i3-330M-Processor-3M-Cache-2_13-GHz
    # of Cores - 2

    http://msdn.microsoft.com/en-us/library/jj131032.aspx#ID4EVC
    Processor

    • Type: Intel Core i5 Desktop Processor Series or superior
    • Clock Speed: 3.0 GHz  or superior
    • Number of Cores: 4 or more
    • Memory: 4 GB DDR3 1333 or superior
    • Operating System: Operating System: 64-bit version of Windows 7 or 64-bit version of Windows 8

     


    Wednesday, June 5, 2013 6:00 PM
  • Yes, I know that Intel Core i3 is not a quad core. My post is maybe confusing :) I tested it on my i3 and on one desktop PC which has a processor with 4 cores. I thought my configuration would be ok for basic navigation between screens. 
    Wednesday, June 5, 2013 6:31 PM
  • What are the specifications for the Desktop machine? Additionally, how does the system behave with the other samples. Isolate whether or not the issue is with WPF itself or with the Kinect processing of the data. Compare these with the D2D (unmanaged) samples to see if there is a significant differences.

    Wednesday, June 5, 2013 6:52 PM
  • I will check tomorrow detailed specifications of the Desktop machine. I was testing on my colleagues PC.

    Samples provided in developer toolkit work fine on my i3 Notebook.

    Wednesday, June 5, 2013 7:36 PM
  • Hello Helios Zyzz. I'm having the same problem. What I realized is that if I use the mouse to interact with the application nothing happens. You may find some solution to the problem?
    Saturday, June 22, 2013 1:53 AM
  • I think that the problem is not in hardware, I have tested this code on my i5 and after few minutes of using applications slow down, but when I am using interaction gallery sample on the same PC everything is OK. So please @Microsoft give us more information how to use Kinect device in production evironment.
    Sunday, June 23, 2013 6:18 PM