Skip to content

Instantly share code, notes, and snippets.

@tomkerkhove
Created November 30, 2014 17:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomkerkhove/3fe7ad2d717de000766c to your computer and use it in GitHub Desktop.
Save tomkerkhove/3fe7ad2d717de000766c to your computer and use it in GitHub Desktop.
Handle multiple faces
private Dictionary<ulong, FaceFrameSource> _faceSources = new Dictionary<ulong, FaceFrameSource>();
private Dictionary<ulong, FaceFrameReader> _faceReaders = new Dictionary<ulong, FaceFrameReader>();
/// <summary>
/// Process body frames
/// </summary>
private void OnBodyFrameReceived(object sender, BodyFrameArrivedEventArgs e)
{
// Get Frame ref
BodyFrameReference bodyRef = e.FrameReference;
if (bodyRef == null) return;
// Get body frame
using (BodyFrame frame = bodyRef.AcquireFrame())
{
if (frame == null) return;
// Allocate array when required
if (_bodies == null)
_bodies = new Body[frame.BodyCount];
// Refresh bodies
frame.GetAndRefreshBodyData(_bodies);
foreach (Body body in _bodies)
{
if (body.IsTracked && !_faceSources.ContainsKey(body.TrackingId))
{
// Create new sources with body TrackingId
FaceFrameSource faceSource = new FaceFrameSource(_kinect, body.TrackingId, _faceFrameFeatures);
// Create new reader
FaceFrameReader faceReader = _faceSource.OpenReader();
// Wire events
faceReader.FrameArrived += OnFaceFrameArrived;
faceSource.TrackingIdLost += OnTrackingIdLost;
// Add sources to dictionary
_faceSources.Add(body.TrackingId, faceSource);
_faceReaders.Add(body.TrackingId, faceReader);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment