【问题标题】:checking if kinect is connected or not检查kinect是否连接
【发布时间】:2012-11-13 12:04:42
【问题描述】:

我正在创建一个 WPF/C# 应用程序,它使用 kinect 来移动对象,但它也可以使用鼠标运行。目前我注释掉了 kinect 代码,因为它可以使用鼠标工作。我需要识别 kinect 是否已连接的方法,因此我不必注释掉代码以在未连接时使用鼠标(不会像当前那样抛出异常)并在连接时使用 kinect。 我该怎么做? 信息:我正在使用官方的 Microsoft Kinect SDK(大约一周前下载)

编辑—— 我正在使用这些

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using GridAnimationDemo;
using System.Windows.Threading;
using HtmlAgilityPack;
using System.Xml.Linq;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Net;
using Microsoft.Research.Kinect.Nui;
using Microsoft.Research.Kinect.Audio;
using Microsoft.Research.Kinect;
using Microsoft.Office.Interop.PowerPoint;
using System.Windows.Data;
using Microsoft.Research.Kinect.Samples.CursorControl;
using Coding4Fun.Kinect.Wpf;
using Coding4Fun;
using System.Speech.Synthesis;

无法添加引用和使用 Microsoft.Kinect,因为它会与其中一些内容产生冲突

编辑--

Device dvc = new Device();
            if (dvc.Count.Equals(0))
                MessageBox.Show("apoellin");

我尝试了上面的代码,如果我使用任何未连接 Kinect 的 Kinect 代码,应用程序会崩溃并出现相同的错误

【问题讨论】:

    标签: c# wpf xaml kinect


    【解决方案1】:

    这是《Beginning Kinect Programming with the Microsoft SDK》一书中的代码,它很好地处理了这个问题

    // (in your page/window constructor):
    
    this.KinectDevice = KinectSensor.KinectSensors
    .FirstOrDefault(x => x.Status == KinectStatus.Connected);
    
    // (and create a property like this:)
    
    public KinectSensor KinectDevice
    {
    get { return this._KinectDevice; }
    set
    {
    if (this._KinectDevice != value)
    {
    //Uninitialize
    if (this._KinectDevice != null)
    {
    this._KinectDevice.Stop();
    this._KinectDevice.SkeletonFrameReady -= KinectDevice_SkeletonFrameReady;
    this._KinectDevice.SkeletonStream.Disable();
    this._FrameSkeletons = null;
    }
    
    this._KinectDevice = value;
    
    //Initialize
    if (this._KinectDevice != null)
    {
    if (this._KinectDevice.Status == KinectStatus.Connected)
    {
    this._KinectDevice.SkeletonStream.Enable();
    this._FrameSkeletons = new
    Skeleton[this._KinectDevice.SkeletonStream.FrameSkeletonArrayLength];
    this.KinectDevice.SkeletonFrameReady +=
    KinectDevice_SkeletonFrameReady;
    ColorImageStream colorStream = this._KinectDevice.ColorStream;
    colorStream.Enable();
    this._ColorImageBitmap = new WriteableBitmap(colorStream.FrameWidth,
    colorStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null);
    this._ColorImageBitmapRect = new Int32Rect(0, 0, colorStream.FrameWidth,
    colorStream.FrameHeight);
    this._ColorImageStride = colorStream.FrameWidth * colorStream.FrameBytesPerPixel;
    ColorImageElement.Source = this._ColorImageBitmap;
    this._KinectDevice.ColorFrameReady += Kinect_ColorFrameReady;
    
    this.ColorImageElement.Dispatcher.BeginInvoke(new Action(() =>
    {
    this._ColorImageBitmap = new WriteableBitmap(colorStream.FrameWidth,
    colorStream.FrameHeight,
    96, 96, PixelFormats.Bgr32,
    null);
    this._ColorImageBitmapRect = new Int32Rect(0, 0, colorStream.FrameWidth,
    colorStream.FrameHeight);
    this._ColorImageStride = colorStream.FrameWidth *
    colorStream.FrameBytesPerPixel;
    this._ColorImagePixelData = new byte[colorStream.FramePixelDataLength];
    
    this.ColorImageElement.Source = this._ColorImageBitmap;
    }));
    this._KinectDevice.Start();
    }
    }
    }
    }
    }
    

    【讨论】:

      【解决方案2】:

      您使用的是非常过时的 Kinect for Windows SDK 版本。 Microsoft.Research.Kinect 命名空间来自测试版。

      最新的SDK可以在这里下载:

      http://www.microsoft.com/en-us/kinectforwindows/develop/developer-downloads.aspx

      完成此操作后,下载开发者工具包,也可以从上面的链接获得。它包含多个如何完成许多任务的示例。

      我强烈建议查看 Kinect Explorer 示例。这将向您展示如何使用名为 KinectSensorManager 的数据容器。此类是数据包装器,而不是 SDK 的一部分——它有助于管理 Kinect 传感器。它包含在几个 Toolkit 示例中。

      除其他外,该类会在 Kinect 传感器状态更改时触发事件。因此,您可以设置程序以在适当的事件处理程序中初始化和取消初始化 Kinect。

      【讨论】:

      • 我有 45 个基于该 SDK 的 A4 页面的代码.....改变似乎不是一种选择,因为很多东西都不起作用
      • 好吧,我想这取决于你。您使用的 SDK 非常过时,缺少许多功能,并且与现在和将来存在的大量库不兼容。使用这么旧的 SDK,您会发现对您的问题的帮助要少得多——人们没有它可以测试。此外,您必须在旧 SDK 中完成的大部分工作在新 SDK 中都是免费的(或少得多的努力)。您最终需要切换,最好是现在。
      • 事实上,这个项目是 HCI 课程的学期项目。我得到了这个代码,我必须改变它来做一些额外的任务。不知何故,我也必须添加此任务
      • 我会去争取额外的功劳,并通过将它升级到最新的 SDK 来给你的导师留下深刻印象。 :)
      • 要修改 45 页代码以匹配新的 SDK?太多额外的信用:P 我会看看我能做什么
      【解决方案3】:

      如果您使用的是最新的 Windows SDK,您可以检查 Runtime.Kinects.Count 的值。

      如果值为 0,则没有连接任何 Kinect -

      if (Runtime.Kinects.Count == 0)
      {
          // No Kinects are connected
      }
      else
      {
          // Kinect is connecetd
      }
      

      【讨论】:

      • 我收到此错误“Microsoft.Research.Kinect.Nui.Runtime”不包含“Kinects”的定义我有最新的 Kinect sdk 和工具包
      • @macrian - 我向你保证 Microsoft.Kintect 命名空间有一个 Kintects 类。具体使用类的完整命名空间。
      • @macrian - 你必须指出你的问题有什么变化,我的陈述仍然有效,Kinects 确实存在,所以你如何导入命名空间一定是个问题。
      • 我不能使用 Microsoft.Kinect,因为它会与我正在使用的其他一些内容产生冲突。对于 Kinect,我使用的是 Microsoft.Research.Kinect
      • @macrian,请参阅下面的答案。 Microsoft.Kinect 导致问题,因为您引用了非常旧版本的 Kinect SDK。您需要删除旧版本并更新您的参考资料(将是Microsoft.Kinect
      【解决方案4】:

      我只想为 Kinect 2.0 SDK 添加一个答案。不幸的是,SDK 不再具有 Runtime 命名空间或任何其他列出设备的方式。但是,您可以使用 WMI 来确定是否连接了 Kinect 2.0。

      为此,您需要添加对 System.Management 库的引用。

      public static bool IsConnected()
      {            
          // Use WMI to find devices with the proper hardware id for the Kinect2
          // note that one Kinect2 is listed as three harwdare devices    
          string query = String.Format(WmiQuery, HardwareId);
          using (var searcher = new ManagementObjectSearcher(query))
          {
              using (var collection = searcher.Get())
              {
                  return collection.Count > 0;
              }
          }            
      }
      
      private const string HardwareId = @"VID_045E&PID_02C4";
      private const string WmiQuery = @"SELECT * FROM Win32_PnPEntity WHERE DeviceID LIKE '%{0}%'";
      

      更新,由于 Microsoft 停止了适用于 Windows 的 Kinect 2,现在使用 Xbox One 的适用于 Windows 的 Kinect,我们发现并非所有 Kinect 2 都使用相同的 ID。目前我们使用这些 ID。这似乎有效。

      <!-- Kinect 2 For Xbox -->
      USB\VID_045E&PID_02C4
      <!-- Kinect 2 For Windows -->
      USB\VID_045E&PID_02D9
      

      【讨论】:

      • 硬件 ID 会随着 SDK 的发布而改变吗?我必须使用 VID_045E&PID_02D8 才能找到我的。
      • 到目前为止,我发现 Xbox 和 WIndows 版本的 ID 略有不同,并且至少有 3 个独立的设备代表了整个 Kinect。所有这些都有自己的ID。我希望有一种更清洁的方法来检测它。我不明白为什么要删除此“功能”。
      猜你喜欢
      • 2013-10-29
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 2021-01-02
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多