Azure Kinect Body Tracking SDK  0.9.5
k4abt.hpp
1 
7 #ifndef K4ABT_HPP
8 #define K4ABT_HPP
9 
10 #include <k4a/k4a.hpp>
11 #include "k4abt.h"
12 
13 namespace k4abt
14 {
15 
31  class frame
32  {
33  public:
39  frame(k4abt_frame_t handle = nullptr) noexcept : m_handle(handle) {}
40 
43  frame(const frame &other) noexcept : m_handle(other.m_handle)
44  {
45  if (m_handle != nullptr)
46  {
47  k4abt_frame_reference(m_handle);
48  }
49  }
50 
53  frame(frame &&other) noexcept : m_handle(other.m_handle)
54  {
55  other.m_handle = nullptr;
56  }
57 
58  ~frame()
59  {
60  reset();
61  }
62 
65  frame &operator=(const frame &other) noexcept
66  {
67  if (this != &other)
68  {
69  reset();
70  m_handle = other.m_handle;
71  if (m_handle != nullptr)
72  {
73  k4abt_frame_reference(m_handle);
74  }
75  }
76  return *this;
77  }
78 
81  frame &operator=(frame &&other) noexcept
82  {
83  if (this != &other)
84  {
85  reset();
86  m_handle = other.m_handle;
87  other.m_handle = nullptr;
88  }
89  return *this;
90  }
91 
94  frame &operator=(std::nullptr_t) noexcept
95  {
96  reset();
97  return *this;
98  }
99 
102  bool operator==(const frame &other) const noexcept
103  {
104  return m_handle == other.m_handle;
105  }
106 
109  bool operator==(std::nullptr_t) const noexcept
110  {
111  return m_handle == nullptr;
112  }
113 
116  bool operator!=(const frame &other) const noexcept
117  {
118  return m_handle != other.m_handle;
119  }
120 
123  bool operator!=(std::nullptr_t) const noexcept
124  {
125  return m_handle != nullptr;
126  }
127 
130  operator bool() const noexcept
131  {
132  return m_handle != nullptr;
133  }
134 
142  k4abt_frame_t handle() const noexcept
143  {
144  return m_handle;
145  }
146 
149  void reset() noexcept
150  {
151  if (m_handle != nullptr)
152  {
153  k4abt_frame_release(m_handle);
154  m_handle = nullptr;
155  }
156  }
157 
162  uint32_t get_num_bodies() noexcept
163  {
164  return k4abt_frame_get_num_bodies(m_handle);
165  }
166 
172  {
173  k4abt_skeleton_t skeleton;
174  get_body_skeleton(index, skeleton);
175  return skeleton;
176  }
177 
182  void get_body_skeleton(uint32_t index, k4abt_skeleton_t& skeleton)
183  {
184  k4a_result_t result = k4abt_frame_get_body_skeleton(m_handle, index, &skeleton);
185  if (K4A_RESULT_SUCCEEDED != result)
186  {
187  throw k4a::error("Get body skeleton failed!");
188  }
189  }
190 
195  uint32_t get_body_id(uint32_t index) noexcept
196  {
197  return k4abt_frame_get_body_id(m_handle, index);
198  }
199 
205  k4abt_body_t get_body(uint32_t index) noexcept
206  {
207  k4abt_body_t body;
208  body.id = k4abt_frame_get_body_id(m_handle, index);
209  get_body_skeleton(index, body.skeleton);
210  return body;
211  }
212 
217  std::chrono::microseconds get_device_timestamp() const noexcept
218  {
219  return std::chrono::microseconds(k4abt_frame_get_device_timestamp_usec(m_handle));
220  }
221 
227  {
228  return k4a::image(k4abt_frame_get_body_index_map(m_handle));
229  }
230 
235  k4a::capture get_capture() const noexcept
236  {
237  return k4a::capture(k4abt_frame_get_capture(m_handle));
238  }
239 
240  private:
241  k4abt_frame_t m_handle;
242  };
243 
249  class tracker
250  {
251  public:
257  tracker(k4abt_tracker_t handle = nullptr) noexcept : m_handle(handle) {}
258 
261  tracker(tracker &&dev) noexcept : m_handle(dev.m_handle)
262  {
263  dev.m_handle = nullptr;
264  }
265 
266  tracker(const tracker &) = delete;
267 
268  ~tracker()
269  {
270  destroy();
271  }
272 
273  tracker &operator=(const tracker &) = delete;
274 
277  tracker &operator=(tracker &&dev) noexcept
278  {
279  if (this != &dev)
280  {
281  destroy();
282  m_handle = dev.m_handle;
283  dev.m_handle = nullptr;
284  }
285  return *this;
286  }
287 
290  operator bool() const noexcept
291  {
292  return m_handle != nullptr;
293  }
294 
299  void destroy() noexcept
300  {
301  if (m_handle != nullptr)
302  {
303  k4abt_tracker_destroy(m_handle);
304  m_handle = nullptr;
305  }
306  }
307 
313  bool enqueue_capture(k4a::capture cap, std::chrono::milliseconds timeout = std::chrono::milliseconds(K4A_WAIT_INFINITE))
314  {
315  int32_t timeout_ms = k4a::internal::clamp_cast<int32_t>(timeout.count());
316  k4a_wait_result_t result = k4abt_tracker_enqueue_capture(m_handle, cap.handle(), timeout_ms);
317  if (result == K4A_WAIT_RESULT_FAILED)
318  {
319  throw k4a::error("Failed to enqueue capture to tracker!");
320  }
321  else if (result == K4A_WAIT_RESULT_TIMEOUT)
322  {
323  return false;
324  }
325 
326  return true;
327  }
328 
334  bool pop_result(k4abt::frame* body_frame, std::chrono::milliseconds timeout = std::chrono::milliseconds(K4A_WAIT_INFINITE))
335  {
336  k4abt_frame_t frame_handle = nullptr;
337  int32_t timeout_ms = k4a::internal::clamp_cast<int32_t>(timeout.count());
338  k4a_wait_result_t result = k4abt_tracker_pop_result(m_handle, &frame_handle, timeout_ms);
339  if (result == K4A_WAIT_RESULT_FAILED)
340  {
341  throw k4a::error("Failed to pop result from tracker!");
342  }
343  else if (result == K4A_WAIT_RESULT_TIMEOUT)
344  {
345  return false;
346  }
347 
348  *body_frame = frame(frame_handle);
349  return true;
350  }
351 
357  k4abt::frame pop_result(std::chrono::milliseconds timeout = std::chrono::milliseconds(K4A_WAIT_INFINITE))
358  {
359  k4abt::frame body_frame = nullptr;
360  if (pop_result(&body_frame, timeout))
361  {
362  return body_frame;
363  }
364  else
365  {
366  return nullptr;
367  }
368  }
369 
374  void set_temporal_smoothing(float smoothing_factor) noexcept
375  {
376  k4abt_tracker_set_temporal_smoothing(m_handle, smoothing_factor);
377  }
378 
383  void shutdown() noexcept
384  {
385  k4abt_tracker_shutdown(m_handle);
386  }
387 
394  {
395  k4abt_tracker_t handle = nullptr;
396  k4a_result_t result = k4abt_tracker_create(&sensor_calibration, config, &handle);
397 
398  if (K4A_RESULT_SUCCEEDED != result)
399  {
400  throw k4a::error("Failed to create k4abt tracker!");
401  }
402  return tracker(handle);
403  }
404 
405  private:
406  k4abt_tracker_t m_handle;
407  };
408 
413 } // namespace k4abt
414 
415 #endif
bool pop_result(k4abt::frame *body_frame, std::chrono::milliseconds timeout=std::chrono::milliseconds(K4A_WAIT_INFINITE))
Gets the next available body frame.
Definition: k4abt.hpp:334
k4abt_skeleton_t get_body_skeleton(uint32_t index)
Get the joint information for a particular person index from the k4abt_frame_t.
Definition: k4abt.hpp:171
void shutdown() noexcept
Shut down the k4abt tracker.
Definition: k4abt.hpp:383
Wrapper for k4abt_tracker_t.
Definition: k4abt.hpp:249
void set_temporal_smoothing(float smoothing_factor) noexcept
Set temporal smoothing.
Definition: k4abt.hpp:374
uint32_t id
An id for the body that can be used for frame-to-frame correlation.
Definition: k4abttypes.h:215
k4a_result_t
Handle to k4a body tracking component.
Definition: k4abttypes.h:34
void destroy() noexcept
Destroys a k4abt tracker.
Definition: k4abt.hpp:299
frame(const frame &other) noexcept
Creates a shallow copy of another k4abt frame.
Definition: k4abt.hpp:43
Structure to define joints for skeleton.
Definition: k4abttypes.h:206
Structure to define body.
Definition: k4abttypes.h:213
K4A_WAIT_RESULT_TIMEOUT
Handle to a k4a body tracking frame.
Definition: k4abttypes.h:41
uint32_t get_body_id(uint32_t index) noexcept
Get the body id for a particular person index from the k4abt frame.
Definition: k4abt.hpp:195
Definition: k4abt.hpp:13
tracker(tracker &&dev) noexcept
Moves another tracker into a new tracker.
Definition: k4abt.hpp:261
bool enqueue_capture(k4a::capture cap, std::chrono::milliseconds timeout=std::chrono::milliseconds(K4A_WAIT_INFINITE))
Add a k4a sensor capture to the tracker input queue to generate its body tracking result asynchronous...
Definition: k4abt.hpp:313
k4abt_frame_t handle() const noexcept
Returns the underlying k4abt_frame_t handle.
Definition: k4abt.hpp:142
bool operator!=(const frame &other) const noexcept
Returns true if two frames wrap different k4abt_frame_t instances, false otherwise.
Definition: k4abt.hpp:116
frame & operator=(frame &&other) noexcept
Moves another frame into this frame; other is set to invalid.
Definition: k4abt.hpp:81
static tracker create(const k4a_calibration_t &sensor_calibration, const k4abt_tracker_configuration_t &config=K4ABT_TRACKER_CONFIG_DEFAULT)
Create a k4abt tracker.
Definition: k4abt.hpp:393
k4a::capture get_capture() const noexcept
Get the original capture that is used to calculate the k4abt frame.
Definition: k4abt.hpp:235
frame(frame &&other) noexcept
Moves another k4abt frame into a new frame.
Definition: k4abt.hpp:53
void get_body_skeleton(uint32_t index, k4abt_skeleton_t &skeleton)
Get the joint information for a particular person index from the k4abt_frame_t.
Definition: k4abt.hpp:182
Configuration parameters for a k4abt body tracker.
Definition: k4abttypes.h:128
bool operator!=(std::nullptr_t) const noexcept
Returns true if the frame is valid, false otherwise.
Definition: k4abt.hpp:123
frame & operator=(std::nullptr_t) noexcept
Invalidates this frame.
Definition: k4abt.hpp:94
frame(k4abt_frame_t handle=nullptr) noexcept
Creates a k4abt frame from a k4abt_frame_t.
Definition: k4abt.hpp:39
static const k4abt_tracker_configuration_t K4ABT_TRACKER_CONFIG_DEFAULT
Default configuration setting for k4abt tracker.
Definition: k4abttypes.h:248
#define K4A_WAIT_INFINITE
k4a::image get_body_index_map() const noexcept
Get the body index map associated with the k4abt frame.
Definition: k4abt.hpp:226
K4A_RESULT_SUCCEEDED
bool operator==(const frame &other) const noexcept
Returns true if two frames refer to the same k4abt_frame_t, false otherwise.
Definition: k4abt.hpp:102
k4abt::frame pop_result(std::chrono::milliseconds timeout=std::chrono::milliseconds(K4A_WAIT_INFINITE))
Gets the next available body frame.
Definition: k4abt.hpp:357
std::chrono::microseconds get_device_timestamp() const noexcept
Get the k4abt frame&#39;s device timestamp in microseconds.
Definition: k4abt.hpp:217
tracker & operator=(tracker &&dev) noexcept
Moves another tracker into this tracker; other is set to invalid.
Definition: k4abt.hpp:277
frame & operator=(const frame &other) noexcept
Sets frame to a shallow copy of other.
Definition: k4abt.hpp:65
k4abt_skeleton_t skeleton
The skeleton information for the body.
Definition: k4abttypes.h:216
k4abt_body_t get_body(uint32_t index) noexcept
Get the full body struct for a particular person index from the k4abt frame.
Definition: k4abt.hpp:205
uint32_t get_num_bodies() noexcept
Get the number of people detected from the k4abt frame.
Definition: k4abt.hpp:162
K4A_WAIT_RESULT_FAILED
void reset() noexcept
Releases the underlying k4abt_frame_t; the frame is set to invalid.
Definition: k4abt.hpp:149
Wrapper for k4abt_frame_t.
Definition: k4abt.hpp:31
k4a_wait_result_t
bool operator==(std::nullptr_t) const noexcept
Returns false if the frame is valid, true otherwise.
Definition: k4abt.hpp:109
tracker(k4abt_tracker_t handle=nullptr) noexcept
Creates a tracker from a k4abt_tracker_t Takes ownership of the handle, i.e.
Definition: k4abt.hpp:257