none
About joint velocity Kinect RRS feed

  • Question

  • I am trying to program to calculate the joint velocity, but always get different  errors. Post my code here, hoping all of you can help me to check it.

    var fs = new FileStream("myfile.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                var writer = new StreamWriter(fs);
                var reader = new StreamReader(fs);
    
                foreach (Skeleton skeleton in (from s in skeletons where s.TrackingState == SkeletonTrackingState.Tracked select s))
                {
                    SkeletonPoint leftHand = skeleton.Joints[JointType.FootLeft].Position;
                    //SkeletonPoint leftWrist = skeleton.Joints[JointType.AnkleLeft].Position;
                    //SkeletonPoint rightHand = skeleton.Joints[JointType.FootRight].Position;
                    //SkeletonPoint rightWrist = skeleton.Joints[JointType.AnkleRight].Position;
    
                    textBox1.Text = leftHand.X.ToString();
    
                    writer.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + "Left_Hand" + " " + leftHand.X.ToString() + " " + leftHand.Y.ToString() + " " + leftHand.Z.ToString());
                    //writer.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + "Left Wrist" + " " + leftWrist.X.ToString() + " " + leftWrist.Y.ToString() + " " + leftWrist.Z.ToString());
                    //writer.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + "Right Hand" + " " + rightHand.X.ToString() + " " + rightHand.Y.ToString() + " " + rightHand.Z.ToString());
                    //writer.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + "Right Wrist" + " " + rightWrist.X.ToString() + " " + rightWrist.Y.ToString() + " " + rightWrist.Z.ToString());
                                  
                    List<string[]> list = new List<string[]>();
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] array = line.Split(' ');
                        list.Add(array);
                    }
                    //when retreiving data loop over list and access to the vlaues by settting indexes:
                    for (int i = 0; i < list.Count; i++)
                    {
    
                        //convert to float:
                        //float floatjoint_X = float.Parse(item[2]);
                        //float floatjoint_Y = float.Parse(item[3]);
                        //float floatjoint_Z = float.Parse(item[4]);
    
    
                        float v_x = float.Parse(list[i][2]) - float.Parse(list[i + 1][2]);
                        float v_y = float.Parse(list[i][3]) - float.Parse(list[i + 1][3]);
                        float v_z = float.Parse(list[i][4]) - float.Parse(list[i + 1][4]);
                        float velocity_frame = (float)(Math.Sqrt(v_x * v_x + v_y * v_y + v_z * v_z));
                        float velocity_second = velocity_frame * 30;
                        textBox2.Text = list[i][0];
                       
                    }
    
                }
    
                writer.Close();
                reader.Close();
                fs.Close();

    Friday, June 8, 2012 9:15 AM

Answers

  • You shouldn't need to use a stream but instead a data structure. In addition to that, you have 2 major problems with your code that relate to the Kinect. 

    1. You are not disposing the SkeletonFrame

    2. You are not checking for null skeletons. 

    The cause of your error is due to the fact you are assuming the string values are floats. You might want to look at the value you are trying to float.Parse.

    Have a look at the Skelton Basics Sample on the best way of eliminating the Kinect issues I mentioned above.

    • Proposed as answer by The Thinker Tuesday, June 12, 2012 7:26 PM
    • Marked as answer by Trtian Wednesday, June 13, 2012 1:53 AM
    Tuesday, June 12, 2012 7:13 PM

All replies

  • Can you post the error and what line of code you are getting the error? Do you see your errors immediately or later? Can you post the entire function since we don't see how you are populating your skeletons and disposing of the object? The syntax looks correct but may have some functional issues. Why do you keep opening the file and reading in the entire contents? Is this happening each frame? FileIO operations are really slow.

    Friday, June 8, 2012 6:13 PM
  • Hi Carmine,

    Thanks for your quick reply. My function is attached below:

    public partial class MainWindow : Window
        {
            private KinectSensor _kinect;
    
            public MainWindow()
            {
                InitializeComponent();
                Loaded += MainWindowLoaded;
    
            }
            void MainWindowLoaded(object sender, RoutedEventArgs e)
            {
                if (KinectSensor.KinectSensors.Count == 0)
                    return;
    
                _kinect = KinectSensor.KinectSensors[0];
                _kinect.SkeletonStream.Enable();
                _kinect.Start();
                _kinect.SkeletonFrameReady += nui_SkeletonFrameReady;
    
            }
            void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
            {
    
                Skeleton[] skeletons = null;
                SkeletonFrame skeletonFrame = e.OpenSkeletonFrame();
                skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                skeletonFrame.CopySkeletonDataTo(skeletons);
                var fs = new FileStream("myfile.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                var writer = new StreamWriter(fs);
                var reader = new StreamReader(fs);
    
                foreach (Skeleton skeleton in (from s in skeletons where s.TrackingState == SkeletonTrackingState.Tracked select s))
                {
                    SkeletonPoint leftHand = skeleton.Joints[JointType.FootLeft].Position;
                    //SkeletonPoint leftWrist = skeleton.Joints[JointType.AnkleLeft].Position;
                    //SkeletonPoint rightHand = skeleton.Joints[JointType.FootRight].Position;
                    //SkeletonPoint rightWrist = skeleton.Joints[JointType.AnkleRight].Position;
    
                    textBox1.Text = leftHand.X.ToString();
    
                    writer.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + "Left_Hand" + " " + leftHand.X.ToString() + " " + leftHand.Y.ToString() + " " + leftHand.Z.ToString());
                    //writer.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + "Left Wrist" + " " + leftWrist.X.ToString() + " " + leftWrist.Y.ToString() + " " + leftWrist.Z.ToString());
                    //writer.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + "Right Hand" + " " + rightHand.X.ToString() + " " + rightHand.Y.ToString() + " " + rightHand.Z.ToString());
                    //writer.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + " " + "Right Wrist" + " " + rightWrist.X.ToString() + " " + rightWrist.Y.ToString() + " " + rightWrist.Z.ToString());
                                  
                    List<string[]> list = new List<string[]>();
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] array = line.Split(' ');
                        list.Add(array);
                    }
                    //when retreiving data loop over list and access to the vlaues by settting indexes:
                    for (int i = 0; i < list.Count; i++)
                    {
    
                        //convert to float:
                        //float floatjoint_X = float.Parse(item[2]);
                        //float floatjoint_Y = float.Parse(item[3]);
                        //float floatjoint_Z = float.Parse(item[4]);
    
    
                        float v_x = float.Parse(list[i][2]) - float.Parse(list[i + 1][2]);
                        float v_y = float.Parse(list[i][3]) - float.Parse(list[i + 1][3]);
                        float v_z = float.Parse(list[i][4]) - float.Parse(list[i + 1][4]);
                        float velocity_frame = (float)(Math.Sqrt(v_x * v_x + v_y * v_y + v_z * v_z));
                        float velocity_second = velocity_frame * 30;
                        textBox2.Text = list[i][0];
                       
                    }
    
                }
    
                writer.Close();
                reader.Close();
                fs.Close();
                                   
            }
        }

    Error: Input string was not in a correct format. The error happened at this line: 

    float v_x = float.Parse(list[i][2]) - float.Parse(list[i + 1][2]);

    I am quite new with C# and intending to write a function to calculate joint velocity.  If any point (the method I am using to calculate velocity, the way I am using streamwrite/streamread, etc)  I don't make it in right way, please feel free to tell me, which will do me great favor in C# and Kinect. 

    Hope you can understand my problem through my poor English. :(

    Monday, June 11, 2012 1:53 AM
  • About saving and writing data on disk, I failed to configure the errors. But on the way I am searching method to solve it, I finally get the idea you tried to tell me: it's slow accessing using filestream and better to using memory to access the data. Now I am trying it.

    Trtian

    Tuesday, June 12, 2012 3:48 AM
  • Trtian for small amounts of data you can put into memory streams or if you want a quick calculation to store to file.

    Input string was not in a correct format means it didnt like the expression given to it in the parse method. Try refining the line that gives the error.

    edit my code recommendation would be this:

    float v_x = float.Parse(list[i][2]- list[i + 1][2]);

    If your doing a bunch of joint velocity calculations only use memory for faster calculations then save the result to disk.

    If my recommendation doesn't work tell me and i will try something else.


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://jefferycarlsonblog.blogspot.com/






    Tuesday, June 12, 2012 1:35 PM
  • You shouldn't need to use a stream but instead a data structure. In addition to that, you have 2 major problems with your code that relate to the Kinect. 

    1. You are not disposing the SkeletonFrame

    2. You are not checking for null skeletons. 

    The cause of your error is due to the fact you are assuming the string values are floats. You might want to look at the value you are trying to float.Parse.

    Have a look at the Skelton Basics Sample on the best way of eliminating the Kinect issues I mentioned above.

    • Proposed as answer by The Thinker Tuesday, June 12, 2012 7:26 PM
    • Marked as answer by Trtian Wednesday, June 13, 2012 1:53 AM
    Tuesday, June 12, 2012 7:13 PM
  • Carmine is right you are not disposing of the skeletonframe or doing any validation/checks on the data which would cause problems too.

    Please do some checking/validation on the data being returned then try it like you had it above and then my solution except

    this should be float instead of string:

     List<string[]> list = new List<string[]>();


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://jefferycarlsonblog.blogspot.com/

    Tuesday, June 12, 2012 7:30 PM
  • Hi Carmine and The Thinker,

    Thanks both for your pretty suggestion. Yesterday I have used data structure finishing the velocity calculation. 

    I will add disposing method for skeletonframe. This function is just part of my designed program, so I wrote it simply to implement velocity calculation without thinking of better dealing with skeleton itself. But I think I should keep good programming habit. I do will remember it.

    Appreciate your sharing and instruction.

    Trtian

    Wednesday, June 13, 2012 2:23 AM