The class provides access to Microsoft's Xbox Kinect controller.

The class allows to manipulate Kinec device by changing its LED color, setting motor's tilt value and accessing its camera. See KinectVideoCamera and KinectDepthCamera classes, which provide access to actual video.

In order to run correctly the class requires freenect.dll library to be put into solution's output folder. This can be found within the AForge.NET framework's distribution in Externals folder.

Sample usage:

// get Kinect device Kinect kinectDevice = Kinect.GetDevice( 0 ); // change LED color kinectDevice.LedColor = LedColorOption.Yellow; // set motor tilt angle to -10 degrees kinectDevice.SetMotorTilt( -10 ); // get video camera KinectVideoCamera videoCamera = kinectDevice.GetVideoCamera( ); // see example for video camera also
Inheritance: IDisposable
Esempio n. 1
0
        /// <summary>
        /// Get initialized instance of the Kinect device.
        /// </summary>
        ///
        /// <param name="deviceID">ID of the Kinect device to get instance of, [0, <see cref="DeviceCount"/>),</param>
        ///
        /// <returns>Returns initialized Kinect device. Use <see cref="Dispose()"/> method
        /// when the device is no longer required.</returns>
        ///
        /// <exception cref="ArgumentException">There is no Kinect device with specified ID connected to the system.</exception>
        /// <exception cref="ConnectionFailedException">Failed connecting to the Kinect device specified ID.</exception>
        ///
        public static Kinect GetDevice(int deviceID)
        {
            if ((deviceID < 0) || (deviceID >= DeviceCount))
            {
                throw new ArgumentException("There is no Kinect device with specified ID connected to the system.");
            }

            bool   needToStartStatusThread = false;
            Kinect kinect = null;

            lock ( openDevices )
            {
                needToStartStatusThread = (openDevices.Count == 0);

                // check if the device is open already
                if (!openDevices.ContainsKey(deviceID))
                {
                    IntPtr devicePointer = IntPtr.Zero;

                    // connect to Kinect device witht the specified ID
                    if (KinectNative.freenect_open_device(KinectNative.Context, ref devicePointer, deviceID) != 0)
                    {
                        throw new ConnectionFailedException("Failed connecting to the Kinect device with ID: " + deviceID);
                    }

                    openDevices.Add(deviceID, new DeviceContext(devicePointer));
                }

                openDevices[deviceID].ReferenceCounter++;
                kinect = new Kinect(openDevices[deviceID].Device, deviceID);
            }

            if (needToStartStatusThread)
            {
                StartStatusThread( );
            }

            return(kinect);
        }
Esempio n. 2
0
        /// <summary>
        /// Stop video source.
        /// </summary>
        ///
        /// <remarks><para>The method stops the video source, so it no longer provides new video frames
        /// and does not consume any resources.</para>
        /// </remarks>
        ///
        public void Stop()
        {
            lock (sync)
            {
                lock (runningCameras)
                {
                    if (device != null)
                    {
                        var deviceFailed = Kinect.IsDeviceFailed(deviceID);

                        if (!deviceFailed)
                        {
                            KinectNative.freenect_stop_video(device.RawDevice);
                        }

                        device.Dispose();
                        device = null;
                        runningCameras.Remove(deviceID);

                        if (PlayingFinished != null)
                        {
                            PlayingFinished(this, (!deviceFailed) ?
                                            ReasonToFinishPlaying.StoppedByUser : ReasonToFinishPlaying.DeviceLost);
                        }
                    }

                    if (imageBuffer != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(imageBuffer);
                        imageBuffer = IntPtr.Zero;
                    }

                    videoCallback = null;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Stop video source.
        /// </summary>
        /// 
        /// <remarks><para>The method stop the video source, so it no longer provides new video frames
        /// and does not consume any resources.</para>
        /// </remarks>
        ///
        public void Stop( )
        {
            lock ( sync )
            {
                lock ( runningCameras )
                {
                    if ( device != null )
                    {
                        bool deviceFailed = device.IsDeviceFailed( deviceID );

                        if ( !deviceFailed )
                        {
                            KinectNative.freenect_stop_depth( device.RawDevice );
                        }

                        device.Dispose( );
                        device = null;
                        runningCameras.Remove( deviceID );

                        if ( PlayingFinished != null )
                        {
                            PlayingFinished( this, ( !deviceFailed ) ?
                                ReasonToFinishPlaying.StoppedByUser : ReasonToFinishPlaying.DeviceLost );
                        }
                    }

                    if ( imageBuffer != IntPtr.Zero )
                    {
                        Marshal.FreeHGlobal( imageBuffer );
                        imageBuffer = IntPtr.Zero;
                    }

                    videoCallback = null;
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Start video source.
        /// </summary>
        /// 
        /// <remarks>Starts video source and returns execution to caller. Video camera will be started
        /// and will provide new video frames through the <see cref="NewFrame"/> event.</remarks>
        /// 
        /// <exception cref="ArgumentException">The specified resolution is not supported for the selected
        /// mode of the Kinect depth sensor.</exception>
        /// <exception cref="ConnectionFailedException">Could not connect to Kinect's depth sensor.</exception>
        /// <exception cref="DeviceBusyException">Another connection to the specified depth sensor is already running.</exception>
        /// 
        public void Start( )
        {
            lock ( sync )
            {
                lock ( runningCameras )
                {
                    if ( device == null )
                    {
                        bool success = false;

                        try
                        {
                            if ( runningCameras.Contains( deviceID ) )
                            {
                                throw new DeviceBusyException( "Another connection to the specified depth camera is already running." );
                            }

                            device = Kinect.GetDevice( deviceID );

                            // find depth format parameters
                            depthModeInfo = KinectNative.freenect_find_depth_mode( resolution, KinectNative.DepthCameraFormat.Format11Bit );

                            if ( depthModeInfo.IsValid == 0 )
                            {
                                throw new ArgumentException( "The specified resolution is not supported for the selected mode of the Kinect depth camera." );
                            }

                            // set depth format
                            if ( KinectNative.freenect_set_depth_mode( device.RawDevice, depthModeInfo ) != 0 )
                            {
                                throw new VideoException( "Could not switch to the specified depth format." );
                            }

                            // allocate video buffer and provide it freenect
                            imageBuffer = Marshal.AllocHGlobal( (int) depthModeInfo.Bytes );
                            KinectNative.freenect_set_depth_buffer( device.RawDevice, imageBuffer );

                            // set video callback
                            videoCallback = new KinectNative.FreenectDepthDataCallback( HandleDataReceived );
                            KinectNative.freenect_set_depth_callback( device.RawDevice, videoCallback );

                            // start the camera
                            if ( KinectNative.freenect_start_depth( device.RawDevice ) != 0 )
                            {
                                throw new ConnectionFailedException( "Could not start depth stream." );
                            }

                            success = true;
                            runningCameras.Add( deviceID );

                            device.AddFailureHandler( deviceID, Stop );
                        }
                        finally
                        {
                            if ( !success )
                            {
                                if ( device != null )
                                {
                                    device.Dispose( );
                                    device = null;
                                }

                                if ( imageBuffer != IntPtr.Zero )
                                {
                                    Marshal.FreeHGlobal( imageBuffer );
                                    imageBuffer = IntPtr.Zero;
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Start video source.
        /// </summary>
        /// 
        /// <remarks>Starts video source and returns execution to caller. Video camera will be started
        /// and will provide new video frames through the <see cref="NewFrame"/> event.</remarks>
        /// 
        /// <exception cref="ArgumentException">The specified resolution is not supported for the selected
        /// mode of the Kinect video camera.</exception>
        /// <exception cref="ConnectionFailedException">Could not connect to Kinect's video camera.</exception>
        /// <exception cref="DeviceBusyException">Another connection to the specified video camera is already running.</exception>
        /// 
        public void Start( )
        {
            lock ( sync )
            {
                lock ( runningCameras )
                {
                    if ( device == null )
                    {
                        bool success = false;

                        try
                        {
                            if ( runningCameras.Contains( deviceID ) )
                            {
                                throw new DeviceBusyException( "Another connection to the specified video camera is already running." );
                            }

                            // get Kinect device
                            device = Kinect.GetDevice( deviceID );

                            KinectNative.VideoCameraFormat dataFormat = KinectNative.VideoCameraFormat.RGB;

                            if ( cameraMode == VideoCameraMode.Bayer )
                            {
                                dataFormat = KinectNative.VideoCameraFormat.Bayer;
                            }
                            else if ( cameraMode == VideoCameraMode.InfraRed )
                            {
                                dataFormat = KinectNative.VideoCameraFormat.IR8Bit;
                            }

                            // find video format parameters
                            videoModeInfo = KinectNative.freenect_find_video_mode( resolution, dataFormat );

                            if ( videoModeInfo.IsValid == 0 )
                            {
                                throw new ArgumentException( "The specified resolution is not supported for the selected mode of the Kinect video camera." );
                            }

                            // set video format
                            if ( KinectNative.freenect_set_video_mode( device.RawDevice, videoModeInfo ) != 0 )
                            {
                                throw new VideoException( "Could not switch to the specified video format." );
                            }

                            // allocate video buffer and provide it freenect
                            imageBuffer = Marshal.AllocHGlobal( (int) videoModeInfo.Bytes );
                            KinectNative.freenect_set_video_buffer( device.RawDevice, imageBuffer );

                            // set video callback
                            videoCallback = new KinectNative.FreenectVideoDataCallback( HandleDataReceived );
                            KinectNative.freenect_set_video_callback( device.RawDevice, videoCallback );

                            // start the camera
                            if ( KinectNative.freenect_start_video( device.RawDevice ) != 0 )
                            {
                                throw new ConnectionFailedException( "Could not start video stream." );
                            }

                            success = true;
                            runningCameras.Add( deviceID );
                        }
                        finally
                        {
                            if ( !success )
                            {
                                if ( device != null )
                                {
                                    device.Dispose( );
                                    device = null;
                                }

                                if ( imageBuffer != IntPtr.Zero )
                                {
                                    Marshal.FreeHGlobal( imageBuffer );
                                    imageBuffer = IntPtr.Zero;
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Get initialized instance of the Kinect device.
        /// </summary>
        /// 
        /// <param name="deviceID">ID of the Kinect device to get instance of, [0, <see cref="DeviceCount"/>),</param>
        /// 
        /// <returns>Returns initialized Kinect device. Use <see cref="Dispose()"/> method
        /// when the device is no longer required.</returns>
        /// 
        /// <exception cref="ArgumentException">There is no Kinect device with specified ID connected to the system.</exception>
        /// <exception cref="ConnectionFailedException">Failed connecting to the Kinect device specified ID.</exception>
        /// 
        public static Kinect GetDevice( int deviceID )
        {
            if ( ( deviceID < 0 ) || ( deviceID >= DeviceCount ) )
            {
                throw new ArgumentException( "There is no Kinect device with specified ID connected to the system." );
            }

            bool needToStartStatusThread = false;
            Kinect kinect = null;

            lock ( openDevices )
            {
                needToStartStatusThread = ( openDevices.Count == 0 );

                // check if the device is open already
                if ( !openDevices.ContainsKey( deviceID ) )
                {
                    IntPtr devicePointer = IntPtr.Zero;

                    // connect to Kinect device witht the specified ID
                    if ( KinectNative.freenect_open_device( KinectNative.Context, ref devicePointer, deviceID ) != 0 )
                    {
                        throw new ConnectionFailedException( "Failed connecting to the Kinect device with ID: " + deviceID );
                    }

                    openDevices.Add( deviceID, new DeviceContext( devicePointer ) );
                }

                openDevices[deviceID].ReferenceCounter++;
                kinect = new Kinect( openDevices[deviceID].Device, deviceID );
            }

            if ( needToStartStatusThread )
            {
                StartStatusThread( );
            }

            return kinect;
        }
Esempio n. 7
0
        /// <summary>
        /// Start video source.
        /// </summary>
        ///
        /// <remarks>Starts video source and returns execution to caller. Video camera will be started
        /// and will provide new video frames through the <see cref="NewFrame"/> event.</remarks>
        ///
        /// <exception cref="ArgumentException">The specified resolution is not supported for the selected
        /// mode of the Kinect video camera.</exception>
        /// <exception cref="ConnectionFailedException">Could not connect to Kinect's video camera.</exception>
        /// <exception cref="DeviceBusyException">Another connection to the specified video camera is already running.</exception>
        ///
        public void Start()
        {
            lock (sync)
            {
                lock (runningCameras)
                {
                    if (device == null)
                    {
                        var success = false;

                        try
                        {
                            if (runningCameras.Contains(deviceID))
                            {
                                throw new DeviceBusyException("Another connection to the specified video camera is already running.");
                            }

                            // get Kinect device
                            device = Kinect.GetDevice(deviceID);

                            var dataFormat = KinectNative.VideoCameraFormat.RGB;

                            if (cameraMode == VideoCameraMode.Bayer)
                            {
                                dataFormat = KinectNative.VideoCameraFormat.Bayer;
                            }
                            else if (cameraMode == VideoCameraMode.InfraRed)
                            {
                                dataFormat = KinectNative.VideoCameraFormat.IR8Bit;
                            }

                            // find video format parameters
                            videoModeInfo = KinectNative.freenect_find_video_mode(resolution, dataFormat);

                            if (videoModeInfo.IsValid == 0)
                            {
                                throw new ArgumentException("The specified resolution is not supported for the selected mode of the Kinect video camera.");
                            }

                            // set video format
                            if (KinectNative.freenect_set_video_mode(device.RawDevice, videoModeInfo) != 0)
                            {
                                throw new VideoException("Could not switch to the specified video format.");
                            }

                            // allocate video buffer and provide it freenect
                            imageBuffer = Marshal.AllocHGlobal((int)videoModeInfo.Bytes);
                            KinectNative.freenect_set_video_buffer(device.RawDevice, imageBuffer);

                            // set video callback
                            videoCallback = new KinectNative.FreenectVideoDataCallback(HandleDataReceived);
                            KinectNative.freenect_set_video_callback(device.RawDevice, videoCallback);

                            // start the camera
                            if (KinectNative.freenect_start_video(device.RawDevice) != 0)
                            {
                                throw new ConnectionFailedException("Could not start video stream.");
                            }

                            success = true;
                            runningCameras.Add(deviceID);

                            Kinect.AddFailureHandler(deviceID, Stop);
                        }
                        finally
                        {
                            if (!success)
                            {
                                if (device != null)
                                {
                                    device.Dispose();
                                    device = null;
                                }

                                if (imageBuffer != IntPtr.Zero)
                                {
                                    Marshal.FreeHGlobal(imageBuffer);
                                    imageBuffer = IntPtr.Zero;
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Start video source.
        /// </summary>
        ///
        /// <remarks>Starts video source and returns execution to caller. Video camera will be started
        /// and will provide new video frames through the <see cref="NewFrame"/> event.</remarks>
        ///
        /// <exception cref="ArgumentException">The specified resolution is not supported for the selected
        /// mode of the Kinect depth sensor.</exception>
        /// <exception cref="ConnectionFailedException">Could not connect to Kinect's depth sensor.</exception>
        /// <exception cref="DeviceBusyException">Another connection to the specified depth sensor is already running.</exception>
        ///
        public void Start( )
        {
            lock ( sync )
            {
                lock ( runningCameras )
                {
                    if (device == null)
                    {
                        bool success = false;

                        try
                        {
                            if (runningCameras.Contains(deviceID))
                            {
                                throw new DeviceBusyException("Another connection to the specified depth camera is already running.");
                            }

                            device = Kinect.GetDevice(deviceID);

                            // find depth format parameters
                            depthModeInfo = KinectNative.freenect_find_depth_mode(resolution, KinectNative.DepthCameraFormat.Format11Bit);

                            if (depthModeInfo.IsValid == 0)
                            {
                                throw new ArgumentException("The specified resolution is not supported for the selected mode of the Kinect depth camera.");
                            }

                            // set depth format
                            if (KinectNative.freenect_set_depth_mode(device.RawDevice, depthModeInfo) != 0)
                            {
                                throw new VideoException("Could not switch to the specified depth format.");
                            }

                            // allocate video buffer and provide it freenect
                            imageBuffer = Marshal.AllocHGlobal((int)depthModeInfo.Bytes);
                            KinectNative.freenect_set_depth_buffer(device.RawDevice, imageBuffer);

                            // set video callback
                            videoCallback = new KinectNative.FreenectDepthDataCallback(HandleDataReceived);
                            KinectNative.freenect_set_depth_callback(device.RawDevice, videoCallback);

                            // start the camera
                            if (KinectNative.freenect_start_depth(device.RawDevice) != 0)
                            {
                                throw new ConnectionFailedException("Could not start depth stream.");
                            }

                            success = true;
                            runningCameras.Add(deviceID);

                            device.AddFailureHandler(deviceID, Stop);
                        }
                        finally
                        {
                            if (!success)
                            {
                                if (device != null)
                                {
                                    device.Dispose( );
                                    device = null;
                                }

                                if (imageBuffer != IntPtr.Zero)
                                {
                                    Marshal.FreeHGlobal(imageBuffer);
                                    imageBuffer = IntPtr.Zero;
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 9
0
        // Connect to Kinect cameras
        private bool Connect()
        {
            bool ret = false;

            Cursor = Cursors.WaitCursor;

            if (Kinect.DeviceCount != 0)
            {
                int deviceID = devicesCombo.SelectedIndex;

                try
                {
                    kinectDevice = Kinect.GetDevice(deviceID);

                    if (videoCamera == null)
                    {
                        videoCamera = kinectDevice.GetVideoCamera();
                        videoCamera.CameraMode = (videoModeCombo.SelectedIndex == 0) ? VideoCameraMode.Color :
                            ((videoModeCombo.SelectedIndex == 1) ? VideoCameraMode.Bayer : VideoCameraMode.InfraRed);
                        videoCameraPlayer.VideoSource = videoCamera;
                        videoCameraPlayer.Start();
                    }

                    if (depthCamera == null)
                    {
                        depthCamera = kinectDevice.GetDepthCamera();
                        depthCameraPlayer.VideoSource = depthCamera;
                        depthCameraPlayer.Start();
                    }

                    ledColorCombo.SelectedIndex = 0;

                    if (tiltUpDown.Value != 0)
                    {
                        tiltUpDown.Value = 0;
                    }
                    else
                    {
                        kinectDevice.SetMotorTilt((int)tiltUpDown.Value);
                    }

                    timer.Start();

                    ret = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed connecting to Kinect device.\n" + ex.Message,
                        "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    Disconnect();
                }
            }

            Cursor = Cursors.Default;

            return ret;
        }
Esempio n. 10
0
        // Disconnect from Kinect cameras
        private void Disconnect()
        {
            timer.Stop();

            if (videoCamera != null)
            {
                videoCameraPlayer.VideoSource = null;
                videoCamera.Stop();
                videoCamera = null;
            }

            if (depthCamera != null)
            {
                depthCameraPlayer.VideoSource = null;
                depthCamera.Stop();
                depthCamera = null;
            }

            if (kinectDevice != null)
            {
                kinectDevice.Dispose();
                kinectDevice = null;
            }
        }
Esempio n. 11
0
        private void btnSelectCamera_Click(object sender, EventArgs e)
        {
            VideoCaptureDeviceForm form = new VideoCaptureDeviceForm();

            if (form.ShowDialog() == DialogResult.OK)
            {
                int deviceId = form.DeviceId;

                kinectDevice = Kinect.GetDevice(deviceId);

                if (videoCamera == null)
                {
                    videoCamera = kinectDevice.GetVideoCamera();
                    videoCamera.CameraMode = VideoCameraMode.Color;
                    videoCamera.NewFrame += new AForge.Video.NewFrameEventHandler(videoCamera_NewFrame);
                    videoCamera.Start();
                }

                if (depthCamera == null)
                {
                    depthCamera = new KinectDepthCamera(deviceId, CameraResolution.Medium,
                        provideOriginalDepthImage: true);

                    videoSourcePlayer1.VideoSource = depthCamera;
                    videoSourcePlayer1.Start();
                }

                toolStripStatusLabel1.Text = "Initializing...";
            }
        }