Skip to content

Commit

Permalink
update samples for version 3.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
P-yver committed Jul 27, 2020
1 parent 4dbf950 commit 5b2c6ec
Show file tree
Hide file tree
Showing 21 changed files with 4,472 additions and 399 deletions.
1 change: 1 addition & 0 deletions body tracking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ This sample shows how to detect and track bodies in space and display it. It dem
- For more information, read the ZED [API documentation](https://www.stereolabs.com/developers/documentation/API/).

*NOTE:* A ZED 2 is required to run use this module.
*NOTE2:* This sample is only available in C++
6 changes: 0 additions & 6 deletions body tracking/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ FILE(GLOB_RECURSE HDR_FILES include/*.h*)
add_executable(${PROJECT_NAME} ${HDR_FILES} ${SRC_FILES})
add_definitions(-std=c++14)

if(CMAKE_BUILD_TYPE STREQUAL "Debug") # Added by Walter... to be removed
add_definitions(-D_DEBUG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_DEBUG")
endif()


if (LINK_SHARED_ZED)
SET(ZED_LIBS ${ZED_LIBRARIES} ${CUDA_CUDA_LIBRARY} ${CUDA_CUDART_LIBRARY} ${CUDA_DEP_LIBRARIES_ZED})
else()
Expand Down
8 changes: 0 additions & 8 deletions body tracking/cpp/include/GLViewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ struct ObjectClassName {
sl::float4 color;
};

struct ObjectExtPosition {
sl::float3 position;
sl::Timestamp timestamp;
};

// This class manages input events, window and Opengl rendering pipeline
class GLViewer {
public:
Expand Down Expand Up @@ -195,9 +190,6 @@ class GLViewer {
Simple3DObject BBox_obj;
Simple3DObject skeletons_obj;

sl::Resolution windowSize;
sl::Resolution imageSize;
sl::CameraParameters camera_parameters;
bool g_showBox=true;
bool g_showLabel=true;
int f_count = 0;
Expand Down
44 changes: 21 additions & 23 deletions body tracking/cpp/src/GLViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ void GLViewer::init(int argc, char **argv, sl::CameraParameters param) {
height = param.image_size.height;
}

camera_parameters = param;
windowSize.width = width;
windowSize.height = height;
glutInitWindowSize(windowSize.width, windowSize.height);
glutInitWindowSize(width, height);
glutInitWindowPosition(wnd_w*0.05, wnd_h*0.05);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_SRGB);
glutCreateWindow("ZED Object Detection Viewer");
Expand All @@ -95,10 +92,9 @@ void GLViewer::init(int argc, char **argv, sl::CameraParameters param) {
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

imageSize = param.image_size;
bool err_pc = image_handler.initialize(imageSize);
if (!err_pc)
std::cout << "ERROR: Failed to initialized point cloud"<<std::endl;
bool status_ = image_handler.initialize(param.image_size);
if (!status_)
std::cout << "ERROR: Failed to initialized Image Renderer"<<std::endl;

glEnable(GL_FRAMEBUFFER_SRGB);

Expand Down Expand Up @@ -193,28 +189,30 @@ const std::vector< sl::float3> bones_colors = {
sl::float3(1, 0, 1), sl::float3(1, 0, 0.666), sl::float3(1, 0, 0.333)
};

void GLViewer::updateView(sl::Mat image, sl::Objects &obj)
inline bool renderObject(const sl::ObjectData& i) {
return (i.tracking_state == sl::OBJECT_TRACKING_STATE::OK || i.tracking_state == sl::OBJECT_TRACKING_STATE::OFF);
}

void GLViewer::updateView(sl::Mat image, sl::Objects &objects)
{
// Update Image
image_handler.pushNewImage(image);

// Clear frames object
BBox_obj.clear();
skeletons_obj.clear();
std::vector<sl::ObjectData> objs = obj.object_list;
objectsName.clear();

// sort objects by depth
std::sort(objects.object_list.begin(), objects.object_list.end(), [ ](const sl::ObjectData& obj1, const sl::ObjectData& obj2) { return obj1.position.z < obj2.position.z;});

// For each object
for (int i = 0; i < objs.size(); i++) {
for (auto &obj: objects.object_list) {
// Only show tracked objects
if (objs[i].tracking_state == sl::OBJECT_TRACKING_STATE::OK && objs[i].id>=0) {

auto bb_ = objs[i].bounding_box;
auto pos_ = objs[i].position;
ObjectExtPosition ext_pos_;
ext_pos_.position = pos_;
ext_pos_.timestamp = obj.timestamp;
auto clr_id = generateColorClass(objs[i].id);
if (renderObject(obj)) {

auto bb_ = obj.bounding_box;
auto clr_id = generateColorClass(obj.id);
// Draw boxes
if (g_showBox && bb_.size()>0) {
if (floor_plane_set) {
Expand All @@ -223,7 +221,7 @@ void GLViewer::updateView(sl::Mat image, sl::Objects &obj)
}
BBox_obj.addBoundingBox(bb_, clr_id);

auto keypoints = objs[i].keypoint;
auto keypoints = obj.keypoint;
if (keypoints.size()) {
for (auto& limb : sl::BODY_BONES) {
sl::float3 kp_1 = keypoints[getIdx(limb.first)];
Expand All @@ -240,12 +238,12 @@ void GLViewer::updateView(sl::Mat image, sl::Objects &obj)
if (g_showLabel) {
if ( bb_.size()>0) {
objectsName.emplace_back();
objectsName.back().name_lineA = "ID : "+ std::to_string(objs[i].id);
objectsName.back().name_lineA = "ID : "+ std::to_string(obj.id);
std::stringstream ss_vel;
ss_vel << std::fixed << std::setprecision(1) << objs[i].velocity.norm();
ss_vel << std::fixed << std::setprecision(1) << obj.velocity.norm();
objectsName.back().name_lineB = ss_vel.str()+" m/s";
objectsName.back().color = clr_id;
objectsName.back().position = pos_;
objectsName.back().position = obj.position;
objectsName.back().position.y =(bb_[0].y + bb_[1].y + bb_[2].y + bb_[3].y)/4.f +0.2f;
}
}
Expand Down
6 changes: 0 additions & 6 deletions camera control/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ if(COMMAND cmake_policy)
cmake_policy(SET CMP0015 OLD)
endif(COMMAND cmake_policy)


if (NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
SET(CMAKE_BUILD_TYPE "RelWithDebInfo")
endif()


SET(EXECUTABLE_OUTPUT_PATH ".")

find_package(ZED 3 REQUIRED)
Expand Down
7 changes: 4 additions & 3 deletions depth sensing/cpp/src/GLViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ void GLViewer::mouseMotionCallback(int x, int y) {

void GLViewer::reshapeCallback(int width, int height) {
glViewport(0, 0, width, height);
float hfov = currentInstance_->camera_.getHorizontalFOV();
currentInstance_->camera_.setProjection(hfov, hfov * (float) height / (float) width, currentInstance_->camera_.getZNear(), currentInstance_->camera_.getZFar());
float hfov = (180.0f / M_PI) * (2.0f * atan(width / (2.0f * 500)));
float vfov = (180.0f / M_PI) * (2.0f * atan(height / (2.0f * 500)));
currentInstance_->camera_.setProjection(hfov, vfov, currentInstance_->camera_.getZNear(), currentInstance_->camera_.getZFar());
}

void GLViewer::keyPressedCallback(unsigned char c, int x, int y) {
Expand Down Expand Up @@ -587,7 +588,7 @@ CameraGL::CameraGL(sl::Translation position, sl::Translation direction, sl::Tran
offset_ = sl::Translation(0, 0, 0);
view_.setIdentity();
updateView();
setProjection(80, 80, 100.f, 900000.f);
setProjection(70, 70, 200.f, 50000.f);
updateVPMatrix();
}

Expand Down
4 changes: 0 additions & 4 deletions object detection/cpp/include/GLViewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,11 @@ class CameraGL {
~CameraGL();

void update();
void setPerspective(bool enable);
void setProjection(float horizontalFOV, float verticalFOV, float znear, float zfar);
const sl::Transform& getViewProjectionMatrix() const;

float getHorizontalFOV() const;
void setHorizontalFOV(float hfov);
float getVerticalFOV() const;
void setVerticalFOV(float vfov);

// Set an offset between the eye of the camera and its position
// Note: Useful to use the camera as a trackball camera with z>0 and x = 0, y = 0
Expand Down Expand Up @@ -301,7 +298,6 @@ class GLViewer {
Simple3DObject BBox_faces;
Simple3DObject skeletons;
Simple3DObject floor_grid;

};

#endif /* __VIEWER_INCLUDE__ */
45 changes: 29 additions & 16 deletions object detection/cpp/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ inline cv::Scalar generateColorID_u(int idx) {

inline sl::float4 generateColorID_f(int idx) {
auto clr_u = generateColorID_u(idx);
return sl::float4(clr_u.val[0] / 255.f, clr_u.val[1] / 255.f, clr_u.val[2] / 255.f, 1.f);
return sl::float4(static_cast<float>(clr_u.val[0]) / 255.f, static_cast<float>(clr_u.val[1]) / 255.f, static_cast<float>(clr_u.val[2]) / 255.f, 1.f);
}

inline bool renderObject(const sl::ObjectData& i) {
Expand All @@ -44,6 +44,28 @@ inline sl::float4 getColorClass(int idx) {
return clr / 255.f;
}

template<typename T>
inline uchar _applyFading(T val, float current_alpha, double current_clr){
return static_cast<uchar>(current_alpha * current_clr + (1.0 - current_alpha) * val);
}

inline cv::Vec3b applyFading(cv::Scalar val, float current_alpha, cv::Scalar current_clr){
cv::Vec3b out;
out[0] = _applyFading(val.val[0], current_alpha, current_clr.val[0]);
out[1] = _applyFading(val.val[1], current_alpha, current_clr.val[1]);
out[2] = _applyFading(val.val[2], current_alpha, current_clr.val[2]);
return out;
}

template<int C>
inline cv::Vec<uchar, C> applyFading(cv::Vec<uchar, C> val, float current_alpha, cv::Scalar current_clr){
cv::Vec<uchar, C> out;
out[0] = _applyFading(val[0], current_alpha, current_clr.val[0]);
out[1] = _applyFading(val[1], current_alpha, current_clr.val[1]);
out[2] = _applyFading(val[2], current_alpha, current_clr.val[2]);
return out;
}

inline void drawFadedLine(
cv::Mat &left_display,
cv::Point2i start_pt,
Expand All @@ -61,32 +83,23 @@ inline void drawFadedLine(
for (int c = 0; c < 4; c++)
current_clr.val[c] = ratio * end_clr.val[c] + (1.0f - ratio) * start_clr.val[c];
#else
current_clr = ratio * end_clr + (1.0f - ratio) * start_clr;
current_clr = ratio * end_clr + (1.f - ratio) * start_clr;
#endif
double current_alpha = current_clr.val[3] / 255.0;
float current_alpha = static_cast<float>(current_clr.val[3]) / 255.f;
cv::Point2i current_position = left_line_it.pos();

cv::Vec4b mid_px = left_display.at<cv::Vec4b>(current_position.y, current_position.x);
mid_px[0] = current_alpha * current_clr.val[0] + (1.0 - current_alpha) * mid_px[0];
mid_px[1] = current_alpha * current_clr.val[1] + (1.0 - current_alpha) * mid_px[1];
mid_px[2] = current_alpha * current_clr.val[2] + (1.0 - current_alpha) * mid_px[2];
left_display.at<cv::Vec4b>(current_position.y, current_position.x) = mid_px;
left_display.at<cv::Vec4b>(current_position.y, current_position.x) = applyFading(mid_px, current_alpha, current_clr);
if (current_position.x - offset >= 0) {
for (int off = 1; off <= offset; ++off) {
cv::Vec4b px = left_display.at<cv::Vec4b>(current_position.y, current_position.x - off);
px[0] = current_alpha * current_clr.val[0] + (1.0 - current_alpha) * px[0];
px[1] = current_alpha * current_clr.val[1] + (1.0 - current_alpha) * px[1];
px[2] = current_alpha * current_clr.val[2] + (1.0 - current_alpha) * px[2];
left_display.at<cv::Vec4b>(current_position.y, current_position.x - off) = px;
cv::Vec4b px = left_display.at<cv::Vec4b>(current_position.y, current_position.x - off);
left_display.at<cv::Vec4b>(current_position.y, current_position.x - off) = applyFading(px,current_alpha, current_clr);
}
}
if (current_position.x + offset < left_display.cols) {
for (int off = 1; off <= offset; ++off) {
cv::Vec4b px = left_display.at<cv::Vec4b>(current_position.y, current_position.x + off);
px[0] = current_alpha * current_clr.val[0] + (1.0 - current_alpha) * px[0];
px[1] = current_alpha * current_clr.val[1] + (1.0 - current_alpha) * px[1];
px[2] = current_alpha * current_clr.val[2] + (1.0 - current_alpha) * px[2];
left_display.at<cv::Vec4b>(current_position.y, current_position.x + off) = px;
left_display.at<cv::Vec4b>(current_position.y, current_position.x + off) = applyFading(px,current_alpha, current_clr);
}
}
}
Expand Down
Loading

0 comments on commit 5b2c6ec

Please sign in to comment.