Kinect and Lync

Hi, I was asked about combination of those two elements. How to write a plugin for Lync to have a chance to use Kinect for video conferencing propose. So, I do not know, but I can help hare with explanation how to get video frames from Kinect and display this video stream. Here is my method below for getting data frame by frame from Kinect RGB camera. And please forgive me I am not any kind of expert in this area. I just do it this way. And it works pretty well for KinectCam. I hope it will help with build solution for lync.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
using Kinect;

namespace KinectCam
{
    public static class KinectHelper
    {
        static KinectSensor Sensor;
        static readonly ReaderWriterLockSlim sensorLocker = new ReaderWriterLockSlim();

        static void InitializeSensor()
        {
            var sensor = Sensor;
            if (sensor != null) return;
            try
            {
                sensorLocker.EnterReadLock();
                if (KinectSensor.KinectSensors != null && KinectSensor.KinectSensors.Count == 0)
				{
				    return;
				}
                sensor = KinectSensor.KinectSensors[0];
                sensor.Start();
                sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                sensor.AllFramesReady += sensor_AllFramesReady;
                Sensor = sensor;
            }
            catch
            {
                Trace.WriteLine("Error of enable the Kinect sensor!");
            }
            finally
            {
                sensorLocker.ExitReadLock();
            }
        }

        static void sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            sensor_ColorFrameReady(e.OpenColorImageFrame());
        }

        static void sensor_ColorFrameReady(ColorImageFrame image)
        {
            if (image == null) return;

            try
            {
                sensorLocker.EnterWriteLock();
                if (sensorColorFrameData == null)
                    sensorColorFrameData = new byte[image.PixelDataLength];
                image.CopyPixelDataTo(sensorColorFrameData);
                image.Dispose();
                image = null;
            }
            finally
            {
                sensorLocker.ExitWriteLock();
            }
        }

        public static void DisposeSensor()
        {
            try
            {
                sensorLocker.EnterWriteLock();
                var sensor = Sensor;
                if (sensor != null && sensor.IsRunning)
                {
                    if (sensor.ColorStream.IsEnabled)
                    {
                        sensor.ColorStream.Disable();
                    }
                    if (sensor.DepthStream.IsEnabled)
                    {
                        sensor.DepthStream.Disable();
                    }
                    sensor.AllFramesReady -= sensor_AllFramesReady;
                    sensor.Stop();
                    sensor.Dispose();
                    sensor = null;
                    Sensor = null;
                    sensorColorFrameData = null;
                }
            }
            catch {
                Trace.WriteLine("Error of disable the Kinect sensor!");
            }
            finally
            {
                sensorLocker.ExitWriteLock();
            }
        }

        static byte[] sensorColorFrameData;        

        public static byte[] GetColorSensorFrame()
        {
            InitializeSensor();
            try
            {
                sensorLocker.EnterReadLock();
                return sensorColorFrameData;
            }
            finally
            {
                sensorLocker.ExitReadLock();
            }
        }

        private static Random Random = new Random();        

        public unsafe static void GenerateFrame(IntPtr _ptr, int length, bool flipImage)
        {
            byte[] colorFrame = GetColorSensorFrame();
            void* camData = _ptr.ToPointer();
            if (colorFrame != null)
            {
                fixed (byte* colorFrameData = &colorFrame[0])
                {
                    byte* pData = (byte*)camData;
                    byte* sData;
                    byte* sGet;
                    for (int y = 0; y < 480; y++)
                    {
                        for (int x = 0; x < 640; x++)
                        {
                            if (flipImage)
                            {
                                sData = &colorFrameData[(x * 4) + ((479 - y) * 640 * 4)];
                            }
                            else
                            {
                                sData = &colorFrameData[((639 - x) * 4) + ((479 - y) * 640 * 4)];
                            }
                            sGet = sData;
                            var r = *sGet++;
                            var g = *sGet++;
                            var b = *sGet++;
                            *pData++ = r;
                            *pData++ = g;
                            *pData++ = b;
                        }
                    }
                }
            }
            else
            {
                byte* pData = (byte*)camData;
                for (int i = 0; i < length; ++i)
                    *pData++ = (byte)Random.Next();
            }
        }
    }
}

Hope it will help.

P ;).

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.