Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / WPF

Kinect Reception

Rate me:
Please Sign up or sign in to vote.
4.94/5 (34 votes)
12 Jan 2012Ms-PL5 min read 67.5K   2K   80   18
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)


Written By
Architect Sela
Israel Israel
Shai Raiten is VS ALM MVP, currently working for Sela Group as a ALM senior consultant and trainer specializes in Microsoft technologies especially Team System and .NET technology. He is currently consulting in various enterprises in Israel, planning and analysis Load and performance problems using Team System, building Team System customizations and adjusts ALM processes for enterprises. Shai is known as one of the top Team System experts in Israel. He conducts lectures and workshops for developers\QA and enterprises who want to specialize in Team System.

My Blog: http://blogs.microsoft.co.il/blogs/shair/

Comments and Discussions

 
QuestionVery good article! Pin
Volynsky Alex8-Jan-14 3:38
professionalVolynsky Alex8-Jan-14 3:38 
GeneralMy vote of 5 Pin
Joezer BH1-Jan-13 19:33
professionalJoezer BH1-Jan-13 19:33 
GeneralMy vote of 5 Pin
ezekjh5-Mar-12 12:37
ezekjh5-Mar-12 12:37 
GeneralRe: My vote of 5 Pin
Shai Raiten30-Sep-12 20:37
Shai Raiten30-Sep-12 20:37 
GeneralMy vote of 5 Pin
delibey22-Feb-12 12:23
delibey22-Feb-12 12:23 
GeneralMy vote of 5 Pin
Sergio Andrés Gutiérrez Rojas12-Jan-12 7:27
Sergio Andrés Gutiérrez Rojas12-Jan-12 7:27 
GeneralMy vote of 5 Pin
thatraja10-Jan-12 3:21
professionalthatraja10-Jan-12 3:21 
GeneralRe: My vote of 5 Pin
Shai Raiten12-Jan-12 2:48
Shai Raiten12-Jan-12 2:48 
GeneralMy vote of 5 Pin
tolw27-Oct-11 3:03
tolw27-Oct-11 3:03 
GeneralRe: My vote of 5 Pin
Shai Raiten12-Jan-12 2:47
Shai Raiten12-Jan-12 2:47 
GeneralMy vote of 5 Pin
noreply-thephoenixprod7-Sep-11 23:34
noreply-thephoenixprod7-Sep-11 23:34 
GeneralRe: My vote of 5 Pin
Shai Raiten8-Sep-11 3:22
Shai Raiten8-Sep-11 3:22 
GeneralWell Done Pin
DaveAuld5-Sep-11 22:42
professionalDaveAuld5-Sep-11 22:42 
GeneralRe: Well Done Pin
Reiss6-Sep-11 0:46
professionalReiss6-Sep-11 0:46 
GeneralMy vote of 5 Pin
GPUToaster™5-Sep-11 19:38
GPUToaster™5-Sep-11 19:38 
GeneralRe: My vote of 5 Pin
Shai Raiten6-Sep-11 18:27
Shai Raiten6-Sep-11 18:27 
Questiongreat fun Pin
ErrolErrol5-Sep-11 18:08
ErrolErrol5-Sep-11 18:08 
AnswerRe: great fun Pin
Shai Raiten6-Sep-11 18:28
Shai Raiten6-Sep-11 18:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.