你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

获取人体跟踪结果

人体跟踪 SDK 使用人体跟踪器对象处理 Azure Kinect DK 捕获并生成人体跟踪结果。 它还可以维护跟踪器、处理队列和输出队列的全局状态。 使用人体跟踪器需要执行三个步骤:

  • 创建跟踪器
  • 使用 Azure Kinect DK 捕获深度图像和 IR 图像
  • 将捕获排入队列并弹出结果。

创建跟踪器

使用人体跟踪的第一步是创建跟踪器,并需要传入传感器校准 k4a_calibration_t 结构。 可以使用 Azure Kinect 传感器 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 硬件规格k4a_depth_mode_t 枚举中介绍了支持的 Azure Kinect DK 模式。

// 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-Samples 中的 simple_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-Samples 中的 simple_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

后续步骤