Kinect / PyKinect2

Wrapper to expose Kinect for Windows v2 API in Python
MIT License
498 stars 237 forks source link

how to save skeleton data? (solved) #47

Open h-jia opened 6 years ago

h-jia commented 6 years ago

Hi all,

I am a newbie in Pykinect2 and python. I have printed both joints and jointPoints in _def drawbody(self, joints, jointPoints, color): and gets no output.

also tried to print body in _def run(self): _body = self.bodies.bodies[i]

Any recommendation will be much appreciated!

TheSOMGuy commented 6 years ago

Your initial question is "how to save skeleton data" and then you ask how to print them. Can you please specify what exactly you want to do?

Thanks.

sxs4337 commented 6 years ago

I have a similar issue so let me try to elaborate based on the original question.

I am trying to dump to disk the following- the raw RGB frame as image, infrared frame as image and the skeletal points as text file. Any related examples would be vert helpful. Thanks in advance.

TheSOMGuy commented 6 years ago

To store the raw RGB frame u can use PIL and the im.save function. This should also work for the infrared frame if u store the infrared value in a RGB-Tripel ( infaredValue[i], infaredValue[i], infaredValue[i] ). Then u have to convert A to a numpy array RGB and finaly store the RGB array.

import numpy
from PIL import Image

RGB = []

# If A is an infrared image.
if( A == infrared )
    for item in A: RGB.append([item,item,item])
    RGB = numpy.array(RGB)
else:
    RGB = numpy.array(A)

#Reshape the matrix to the original image dimensions.
RGB.reshape( imageHeight, imageWidth )

# Save the RGB matrix as image to disk
im = Image.fromarray(RGB)
# U can use different file formats here. Read the desciption of the function.
im.save("your_file.jpeg")

This should work for the infrared and RGB data. If you want to store the skeletal joints as csv us the csv class.

import csv

# Store all the joints in a matrix jointdata,
# Then write the matrix to the joint.csv file.

with open('joints.csv', 'w') as csvfile:
     csvfile.write(jointdata)

It's just a simple example, but it should do what u want.

sxs4337 commented 6 years ago

Thanks a lot for the reply! I guess the trouble is on how to get the frame values in variable A. (as in the the step before your code above)

I am reading through the examples and at line- https://github.com/Kinect/PyKinect2/blob/master/examples/PyKinectInfraRed.py#L83

I get the following values- (Pdb) p frame array([ 0, 2247, 1649, ..., 8948, 794, 0], dtype=uint16) (Pdb) p frame.shape (217088L,)

The frame dimensions are 512*424 = 217088. Can we simply resize this to get the values and then follow from your instructions above to dump it as an image.

TheSOMGuy commented 6 years ago

Not really.

You have to compute a RGB like representation if you want to store them as an .jpg or .png or something. This means you have to map the infrared value for each (x,y) image grid position to a range between 0 and 1 ( RGB double triple ) or to a range between 0 and 255 ( RGB int tripel ). Do something like this:

min = math.min(frame)
max = math.max(frame)

# For double RGB triple
mmRange =  1 / ( max - min )

# For int RGB triple
mmRange =  255 / ( max - min )

# Classic pythonic way of vector multiplication (  list comprehension ) 
frame = [ x * mmRange for x in frame ]
# or using numpy
frame = numpy.multiply( numpy.array(frame), mmRange )

Then store the frame as .png or .jpg or something else using the methods described above.

sxs4337 commented 6 years ago

That worked! Thanks. I was able to dump color and IR as PNG files and the corresponding body joints data into a CSV file.

Here is my version- https://github.com/sxs4337/PyKinect2/blob/master/examples/PyKinectSave.py

juanmaro97 commented 4 years ago

That worked! Thanks. I was able to dump color and IR as PNG files and the corresponding body joints data into a CSV file.

Here is my version- https://github.com/sxs4337/PyKinect2/blob/master/examples/PyKinectSave.py

You didn't save the Z coordinate data here, did you?