Kinect 启用流时出错

2024-04-25

这是我第一次尝试制作一个使用 Kinect 的程序,我不知道为什么我总是得到一个null错误。也许更了解 KinectSDK 的人可以提供帮助?

public ProjKinect()
{
    InitializeComponent();
    updateSensor(0);//set current sensor as 0 since we just started
}

public void updateSensor(int sensorI)
{
    refreshSensors();//see if any new ones connected
    if (sensorI >= sensors.Length)//if it goes to end, then repeat
    {
        sensorI = 0;
    }
    currentSensorInt = sensorI;
    if (activeSensor != null && activeSensor.IsRunning)
    {
        activeSensor.Stop();//stop so we can cahnge
    }
    MessageBox.Show(sensors.Length + " Kinects Found");
    activeSensor = KinectSensor.KinectSensors[currentSensorInt];
    activeSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30); //ERROR IS RIGHT HERE
    activeSensor.DepthStream.Enable();
    activeSensor.SkeletonStream.Enable();
    activeSensor.SkeletonFrameReady += runtime_SkeletonFrameReady;
    activeSensor.DepthFrameReady += runtime_DepthFrameReady;
    activeSensor.ColorFrameReady += runtime_ImageFrameReady;
    activeSensor.Start();//start the newly enabled one
}
public void refreshSensors()
{
    sensors = KinectSensor.KinectSensors.ToArray();
}

Error:

Object reference not set to an instance of an object.

顺便说一句,它说我连接了 1 个 Kinect,所以我知道它至少识别出我有要连接的东西。如果我只是说它也不起作用0代替currentSensorInt。也出现错误DepthStream.Enable如果我注释掉ColorStream.Enable。所以我猜我只是在创建传感器时做错了什么?

希望这是一件小事。 提前致谢 :)


我没有看到任何明显的错误,但我之前也没有看到传感器以这种方式获取和设置。你有没有看过Kinect for Windows 开发者工具包 http://www.microsoft.com/en-us/kinectforwindows/develop/developer-downloads.aspx例子?有多个如何连接到 Kinect 的示例,有些只是简单的强力连接,而另一些则非常强大。

例如,这是 SlideshowGestures-WPF 示例中连接代码的精简版本:

public partial class MainWindow : Window
{
    /// <summary>
    /// Active Kinect sensor
    /// </summary>
    private KinectSensor sensor;

    /// <summary>
    /// Execute startup tasks
    /// </summary>
    /// <param name="sender">object sending the event</param>
    /// <param name="e">event arguments</param>
    private void WindowLoaded(object sender, RoutedEventArgs e)
    {
        // Look through all sensors and start the first connected one.
        // This requires that a Kinect is connected at the time of app startup.
        // To make your app robust against plug/unplug, 
        // it is recommended to use KinectSensorChooser provided in Microsoft.Kinect.Toolkit
        foreach (var potentialSensor in KinectSensor.KinectSensors)
        {
            if (potentialSensor.Status == KinectStatus.Connected)
            {
                this.sensor = potentialSensor;
                break;
            }
        }

        if (null != this.sensor)
        {
            // Turn on the color stream to receive color frames
            this.sensor.ColorStream.Enable(ColorImageFormat.InfraredResolution640x480Fps30);

            // Add an event handler to be called whenever there is new color frame data
            this.sensor.ColorFrameReady += this.SensorColorFrameReady;

            // Start the sensor!
            try
            {
                this.sensor.Start();
            }
            catch (IOException)
            {
                this.sensor = null;
            }
        }
    }

    /// <summary>
    /// Execute shutdown tasks
    /// </summary>
    /// <param name="sender">object sending the event</param>
    /// <param name="e">event arguments</param>
    private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (null != this.sensor)
        {
            this.sensor.Stop();
        }
    }
}

获得传感器的最简单方法是使用KinectSensorChooser类,它是Microsoft.Kinect.Toolkit命名空间。它为您完成所有工作。例如,这是我的设置的修剪版本:

public class MainViewModel : ViewModelBase
{
    private readonly KinectSensorChooser _sensorChooser = new KinectSensorChooser();

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel(IDataService dataService)
    {
        if (IsInDesignMode)
        {
            // do something special, only for design mode
        }
        else
        {
            _sensorChooser.Start();

            if (_sensorChooser.Kinect == null)
            {
                MessageBox.Show("Unable to detect an available Kinect Sensor");
                Application.Current.Shutdown();
            }
        }
    }

就是这样。我有一个传感器,我可以开始使用它。我如何连接和控制 Kinect 的更大示例使用KinectSensorManager工具包中的类,位于KinectWpfViewers命名空间:

public class MainViewModel : ViewModelBase
{
    private readonly KinectSensorChooser _sensorChooser = new KinectSensorChooser();

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel(IDataService dataService)
    {
        if (IsInDesignMode)
        {
            // do something special, only for design mode
        }
        else
        {
            KinectSensorManager = new KinectSensorManager();
            KinectSensorManager.KinectSensorChanged += OnKinectSensorChanged;

            _sensorChooser.Start();

            if (_sensorChooser.Kinect == null)
            {
                MessageBox.Show("Unable to detect an available Kinect Sensor");
                Application.Current.Shutdown();
            }

            // Bind the KinectSensor from the sensorChooser to the KinectSensor on the KinectSensorManager
            var kinectSensorBinding = new Binding("Kinect") { Source = _sensorChooser };
            BindingOperations.SetBinding(this.KinectSensorManager, KinectSensorManager.KinectSensorProperty, kinectSensorBinding);
        }
    }

    #region Kinect Discovery & Setup

    private void OnKinectSensorChanged(object sender, KinectSensorManagerEventArgs<KinectSensor> args)
    {
        if (null != args.OldValue)
            UninitializeKinectServices(args.OldValue);

        if (null != args.NewValue)
            InitializeKinectServices(KinectSensorManager, args.NewValue);
    }

    /// <summary>
    /// Initialize Kinect based services.
    /// </summary>
    /// <param name="kinectSensorManager"></param>
    /// <param name="sensor"></param>
    private void InitializeKinectServices(KinectSensorManager kinectSensorManager, KinectSensor sensor)
    {
        // configure the color stream
        kinectSensorManager.ColorFormat = ColorImageFormat.RgbResolution640x480Fps30;
        kinectSensorManager.ColorStreamEnabled = true;

        // configure the depth stream
        kinectSensorManager.DepthStreamEnabled = true;

        kinectSensorManager.TransformSmoothParameters =
            new TransformSmoothParameters
            {
                // as the smoothing value is increased responsiveness to the raw data
                // decreases; therefore, increased smoothing leads to increased latency.
                Smoothing = 0.5f,
                // higher value corrects toward the raw data more quickly,
                // a lower value corrects more slowly and appears smoother.
                Correction = 0.5f,
                // number of frames to predict into the future.
                Prediction = 0.5f,
                // determines how aggressively to remove jitter from the raw data.
                JitterRadius = 0.05f,
                // maximum radius (in meters) that filtered positions can deviate from raw data.
                MaxDeviationRadius = 0.04f
            };

        // configure the skeleton stream
        sensor.SkeletonFrameReady += OnSkeletonFrameReady;
        kinectSensorManager.SkeletonStreamEnabled = true;

        // initialize the gesture recognizer
        _gestureController = new GestureController();
        _gestureController.GestureRecognized += OnGestureRecognized;

        kinectSensorManager.KinectSensorEnabled = true;

        if (!kinectSensorManager.KinectSensorAppConflict)
        {
            // set up addition Kinect based services here
            // (e.g., SpeechRecognizer)
        }

        kinectSensorManager.ElevationAngle = Settings.Default.KinectAngle;
    }

    /// <summary>
    /// Uninitialize all Kinect services that were initialized in InitializeKinectServices.
    /// </summary>
    /// <param name="sensor"></param>
    private void UninitializeKinectServices(KinectSensor sensor)
    {
        sensor.SkeletonFrameReady -= this.OnSkeletonFrameReady;
    }

    #endregion Kinect Discovery & Setup

    #region Properties

    public KinectSensorManager KinectSensorManager { get; private set; }

    #endregion Properties
}

所有这些额外代码的优点可以在KinectExplorer工具包中的示例。简而言之 - 我可以使用此代码管理多个 Kinect,拔掉其中一个,程序就会切换到另一个。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Kinect 启用流时出错 的相关文章

  • 数据模板绑定垃圾邮件输出窗口出现错误:找不到管理 FrameworkElemen

    我有问题 System Windows Data 错误 2 找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement BindingExpression 无路径 数据项 空 目标元素是 So
  • 在实体框架拦截器中向 DbScanExpression 添加内部联接

    我正在尝试使用实体框架 CommandTree 拦截器通过 DbContext 向每个查询添加过滤器 为了简单起见 我有两个表 一个称为 User 有两列 UserId 和 EmailAddress 另一个称为 TenantUser 有两列
  • 更改 Qt OpenGL 窗口示例以使用 OpenGL 3.3

    我正在尝试更改 Qt OpenGL 示例以使用更现代的 opengl 版本 330 似乎合适 所以我做了 在 main cpp 上设置版本和配置文件 设置着色器版本 更改着色器以使用统一 它现在构建没有任何错误 但我只看到一个空白窗口 我错
  • 在 Xamarin 中隐藏软键盘

    如何隐藏软键盘以便在聚焦时显示Entry在 Xamarin forms 便携式表单项目中 我假设我们必须为此编写特定于平台的渲染器 但以下内容不起作用 我创建自己的条目子类 public class MyExtendedEntry Entr
  • VS 程序在调试模式下崩溃,但在发布模式下不崩溃?

    我正在 VS 2012 中运行以下程序来尝试 Thrust 函数查找 include cuda runtime h include device launch parameters h include
  • 找不到 assimp-vc140-mt.dll ASSIMP

    我已经从以下位置下载了 Assimp 项目http assimp sourceforge net main downloads html http assimp sourceforge net main downloads html Ass
  • ASP.Net Core 内容配置附件/内联

    我正在从 WebAPI 控制器返回一个文件 Content Disposition 标头值自动设置为 附件 例如 处置 附件 文件名 30956 pdf 文件名 UTF 8 30956 pdf 当它设置为附件时 浏览器将要求保存文件而不是打
  • 从 WebBrowser 控件 C# 获取滚动值

    我试图在 WebBrowser 控件中获取网页的 Y 滚动索引 但无法访问内置滚动条的值 有任何想法吗 对于标准模式下的 IE 使用文档类型 正如你所说 scrollTop是的财产元素 而不是 HtmlDocument htmlDoc th
  • C# 构建一个 webservice 方法,它接受 POST 方法,如 HttpWebRequest 方法

    我需要一个接受 POST 方法的 Web 服务 访问我的服务器正在使用 POST 方法 它向我发送了一个 xml 我应该用一些 xml 进行响应 另一方面 当我访问他时 我已经使用 HttpWebRequest 类进行了管理 并且工作正常
  • 如何从文本文件读取整数到数组

    这就是我想做的 我对此有些不满 但我希望你能容忍我 这对我来说是一个非常新的概念 1 在我的程序中 我希望创建一个包含 50 个整数的数组来保存来自文件的数据 我的程序必须获取用户的文档文件夹的路径 2 文件的名称为 grades txt
  • 将二进制数据从 C# 上传到 PHP

    我想将文件从 Windows C 应用程序上传到运行 PHP 的 Web 服务器 我知道 WebClient UploadFile 方法 但我希望能够分块上传文件 以便我可以监控进度并能够暂停 恢复 因此 我正在读取文件的一部分并使用 We
  • AES 输出是否小于输入?

    我想加密一个字符串并将其嵌入到 URL 中 因此我想确保加密的输出不大于输入 AES 是可行的方法吗 不可能创建任何始终会创建比输入更小的输出的算法 但可以将任何输出反转回输入 如果您允许 不大于输入 那么基本上您只是在谈论同构算法alwa
  • 每个租户的唯一用户名和电子邮件

    我正在使用以下代码编写多租户应用程序ASP NET Core 2 1 我想覆盖默认的与用户创建相关的验证机制 目前我无法创建多个具有相同的用户UserName My ApplicationUser模型有一个名为TenantID 我想要实现的
  • ASP.NET MailMessage.BodyEncoding 和 MailMessage.SubjectEncoding 默认值

    很简单的问题 但我在 MSDN 上找不到答案 查找 ASP NET 将用于的默认值 MailMessage BodyEncoding and MailMessage SubjectEncoding 如果你不在代码中设置它们 Thanks F
  • .NET Core 中的跨平台文件名处理

    如何处理文件名System IO以跨平台方式运行类以使其在 Windows 和 Linux 上运行 例如 我编写的代码在 Windows 上完美运行 但它不会在 Ubuntu Linux 上创建文件 var tempFilename Dat
  • cout 和字符串连接

    我刚刚复习了我的 C 我尝试这样做 include
  • 跨多个域的 ASP.NET 会话

    是否有合适的 NET 解决方案来在多个域上提供持久服务器会话 即 如果该网站的用户在 www site1 com 下登录 他们也将在 www site2 com 下登录 安全是我们正在开发的程序的一个问题 Thanks 它是否需要在会话中
  • C++ Streambuf 方法可以抛出异常吗?

    我正在尝试找到一种方法来获取读取或写入流的字符数 即使存在错误并且读 写结束时间较短 该方法也是可靠的 我正在做这样的事情 return stream rdbuf gt sputn buffer buffer size 但如果streamb
  • 如何在 DropDownList 中保留空格 - ASP.net MVC Razor 视图

    我在视图中通过以下方式绑定我的模型 问题是我的项目文本是格式化文本 单词之间有空格 如下所示 123 First 234 00 123 AnotherItem 234 00 123 Second 234 00 我想保留此项目文本中的空格 即
  • 使我的 COM 程序集调用异步

    我刚刚 赢得 了在当前工作中维护用 C 编码的遗留库的特权 这个dll 公开使用 Uniface 构建的大型遗留系统的方法 除了调用 COM 对象之外别无选择 充当此遗留系统与另一个系统的 API 之间的链接 在某些情况下 使用 WinFo

随机推荐