Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WPF

Kinect Reception

4.94/5 (34 votes)
12 Jan 2012Ms-PL5 min read 71.9K   2K  
TV Screen in the Reception, When nothing happens (no one is in the reception) we can display videos on the screen but when someone enters the frame show him the Kinect Image, and if the user is doing something funny, capture his image and save it.
1.png

Introduction

The Idea: TV Screen in the Reception, When nothing happens (no one is in the reception) we can display videos on the screen but when someone enters the frame, show him the Kinect Image, and if the user is doing something funny, capture his image and save it.

The question is how can I know if the person is doing something funny?

For that, I’ve created Image AuthenticManager that contains a set of rules defining what positions or combinations are funny.

For example: If right hand position is higher than head position, then add 2 points, if the left foot position is crossing the right foot, then add an additional 2 points, etc.
When the user reaches the goal, we decide then to take his picture.

Before jumping to coding, let’s talk about the application flow:
The main window is controlled by two Timers and the AuthenticManager:

SeriousTimer set to 10 secs and it starts Ticking as the Kinect Skeleton Event is first fired (this event will only work when the Kinect identifies a full person skeleton).
Inside the SkeletonFrameReady, we also have integer called _fpsCount that increases itself by 1 each time the SkeletonFrameReady is called after starting the SeriousTimer, this will help us to make sure the user is standing in front of the Kinect and not just walk by him.

Now how can the _fbsCount tell me that? ll we need to do is multiply the SeriousTimer seconds interval by the Minimum Fps (for example 10) and the _fpsCount should be bigger if the user stands in front of the Kinect.

In this case, the Timer will stop the Video feed and will display the Kinect Image.

IdleTimer by default is set to 30 seconds and each time the SkeletonFrameReady method is fired, we restart the IdleTimer.

So if there are no events for SkeletonFrameReady, the IdleTimer will return the Video feed.

JointID - AuthenticManager works with RuleObject that contains JointID Source and JointID Target (More about Joints - Kinect – Getting Started – Become The Incredible Hulk).

AuthenticManager is the heart of Kinect Reception application, this class will check if the user position is funny by your own rules.

11.png

What is Joint?

The data is provided from the Kinect application code as a set of points, called skeleton positions, that compose a skeleton structure.

C#
public enum JointID
  {
    HipCenter = 0,
    Spine = 1,
    ShoulderCenter = 2,
    Head = 3,
    ShoulderLeft = 4,
    ElbowLeft = 5,
    WristLeft = 6,
    HandLeft = 7,
    ShoulderRight = 8,
    ElbowRight = 9,
    WristRight = 10,
    HandRight = 11,
    HipLeft = 12,
    KneeLeft = 13,
    AnkleLeft = 14,
    FootLeft = 15,
    HipRight = 16,
    KneeRight = 17,
    AnkleRight = 18,
    FootRight = 19,
    Count = 20,
  } 
  • Vector - For Source and Target Joints, you also have to define the Vector to check X or Y against one another.
  • Operator – Do you expect the Source Vector to be Bigger or Smaller than the Target Vector?
  • Score – If the rule is true, what is the score for the rule.

3.png

Background

Since Microsoft has released the Kinect.NET SDK, I wrote many articles on that subject:

I think Kinect is very cool and I’m searching for more projects and good ideas for Kinect. A couple of days ago, I talked with my friend Guy Burstein and he came up with an idea for Kinect application, he said what if people will enter the Microsoft Israel reception and instead of video screen showing commercials, let's add something interesting with Kinect.

Using the Code

Using Kinect events, I can see when user enters the frame, using two timers I can check if the user is just passing by or is standing in front of the camera.

The below image describes the application flow, at the beginning the application will show random videos, when the Kinect Skeleton Event will raise then the Serious Timer will begin ticking, each tick based on the FPS rate will be aggregated to variable called NumTicks, when the Serious Timer completes we check if NumTicks is big enough based on the FPS, if so then we’ll start the Idle Timer and switch to Kinect Image.

Idle Timer – Each time the Kinect Skeleton Event is raised, the Idle Timer will be restart, so if there is no one in front of the Kinect camera, the Idle Timer will switch back to Videos.

4.png

As you can see from the images below (or Full Video), when I moved my hands or legs, the Score Bar has changed based on the rules you defined:

5.png

6.png

7.png

You reached the Goal!!!

8.png

The entire application works with 4 managers:

  1. Kinect Manager
  2. Configuration Manager
  3. Video Manager
  4. Authentic Manager

Kinect Manager

I’ve shown how to get started with Kinect many times in my previous posts, but for this application, I’ve created a singleton class to handle everything related to the Kinect settings, from restarting the Kinect RunTime object to changing the camera angle.

C#
public KinectManager()
{
    try
    {

    KinectNui = new Runtime();

    KinectNui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseSkeletalTracking |
                            RuntimeOptions.UseColor);

    KinectNui.VideoStream.Open(ImageStreamType.Video, 2, 
    ImageResolution.Resolution640x480, 
            ImageType.ColorYuv);

    KinectNui.SkeletonEngine.TransformSmooth = true;
    var parameters = new TransformSmoothParameters
                            {
                            Smoothing = 1.0f,
                            Correction = 0.1f,
                            Prediction = 0.1f,
                            JitterRadius = 0.05f,
                            MaxDeviationRadius = 0.05f
                            };
    KinectNui.SkeletonEngine.SmoothParameters = parameters;

    _lastTime = DateTime.Now;
    Camera = KinectNui.NuiCamera;

    IsInitialize = true;
    StatusMessage = Properties.Resources.KinectReady;
    }
    catch (InvalidOperationException ex)
    {
    IsInitialize = false;
    StatusMessage = ex.Message;
    }
} 

Another important method the KinectManager has is the CameraAngle control.

C#
public void ChangeCameraAngle(ChangeDirection dir)
{
    if (!IsInitialize) return;

    try
    {
    if (dir == ChangeDirection.Up)
        Camera.ElevationAngle = Camera.ElevationAngle +
        Properties.Settings.Default.ElevationAngleInterval;
    else
        Camera.ElevationAngle = Camera.ElevationAngle -
        Properties.Settings.Default.ElevationAngleInterval;

    StatusMessage = Properties.Resources.KinectReady;
    }
    catch (InvalidOperationException ex)
    {
    StatusMessage = ex.Message;
    }
    catch (ArgumentOutOfRangeException outOfRangeException)
    {
    StatusMessage = outOfRangeException.Message;
    }
}  

Video Manager

The Video Manager has two specific types of Videos to play and the main Videos folders to pick random video each time.

Specific? When the user enters the Kinect Range, you can choose to play a specific video just before the Kinect Image will appear, and when the user leaves Kinect Range, you can choose to play the Out Video.

Define the type of video you want to play. If you ask for out video and there isn't one, return null - Stop Video and start showing Kinect Image. If you ask for in video and there isn't one, then return random video.

9.png

C#
public static Uri GetVideo(VideoType type)
{
  if (string.IsNullOrEmpty(Properties.Settings.Default.VideosLibraryFolder) ||
      !Directory.Exists(Properties.Settings.Default.VideosLibraryFolder))
      return null;
  else
  {
      string value = null;
      switch (type)
      {
          case VideoType.In:
              value = Properties.Settings.Default.VideosLibraryInFile;
              return string.IsNullOrEmpty(value) || !File.Exists(value) ?
                  CollectRandomMovie() : new Uri(value);
          case VideoType.Out:
              value = Properties.Settings.Default.VideosLibraryOutFile;
              return string.IsNullOrEmpty(value) || !File.Exists(value) ?
                  null : new Uri(value);
          default:
              return CollectRandomMovie();
      }
  }
} 
 private static Uri CollectRandomMovie()
{
    var movies = new ArrayList();

    foreach (var filter in Properties.Settings.Default.VideoFilter)
        movies.AddRange(Directory.GetFiles(Properties.Settings.Default.VideosLibraryFolder,
            filter));

    if (movies.Count == 0) return null;

    var rnd = new Random();
    return new Uri(movies[rnd.Next(movies.Count)].ToString());
} 

Configuration Manager

Kinect Reception allows you to set a range or rules to define what is considered Funny, the rule are based on Joint to Joint and each rule defines the Score if the rule applies.

10.png

The RuleObject contains the Source Joint and Target Joint, Vector to check for both, the operator (Bigger or Smaller) and the Score.

2.png

So what does the configuration manager do? It saves the rules. (Using MemoryStream to translate the Rule to string and then save it into the application resources.)

C#
public static ObservableCollection<RuleObject> Load()
{
    var deserializer = new XmlSerializer(typeof(ObservableCollection<RuleObject>));
    try
    {
        var xs = new XmlSerializer(typeof(ObservableCollection<RuleObject>));
        var memoryStream = 
    new MemoryStream(Encoding.UTF8.GetBytes(Properties.Settings.Default.Rules));
        var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
        return (ObservableCollection<RuleObject>)xs.Deserialize(memoryStream);
    }
    catch (Exception)
    {
        return new ObservableCollection<RuleObject>();
    }
} 
public static void Save(ObservableCollection<RuleObject> items)
{
    try
    {
        var memoryStream = new MemoryStream();
        var xs = new XmlSerializer(typeof(ObservableCollection<RuleObject>));

        var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
        xs.Serialize(xmlTextWriter, items);

        memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
        var xmlizedString = Encoding.UTF8.GetString(memoryStream.ToArray());

        Properties.Settings.Default.Rules = xmlizedString;
    }
    catch (Exception ex)
    {
        throw new ArgumentException(ex.Message);
    }
} 

Authentic Manager

The Authentic Manager is the core of Kinect Reception, he will take all rules defined by you and check them against the Skeleton Joints.

The method below will extract the UnTracked joints and will make sure the joints quality are enough for calculation (We don’t want the user moving out of the picture to be considered as funny Sad smile).

If the Skeleton Joints reach the Goal Score you define then an event will raise telling the main window to save the current Image and display for the user.

C#
public void ChecksForAuthentic(JointsCollection joints)
{
  if (_rules.Count == 0) return;

  var fixJoints =
      joints.Cast<Joint>().Where(
          joint => joint.Position.W >= 0.6f &&
              joint.TrackingState == JointTrackingState.Tracked).ToList();

  var sb = new StringBuilder();
  for (var index = 0; index < _rules.Count; index++)
  {
    var rule = _rules[index];
    var s = (from j in fixJoints.Where(joint => joint.ID == rule.Source) select j).
        DefaultIfEmpty(new Joint() { TrackingState = JointTrackingState.NotTracked }).
        Single();

    var t = (from j in fixJoints.Where(joint => joint.ID == rule.Target) select j).
        DefaultIfEmpty(new Joint() { TrackingState = JointTrackingState.NotTracked }).
        Single();

    if (s.TrackingState == JointTrackingState.NotTracked ||
        t.TrackingState == JointTrackingState.NotTracked) break;

    var sv = s.ToFloat(rule.SourceVector);
    var tv = t.ToFloat(rule.TargetVector);

    if (rule.Operator == Operators.Bigger && sv > tv)
    {
      Score = Score + rule.Score;
      sb.AppendLine(string.Format("Bigger -> Source: {0}, Target:{1} , Vector:{2}",
          rule.Source, rule.Target, rule.SourceVector));
    }
    else if (rule.Operator == Operators.Smaller && sv < tv)
    {
      Score = Score + rule.Score;
      sb.AppendLine(string.Format("Smaller -> Source: {0}, Target:{1} , Vector:{2}",
          rule.Source, rule.Target, rule.SourceVector));
    }
  }

  if (Score >= _goal)
    IsAuthentic(Score, sb.ToString());
} 

History

  • 2nd September, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)