繁体   English   中英

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

[英]WPF application very slow - Kinect SDK 1.7

我有一个Kinect的简单应用程序,但它似乎消耗了大量资源。 它正常工作1-2分钟,然后滞后变得难以忍受。

这是我的配置:

英特尔酷睿i3 CPU M330 2.13 GHz

4 GB内存

ATI Radeon HD 4570

这是应用程序窗口的代码:

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

这是主菜单(第一个屏幕)的代码:

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

屏幕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);
    }
}

还有更多屏幕,我选择了此屏幕,并在其中使用WpfViewers的KinectDepthViewer。 该屏幕具有最差的延迟,应用程序几乎立即无法使用。 我仅有按钮的其他屏幕开始时没有滞后,但使用2-3分钟后便会滞后。

我不知道我在初始化时做错了什么。 我希望有人能提供一些建议。 我读过,即使在较弱的配置下,人们也不会遇到开发问题,所以我希望不是因为这个原因。

谢谢!

您的图形适配器建议您使用的是笔记本计算机。 尽管您的代码中可能有一些需要改进的地方,但我确实认为问题出在您的硬件上。 具有您列出的规格的笔记本电脑在运行CPU时将遇到麻烦,而您要尝试使用Kinect时会遇到麻烦。

暂无
暂无

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

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