简体   繁体   English

WPF应用程序非常慢-Kinect SDK 1.7

[英]WPF application very slow - Kinect SDK 1.7

I have one simple application for Kinect but it seems it consumes a lot of resources. 我有一个Kinect的简单应用程序,但它似乎消耗了大量资源。 It works normally for a 1-2 minutes and then lag becomes unbearable. 它正常工作1-2分钟,然后滞后变得难以忍受。

This is my configuration: 这是我的配置:

Intel Core i3 CPU M330 2.13 GHz 英特尔酷睿i3 CPU M330 2.13 GHz

4 GB RAM 4 GB内存

ATI Radeon HD 4570 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("Depth.xaml", UriKind.Relative);
    }     
}

And this code for screen1: 屏幕1的代码如下:

public partial class Depth : 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 Depth()
    {

        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. 还有更多屏幕,我选择了此屏幕,并在其中使用WpfViewers的KinectDepthViewer。 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. 我仅有按钮的其他屏幕开始时没有滞后,但使用2-3分钟后便会滞后。

I don't know am I doing something wrong in initialization maybe. 我不知道我在初始化时做错了什么。 I hope someone has some advice. 我希望有人能提供一些建议。 I've read that people didn't have problems with development on even weaker configurations so I hope it's not because of that. 我读过,即使在较弱的配置下,人们也不会遇到开发问题,所以我希望不是因为这个原因。

Thank you! 谢谢!

your graphics adapter suggests you're using a notebook computer. 您的图形适配器建议您使用的是笔记本计算机。 While there might be something in your code that could be improved, I really think the problem is your hardware. 尽管您的代码中可能有一些需要改进的地方,但我确实认为问题出在您的硬件上。 Notebooks with the specs you listed will have trouble running something as CPU intensive as what you're trying to do with the Kinect. 具有您列出的规格的笔记本电脑在运行CPU时将遇到麻烦,而您要尝试使用Kinect时会遇到麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM