为什么我的SkeletonFrameReady不起作用 [英] Why doesn't my SkeletonFrameReady work

查看:121
本文介绍了为什么我的SkeletonFrameReady不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,所以我正在为Kinect编写一个控制台应用程序,我遇到了一个问题。由于某种原因我的sensor_SkeletonFrameReady; event不会处理kinect骨架事件,也不会运行代码。所以我得到的只是sensor.Start();
sensor.Stop(); 

Hello everyone so I am writing a console application for the Kinect and I am running into a problem. For some reason with my sensor_SkeletonFrameReady; event wont handle the kinect skeleton events and never runs the code. So all I get is sensor.Start(); sensor.Stop(); 

不是100%肯定我做错了但是如果你们可以查看我的代码并看看我做错了会是大。谢谢!

Not 100% sure what I am doing wrong but if you guys can look over my code and see if I did anything wrong would be great. Thanks!

namespace FinalProject
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Threading;
    using Microsoft.Kinect;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Diagnostics;

    public class program
    {
        public static void Main(String[] args)
        {
            KinectSensor sensor = KinectSensor.KinectSensors.Where(
                s => s.Status == KinectStatus.Connected).FirstOrDefault();

            if (sensor == null)
            {
                Console.WriteLine("No Kinect Sensor was found! Ensure the Kinect is " +
                    "connected\n and reopen the application");
                Console.ReadKey(true);
                return;
            }

            Tracker tracker = new Tracker(sensor);

            sensor.Start();

            sensor.Stop();
        }

        internal class Tracker
        {
            private Skeleton[] skeletons = null;

            public Tracker(KinectSensor sensor)
            {
                sensor.SkeletonFrameReady += sensor_SkeletonFrameReady;
                sensor.SkeletonStream.Enable();
            }

            private void sensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
            {
                using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
                {
                    if (skeletonFrame != null)
                    {
                        if (this.skeletons == null)
                        {
                            this.skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                        }

                        skeletonFrame.CopySkeletonDataTo(this.skeletons);

                        Skeleton skeleton = this.skeletons.Where(s => s.TrackingState ==
                            SkeletonTrackingState.Tracked).FirstOrDefault();

                        if (skeleton != null)
                        {
                            Joint rightHand = skeleton.Joints[JointType.HandRight];

                            if (rightHand.TrackingState == JointTrackingState.Tracked)
                            {
                                Console.WriteLine("Right Hand Position: " + rightHand.Position.X +
                                    "," + rightHand.Position.Y + "," + rightHand.Position.Z);
                            }
                        }
                    }
                }
            }
        }
    }
}

推荐答案

控制台应用程序无法处理事件。此外,您将应用程序设计设置为以main开头,然后结束。您没有停止执行代码来完成主函数,因此应用程序已完成。

A console application has no way to handle events. Additionally, you application design is set to start with main and then will end. You are not stopping the execution of the code from completing the main function and therefore the application is done.

如果要使用事件模型,则需要一个提供Windows消息泵的应用程序框架,以便您可以收到帧就绪事件的通知。这是一个如何在Windows中编写应用程序而不是特定于Kinect的主题。如果您需要代码
开始,我们建议您查看提供的样本并从那里开始。

If you want to use event model, you need an application framework that provides a Windows message pump so you can be notified of the frame ready events. This is a topic for how to write application in Windows and not specific to Kinect. If you need code to start from, we recommend you have a look at the provided samples and go from there.


这篇关于为什么我的SkeletonFrameReady不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆