Skip to content

Instantly share code, notes, and snippets.

@Vangos
Created January 14, 2015 01:15
Show Gist options
  • Save Vangos/83dd1fb433a109c4825b to your computer and use it in GitHub Desktop.
Save Vangos/83dd1fb433a109c4825b to your computer and use it in GitHub Desktop.
using Microsoft.Kinect;
using Microsoft.Kinect.Face;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Kinect2FaceBasics_NET
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
KinectSensor _sensor = null;
ColorFrameReader _colorReader = null;
BodyFrameReader _bodyReader = null;
IList<Body> _bodies = null;
FaceFrameSource _faceSource = null;
FaceFrameSource _faceSource2 = null;
FaceFrameReader _faceReader = null;
FaceFrameReader _faceReader2 = null;
public MainWindow()
{
InitializeComponent();
_sensor = KinectSensor.GetDefault();
if (_sensor != null)
{
_sensor.Open();
_bodies = new Body[_sensor.BodyFrameSource.BodyCount];
_colorReader = _sensor.ColorFrameSource.OpenReader();
_colorReader.FrameArrived += ColorReader_FrameArrived;
_bodyReader = _sensor.BodyFrameSource.OpenReader();
_bodyReader.FrameArrived += BodyReader_FrameArrived;
_faceSource = new FaceFrameSource(_sensor, 0, FaceFrameFeatures.BoundingBoxInColorSpace |
FaceFrameFeatures.FaceEngagement |
FaceFrameFeatures.Glasses |
FaceFrameFeatures.Happy |
FaceFrameFeatures.LeftEyeClosed |
FaceFrameFeatures.MouthOpen |
FaceFrameFeatures.PointsInColorSpace |
FaceFrameFeatures.RightEyeClosed);
_faceSource2 = new FaceFrameSource(_sensor, 0, FaceFrameFeatures.BoundingBoxInColorSpace |
FaceFrameFeatures.FaceEngagement |
FaceFrameFeatures.Glasses |
FaceFrameFeatures.Happy |
FaceFrameFeatures.LeftEyeClosed |
FaceFrameFeatures.MouthOpen |
FaceFrameFeatures.PointsInColorSpace |
FaceFrameFeatures.RightEyeClosed);
_faceReader = _faceSource.OpenReader();
_faceReader2 = _faceSource2.OpenReader();
_faceReader.FrameArrived += FaceReader_FrameArrived;
_faceReader2.FrameArrived += FaceReader_FrameArrived_2;
}
}
void ColorReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e)
{
using (var frame = e.FrameReference.AcquireFrame())
{
if (frame != null)
{
camera.Source = frame.ToBitmap();
}
}
}
void BodyReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
using (var frame = e.FrameReference.AcquireFrame())
{
if (frame != null)
{
frame.GetAndRefreshBodyData(_bodies);
var bodies = _bodies.Where(b => b.IsTracked).ToList();
if (bodies.Count == 2)
{
Body body = bodies[0];
Body body2 = bodies[1];
if (!_faceSource.IsTrackingIdValid)
{
if (body != null)
{
_faceSource.TrackingId = body.TrackingId;
}
}
if (!_faceSource2.IsTrackingIdValid)
{
if (body2 != null)
{
_faceSource2.TrackingId = body2.TrackingId;
}
}
}
}
}
}
void FaceReader_FrameArrived_2(object sender, FaceFrameArrivedEventArgs e)
{
using (var frame = e.FrameReference.AcquireFrame())
{
if (frame != null)
{
Debug.WriteLine("Face 2");
}
}
}
void FaceReader_FrameArrived(object sender, FaceFrameArrivedEventArgs e)
{
using (var frame = e.FrameReference.AcquireFrame())
{
if (frame != null)
{
Debug.WriteLine("Face 1");
}
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (_colorReader != null)
{
_colorReader.Dispose();
_colorReader = null;
}
if (_bodyReader != null)
{
_bodyReader.Dispose();
_bodyReader = null;
}
if (_faceReader != null)
{
_faceReader.Dispose();
_faceReader = null;
}
if (_faceSource != null)
{
_faceSource.Dispose();
_faceSource = null;
}
if (_faceReader2 != null)
{
_faceReader2.Dispose();
_faceReader2 = null;
}
if (_faceSource2 != null)
{
_faceSource2.Dispose();
_faceSource2 = null;
}
if (_sensor != null)
{
_sensor.Close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment