取得全身追蹤結果

Body Tracking SDK 會使用全身追蹤器物件來處理 Azure Kinect DK 擷取並產生全身追蹤結果。 它也會維護追蹤器、處理佇列和輸出佇列的全域狀態。 使用全身追蹤器有三個步驟:

  • 建立追蹤器
  • 使用 Azure Kinect DK 擷取深度和 IR 影像
  • 將擷取排入佇列並快顯結果。

建立追蹤器

使用全身追蹤的第一個步驟是建立追蹤器,而且需要傳入感應器校正 k4a_calibration_t 結構。 您可使用 Azure Kinect Sensor SDK k4a_device_get_calibration() 函式來查詢感應器校正。

k4a_calibration_t sensor_calibration;
if (K4A_RESULT_SUCCEEDED != k4a_device_get_calibration(device, device_config.depth_mode, K4A_COLOR_RESOLUTION_OFF, &sensor_calibration))
{
    printf("Get depth camera calibration failed!\n");
    return 0;
}

k4abt_tracker_t tracker = NULL;
k4abt_tracker_configuration_t tracker_config = K4ABT_TRACKER_CONFIG_DEFAULT;
if (K4A_RESULT_SUCCEEDED != k4abt_tracker_create(&sensor_calibration, tracker_config, &tracker))
{
    printf("Body tracker initialization failed!\n");
    return 0;
}

擷取深度和 IR 影像

擷取影像頁面涵蓋使用 Azure Kinect DK 的影像擷取。

注意

建議使用 K4A_DEPTH_MODE_NFOV_UNBINNEDK4A_DEPTH_MODE_WFOV_2X2BINNED 模式,以獲得最佳效能和精確度。 請勿使用 K4A_DEPTH_MODE_OFFK4A_DEPTH_MODE_PASSIVE_IR 模式。

支援的 Azure Kinect DK 模式會在 Azure Kinect DK 硬體規格k4a_depth_mode_t列舉中說明。

// Capture a depth frame
switch (k4a_device_get_capture(device, &capture, TIMEOUT_IN_MS))
{
case K4A_WAIT_RESULT_SUCCEEDED:
    break;
case K4A_WAIT_RESULT_TIMEOUT:
    printf("Timed out waiting for a capture\n");
    continue;
    break;
case K4A_WAIT_RESULT_FAILED:
    printf("Failed to read a capture\n");
    goto Exit;
}

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

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

無等候處理

針對需要立即結果的單一執行緒應用程式使用此模式,可容納已捨棄的畫面 (例如從裝置檢視即時影片)。 位於 GitHub Azure-Kinect-Samplessimple_3d_viewer 樣本是無等候處理的範例。

k4a_wait_result_t queue_capture_result = k4abt_tracker_enqueue_capture(tracker, sensor_capture, 0);
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, 0);
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
}

等候處理

針對不需要每個畫面結果的應用程式使用此模式 (例如處理檔案中的影片)。 位於 GitHub Azure-Kinect-Samplessimple_sample.exe 樣本是等候處理的範例。

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_SUCCEEDED)
{
    // It should never hit timeout or error when K4A_WAIT_INFINITE is set.
    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)
{
    // It should never hit timeout or error when K4A_WAIT_INFINITE is set.
    printf("Error! Popping body tracking result failed!\n");
    break;
}
// 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

下一步