Tutorial - Using 3D Object Detection

Open in ClaudeOpen in ChatGPT

This tutorial shows how to use your ZED 3D camera to detect, classify and locate persons in space (compatible with ZED 2 only). Detection and localization works with both a static or moving camera.

Getting Started

  • First, download the latest version of the ZED SDK.
  • Download the Object Detection sample code in C++, Python or C#.

Code Overview

Open the camera

In this tutorial, we will use the Object Detection AI module of the ZED SDK. As in previous tutorials, we create, configure and open the camera.

1// Create ZED objects
2Camera zed;
3InitParameters init_parameters;
4init_parameters.camera_resolution = RESOLUTION::HD720;
5init_parameters.depth_mode = DEPTH_MODE::ULTRA;
6
7// Open the camera
8ERROR_CODE zed_error = zed.open(init_parameters);
9if (zed_error != ERROR_CODE::SUCCESS) {
10 std::cout << "Error " << zed_error << ", exit program.\n";
11 return 1; // Quit if an error occurred
12}

Enable 3D Object detection

Before enabling object detection, we specify the ObjectDetectionParameters of the module. In this tutorial, we use the following settings:

1// Define the Object Detection module parameters
2ObjectDetectionParameters detection_parameters;
3detection_parameters.image_sync = true;
4detection_parameters.enable_tracking = true;
5detection_parameters.enable_mask_output = true;
6
7// Object tracking requires camera tracking to be enabled
8if (detection_parameters.enable_tracking)
9 zed.enablePositionalTracking();
  • image_sync determines if object detection runs for each frame or asynchronously in a separate thread.
  • enable_tracking allows objects to be tracked across frames and keep the same ID as long as possible. Positional tracking must be active in order to track objects’ movements independently from camera motion.
  • enable_mask_output outputs 2D masks over detected objects. Since it requires additional processing, disable this option if not used.

Now let’s enable object detection which will load an AI model. This operation can take a few seconds. The first time the module is used, the model will be optimized for your hardware and this can take up to a few minutes. The model optimization operation is done only once.

1cout << "Object Detection: Loading Module..." << endl;
2err = zed.enableObjectDetection(detection_parameters);
3if (err != ERROR_CODE::SUCCESS) {
4 cout << "Error " << err << ", exit program.\n";
5 zed.close();
6 return 1;
7}

Retrieve object data

To retrieve detected objects in an image, use the retrieveObjects() function with an Objects parameter that will store objects’ data.

Since image_sync is enabled, for each grab call, the image will be fed into the AI module that will output the detected objects for each frame. We also set the object confidence threshold at 40 to keep only very confident detections.

1// Set runtime parameter confidence to 40
2ObjectDetectionRuntimeParameters detection_parameters_runtime;
3detection_parameters_runtime.detection_confidence_threshold = 40;
4
5Objects objects;
6
7// Grab new frames and detect objects
8while (zed.grab() == ERROR_CODE::SUCCESS) {
9 err = zed.retrieveObjects(objects, detection_parameters_runtime);
10
11 if (objects.is_new) {
12 // Count the number of objects detected
13 cout << objects.object_list.size() << " Object(s) detected" << endl;
14
15 // Display the 3D location of an object
16 first_object = objects.object_list[0];
17 cout << " 3D position: " << first_object.position;
18
19 // Display its 3D bounding box coordinates
20 cout << " Bounding box 3D \n";
21 for (auto it : first_object.bounding_box)
22 cout << " " << it;
23 }
24}

Disable modules and exit

Before exiting the application, modules need to be disabled and the camera closed.

zed.close() can also disable properly all active modules. The close() function is also called automatically by the destructor if necessary.

1// Disable object detection and close the camera
2zed.disableObjectDetection();
3zed.close();

And this is it!