CodeTheRobot

Ubuntu Linux for Robotics · Lesson 71 of 96

Object detection

Running a neural-network detector on Ubuntu, GPU use and detection messages.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain object detection in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

Object detection finds and labels objects in an image, giving bounding boxes with a class and confidence. On Ubuntu you run a model (YOLO, exported to ONNX or TensorRT) inside a ROS 2 node that subscribes to the camera and publishes vision_msgs/Detection2DArray. A GPU makes it many times faster.

Watch performance with nvidia-smi (or tegrastats on a Jetson) and htop, and lower the input size or frame rate if the robot cannot keep up. Drop old frames instead of queueing them so detections stay current, and record a test bag to compare models fairly.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the object detection commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

nvidia-smi
# Check that the GPU is available and how busy it is.
pip install onnxruntime-gpu
# Install a GPU inference runtime inside a virtual environment.
ros2 run detector yolo_node --ros-args -p model:=yolov8n.onnx -p confidence:=0.5
# Start the detector node (name and parameters depend on your package).
ros2 topic hz /detections
# Measure how many detections per second the pipeline achieves.
ros2 topic echo /detections --once
# Read one detection message.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · Sensor view

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

SENSOR VIEW

Demo for Object detection. Expected result: A table with the GPU utilization while the detector runs

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: show the GPU status once per second with nvidia-smi

Expected output

A table with the GPU utilization while the detector runs
Stuck? Show the full solution (try it yourself first)
nvidia-smi -l 1

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Run a detector on a camera or bag, record frames per second with and without the GPU, and choose a confidence threshold that removes false detections in your scene.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.