Search Unity

Animate a model with Kinect body tracking. (Kinect v2)

Discussion in 'Scripting' started by Abyss000, Jan 20, 2015.

  1. Abyss000

    Abyss000

    Joined:
    May 12, 2013
    Posts:
    10
    Hi all.

    I am developing an app for unity with the official Kinect v2 SDK plugin for Unity. I have Unity Pro by the way.

    I have a model rigged with the same joints that kinect generates by itself and I want to animate my model by moving each of the joints by the value that kinect gets.

    The thing is that I am getting some troubles with this and I need some help.

    I am able to pay whoever that help me to achieve my goal 150€. This can be negociated.

    This is my actual code based in the official Kinect v2 SDK examples for unity.
    I use public GameObjects that I asign manually to test. Actually I am just trying to animate the left arm for now.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Kinect = Windows.Kinect;
    5.  
    6. public class BodySourceView : MonoBehaviour
    7. {
    8.     //TEST
    9.     public GameObject shoulderLeft;
    10.     public GameObject elbowLeft;
    11.     public GameObject wristLeft;
    12.     public GameObject handLeft;
    13.     public GameObject thumbLeft;
    14.     public GameObject handTipLeft;
    15.  
    16.     public Material BoneMaterial;
    17.     public GameObject BodySourceManager;
    18.  
    19.     private Dictionary<ulong, GameObject> _Bodies = new Dictionary<ulong, GameObject>();
    20.     private BodySourceManager _BodyManager;
    21.  
    22.     private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
    23.     {
    24.         { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
    25.         { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
    26.         { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
    27.         { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
    28.      
    29.         { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
    30.         { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
    31.         { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
    32.         { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
    33.      
    34.         { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
    35.         { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
    36.         { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
    37.         { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
    38.         { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
    39.         { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
    40.      
    41.         { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
    42.         { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
    43.         { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
    44.         { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
    45.         { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
    46.         { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
    47.      
    48.         { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
    49.         { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
    50.         { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
    51.         { Kinect.JointType.Neck, Kinect.JointType.Head },
    52.     };
    53.  
    54.     void Update ()
    55.     {
    56.         if (BodySourceManager == null)
    57.         {
    58.             return;
    59.         }
    60.      
    61.         _BodyManager = BodySourceManager.GetComponent<BodySourceManager>();
    62.         if (_BodyManager == null)
    63.         {
    64.             return;
    65.         }
    66.      
    67.         Kinect.Body[] data = _BodyManager.GetData();
    68.         if (data == null)
    69.         {
    70.             return;
    71.         }
    72.      
    73.         List<ulong> trackedIds = new List<ulong>();
    74.         foreach(var body in data)
    75.         {
    76.             if (body == null)
    77.             {
    78.                 continue;
    79.               }
    80.              
    81.             if(body.IsTracked)
    82.             {
    83.                 trackedIds.Add (body.TrackingId);
    84.             }
    85.         }
    86.      
    87.         List<ulong> knownIds = new List<ulong>(_Bodies.Keys);
    88.      
    89.         // First delete untracked bodies
    90.         foreach(ulong trackingId in knownIds)
    91.         {
    92.             if(!trackedIds.Contains(trackingId))
    93.             {
    94.                 Destroy(_Bodies[trackingId]);
    95.                 _Bodies.Remove(trackingId);
    96.             }
    97.         }
    98.  
    99.         // Processing just the first body.
    100.         if (data[0] != null) {
    101.             if (data[0].IsTracked) {
    102.                 if(!_Bodies.ContainsKey(data[0].TrackingId))
    103.                 {
    104.                     _Bodies[data[0].TrackingId] = CreateBodyObject(data[0].TrackingId);
    105.                 }
    106.              
    107.                 RefreshBodyObject(data[0], _Bodies[data[0].TrackingId]);
    108.             }
    109.         }
    110.  
    111.         /* Kinect SDK Example fragment
    112.         foreach(var body in data)
    113.         {
    114.             if (body == null)
    115.             {
    116.                 continue;
    117.             }
    118.          
    119.             if(body.IsTracked)
    120.             {
    121.                 if(!_Bodies.ContainsKey(body.TrackingId))
    122.                 {
    123.                     _Bodies[body.TrackingId] = CreateBodyObject(body.TrackingId);
    124.                 }
    125.              
    126.                 RefreshBodyObject(body, _Bodies[body.TrackingId]);
    127.             }
    128.         }
    129.         */
    130.     }
    131.  
    132.     // Kinect SDK Example code method (Actually not called by any part of the code)
    133.     private GameObject CreateBodyObject(ulong id)
    134.     {
    135.         GameObject body = new GameObject("Body:" + id);
    136.      
    137.         for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
    138.         {
    139.             GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
    140.          
    141.             LineRenderer lr = jointObj.AddComponent<LineRenderer>();
    142.             lr.SetVertexCount(2);
    143.             lr.material = BoneMaterial;
    144.             lr.SetWidth(0.05f, 0.05f);
    145.          
    146.             jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
    147.             jointObj.name = jt.ToString();
    148.             jointObj.transform.parent = body.transform;
    149.         }
    150.      
    151.         return body;
    152.     }
    153.  
    154.     private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject)
    155.     {
    156.         /* Kinect SDK Example code fragment.
    157.         for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
    158.         {
    159.             Kinect.Joint sourceJoint = body.Joints[jt];
    160.             Kinect.Joint? targetJoint = null;
    161.          
    162.             if(_BoneMap.ContainsKey(jt))
    163.             {
    164.                 targetJoint = body.Joints[_BoneMap[jt]];
    165.             }
    166.          
    167.             Transform jointObj = bodyObject.transform.FindChild(jt.ToString());
    168.             jointObj.localPosition = GetVector3FromJoint(sourceJoint);
    169.          
    170.             LineRenderer lr = jointObj.GetComponent<LineRenderer>();
    171.             if(targetJoint.HasValue)
    172.             {
    173.                 lr.SetPosition(0, jointObj.localPosition);
    174.                 lr.SetPosition(1, GetVector3FromJoint(targetJoint.Value));
    175.                 lr.SetColors(GetColorForState (sourceJoint.TrackingState), GetColorForState(targetJoint.Value.TrackingState));
    176.             }
    177.             else
    178.             {
    179.                 lr.enabled = false;
    180.             }
    181.         }
    182.         */
    183.  
    184.         // Here I am "hardcoding" the movement of the entire left arm.
    185.         shoulderLeft.transform.localPosition = GetVector3FromJoint (body.Joints[Kinect.JointType.ShoulderLeft]);
    186.         elbowLeft.transform.localPosition = GetVector3FromJoint (body.Joints[Kinect.JointType.ElbowLeft]);
    187.         wristLeft.transform.localPosition = GetVector3FromJoint (body.Joints[Kinect.JointType.WristLeft]);
    188.         handLeft.transform.localPosition = GetVector3FromJoint (body.Joints[Kinect.JointType.HandLeft]);
    189.         thumbLeft.transform.localPosition = GetVector3FromJoint (body.Joints[Kinect.JointType.ThumbLeft]);
    190.         handTipLeft.transform.localPosition = GetVector3FromJoint (body.Joints[Kinect.JointType.HandTipLeft]);
    191.     }
    192.  
    193.     private static Color GetColorForState(Kinect.TrackingState state)
    194.     {
    195.         switch (state)
    196.         {
    197.         case Kinect.TrackingState.Tracked:
    198.             return Color.green;
    199.  
    200.         case Kinect.TrackingState.Inferred:
    201.             return Color.red;
    202.  
    203.         default:
    204.             return Color.black;
    205.         }
    206.     }
    207.  
    208.     private static Vector3 GetVector3FromJoint(Kinect.Joint joint)
    209.     {
    210.         float mod = 10;
    211.         // joint.Position.Z
    212.         return new Vector3(joint.Position.X * mod, joint.Position.Y * mod, 0);
    213.     }
    214. }
    215.  
    I am running out of time. I need this asap!

    If you have skype and want to speak directly with me, send me a PM.
     
    Last edited: Jan 20, 2015