Skip to content

Commit

Permalink
Nested using for bodyFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
KimihikoAkayasaki committed Jan 12, 2024
1 parent b64b65c commit 2fc55bc
Showing 1 changed file with 20 additions and 51 deletions.
71 changes: 20 additions & 51 deletions plugin_KinectOne/KinectOne.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,8 @@ public void Initialize()
{
try
{
try
{
if(InitKinect()) InitializeSkeleton(); // Init?
ShutdownInternal(false); // Shut down
}
catch (Exception)
{
// ignored
}

IsInitialized = InitKinect();
Host.Log($"Tried to initialize the Kinect sensor with status: {DeviceStatusString}");

// Try to start the stream
InitializeSkeleton();
}
catch (Exception e)
{
Expand Down Expand Up @@ -158,10 +145,18 @@ private void ShutdownInternal(bool unInitialize = true)

private bool InitKinect()
{
// One sensor is currently supported
if ((KinectSensor = KinectSensor.GetDefault()) is null) return false;

// Open the reader for the body frames
BodyFrameReader = KinectSensor.BodyFrameSource.OpenReader();

try
{
// Unregister sensor events
KinectSensor.IsAvailableChanged -= StatusChangedHandler;
BodyFrameReader.FrameArrived -= OnBodyFrameArrivedHandler;

// Try to open the kinect sensor
KinectSensor.Open(); // Open 1st
for (var i = 0; i < 20; i++)
Expand All @@ -174,25 +169,9 @@ private bool InitKinect()
// Get connected get connected
Thread.Sleep(1000);

try
{
// Try to open the kinect sensor
KinectSensor.Open(); // Open 2nd
for (var i = 0; i < 20; i++)
{
// Wait for the device to initialize
Thread.Sleep(200);
if (KinectSensor.IsAvailable) break;
}
}
catch
{
// ignored
}

// Register a watchdog (remove, add)
KinectSensor.IsAvailableChanged -= StatusChangedHandler;
// Register a sensor status event, new frame event
KinectSensor.IsAvailableChanged += StatusChangedHandler;
BodyFrameReader.FrameArrived += OnBodyFrameArrivedHandler;

// Check the status and return
return KinectSensor.IsAvailable;
Expand All @@ -210,31 +189,21 @@ private void StatusChangedHandler(object o, IsAvailableChangedEventArgs isAvaila
Host?.RefreshStatusInterface();
}

private void InitializeSkeleton()
{
BodyFrameReader?.Dispose();
BodyFrameReader = KinectSensor.BodyFrameSource.OpenReader();

if (BodyFrameReader is null) return;

// Register a watchdog (remove, add)
BodyFrameReader.FrameArrived -= OnBodyFrameArrivedHandler;
BodyFrameReader.FrameArrived += OnBodyFrameArrivedHandler;
}

private void OnBodyFrameArrivedHandler(object _, BodyFrameArrivedEventArgs args)
{
var dataReceived = false;
using var bodyFrame = args.FrameReference.AcquireFrame();
if (bodyFrame != null)
using (var bodyFrame = args.FrameReference.AcquireFrame())
{
Bodies ??= new Body[bodyFrame.BodyCount];
if (bodyFrame != null)
{
Bodies ??= new Body[bodyFrame.BodyCount];

// The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
// As long as those body objects are not disposed and not set to null in the array,
// those body objects will be re-used.
bodyFrame.GetAndRefreshBodyData(Bodies);
dataReceived = true;
// The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
// As long as those body objects are not disposed and not set to null in the array,
// those body objects will be re-used.
bodyFrame.GetAndRefreshBodyData(Bodies);
dataReceived = true;
}
}

// Validate the result from the sensor
Expand Down

0 comments on commit 2fc55bc

Please sign in to comment.