Skip to content

Commit 3472a79

Browse files
authored
Update for ZED SDK 4.2 (#659)
* adding 4.2 samples * Fixing #645 yolo python color conversion
1 parent 229593e commit 3472a79

File tree

162 files changed

+16998
-6770
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+16998
-6770
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
---
3434

35-
:tada: The **ZED SDK 4.1** is released! We support the [**ZED X**](https://www.stereolabs.com/zed-x/) and [**ZED X Mini**](https://www.stereolabs.com/zed-x/) cameras, added the **Fusion API** for multi-camera Body Tracking, and more! Please check the [Release Notes](https://www.stereolabs.com/developers/release/) of the latest version for more details.
35+
:tada: The **ZED SDK 4.2** is released! We support the [**ZED X**](https://www.stereolabs.com/zed-x/) and [**ZED X Mini**](https://www.stereolabs.com/zed-x/) cameras, added the **Fusion API** for multi-camera Body Tracking, and more! Please check the [Release Notes](https://www.stereolabs.com/developers/release/) of the latest version for more details.
3636

3737
## Overview
3838

body tracking/body tracking/cpp/include/GLViewer.hpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,19 @@ const std::vector<std::pair<BODY_38_PARTS, BODY_38_PARTS>> BODY_BONES_FAST_RENDE
127127
class Shader {
128128
public:
129129

130-
Shader() {
131-
}
130+
Shader() : verterxId_(0), fragmentId_(0), programId_(0) {}
132131
Shader(const GLchar* vs, const GLchar* fs);
133132
~Shader();
133+
134+
// Delete the move constructor and move assignment operator
135+
Shader(Shader&&) = delete;
136+
Shader& operator=(Shader&&) = delete;
137+
138+
// Delete the copy constructor and copy assignment operator
139+
Shader(const Shader&) = delete;
140+
Shader& operator=(const Shader&) = delete;
141+
142+
void set(const GLchar* vs, const GLchar* fs);
134143
GLuint getProgramId();
135144

136145
static const GLint ATTRIB_VERTICES_POS = 0;

body tracking/body tracking/cpp/src/GLViewer.cpp

+11-8
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ void GLViewer::init(int argc, char **argv) {
149149
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
150150

151151
// Compile and create the shader for 3D objects
152-
shaderSK.it = Shader(SK_VERTEX_SHADER, SK_FRAGMENT_SHADER);
152+
shaderSK.it.set(SK_VERTEX_SHADER, SK_FRAGMENT_SHADER);
153153
shaderSK.MVP_Mat = glGetUniformLocation(shaderSK.it.getProgramId(), "u_mvpMatrix");
154154

155-
shaderLine.it = Shader(VERTEX_SHADER, FRAGMENT_SHADER);
155+
shaderLine.it.set(VERTEX_SHADER, FRAGMENT_SHADER);
156156
shaderLine.MVP_Mat = glGetUniformLocation(shaderLine.it.getProgramId(), "u_mvpMatrix");
157157

158158
// Create the camera
@@ -384,8 +384,7 @@ void GLViewer::idle() {
384384
glutPostRedisplay();
385385
}
386386

387-
Simple3DObject::Simple3DObject() {
388-
}
387+
Simple3DObject::Simple3DObject() : vaoID_(0) {}
389388

390389
Simple3DObject::~Simple3DObject() {
391390
if (vaoID_ != 0) {
@@ -732,6 +731,10 @@ sl::Transform Simple3DObject::getModelMatrix() const {
732731
}
733732

734733
Shader::Shader(const GLchar* vs, const GLchar* fs) {
734+
set(vs, fs);
735+
}
736+
737+
void Shader::set(const GLchar* vs, const GLchar* fs) {
735738
if (!compile(verterxId_, GL_VERTEX_SHADER, vs)) {
736739
std::cout << "ERROR: while compiling vertex shader" << std::endl;
737740
}
@@ -767,12 +770,12 @@ Shader::Shader(const GLchar* vs, const GLchar* fs) {
767770
}
768771

769772
Shader::~Shader() {
770-
if (verterxId_ != 0)
773+
if (verterxId_ != 0 && glIsShader(verterxId_))
771774
glDeleteShader(verterxId_);
772-
if (fragmentId_ != 0)
775+
if (fragmentId_ != 0 && glIsShader(fragmentId_))
773776
glDeleteShader(fragmentId_);
774-
if (programId_ != 0)
775-
glDeleteShader(programId_);
777+
if (programId_ != 0 && glIsProgram(programId_))
778+
glDeleteProgram(programId_);
776779
}
777780

778781
GLuint Shader::getProgramId() {

body tracking/body tracking/csharp/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_executable(${PROJECT_NAME}
77
MainWindow.cs
88
GLViewer.cs
99
TrackingViewer.cs
10+
Utils.cs
1011
App.config
1112
packages.config
1213
Properties/AssemblyInfo.cs

body tracking/body tracking/csharp/MainWindow.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public MainWindow(string[] args)
9494
isTrackingON = bt_params.enableObjectTracking;
9595
bt_params.enableSegmentation = false;
9696
bt_params.enableBodyFitting = true; // smooth skeletons moves
97-
bt_params.imageSync = true; // the body tracking is synchronized to the image
9897
bt_params.detectionModel = sl.BODY_TRACKING_MODEL.HUMAN_BODY_ACCURATE;
9998
bt_params.bodyFormat = sl.BODY_FORMAT.BODY_38;
10099

@@ -125,7 +124,7 @@ public MainWindow(string[] args)
125124
bt_runtime_parameters.detectionConfidenceThreshold = 20;
126125

127126
window_name = "ZED| 2D View";
128-
Cv2.NamedWindow(window_name, WindowMode.Normal);// Create Window
127+
Cv2.NamedWindow(window_name);// Create Window
129128

130129
// Create OpenGL window
131130
CreateWindow();
@@ -234,7 +233,7 @@ private void NativeWindow_Render(object sender, NativeWindowEventArgs e)
234233
// Retrieve Objects
235234
zedCamera.RetrieveBodies(ref bodies, ref bt_runtime_parameters);
236235

237-
TrackingViewer.render_2D(ref imageLeftOcv, imgScale, ref bodies, isTrackingON, bt_params.bodyFormat);
236+
TrackingViewer.render_2D(ref imageLeftOcv, imgScale, ref bodies, isTrackingON, bt_params.bodyFormat, bt_params.enableSegmentation);
238237

239238
//Update GL View
240239
viewer.update(pointCloud, bodies, camPose);

body tracking/body tracking/csharp/TrackingViewer.cs

+25-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using sl;
77

88
using OpenCvSharp;
9+
using System.Windows.Media.Media3D;
910

1011

1112
public class TrackingViewer
@@ -58,7 +59,7 @@ static sl.float2 getImagePosition(Vector2[] bounding_box_image, sl.float2 img_sc
5859
return position;
5960
}
6061

61-
public static void render_2D(ref OpenCvSharp.Mat left_display, sl.float2 img_scale, ref sl.Bodies bodies, bool showOnlyOK, sl.BODY_FORMAT body_format)
62+
public static void render_2D(ref OpenCvSharp.Mat left_display, sl.float2 img_scale, ref sl.Bodies bodies, bool showOnlyOK, sl.BODY_FORMAT body_format, bool render_mask)
6263
{
6364
OpenCvSharp.Mat overlay = left_display.Clone();
6465
OpenCvSharp.Rect roi_render = new OpenCvSharp.Rect(1, 1, left_display.Size().Width, left_display.Size().Height);
@@ -93,6 +94,29 @@ public static void render_2D(ref OpenCvSharp.Mat left_display, sl.float2 img_sca
9394
}
9495
}
9596
}
97+
98+
if (render_mask)
99+
{
100+
Point top_left_corner = Utils.cvt(bod.boundingBox2D[0], img_scale);
101+
Point top_right_corner = Utils.cvt(bod.boundingBox2D[1], img_scale);
102+
Point bottom_left_corner = Utils.cvt(bod.boundingBox2D[3], img_scale);
103+
var width = (int)Math.Abs(top_right_corner.X - top_left_corner.X);
104+
var height = (int)Math.Abs(bottom_left_corner.Y - top_left_corner.Y);
105+
// Scaled ROI
106+
OpenCvSharp.Rect roi = new OpenCvSharp.Rect(top_left_corner.X, top_left_corner.Y, width, height);
107+
sl.Mat mask = new sl.Mat(bod.mask);
108+
if (mask.IsInit())
109+
{
110+
sl.Mat m = new sl.Mat();
111+
mask.CopyTo(m);
112+
OpenCvSharp.Mat tmp_mask = new OpenCvSharp.Mat(mask.GetHeight(), mask.GetWidth(), OpenCvSharp.MatType.CV_8UC1, m.GetPtr());
113+
if (!tmp_mask.Empty())
114+
{
115+
var mask_resized = tmp_mask.Resize(roi.Size);
116+
overlay.SubMat(roi).SetTo(base_color, mask_resized);
117+
}
118+
}
119+
}
96120
}
97121
}
98122

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
//======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
2+
using System;
3+
using System.Numerics;
4+
using System.Runtime.InteropServices;
5+
using System.Threading.Tasks;
6+
using OpenCvSharp;
7+
8+
namespace sl
9+
{
10+
public class Utils
11+
{
12+
/// <summary>
13+
/// Creates an OpenCV version of a ZED Mat.
14+
/// </summary>
15+
/// <param name="zedmat">Source ZED Mat.</param>
16+
/// <param name="zedmattype">Type of ZED Mat - data type and channel number.
17+
/// <returns></returns>
18+
public static OpenCvSharp.Mat SLMat2CVMat(ref sl.Mat zedmat, MAT_TYPE zedmattype)
19+
{
20+
int cvmattype = SLMatType2CVMatType(zedmattype);
21+
OpenCvSharp.Mat cvmat = new OpenCvSharp.Mat(zedmat.GetHeight(), zedmat.GetWidth(), cvmattype, zedmat.GetPtr());
22+
23+
return cvmat;
24+
}
25+
26+
/// <summary>
27+
/// Returns the OpenCV type that corresponds to a given ZED Mat type.
28+
/// </summary>
29+
private static int SLMatType2CVMatType(MAT_TYPE zedmattype)
30+
{
31+
switch (zedmattype)
32+
{
33+
case sl.MAT_TYPE.MAT_32F_C1:
34+
return OpenCvSharp.MatType.CV_32FC1;
35+
case sl.MAT_TYPE.MAT_32F_C2:
36+
return OpenCvSharp.MatType.CV_32FC2;
37+
case sl.MAT_TYPE.MAT_32F_C3:
38+
return OpenCvSharp.MatType.CV_32FC3;
39+
case sl.MAT_TYPE.MAT_32F_C4:
40+
return OpenCvSharp.MatType.CV_32FC4;
41+
case sl.MAT_TYPE.MAT_8U_C1:
42+
return OpenCvSharp.MatType.CV_8UC1;
43+
case sl.MAT_TYPE.MAT_8U_C2:
44+
return OpenCvSharp.MatType.CV_8UC2;
45+
case sl.MAT_TYPE.MAT_8U_C3:
46+
return OpenCvSharp.MatType.CV_8UC3;
47+
case sl.MAT_TYPE.MAT_8U_C4:
48+
return OpenCvSharp.MatType.CV_8UC4;
49+
default:
50+
return -1;
51+
}
52+
}
53+
54+
public static ulong getMilliseconds(ulong ts_ns)
55+
{
56+
return ts_ns / 1000000;
57+
}
58+
59+
public static void drawVerticalLine(ref OpenCvSharp.Mat left_display, Point start_pt, Point end_pt, Scalar clr, int thickness)
60+
{
61+
int n_steps = 7;
62+
Point pt1, pt4;
63+
pt1.X = ((n_steps - 1) * start_pt.X + end_pt.X) / n_steps;
64+
pt1.Y = ((n_steps - 1) * start_pt.Y + end_pt.Y) / n_steps;
65+
66+
pt4.X = (start_pt.X + (n_steps - 1) * end_pt.X) / n_steps;
67+
pt4.Y = (start_pt.Y + (n_steps - 1) * end_pt.Y) / n_steps;
68+
69+
Cv2.Line(left_display, start_pt, pt1, clr, thickness);
70+
Cv2.Line(left_display, pt4, end_pt, clr, thickness);
71+
}
72+
73+
public static Point cvt(Vector2 pt, sl.float2 scale)
74+
{
75+
return new Point(pt.X * scale.x, pt.Y * scale.y);
76+
}
77+
78+
public static sl.float4 generateColorID(int idx)
79+
{
80+
int offset = Math.Max(0, idx % 5);
81+
sl.float4 color = new float4();
82+
color.x = id_colors[offset, 0];
83+
color.y = id_colors[offset, 1];
84+
color.z = id_colors[offset, 2];
85+
color.w = 1.0f;
86+
return color;
87+
}
88+
89+
public static OpenCvSharp.Scalar generateColorID_u(int idx)
90+
{
91+
int offset = Math.Max(0, idx % 5);
92+
OpenCvSharp.Scalar color = new OpenCvSharp.Scalar();
93+
color[0] = id_colors[offset, 2] * 255;
94+
color[1] = id_colors[offset, 1] * 255;
95+
color[2] = id_colors[offset, 0] * 255;
96+
color[3] = 1.0f * 255;
97+
return color;
98+
}
99+
100+
public static float[,] id_colors = new float[5, 3]{
101+
102+
{.231f, .909f, .69f},
103+
{.098f, .686f, .816f},
104+
{.412f, .4f, .804f},
105+
{1, .725f, .0f},
106+
{.989f, .388f, .419f}
107+
};
108+
109+
public static float[,] class_colors = new float[6, 3]{
110+
{ 44.0f, 117.0f, 255.0f}, // PEOPLE
111+
{ 255.0f, 0.0f, 255.0f}, // VEHICLE
112+
{ 0.0f, 0.0f, 255.0f},
113+
{ 0.0f, 255.0f, 255.0f},
114+
{ 0.0f, 255.0f, 0.0f},
115+
{ 255.0f, 255.0f, 255.0f}
116+
};
117+
118+
public static float4 generateColorClass(int idx)
119+
{
120+
idx = Math.Min(5, idx);
121+
sl.float4 color = new float4();
122+
color.x = class_colors[idx, 0];
123+
color.y = class_colors[idx, 1];
124+
color.z = class_colors[idx, 2];
125+
color.w = 255.0f;
126+
return color;
127+
}
128+
129+
public static OpenCvSharp.Scalar generateColorClass_u(int idx)
130+
{
131+
idx = Math.Min(5, idx);
132+
OpenCvSharp.Scalar color = new OpenCvSharp.Scalar();
133+
color[0] = class_colors[idx, 0];
134+
color[0] = class_colors[idx, 1];
135+
color[0] = class_colors[idx, 2];
136+
color[0] = 255.0f;
137+
return color;
138+
}
139+
140+
public static bool renderObject(ObjectData i, bool showOnlyOK)
141+
{
142+
if (showOnlyOK)
143+
return (i.objectTrackingState == sl.OBJECT_TRACKING_STATE.OK);
144+
else
145+
return (i.objectTrackingState == sl.OBJECT_TRACKING_STATE.OK || i.objectTrackingState == sl.OBJECT_TRACKING_STATE.OFF);
146+
}
147+
148+
149+
public static byte _applyFading(double val, float current_alpha, double current_clr)
150+
{
151+
return (byte)(current_alpha * current_clr + (1.0 - current_alpha) * val);
152+
}
153+
154+
public static Vec4b applyFading(Scalar val, float current_alpha, Scalar current_clr){
155+
Vec4b out_ = new Vec4b();
156+
out_[0] = _applyFading(val[0], current_alpha, current_clr[0]);
157+
out_[1] = _applyFading(val[1], current_alpha, current_clr[1]);
158+
out_[2] = _applyFading(val[2], current_alpha, current_clr[2]);
159+
out_[3] = 255;
160+
return out_;
161+
}
162+
}
163+
}

body tracking/export/JSON export/cpp/include/GLViewer.hpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,19 @@ const std::vector<std::pair<BODY_38_PARTS, BODY_38_PARTS>> BODY_BONES_FAST_RENDE
127127
class Shader {
128128
public:
129129

130-
Shader() {
131-
}
130+
Shader() : verterxId_(0), fragmentId_(0), programId_(0) {}
132131
Shader(const GLchar* vs, const GLchar* fs);
133132
~Shader();
133+
134+
// Delete the move constructor and move assignment operator
135+
Shader(Shader&&) = delete;
136+
Shader& operator=(Shader&&) = delete;
137+
138+
// Delete the copy constructor and copy assignment operator
139+
Shader(const Shader&) = delete;
140+
Shader& operator=(const Shader&) = delete;
141+
142+
void set(const GLchar* vs, const GLchar* fs);
134143
GLuint getProgramId();
135144

136145
static const GLint ATTRIB_VERTICES_POS = 0;

0 commit comments

Comments
 (0)