Astra SDK  v2.1.3
BodyReaderPoll\main.c
// This file is part of the Orbbec Astra SDK [https://orbbec3d.com]
// Copyright (c) 2015-2017 Orbbec 3D
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Be excellent to each other.
#include <astra/capi/astra.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <key_handler.h>
void output_floor(astra_bodyframe_t bodyFrame)
{
astra_floor_info_t floorInfo;
astra_status_t rc = astra_bodyframe_floor_info(bodyFrame, &floorInfo);
if (rc != ASTRA_STATUS_SUCCESS)
{
printf("Error %d in astra_bodyframe_floor_info()\n", rc);
return;
}
const astra_bool_t floorDetected = floorInfo.floorDetected;
const astra_plane_t* floorPlane = &floorInfo.floorPlane;
const astra_floormask_t* floorMask = &floorInfo.floorMask;
if (floorDetected != ASTRA_FALSE)
{
printf("Floor plane: [%f, %f, %f, %f]\n",
floorPlane->a,
floorPlane->b,
floorPlane->c,
floorPlane->d);
const int32_t bottomCenterIndex = floorMask->width / 2 + floorMask->width * (floorMask->height - 1);
printf("Floor mask: width: %d height: %d bottom center value: %d\n",
floorMask->width,
floorMask->height,
floorMask->data[bottomCenterIndex]);
}
}
void output_body_mask(astra_bodyframe_t bodyFrame)
{
astra_bodymask_t bodyMask;
const astra_status_t rc = astra_bodyframe_bodymask(bodyFrame, &bodyMask);
if (rc != ASTRA_STATUS_SUCCESS)
{
printf("Error %d in astra_bodyframe_bodymask()\n", rc);
return;
}
const int32_t centerIndex = bodyMask.width / 2 + bodyMask.width * bodyMask.height / 2;
printf("Body mask: width: %d height: %d center value: %d\n",
bodyMask.width,
bodyMask.height,
bodyMask.data[centerIndex]);
}
void output_bodyframe_info(astra_bodyframe_t bodyFrame)
{
const astra_status_t rc = astra_bodyframe_info(bodyFrame, &info);
if (rc != ASTRA_STATUS_SUCCESS)
{
printf("Error %d in astra_bodyframe_info()\n", rc);
return;
}
// width and height of floor mask, body mask, and the size of depth image
// that joint depth position is relative to.
const int32_t width = info.width;
const int32_t height = info.height;
printf("BodyFrame info: Width: %d Height: %d\n",
width,
height);
}
void output_joint(const int32_t bodyId, const astra_joint_t* joint)
{
// jointType is one of ASTRA_JOINT_* which exists for each joint type
const astra_joint_type_t jointType = joint->type;
// jointStatus is one of:
// ASTRA_JOINT_STATUS_NOT_TRACKED = 0,
// ASTRA_JOINT_STATUS_LOW_CONFIDENCE = 1,
// ASTRA_JOINT_STATUS_TRACKED = 2,
const astra_joint_status_t jointStatus = joint->status;
const astra_vector3f_t* worldPos = &joint->worldPosition;
// depthPosition is in pixels from 0 to width and 0 to height
// where width and height are member of astra_bodyframe_info_t
// which is obtained from astra_bodyframe_info().
const astra_vector2f_t* depthPos = &joint->depthPosition;
printf("Body %u Joint %d status %d @ world (%.1f, %.1f, %.1f) depth (%.1f, %.1f)\n",
bodyId,
jointType,
jointStatus,
worldPos->x,
worldPos->y,
worldPos->z,
depthPos->x,
depthPos->y);
// orientation is a 3x3 rotation matrix where the column vectors also
// represent the orthogonal basis vectors for the x, y, and z axes.
const astra_matrix3x3_t* orientation = &joint->orientation;
const astra_vector3f_t* xAxis = &orientation->xAxis; // same as orientation->m00, m10, m20
const astra_vector3f_t* yAxis = &orientation->yAxis; // same as orientation->m01, m11, m21
const astra_vector3f_t* zAxis = &orientation->zAxis; // same as orientation->m02, m12, m22
printf("Head orientation x: [%f %f %f]\n", xAxis->x, xAxis->y, xAxis->z);
printf("Head orientation y: [%f %f %f]\n", yAxis->x, yAxis->y, yAxis->z);
printf("Head orientation z: [%f %f %f]\n", zAxis->x, zAxis->y, zAxis->z);
}
void output_hand_poses(const astra_body_t* body)
{
const astra_handpose_info_t* handPoses = &body->handPoses;
// astra_handpose_t is one of:
// ASTRA_HANDPOSE_UNKNOWN = 0
// ASTRA_HANDPOSE_GRIP = 1
const astra_handpose_t leftHandPose = handPoses->leftHand;
const astra_handpose_t rightHandPose = handPoses->rightHand;
printf("Body %d Left hand pose: %d Right hand pose: %d\n",
body->id,
leftHandPose,
rightHandPose);
}
void output_bodies(astra_bodyframe_t bodyFrame)
{
int i;
const astra_status_t rc = astra_bodyframe_body_list(bodyFrame, &bodyList);
if (rc != ASTRA_STATUS_SUCCESS)
{
printf("Error %d in astra_bodyframe_body_list()\n", rc);
return;
}
for(i = 0; i < bodyList.count; ++i)
{
astra_body_t* body = &bodyList.bodies[i];
// Pixels in the body mask with the same value as bodyId are
// from the same body.
astra_body_id_t bodyId = body->id;
// bodyStatus is one of:
// ASTRA_BODY_STATUS_NOT_TRACKING = 0,
// ASTRA_BODY_STATUS_LOST = 1,
// ASTRA_BODY_STATUS_TRACKING_STARTED = 2,
// ASTRA_BODY_STATUS_TRACKING = 3,
astra_body_status_t bodyStatus = body->status;
if (bodyStatus == ASTRA_BODY_STATUS_TRACKING_STARTED)
{
printf("Body Id: %d Status: Tracking started\n", bodyId);
}
if (bodyStatus == ASTRA_BODY_STATUS_TRACKING)
{
printf("Body Id: %d Status: Tracking\n", bodyId);
}
if (bodyStatus == ASTRA_BODY_STATUS_TRACKING_STARTED ||
bodyStatus == ASTRA_BODY_STATUS_TRACKING)
{
const astra_vector3f_t* centerOfMass = &body->centerOfMass;
const astra_body_tracking_feature_flags_t features = body->features;
const bool jointTrackingEnabled = (features & ASTRA_BODY_TRACKING_JOINTS) == ASTRA_BODY_TRACKING_JOINTS;
const bool handPoseRecognitionEnabled = (features & ASTRA_BODY_TRACKING_HAND_POSES) == ASTRA_BODY_TRACKING_HAND_POSES;
printf("Body %d CenterOfMass (%f, %f, %f) Joint Tracking Enabled: %s Hand Pose Recognition Enabled: %s\n",
bodyId,
centerOfMass->x, centerOfMass->y, centerOfMass->z,
jointTrackingEnabled ? "True" : "False",
handPoseRecognitionEnabled ? "True" : "False");
const astra_joint_t* joint = &body->joints[ASTRA_JOINT_HEAD];
output_joint(bodyId, joint);
output_hand_poses(body);
}
else if (bodyStatus == ASTRA_BODY_STATUS_LOST)
{
printf("Body %u Status: Tracking lost.\n", bodyId);
}
else // bodyStatus == ASTRA_BODY_STATUS_NOT_TRACKING
{
printf("Body Id: %d Status: Not Tracking\n", bodyId);
}
}
}
void output_bodyframe(astra_bodyframe_t bodyFrame)
{
output_floor(bodyFrame);
output_body_mask(bodyFrame);
output_bodyframe_info(bodyFrame);
output_bodies(bodyFrame);
}
int main(int argc, char* argv[])
{
set_key_handler();
astra_initialize();
const char* licenseString = "<INSERT LICENSE KEY HERE>";
astra_streamsetconnection_t sensor;
astra_streamset_open("device/default", &sensor);
astra_reader_t reader;
astra_reader_create(sensor, &reader);
astra_bodystream_t bodyStream;
astra_reader_get_bodystream(reader, &bodyStream);
astra_stream_start(bodyStream);
do
{
astra_update();
astra_reader_frame_t frame;
astra_status_t rc = astra_reader_open_frame(reader, 0, &frame);
if (rc == ASTRA_STATUS_SUCCESS)
{
astra_bodyframe_t bodyFrame;
astra_frame_get_bodyframe(frame, &bodyFrame);
astra_frame_index_t frameIndex;
astra_bodyframe_get_frameindex(bodyFrame, &frameIndex);
printf("Frame index: %d\n", frameIndex);
output_bodyframe(bodyFrame);
printf("----------------------------\n");
astra_reader_close_frame(&frame);
}
} while (shouldContinue);
astra_reader_destroy(&reader);
astra_streamset_close(&sensor);
astra_terminate();
}
ASTRA_API_EX astra_status_t orbbec_body_tracking_set_license(const char *licenseString)
Struct representing a 2-dimensional bitmask
Definition: body_types.h:344
uint8_t data[ASTRA_TEMP_IMAGE_LENGTH]
mask pixels
Definition: body_types.h:347
int32_t height
height in pixels
Definition: body_types.h:351
int32_t width
width in pixels
Definition: body_types.h:349
Struct containing a variable-sized array of astra_body_t structs
Definition: body_types.h:329
int32_t count
Number of astra_body_t in bodies
Definition: body_types.h:334
astra_body_t bodies[ASTRA_MAX_BODIES]
Pointer to an array of astra_body_t
Definition: body_types.h:332
Struct containing body tracking information
Definition: body_types.h:293
astra_body_status_t status
Tracking status
Definition: body_types.h:321
astra_body_tracking_feature_flags_t features
Bitmask of features that are enabled.
Definition: body_types.h:298
astra_joint_t joints[ASTRA_MAX_JOINTS]
Array of tracked joints
Definition: body_types.h:306
astra_vector3f_t centerOfMass
Single point describing the center of mass of a body.
Definition: body_types.h:304
astra_handpose_info_t handPoses
Hand poses
Definition: body_types.h:308
astra_body_id_t id
Persistent Id
Definition: body_types.h:315
Struct containing general body frame information
Definition: body_types.h:401
int32_t width
Frame width
Definition: body_types.h:403
int32_t height
Frame height
Definition: body_types.h:405
Definition: stream_types.h:62
Struct containing floor information about the scene
Definition: body_types.h:414
astra_floormask_t floorMask
Floor mask
Definition: body_types.h:416
astra_bool_t floorDetected
If the floor is detected, ASTRA_TRUE. Otherwise, ASTRA_FALSE.
Definition: body_types.h:424
astra_plane_t floorPlane
Floor plane
Definition: body_types.h:418
Definition: body_types.h:187
Joint information
Definition: body_types.h:123
astra_vector2f_t depthPosition
Depth ("projective") position of joint
Definition: body_types.h:135
astra_joint_status_t status
Joint status
Definition: body_types.h:133
astra_matrix3x3_t orientation
3x3 Rotation matrix representing the rotation of this joint.
Definition: body_types.h:140
astra_vector3f_t worldPosition
Real world position of joint
Definition: body_types.h:137
astra_joint_type_t type
Joint type
Definition: body_types.h:128
Represents a 3x3 rotation matrix
Definition: astra_ctypes.h:50
Struct representing a geometric 3-dimensional plane
Definition: body_types.h:386
float d
D coefficient
Definition: body_types.h:394
float c
C coefficient
Definition: body_types.h:392
float a
A coefficient
Definition: body_types.h:388
float b
B coefficient
Definition: body_types.h:390
Definition: astra_plugin.h:24
Definition: astra_ctypes.h:27
Definition: astra_ctypes.h:32