参考官网教程:https://docs.microsoft.com/zh-cn/azure/kinect-dk/body-sdk-setup
https://docs.microsoft.com/zh-cn/azure/kinect-dk/build-first-body-app
使用环境:Azure Kinect SDK v1.4.1 + Azure Kinect Body Tracking SDK 1.0.1 + VS2019
文章目录
一、设置人体跟踪SDK
1. 官网下载SDK并安装
下载地址为https://docs.microsoft.com/zh-cn/azure/kinect-dk/body-sdk-download,选择1.0.1版本即可,下载好后可以默认安装,也可以自定义安装路径。(我是自定义路径)
注:我开始安装的1.1.0版本,后来运行过程中始终出错,后来使用1.0.1版本运行正常,出错原因未知。
2. 验证人体跟踪
- 可以在 开始菜单 或 (SDK Installation Path)\tools\k4abt_simple_3d_viewer.exe 中找到它双击运行。
- 如果没有足够强大的 GPU 但仍想测试结果,可以通过以下命令在命令行中启动 Azure Kinect 人体跟踪查看器:(SDK Installation Path)\tools\k4abt_simple_3d_viewer.exe
出现以下窗口,则说明安装正确。
二、生成第一个人体跟踪应用程序
1. 项目配置
过程和上节内容类似,未详细说明就是与上节完全相同。
1.1 新建空白工程并配置文件
1.2 安装 Azure Kinect NuGet 包
1.3 添加头文件和库文件
- 加入头文件k4a.h和k4abt.h
- 配置头文件目录和库文件目录
2.1 C/C++ - 常规 - 附加包含目录,加入传感器 SDK 和人体跟踪 SDK 的include路径
2.2 链接器 - 常规 - 附加库目录,加入传感器 SDK 和人体跟踪 SDK 的lib路径
2.3 链接器 - 输入 - 附加依赖项,加入k4a.lib和k4abt.lib
注意:每添加一项后,点击右下角应用。
2. 完整源代码
代码用于检测镜头中人数。
#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;
}
3. 结果展示
出现如图所示窗口,则说明运行成功。