快速入門: 建立 Azure Kinect 全身追蹤應用程式

想要開始使用全身追蹤 SDK 嗎? 本快速入門可讓您啟動並執行全身追蹤! 您可以在此 Azure-Kinect-Sample 存放庫中找到更多範例。

必要條件

標頭

全身追蹤會使用單一標頭 k4abt.h。 除了 k4a.h 外,也請包含這個標頭。 請確定您選擇的編譯器已針對感應器 SDK 和全身追蹤 SDK 的 libinclude 資料夾完成設定。 您也需要連結到 k4a.libk4abt.lib 檔案。 若要執行應用程式,k4a.dllk4abt.dllonnxruntime.dlldnn_model.onnx 必須在應用程式的執行路徑中。

#include <k4a/k4a.h>
#include <k4abt.h>

開啟裝置並啟動相機

您的第一個全身追蹤應用程式會假設已有一個 Azure Kinect 裝置與電腦連線。

全身追蹤建置在感應器 SDK 之上。 若要使用全身追蹤,您必須先開啟並設定裝置。 使用 k4a_device_open() 函式來開啟裝置,然後使用 k4a_device_configuration_t 物件來設定裝置。 若要獲得最佳結果,請將景深模式設定為 K4A_DEPTH_MODE_NFOV_UNBINNEDK4A_DEPTH_MODE_WFOV_2X2BINNED。 如果景深模式設定為 K4A_DEPTH_MODE_OFFK4A_DEPTH_MODE_PASSIVE_IR,則不會執行全身追蹤程式。

您可以在此頁面找到有關如何尋找及開啟裝置的詳細資訊。

您可以在這些頁面上找到有關 Azure Kinect 景深模式的詳細資訊:硬體規格k4a_depth_mode_t 列舉。

k4a_device_t device = NULL;
k4a_device_open(0, &device);

// Start camera. Make sure depth camera is enabled.
k4a_device_configuration_t deviceConfig = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
deviceConfig.depth_mode = K4A_DEPTH_MODE_NFOV_UNBINNED;
deviceConfig.color_resolution = K4A_COLOR_RESOLUTION_OFF;
k4a_device_start_cameras(device, &deviceConfig);

建立追蹤程式

若要取得全身追蹤結果,第一步是建立全身追蹤程式。 其需要感應器校正 k4a_calibration_t 結構。 您可以使用 k4a_device_get_calibration() 函式來查詢感應器校正。

k4a_calibration_t sensor_calibration;
k4a_device_get_calibration(device, deviceConfig.depth_mode, deviceConfig.color_resolution, &sensor_calibration);

k4abt_tracker_t tracker = NULL;
k4abt_tracker_configuration_t tracker_config = K4ABT_TRACKER_CONFIG_DEFAULT;
k4abt_tracker_create(&sensor_calibration, tracker_config, &tracker);

從 Azure Kinect 裝置進行擷取

您可以在此頁面找到有關如何擷取影像資料的詳細資訊。

// Capture a depth frame
k4a_capture_t sensor_capture;
k4a_device_get_capture(device, &sensor_capture, TIMEOUT_IN_MS);

將擷取排入佇列並快顯結果

追蹤程式會在內部維持輸入佇列和輸出佇列,以便透過非同步方式更有效率地處理 Azure Kinect DK 的擷取。 下一步是使用 k4abt_tracker_enqueue_capture() 函式將新的擷取新增至輸入佇列。 使用 k4abt_tracker_pop_result() 函式從輸出佇列中彈出結果。 逾時值取決於應用程式,並且會控制佇列等待時間。

您的第一個全身追蹤應用程式會使用即時處理模式。 如需其他模式的詳細說明,請參閱取得全身追蹤結果

k4a_wait_result_t queue_capture_result = k4abt_tracker_enqueue_capture(tracker, sensor_capture, K4A_WAIT_INFINITE);
k4a_capture_release(sensor_capture); // Remember to release the sensor capture once you finish using it
if (queue_capture_result == K4A_WAIT_RESULT_FAILED)
{
    printf("Error! Adding capture to tracker process queue failed!\n");
    break;
}

k4abt_frame_t body_frame = NULL;
k4a_wait_result_t pop_frame_result = k4abt_tracker_pop_result(tracker, &body_frame, K4A_WAIT_INFINITE);
if (pop_frame_result == K4A_WAIT_RESULT_SUCCEEDED)
{
    // Successfully popped the body tracking result. Start your processing
    ...

    k4abt_frame_release(body_frame); // Remember to release the body frame once you finish using it
}

存取全身追蹤結果資料

每個感應器擷取的全身追蹤結果都會儲存在身體框架 k4abt_frame_t 結構中。 每個身體框架包含三個主要元件:身體結構的集合、2D 身體索引對應,以及輸入擷取。

您的第一個全身追蹤應用程式只會存取偵測到的身體數目。 如需身體框架中資料的詳細說明,請參閱存取身體框架中的資料

size_t num_bodies = k4abt_frame_get_num_bodies(body_frame);
printf("%zu bodies are detected!\n", num_bodies);

清理

最後一步是關閉全身追蹤程式,並釋放全身追蹤物件。 您也必須停止並關閉裝置。

k4abt_tracker_shutdown(tracker);
k4abt_tracker_destroy(tracker);
k4a_device_stop_cameras(device);
k4a_device_close(device);

完整來源

#include <stdio.h>
#include <stdlib.h>

#include <k4a/k4a.h>
#include <k4abt.h>

#define VERIFY(result, error)                                                                            \
    if(result != K4A_RESULT_SUCCEEDED)                                                                   \
    {                                                                                                    \
        printf("%s \n - (File: %s, Function: %s, Line: %d)\n", error, __FILE__, __FUNCTION__, __LINE__); \
        exit(1);                                                                                         \
    }                                                                                                    \

int main()
{
    k4a_device_t device = NULL;
    VERIFY(k4a_device_open(0, &device), "Open K4A Device failed!");

    // Start camera. Make sure depth camera is enabled.
    k4a_device_configuration_t deviceConfig = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
    deviceConfig.depth_mode = K4A_DEPTH_MODE_NFOV_UNBINNED;
    deviceConfig.color_resolution = K4A_COLOR_RESOLUTION_OFF;
    VERIFY(k4a_device_start_cameras(device, &deviceConfig), "Start K4A cameras failed!");

    k4a_calibration_t sensor_calibration;
    VERIFY(k4a_device_get_calibration(device, deviceConfig.depth_mode, deviceConfig.color_resolution, &sensor_calibration),
        "Get depth camera calibration failed!");

    k4abt_tracker_t tracker = NULL;
    k4abt_tracker_configuration_t tracker_config = K4ABT_TRACKER_CONFIG_DEFAULT;
    VERIFY(k4abt_tracker_create(&sensor_calibration, tracker_config, &tracker), "Body tracker initialization failed!");

    int frame_count = 0;
    do
    {
        k4a_capture_t sensor_capture;
        k4a_wait_result_t get_capture_result = k4a_device_get_capture(device, &sensor_capture, K4A_WAIT_INFINITE);
        if (get_capture_result == K4A_WAIT_RESULT_SUCCEEDED)
        {
            frame_count++;
            k4a_wait_result_t queue_capture_result = k4abt_tracker_enqueue_capture(tracker, sensor_capture, K4A_WAIT_INFINITE);
            k4a_capture_release(sensor_capture); // Remember to release the sensor capture once you finish using it
            if (queue_capture_result == K4A_WAIT_RESULT_TIMEOUT)
            {
                // It should never hit timeout when K4A_WAIT_INFINITE is set.
                printf("Error! Add capture to tracker process queue timeout!\n");
                break;
            }
            else if (queue_capture_result == K4A_WAIT_RESULT_FAILED)
            {
                printf("Error! Add capture to tracker process queue failed!\n");
                break;
            }

            k4abt_frame_t body_frame = NULL;
            k4a_wait_result_t pop_frame_result = k4abt_tracker_pop_result(tracker, &body_frame, K4A_WAIT_INFINITE);
            if (pop_frame_result == K4A_WAIT_RESULT_SUCCEEDED)
            {
                // Successfully popped the body tracking result. Start your processing

                size_t num_bodies = k4abt_frame_get_num_bodies(body_frame);
                printf("%zu bodies are detected!\n", num_bodies);

                k4abt_frame_release(body_frame); // Remember to release the body frame once you finish using it
            }
            else if (pop_frame_result == K4A_WAIT_RESULT_TIMEOUT)
            {
                //  It should never hit timeout when K4A_WAIT_INFINITE is set.
                printf("Error! Pop body frame result timeout!\n");
                break;
            }
            else
            {
                printf("Pop body frame result failed!\n");
                break;
            }
        }
        else if (get_capture_result == K4A_WAIT_RESULT_TIMEOUT)
        {
            // It should never hit time out when K4A_WAIT_INFINITE is set.
            printf("Error! Get depth frame time out!\n");
            break;
        }
        else
        {
            printf("Get depth capture returned error: %d\n", get_capture_result);
            break;
        }

    } while (frame_count < 100);

    printf("Finished body tracking processing!\n");

    k4abt_tracker_shutdown(tracker);
    k4abt_tracker_destroy(tracker);
    k4a_device_stop_cameras(device);
    k4a_device_close(device);

    return 0;
}

下一步