Azure Kinect DK の使用を開始しますか? このクイックスタートガイドを使えば、デバイスの準備が整い、すぐに使い始めることができます。
Azure サブスクリプションをお持ちでない場合は、開始する前に 無料アカウント を作成してください。
次の関数を扱っています。
k4a_device_get_installed_count()k4a_device_open()k4a_device_get_serialnum()k4a_device_start_cameras()k4a_device_stop_cameras()k4a_device_close()
[前提条件]
- Azure Kinect DK デバイスを設定します。
- Azure Kinect Sensor SDK をダウンロードしてインストールします。
ヘッダー
必要なヘッダーは 1 つだけで、 k4a.h。 選択したコンパイラが SDK の lib とインクルード フォルダーで設定されていることを確認します。 また、 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 デバイスを検索して開く方法について説明します