Hello!
When you work with apps on Kinect that require a special code for each body recognized by the sensor, the entry point tends to be the collection of bodies returning to us in the line 15 of the first block in the following code example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Array for the bodies | |
/// </summary> | |
private readonly Body[] _bodies; | |
private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) | |
{ | |
var dataReceived = false; | |
var hasTrackedBody = false; | |
using (var bodyFrame = e.FrameReference.AcquireFrame()) | |
{ | |
if (bodyFrame != null) | |
{ | |
bodyFrame.GetAndRefreshBodyData(_bodies); | |
dataReceived = true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) | |
{ | |
var dataReceived = false; | |
var hasTrackedBody = false; | |
var fullBodies = new Body[_kinectSensor.BodyFrameSource.BodyCount]; | |
using (var bodyFrame = e.FrameReference.AcquireFrame()) | |
{ | |
if (bodyFrame != null) | |
{ | |
bodyFrame.GetAndRefreshBodyData(fullBodies); | |
dataReceived = true; | |
} | |
} | |
var trackedBodiesCount = fullBodies.Count(body => body.IsTracked); | |
_bodies = new Body[trackedBodiesCount]; | |
var bodyToAddIndex = 0; | |
for (var bodyIndex = 0; bodyIndex < fullBodies.Length; bodyIndex++) | |
{ | |
var body = _bodies[bodyIndex]; | |
if (!body.IsTracked) continue; | |
_bodies[bodyToAddIndex] = body; | |
bodyToAddIndex++; | |
} |
The problem with these lines is the collection of bodies it is usually complete by 6 elements, however there are no 6 people in front of the Kinect. The following image shows the collection and at the same, the 6th element is a correct body, but with the property IsTracked = False .
The solution is quite simple, a small array preprocessing, filtering by the bodies that are correctly identified. In line 16 of the second block of code we obtain the total number of bodies where IsTracked == True and from there to be processed.
Happy Codding
Greetings @ Home
/El Bruno