bibigone / k4a.net

K4A.Net - Three-in-one .NET library to work with Azure Kinect devices (also known as Kinect for Azure, K4A, Kinect v4). It includes sensor API, recording and playback API, body tracking API. Samples for WPF, .NET Core and Unity are included.
MIT License
164 stars 39 forks source link

capture function problem #36

Closed fberna closed 3 years ago

fberna commented 3 years ago

Hi guys, I am using the capture function in order to create directly a .mkv file from Unity. From the code below a get only a frame into the file. It looks like am continuously overwriting the file. Have some of you any suggestions?

Thx FB

private async Task KinectLoop()
    { 
    while (true)
        {
            using (K4AdotNet.Sensor.Capture capture = await Task.Run(() => kinect.GetCapture()).ConfigureAwait(true))
                {
                     using (var recorder = new Recorder(dstFilePath + "00.mkv", kinect, configuration))
                     {
                        if (capture != null)
                        {
                                recorder.WriteHeader();
                                recorder.WriteCapture(capture);
                       }
                    }
                }
        }
    }
baSSiLL commented 3 years ago

It looks like am continuously overwriting the file. Yes, that's exactly what's happening. Move createing/disposing of recorder and recorder.WriteHeader call out of the loop.

using (var recorder = new Recorder(dstFilePath + "00.mkv", kinect, configuration))
{
recorder.WriteHeader();
while (true)
{
// get capture
recorder.WriteCapture(capture);
}
}
fberna commented 3 years ago

Move createing/disposing of recorder and recorder.WriteHeader call out of the loop.

I tried but like that Unity crashes every time. It is probably due to RAM overload.

baSSiLL commented 3 years ago

You should dispose Capture objects when finished working with them. In this case, right after calling WriteCapture.

fberna commented 3 years ago

You should dispose Capture objects when finished working with them. In this case, right after calling WriteCapture.

In this way, I will have my file in the right format but with zero frames in it.

using (K4AdotNet.Sensor.Capture capture = await Task.Run(() => kinect.GetCapture()).ConfigureAwait(true)
           {
                using (var recorder = new Recorder(dstFilePath + "00.mkv", kinect, configuration))
                {
                    recorder.WriteHeader();
                    while (true)
                    {
                          recorder.WriteCapture(capture);
                          capture.Dispose();
                     }
                }
            }
baSSiLL commented 3 years ago

Apparently you should be getting Capture inside the loop before writing it to recorder.

fberna commented 3 years ago

It works! Thank you