Kinect的错误使流 [英] Kinect error enabling stream
问题描述
这是我第一次试图让使用Kinect的程序,我不知道为什么我总是收到空
错误。也许有人谁知道KinectSDK可以更好地帮助?
公共ProjKinect()
{
的InitializeComponent() ;
updateSensor(0); //设置电流传感器为0,因为我们刚刚开始
}
公共无效updateSensor(INT sensorI)
{
refreshSensors (); //看看是否有任何新的连接
如果(sensorI> = sensors.Length)//如果去到结束,然后重复
{
sensorI = 0;
}
currentSensorInt = sensorI;
如果(activeSensor = NULL&放大器;!&安培; activeSensor.IsRunning)
{
activeSensor.Stop(); //停止,所以我们可以cahnge
}
的MessageBox .Show(sensors.Length +Kinects找到);
activeSensor = KinectSensor.KinectSensors [currentSensorInt]
activeSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30); //错误就在这里
activeSensor.DepthStream.Enable();
activeSensor.SkeletonStream.Enable();
activeSensor.SkeletonFrameReady + = runtime_SkeletonFrameReady;
activeSensor.DepthFrameReady + = runtime_DepthFrameReady;
activeSensor.ColorFrameReady + = runtime_ImageFrameReady;
activeSensor.Start(); //启动新启用了一个
}
公共无效refreshSensors()
{
传感器= KinectSensor.KinectSensors.ToArray();
}
错误:
<预类=郎无prettyprint-覆盖>
对象引用未设置到对象的实例。
顺便说一句,它说我有1 Kinect的连接,所以我知道它至少承认我有东西连接。它还如果我只是说,不工作 0
而不是 currentSensorInt
的。此外,在 DepthStream.Enable
一个错误,如果我注释掉 ColorStream.Enable
。所以我猜我只是做一些错误创建传感器时?
希望这小东西。提前
感谢:)
我看不出有什么不对公开,但我也没见过传感器获取和设置正好在此之前的方式。你有没有通过 Kinect的为Windows开发工具包的例子吗?还有如何连接到Kinect的多个实例,有的只是蛮力连接,而有些则是相当强劲。
例如,这是对一个修剪版本从SlideshowGestures-WPF例如连接代码:
公共部分类主窗口:窗口
{
// /<总结>
///活动Kinect感应器
///< /总结>
私人KinectSensor传感器;
///<总结>
///执行启动任务
///< /总结>
///< PARAM NAME =发件人>对象发送该事件< /参数>
///< PARAM NAME =E>事件参数< /参数>
私人无效WindowLoaded(对象发件人,RoutedEventArgs E)
{
//翻阅所有的传感器,并开始第一次连接之一。
//这需要一个Kinect是在应用程序启动时连接。
//为了让您的应用程序对插/拔,
稳健//推荐使用KinectSensorChooser在Microsoft.Kinect.Toolkit
的foreach(在KinectSensor.KinectSensors VAR potentialSensor)$ B提供$ b {
如果(potentialSensor.Status == KinectStatus.Connected)
{
this.sensor = potentialSensor;
中断;
}
}
如果(空!= this.sensor)
{
//打开色彩流接收彩色框架
this.sensor.ColorStream.Enable(ColorImageFormat.InfraredResolution640x480Fps30);
//添加事件处理程序,每当有新的颜色帧数据
this.sensor.ColorFrameReady + = this.SensorColorFrameReady被称为;
//启动传感器!
试
{
this.sensor.Start();
}
赶上(IOException异常)
{
this.sensor = NULL;
}
}
}
///<总结>
///执行关闭任务
///< /总结>
///< PARAM NAME =发件人>对象发送该事件< /参数>
///< PARAM NAME =E>事件参数< /参数>
私人无效的windowClosing(对象发件人,System.ComponentModel.CancelEventArgs E)
{
如果(空!= this.sensor)
{
this.sensor.Stop ();
}
}
}
要得到一个最简单的方法传感器虽然是使用 KinectSensorChooser
类,这是 Microsoft.Kinect.Toolkit
命名空间的一部分。它所有的工作适合你。例如,这里是我的设置的修剪版本:
公共类MainViewModel:ViewModelBase
{
私人只读KinectSensorChooser _sensorChooser =新KinectSensorChooser();
///<总结>
///初始化MainViewModel类的新实例。
///< /总结>
公共MainViewModel(IDataService的DataService)
{
如果(IsInDesignMode)
{
//只做设计模式
} $ B $一些特别的东西, b,否则
{
_sensorChooser.Start();
如果(_sensorChooser.Kinect == NULL)
{
MessageBox.Show(无法检测到可用的Kinect感应器);
Application.Current.Shutdown();
}
}
}
这就是它。我有一个感应器,我可以开始使用它。我如何连接并控制的Kinect使用的更大的示例的 KinectSensorManager
从工具包,这是在课堂上 KinectWpfViewers
命名空间:
公共类MainViewModel:ViewModelBase
{
私人只读KinectSensorChooser _sensorChooser =新KinectSensorChooser();
///<总结>
///初始化MainViewModel类的新实例。
///< /总结>
公共MainViewModel(IDataService的DataService)
{
如果(IsInDesignMode)
{
//只做设计模式
} $ B $一些特别的东西, b,否则
{
KinectSensorManager =新KinectSensorManager();
KinectSensorManager.KinectSensorChanged + = OnKinectSensorChanged;
_sensorChooser.Start();
如果(_sensorChooser.Kinect == NULL)
{
MessageBox.Show(无法检测到可用的Kinect感应器);
Application.Current.Shutdown();
}
//从sensorChooser绑定KinectSensor到KinectSensor上KinectSensorManager
变种kinectSensorBinding =新的绑定(Kinect的){源= _sensorChooser};
BindingOperations.SetBinding(this.KinectSensorManager,KinectSensorManager.KinectSensorProperty,kinectSensorBinding);
}
}
#地区的Kinect的发现和放大器;设置
私人无效OnKinectSensorChanged(对象发件人,KinectSensorManagerEventArgs< KinectSensor>参数)
{
如果(!空= args.OldValue)
UninitializeKinectServices(args.OldValue) ;
如果(空= args.NewValue!)
InitializeKinectServices(KinectSensorManager,args.NewValue);
}
///<总结>基于
///初始化Kinect的服务。
///< /总结>
///< PARAM NAME =kinectSensorManager>< /参数>
///< PARAM NAME =传感器>< /参数>
私人无效InitializeKinectServices(KinectSensorManager kinectSensorManager,KinectSensor传感器)
{
//配置颜色流
kinectSensorManager.ColorFormat = ColorImageFormat.RgbResolution640x480Fps30;
kinectSensorManager.ColorStreamEnabled = TRUE;
//配置深度流
kinectSensorManager.DepthStreamEnabled = TRUE;
kinectSensorManager.TransformSmoothParameters =
新TransformSmoothParameters
{
//作为平滑值增加的响应,以原始数据
//减小;因此,增加了平滑导致延迟增加。
平滑= 0.5F,
//更高的价值走向的原始数据更迅速纠正,
//一个较低的值修正更加缓慢并出现平滑。
校正= 0.5F,
//帧数来预测未来。
=预测0.5F,
//确定如何积极地从原始数据消除抖动。
JitterRadius = 0.05f,
//最大半径(米),该过滤位置可以从原始数据偏离。
MaxDeviationRadius = 0.04f
};
//配置骨架流
sensor.SkeletonFrameReady + = OnSkeletonFrameReady;
kinectSensorManager.SkeletonStreamEnabled = TRUE;
//初始化手势识别
_gestureController =新GestureController();
_gestureController.GestureRecognized + = OnGestureRecognized;
kinectSensorManager.KinectSensorEnabled = TRUE;如果
(!kinectSensorManager.KinectSensorAppConflict)
{
//设置除了Kinect的基础的服务在这里
//(例如,SpeechRecognizer)
$} b
$ b kinectSensorManager.ElevationAngle = Settings.Default.KinectAngle;
}
///<总结>
///未初始化的是在InitializeKinectServices初始化所有Kinect的服务。
///< /总结>
///< PARAM NAME =传感器>< /参数>
私人无效UninitializeKinectServices(KinectSensor传感器)
{
sensor.SkeletonFrameReady - = this.OnSkeletonFrameReady;
}
#endregion Kinect的发现和放大器;设置
#区域属性
公共KinectSensorManager KinectSensorManager {搞定;私人集; }
#endregion属性
}
所有的优点这种额外的代码可以在工具包中的 KinectExplorer
的例子可以看出。总之 - 我可以管理多个Kinects与此代码,拔掉一个,只是切换到一个不同的程序
This is my first time trying to make a program that uses the Kinect and I have NO idea why I keep getting a null
error. Maybe someone who knows the KinectSDK better can help?
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.
BTW, it says I have 1 Kinect connected, so I know that it at least recognizes that I have things to connect to. It also doesn't work if I just say 0
instead of currentSensorInt
. Also an error at the DepthStream.Enable
if I comment out the ColorStream.Enable
. So I am guessing I am just doing something wrong when creating the sensor?
Hopefully it's something small. Thanks in advance :)
I don't see anything overtly wrong, but I've also not seen the sensor acquired and setup exactly this way before. Have you had a look through the Kinect for Windows Developer Toolkit examples? There are multiple examples of how to connect to a Kinect, some are simply brute-force connections while others are pretty robust.
For example, this is a trimmed version of the connection code from the SlideshowGestures-WPF example:
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();
}
}
}
The easiest way to get a sensor though is to use the KinectSensorChooser
class, which is part of the Microsoft.Kinect.Toolkit
namespace. It does all the work for you. For example, here is a trimmed version of my setup:
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();
}
}
}
That's it. I've got a sensor and I can start working with it. The larger example of how I connect and control the Kinect uses the KinectSensorManager
class from the Toolkit, which is in the KinectWpfViewers
namespace:
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
}
The advantage of all this extra code can be seen in the KinectExplorer
example in the Toolkit. In short - I can manage multiple Kinects with this code, unplugging one and the program just switches to a different one.
这篇关于Kinect的错误使流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!