Quickstart: Build your first Azure Kinect application
Getting started with the Azure Kinect DK? This quickstart will get you up and running with the device!
If you don't have an Azure subscription, create a free account before you begin.
The following functions are covered:
k4a_device_get_installed_count()
k4a_device_open()
k4a_device_get_serialnum()
k4a_device_start_cameras()
k4a_device_stop_cameras()
k4a_device_close()
Prerequisites
- Set up the Azure Kinect DK device.
- Download and install the Azure Kinect Sensor SDK.
Headers
There's only one header that you'll need, and that's k4a.h
. Make sure your compiler of choice is set up with the SDK's lib and include folders. You'll also need the k4a.lib
and k4a.dll
files linked up. You may want to refer to adding the Azure Kinect library to your project.
#include <k4a/k4a.h>
Finding an Azure Kinect DK device
Multiple Azure Kinect DK devices can be connected to your computer. We'll first start by finding out how many, or if any are connected at all using the k4a_device_get_installed_count()
function. This function should work right away, without any additional setup.
uint32_t count = k4a_device_get_installed_count();
Once you've determined there's a device connected to the computer, you can open it using k4a_device_open()
. You can provide the index of the device you want to open, or you can just use K4A_DEVICE_DEFAULT
for the first one.
// Open the first plugged in Kinect device
k4a_device_t device = NULL;
k4a_device_open(K4A_DEVICE_DEFAULT, &device);
As with most things in the Azure Kinect library, when you open something, you should also close it when you're finished with it! When you're shutting down, remember to make a call to k4a_device_close()
.
k4a_device_close(device);
Once the device is open, we can make a test to ensure it's working. So let's read the device's serial number!
// Get the size of the serial number
size_t serial_size = 0;
k4a_device_get_serialnum(device, NULL, &serial_size);
// Allocate memory for the serial, then acquire it
char *serial = (char*)(malloc(serial_size));
k4a_device_get_serialnum(device, serial, &serial_size);
printf("Opened device: %s\n", serial);
free(serial);
Starting the cameras
Once you've opened the device, you'll need to configure the camera with a k4a_device_configuration_t
object. Camera configuration has a number of different options. Choose the settings that best fit your own scenario.
// Configure a stream of 4096x3072 BRGA color data at 15 frames per second
k4a_device_configuration_t config = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
config.camera_fps = K4A_FRAMES_PER_SECOND_15;
config.color_format = K4A_IMAGE_FORMAT_COLOR_BGRA32;
config.color_resolution = K4A_COLOR_RESOLUTION_3072P;
// Start the camera with the given configuration
k4a_device_start_cameras(device, &config);
// ...Camera capture and application specific code would go here...
// Shut down the camera when finished with application logic
k4a_device_stop_cameras(device);
Error handling
For the sake of brevity and clarity, we don't show error handling in some inline examples. However, error handling is always important! Many functions will return a general success/failure type k4a_result_t
, or a more specific variant with detailed information such as k4a_wait_result_t
. Check the docs or IntelliSense for each function to see what error messages you should expect to see from it!
You can use the K4A_SUCCEEDED
and K4A_FAILED
macros to check the result of a function. So instead of just opening an Azure Kinect DK device, we might guard the function call like this:
// Open the first plugged in Kinect device
k4a_device_t device = NULL;
if ( K4A_FAILED( k4a_device_open(K4A_DEVICE_DEFAULT, &device) ) )
{
printf("Failed to open k4a device!\n");
return;
}
Full source
#pragma comment(lib, "k4a.lib")
#include <k4a/k4a.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
uint32_t count = k4a_device_get_installed_count();
if (count == 0)
{
printf("No k4a devices attached!\n");
return 1;
}
// Open the first plugged in Kinect device
k4a_device_t device = NULL;
if (K4A_FAILED(k4a_device_open(K4A_DEVICE_DEFAULT, &device)))
{
printf("Failed to open k4a device!\n");
return 1;
}
// Get the size of the serial number
size_t serial_size = 0;
k4a_device_get_serialnum(device, NULL, &serial_size);
// Allocate memory for the serial, then acquire it
char *serial = (char*)(malloc(serial_size));
k4a_device_get_serialnum(device, serial, &serial_size);
printf("Opened device: %s\n", serial);
free(serial);
// Configure a stream of 4096x3072 BRGA color data at 15 frames per second
k4a_device_configuration_t config = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
config.camera_fps = K4A_FRAMES_PER_SECOND_15;
config.color_format = K4A_IMAGE_FORMAT_COLOR_BGRA32;
config.color_resolution = K4A_COLOR_RESOLUTION_3072P;
// Start the camera with the given configuration
if (K4A_FAILED(k4a_device_start_cameras(device, &config)))
{
printf("Failed to start cameras!\n");
k4a_device_close(device);
return 1;
}
// Camera capture and application specific code would go here
// Shut down the camera when finished with application logic
k4a_device_stop_cameras(device);
k4a_device_close(device);
return 0;
}
Next steps
Learn how to find and open a Azure Kinect DK device using Sensor SDK
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for