Share via


クイック スタート:最初の Azure Kinect アプリケーションを構築する

Azure Kinect DK へようこそ。 このクイックスタートでは、デバイスを起動して実行してみましょう。

Azure サブスクリプションをお持ちでない場合は、開始する前に 無料アカウント を作成してください。

次の関数を扱っています。

前提条件

  1. Azure Kinect DK デバイスを設定します
  2. Azure Kinect Sensor SDK をダウンロードしてインストールします。

ヘッダー

必要なヘッダーは k4a.h の 1 つだけです。 使用するコンパイラが SDK の lib および include フォルダーで設定されていることをご確認ください。 k4a.lib および k4a.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_SUCCEEDED および K4A_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;
}

次のステップ

Sensor SDK を使用して Azure Kinect DK デバイスを見つけて開く方法を学ぶ