Search code examples
pythonkinectpointpykinect

Converting Python (PyKinect) Skeleton data to Points (X,Y)


This is a pretty simple issue but I am new to Python and I can't seem to accomplish it.

I want to convert Skeleton data co-ordinates in Python (PyKinect) to Points (X,Y). I know that the similar task can be achieved in C# (using Microsoft.Kinect libraries) like the below code:

var p = new Point[6];
Point shoulderRight = new Point(), shoulderLeft = new Point();

foreach (Joint j in data.Joints)
{
    switch (j.ID)
    {
        case JointID.HandLeft:
            p[0] = new Point(j.Position.X, j.Position.Y);

            // ...

In Python (using PyKinect) though, I am able to get the data object:

for index, data in enumerate(skeletons):
p2 = data.SkeletonPositions[JointId.wrist_left] #test left wrist joint data object
print ('p2', p2)

The output is:

('p2', <x=-0.5478253364562988, y=-0.5376561880111694, z=1.7154035568237305, w=1.0>)

But, I can't seem to convert it into Point(X,Y) format. Will I need to use NumPy or some other external Python library for this? Any suggestions would really be appreciated.


Solution

  • You can use regex to extract the values, I am not sure if this is what you are looking for but try this:

    import re
    
    p2 = "<x=-0.5478253364562988, y=-0.5376561880111694, z=1.7154035568237305, w=1.0>"
    p2 = re.findall("-?\d+.\d+",p2)
    p2_xy = p2[0],p2[1]
    print ("p2",p2_xy)
    

    Output:

    ('ps', ('-0.5478253364562988', '-0.5376561880111694'))
    

    Or if you want a more cleaner version:

    import re
    
    p2 = "<x=-0.5478253364562988, y=-0.5376561880111694, z=1.7154035568237305, w=1.0>"
    p2 = re.findall("-?\d+.\d+",p2)
    print ("p2",p2[0],p2[1])
    

    Output:

    ('p2', '-0.5478253364562988', '-0.5376561880111694')