Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/body tracking #96

Open
wants to merge 5 commits into
base: feature/body-tracking
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions pyk4a/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(
self._transformed_ir: Optional[np.ndarray] = None
self._body_skeleton: Optional[np.ndarray] = None
self._body_index_map: Optional[np.ndarray] = None
self._body_ids: Optional[np.ndarray] = None

@property
def color(self) -> Optional[np.ndarray]:
Expand Down Expand Up @@ -149,7 +150,7 @@ def body_skeleton(self) -> Optional[np.ndarray]:
)
"""
if self._body_skeleton is None:
self._body_skeleton, self._body_index_map = k4a_module.capture_get_body_tracking(
self._body_ids, self._body_skeleton, self._body_index_map = k4a_module.capture_get_body_tracking(
self._capture_handle,
self._calibration._calibration_handle,
self._calibration.body_tracker_handle,
Expand All @@ -161,10 +162,22 @@ def body_skeleton(self) -> Optional[np.ndarray]:
def body_index_map(self) -> Optional[np.ndarray]:
# use at your own risk.
if self._body_index_map is None:
self._body_skeleton, self._body_index_map = k4a_module.capture_get_body_tracking(
self._body_ids, self._body_skeleton, self._body_index_map = k4a_module.capture_get_body_tracking(
self._capture_handle,
self._calibration._calibration_handle,
self._calibration.body_tracker_handle,
self.thread_safe,
)
return self._body_index_map

@property
def body_ids(self) -> Optional[np.ndarray]:
# use at your own risk.
if self._body_ids is None:
self._body_ids, self._body_skeleton, self._body_index_map = k4a_module.capture_get_body_tracking(
self._capture_handle,
self._calibration._calibration_handle,
self._calibration.body_tracker_handle,
self.thread_safe,
)
return self._body_ids
27 changes: 21 additions & 6 deletions pyk4a/pyk4a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,7 @@ extern "C" {
#ifdef ENABLE_BODY_TRACKING
const char* CAPSULE_BODY_TRACKER_NAME = "pyk4a body tracker handle";
const char* CAPSULE_BODY_DATA_NAME = "pyk4a body data";
const char* CAPSULE_BODY_ID_NAME = "pyk4a body id";

static void capsule_cleanup_body_tracker(PyObject *capsule) {
k4abt_tracker_t *body_tracker = (k4abt_tracker_t*)PyCapsule_GetPointer(capsule, CAPSULE_BODY_TRACKER_NAME);
Expand All @@ -1254,6 +1255,12 @@ extern "C" {
free(buffer);
}

static void capsule_cleanup_body_ids(PyObject *capsule) {
fprintf(stdout, "free called on body ids");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fprintf should be removed after you confirm it is called properly

void* buffer = PyCapsule_GetPointer(capsule, CAPSULE_BODY_ID_NAME);
free(buffer);
}

static PyObject* body_tracker_create(PyObject* self, PyObject *args) {
k4a_calibration_t* calibration_handle;
PyObject *calibration_capsule;
Expand Down Expand Up @@ -1303,28 +1310,31 @@ extern "C" {
wait_res = k4abt_tracker_enqueue_capture(*body_tracker, *capture, K4A_WAIT_INFINITE);
if (wait_res != K4A_WAIT_RESULT_SUCCEEDED ) {
fprintf(stderr, "fail to enqueue capture\n");
return Py_BuildValue("NN", Py_None, Py_None);
return Py_BuildValue("NNN", Py_None, Py_None, Py_None);
}
wait_res = k4abt_tracker_pop_result(*body_tracker, &body_frame, K4A_WAIT_INFINITE);
if (wait_res == K4A_WAIT_RESULT_SUCCEEDED) {
uint32_t num_bodies = k4abt_frame_get_num_bodies(body_frame);
if (num_bodies == 0) {
k4abt_frame_release(body_frame);
return Py_BuildValue("NN", Py_None, Py_None);
return Py_BuildValue("NNN", Py_None, Py_None, Py_None);
}
k4abt_skeleton_t skeleton;
npy_intp dims[3];
dims[0] = num_bodies;
dims[1] = K4ABT_JOINT_COUNT;
dims[2] = DATA_PER_JOINT;
size_t body_stride = K4ABT_JOINT_COUNT*DATA_PER_JOINT;
uint32_t* buffer_id = (uint32_t*) malloc(sizeof(uint32_t)*num_bodies);
float* buffer_pose = (float*) malloc(sizeof(float)*num_bodies*body_stride);
if (capture == NULL) {
fprintf(stderr, "Cannot allocate memory");
return Py_BuildValue("NN", Py_None, Py_None);
return Py_BuildValue("NNN", Py_None, Py_None, Py_None);
}

for (uint32_t body_index = 0; body_index < num_bodies; body_index++) {
uint32_t body_id = k4abt_frame_get_body_id(body_frame, body_index);
buffer_id[body_index] = body_id;
k4a_result_t result = k4abt_frame_get_body_skeleton(body_frame, body_index, &skeleton);
size_t body_offset = body_index * body_stride;
for (int joint_index=0; joint_index<K4ABT_JOINT_COUNT; joint_index++) {
Expand Down Expand Up @@ -1352,8 +1362,13 @@ extern "C" {
}
}

PyArrayObject* np_body_id = (PyArrayObject*) PyArray_SimpleNewFromData(1, dims, NPY_UINT32, buffer_id);
PyObject *capsule_id = PyCapsule_New(buffer_id, CAPSULE_BODY_DATA_NAME, capsule_cleanup_body_ids);
PyCapsule_SetContext(capsule_id, buffer_id);
PyArray_SetBaseObject((PyArrayObject *) np_body_id, capsule_id);

PyArrayObject* np_body_data = (PyArrayObject*) PyArray_SimpleNewFromData(3, dims, NPY_FLOAT, buffer_pose);
PyObject *capsule = PyCapsule_New(buffer_pose, CAPSULE_BODY_DATA_NAME, capsule_cleanup_body_data);
PyObject *capsule = PyCapsule_New(buffer_pose, CAPSULE_BODY_ID_NAME, capsule_cleanup_body_data);
PyCapsule_SetContext(capsule, buffer_pose);
PyArray_SetBaseObject((PyArrayObject *) np_body_data, capsule);

Expand All @@ -1362,10 +1377,10 @@ extern "C" {
k4a_image_to_numpy(&body_index_map, &np_body_index_map);

k4abt_frame_release(body_frame);
return Py_BuildValue("OO", np_body_data, np_body_index_map);
return Py_BuildValue("OOO", np_body_id, np_body_data, np_body_index_map);
}
else {
return Py_BuildValue("NN", Py_None, Py_None);
return Py_BuildValue("NNN", Py_None, Py_None, Py_None);
}
}

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class pyk4a_build_ext(build_ext):
boolean_options = build_ext.boolean_options + ['enable-body-tracking', ]

def initialize_options(self):
self.enable_body_tracking = False
self.enable_body_tracking = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plan was to merge the body tracking branch into the master branch someday.
I worry we will forget to reset this to False before the merge.

build_ext.initialize_options(self)

def finalize_options(self):
Expand Down