First Azure Kinect App C API vs C# K4A Nuget Package

Hello!

I started to do some test with the new Azure Kinect DK and the results are amazing. I´m not an AI expert, so I decided to read/watch/learn while testing.

I started with the basic reading provided by Microsoft https://azure.microsoft.com/es-es/services/kinect-dk/. I´ve been using C API, from Azure Kinect Sensor DK.  Now the thing is… i am a c# fan… and learning console with C++ was not ideal… did not have time to import the entire C library into .net so i researched and found this amazing NuGet package: https://www.nuget.org/packages/K4AdotNet just developed and still mantained by Andrey Bibichev.

I will write a series of “How To” articles using both C API and K4A Nuget Package for .net.

Let´s Begin

To start using the sensor first of all you need to understand the architecture you need to implement.

Untitled-1

This is how the Kinect Azure Sensor works.

  1. The device needs to be opened.
  2. You need to disable all configuration to assign a new one trough code.
  3. Then you need to start the cameras. (with the configuration you assigned).
  4. Now you are able to Capture the image, and work with it…
  5. Then camera needs to stop
  6. Finally Dispose your device.

Lets build our first application.

BUILD MY FIRST KINECT AZURE APPLICATION WITH C API

1 . Open VStudio

2 . File > New Project > Console App C++ Project

3 . Select x64 as Debug Mode

4 . Manage Nuget Package

5 . Browse > Type and select: “Microsoft.Azure.Kinect.Sensor v1.1.1” and Install it.

6 . In your main.cpp file #include <k4a/k4a.h>

7 . Now we need to find our Kinect Device connected. As you know you can connect multiple devices. In my case i only have one device, and by default the value to open one device is 0. So to open my device i have to declare a k4a_device_t and then use the function d4a_device_open. 

k4a_device_t device = NULL;

k4a_device_open(K4A_DEVICE_DEFAULT, &device);

8 . We will now do something simple like getting information from the device itself. Like the serial number.

size_t serial_size= 0;

k4a_device_get_serialnum(device, NULL, &serial_size);

char *serial = (char*)(malloc(serial_size));

printf(“Opened device: %s \n”, serial);

free(serial);

9 . Close the device.

k4a_device_close(device);

return 0;

 image

BUILD MY FIRST KINECT AZURE APPLICATION WITH K4A NUGET PACKAGE

1 . Open VStudio

2 . File > New WPF App .net Application

3. Select the Project > Properties > Build and make sure option “Prefer 32-bit” is dissabled

4 . Manage Nuget Package

5 . Browse > Type and select: “K4AdotNet” and Install it:

6 . In your MainWindow.xaml.cs  file include:  using K4AdotNet.Sensor;

7. Lets declare a device and open it.

Device device = null;
device = Device.Open();

8 . Now we will add a textblock in our MainWindow.xaml file.

<TextBlock x:Name=”txtResult” HorizontalAlignment=”Left” TextWrapping=”Wrap” Text=”TextBlock” VerticalAlignment=”Top”/>

9 . Assign the property of the opened device to that textblock

txtResult.Text = device.SerialNumber;

10 . Close the device.

device.Dispose();

image

The code is simple and easy to read. We have now our first application and we can already point our device. Now we need to get to our cameras… before programming this we need to understand the different views we have available. You can read about them in this post.

Lets Configure and Start the camera.

10 . Lets configure our camera.

k4a_device_configuration_t config = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;

config.camera_fps = K4A_FRAMES_PER_SECOND_30;

config.color_format = K4A_IMAGE_FORMAT_COLOR_MJPG;

config.color_resolution = K4A_COLOR_RESOLUTION_2160P;

config.depth_mode = K4A_DEPTH_MODE_NFOV_UNBINNED;

11 . Now we can start the camera

if (K4A_RESULT_SUCCEEDED != k4a_device_start_cameras(device, &config))
{         printf(“Failed to start device.”);   }
else {
printf
(“camera started wifh configuration indicated.”);
}

image

10 . Configuration of the camera

DeviceConfiguration config = DeviceConfiguration.DisableAll;

config.CameraFps = FrameRate.Thirty;

config.ColorFormat = ImageFormat.ColorMjpg;

config.ColorResolution = ColorResolution.R2160p;

config.DepthMode = DepthMode.NarrowViewUnbinned;

11 . Start cameras

try { device.StartCameras(config);
    txtResult.Text = “camera started wifh configuration indicated”;
    }  catch (Exception)
   {txtResult.Text = “Failed to start device”;
}

image

The output right now is the real time feed from the sensor with the parameters sent by code. In our next “How To” we will get the captures from Azure Kinect, segmentate and draw the image into a WPF application

Happy coding! Greetings @ivanatilca

One thought on “First Azure Kinect App C API vs C# K4A Nuget Package

Add yours

Leave a comment

Blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started