/// <summary> /// Deactivate this sensor and free up the memory. /// </summary> public void Stop() { // Acquire the control mutex. mControlMutex.WaitOne(); // If we are already not active, bail. if (!bActive) { mControlMutex.ReleaseMutex(); return; } // If we have a processing thread, shut it down. if (pProcessingThread != null) { if (!pProcessingThread.Join(10000)) { mControlMutex.ReleaseMutex(); Log.Write("Could not stop processing thread within 1 second. Aborting. You may need to restart the application.", "Application", Log.Type.AppError); pProcessingThread.Abort(); mControlMutex.WaitOne(); } pProcessingThread = null; } #region Sensor shutdown and buffer removal // Unhook the events. this.pSensor.AllFramesReady -= Handle_AllFramesReady; this.pSensor.Stop(); // Remove the buffers. pInterfacePixels = null; pPointCloud = null; tDepthPixels = null; tDepthToColourIB = null; tDepthToSkeleton = null; tColourPixels = null; // Remove the reference to the sensor. this.pSensor = null; #endregion // Set our flag to be not active. bActive = false; // Release the mutex. mControlMutex.ReleaseMutex(); }
/// <summary> /// Activate this sensor. /// </summary> /// <param name="pSensor">The sensor which we want to invoke processing on.</param> public void Start(KinectSensor pSensor, bool bUseThread = false) { // Acquire the control mutex. mControlMutex.WaitOne(); // If we are already active, bail. if (bActive) { mControlMutex.ReleaseMutex(); throw new ArgumentException("Cannot start processing on Kinect sensor when another one is already active."); } // Reset the streaming flag. bStreamedFirstFrame = false; // If we are using threading. if (bUseThread) { // Release the mutex (so we can re-enter). mControlMutex.ReleaseMutex(); // Check for problems. if (pProcessingThread != null) { throw new Exception("Cannot start Kinect. Thread is already active."); } // Create a thread which will start the sensor. pProcessingThread = new Thread(() => { this.Start(pSensor, false); Console.WriteLine("Kinect Thread = " + pProcessingThread.Name); }); //pProcessingThread.Name = "Processing Thread"; pProcessingThread.Start(); return; } #region Sensor Setup and Buffer Creation // Set the reference on the sensor. this.pSensor = pSensor; this.pSensor.AllFramesReady += Handle_AllFramesReady; // Enable the streams we are interested in. pSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30); pSensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30); // Work out the maximum amount of pixels we will need to process. var iMaxDepthPixelCount = pSensor.DepthStream.FrameWidth * pSensor.DepthStream.FrameHeight; // Create double buffers for the interface pixels and point cloud. pInterfacePixels = new Utilities.DoubleBuffer <byte[]>(new byte[iMaxDepthPixelCount * 4], new byte[iMaxDepthPixelCount * 4]); pPointCloud = new Utilities.DoubleBuffer <Utilities.Triple <int, SlimMath.Vector3[], int[]> >( new Utilities.Triple <int, SlimMath.Vector3[], int[]>(0, new SlimMath.Vector3[iMaxDepthPixelCount], new int[iMaxDepthPixelCount]), new Utilities.Triple <int, SlimMath.Vector3[], int[]>(0, new SlimMath.Vector3[iMaxDepthPixelCount], new int[iMaxDepthPixelCount]) ); // Create buffers (each is the size of the depth stream * 4bpp where necessary). tDepthPixels = new DepthImagePixel[iMaxDepthPixelCount]; tDepthToColourIB = new ColorImagePoint[iMaxDepthPixelCount]; tDepthToSkeleton = new SkeletonPoint[iMaxDepthPixelCount]; // Create a buffer for the colour video feed (the size of the colour stream * 4bpp). tColourPixels = new byte[pSensor.ColorStream.FramePixelDataLength]; // Setup stream properties. this.iDepthWidth = pSensor.DepthStream.FrameWidth; this.iDepthHeight = pSensor.DepthStream.FrameHeight; this.iColourWidth = pSensor.ColorStream.FrameWidth; this.iColourHeight = pSensor.ColorStream.FrameHeight; this.iColourToDepthDivisor = this.iColourWidth / this.iDepthWidth; // If we have not got an enabled pixel array, make one. if (this._EnabledPixels == null) { this._EnabledPixels = new System.Collections.BitArray(iMaxDepthPixelCount, true); } #endregion // Reset the FPS counter. _FPSCounter.Reset(); // Start the sensor. this.pSensor.Start(); // Set our flag to be active. bActive = true; // Release the mutex. mControlMutex.ReleaseMutex(); }
/// <summary> /// Activate this sensor. /// </summary> /// <param name="pSensor">The sensor which we want to invoke processing on.</param> public void Start(KinectSensor pSensor, bool bUseThread = false) { // Acquire the control mutex. mControlMutex.WaitOne(); // If we are already active, bail. if (bActive) { mControlMutex.ReleaseMutex(); throw new ArgumentException("Cannot start processing on Kinect sensor when another one is already active."); } // Reset the streaming flag. bStreamedFirstFrame = false; // If we are using threading. if (bUseThread) { // Release the mutex (so we can re-enter). mControlMutex.ReleaseMutex(); // Check for problems. if (pProcessingThread != null) throw new Exception("Cannot start Kinect. Thread is already active."); // Create a thread which will start the sensor. pProcessingThread = new Thread(() => { this.Start(pSensor, false); Console.WriteLine("Kinect Thread = " + pProcessingThread.Name); }); //pProcessingThread.Name = "Processing Thread"; pProcessingThread.Start(); return; } #region Sensor Setup and Buffer Creation // Set the reference on the sensor. this.pSensor = pSensor; this.pSensor.AllFramesReady += Handle_AllFramesReady; // Enable the streams we are interested in. pSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30); pSensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30); // Work out the maximum amount of pixels we will need to process. var iMaxDepthPixelCount = pSensor.DepthStream.FrameWidth * pSensor.DepthStream.FrameHeight; // Create double buffers for the interface pixels and point cloud. pInterfacePixels = new Utilities.DoubleBuffer<byte[]>(new byte[iMaxDepthPixelCount * 4], new byte[iMaxDepthPixelCount * 4]); pPointCloud = new Utilities.DoubleBuffer<Utilities.Triple<int, SlimMath.Vector3[], int[]>>( new Utilities.Triple<int, SlimMath.Vector3[], int[]>(0, new SlimMath.Vector3[iMaxDepthPixelCount], new int[iMaxDepthPixelCount]), new Utilities.Triple<int, SlimMath.Vector3[], int[]>(0, new SlimMath.Vector3[iMaxDepthPixelCount], new int[iMaxDepthPixelCount]) ); // Create buffers (each is the size of the depth stream * 4bpp where necessary). tDepthPixels = new DepthImagePixel[iMaxDepthPixelCount]; tDepthToColourIB = new ColorImagePoint[iMaxDepthPixelCount]; tDepthToSkeleton = new SkeletonPoint[iMaxDepthPixelCount]; // Create a buffer for the colour video feed (the size of the colour stream * 4bpp). tColourPixels = new byte[pSensor.ColorStream.FramePixelDataLength]; // Setup stream properties. this.iDepthWidth = pSensor.DepthStream.FrameWidth; this.iDepthHeight = pSensor.DepthStream.FrameHeight; this.iColourWidth = pSensor.ColorStream.FrameWidth; this.iColourHeight = pSensor.ColorStream.FrameHeight; this.iColourToDepthDivisor = this.iColourWidth / this.iDepthWidth; // If we have not got an enabled pixel array, make one. if (this._EnabledPixels == null) this._EnabledPixels = new System.Collections.BitArray(iMaxDepthPixelCount, true); #endregion // Reset the FPS counter. _FPSCounter.Reset(); // Start the sensor. this.pSensor.Start(); // Set our flag to be active. bActive = true; // Release the mutex. mControlMutex.ReleaseMutex(); }