快速入門: 建置第一個 Azure Kinect 應用程式

想要開始使用 Azure Kinect DK 嗎? 本快速入門可讓您啟動並執行裝置!

如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶

涵蓋的函式如下:

必要條件

  1. 設定 Azure Kinect DK 裝置
  2. 下載並安裝 Azure Kinect 感應器 SDK。

標頭

您只需要一個標頭,那就是 k4a.h。 請確定您選擇的編譯器已設定了 SDK 的程式庫並包含資料夾。 您也需要連結 k4a.libk4a.dll 檔案。 您可以參考將 Azure Kinect 程式庫新增至專案

#include <k4a/k4a.h>

尋找 Azure Kinect DK 裝置

可能有多個 Azure Kinect DK 裝置連線到您的電腦。 我們會先了解數量有多少,或使用 k4a_device_get_installed_count() 函式來了解究竟是否有連線的裝置。 此函式應該能立即運作,而不需要任何額外的設定。

uint32_t count = k4a_device_get_installed_count();

一旦您確定有裝置連線到電腦後,就可以使用 k4a_device_open() 來加以開啟。 您可以提供所要開啟裝置的索引,也可以直接使用第一個裝置的 K4A_DEVICE_DEFAULT

// Open the first plugged in Kinect device
k4a_device_t device = NULL;
k4a_device_open(K4A_DEVICE_DEFAULT, &device);

和 Azure Kinect 程式庫中的大部分項目一樣,當您開啟某個項目時,就也應該在使用完後將其關閉! 當您關閉時,請記得對 k4a_device_close() 進行呼叫。

k4a_device_close(device);

裝置開啟後,我們可以進行測試以確保其運作正常。 讓我們讀取裝置的序號吧!

// 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);

啟動相機

開啟裝置之後,您必須使用 k4a_device_configuration_t 物件來設定相機。 相機設定有許多不同選項。 請選擇最適合您自己案例的設定。

// 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);

錯誤處理

為求簡潔明瞭,我們不會在某些內嵌範例中顯示如何處理錯誤。 不過,處理錯誤的方式一向很重要! 許多函式會傳回一般的成功/失敗類型 k4a_result_t,有時也會傳回有詳細資訊的更具體變體,例如 k4a_wait_result_t。 請查看每個函式的文件或 IntelliSense,以了解您預期會看到的錯誤訊息!

您可以使用 K4A_SUCCEEDEDK4A_FAILED 巨集來檢查函式的結果。 因此,我們可能會以下列方式保護函式呼叫,而不只是開啟 Azure Kinect DK 裝置:

// 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;
}

完整來源

#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;
}

下一步

了解如何使用感應器 SDK 來尋找和開啟 Azure Kinect DK 裝置