Skip to content

Computer vision

sub_vision turns camera images into something the mission can use: “a red pipe, 0.3 radians to the left, 2 meters away”.

An image is a grid of pixels, and each pixel is three numbers: how much red, green and blue. The front camera’s images are 1920 × 1080, so each one is around 6 million numbers, 24 times a second.

Hand-written rules for finding a gate in those numbers are hard to get right underwater, where color and lighting keep changing, so a trained neural network does it.

Object detection means finding objects in an image, and for each one reporting:

  • a box around it, called a bounding box,
  • a class: what it is, such as “red pipe”, usually stored as a number,
  • a score from 0 to 1: how confident the network is.

We use YOLO (“You Only Look Once”), a family of detection networks fast enough to run on every frame.

A neural network contains millions of numbers, called weights. Using one has two stages:

  • Training shows it thousands of images on which people have drawn the correct boxes (labels), and adjusts the weights until it gets them right. It takes hours on a powerful GPU.
  • Inference runs the finished network on new images, in a few tens of milliseconds. This is what the robot does.

The weights are fixed during a run. pixi run fetch-weights downloads them into weights/ as .onnx files.

ONNX is a standard file format for neural networks, and ONNX Runtime is the library that runs them. On the robot it uses the Jetson’s GPU, and on most laptops the CPU.

There is one network for each task, and only one is loaded at a time per camera. The mission asks for the one it needs with a LoadModel step:

<LoadModel camera="front" task="gate" timeout_msec="20000"/>

To load one by hand, with the windowed simulator running (headless:=true has no cameras), in a pixi shell:

Terminal window
ros2 service call /marlin_v3/sub_vision/load_model sub_vision_interfaces/srv/LoadModel "{task: 'gate'}"
response:
sub_vision_interfaces.srv.LoadModel_Response(success=True, message="loaded 'gate' (ffc_rs_26) via onnxruntime/cpu (warmup 78.0 ms/iter)", active_model='gate (onnxruntime/cpu)', load_time_s=0.3296832740306854)

It loaded, on the CPU, at 78 ms per image. See what it detects:

Terminal window
ros2 topic echo --once --field detections /marlin_v3/vision/detections
[]
---

An empty list means it sees nothing, as expected for this model in the simulator. The Foxglove vision extension draws the detections on the camera image, the easiest way to check a model.

To steer, the robot converts the box’s position in pixels into an angle, a bearing.

The camera’s focal length fx, measured in pixels, sets how pixels relate to angles. If the center of the image is at cx, then a box centered at pixel u is at this angle:

bearing = atan((u − cx) / fx)

The front camera’s image is 1920 pixels wide, so cx is about 960, and in the simulator fx is about 1361. The real value is in the camera’s calibration:

Terminal window
ros2 topic echo --once --field k /marlin_v3/front_camera/camera_info

So a box centered at pixel 1300 is at atan((1300 − 960) / 1361) ≈ 0.245 radians, or 14° to the right.

Bearings use the camera’s convention, so positive means right of center and below center. A mission step like AlignToDetection turns the robot until the bearing is close to zero. See Camera frames.

An object looks half the size from twice as far away, so the size in pixels of an object of known size gives its distance:

distance = real size × fx / size in pixels

For example, the octagon’s table is 0.546 m wide. If its box is 200 pixels wide, it’s 0.546 × 1361 / 200 ≈ 3.7 m away.

After the network runs, a post-processor does the task-specific work on its detections. Most of our vision code is in post-processors. This is the one for the octagon’s table, from sub_vision/post_processors/octagon.py:

TABLE_WIDTH_M = 21.5 * 0.0254 # 0.5461 m
def _width_distance_m(width_m: float, bbox, camera_info) -> float:
"""Range from an object's known width and its box width: ``width_m * fx / width_px``."""
fx = float(camera_info.k[0])
if bbox.size_x <= 0.0 or fx <= 0.0:
return float("nan")
return float(width_m * fx / bbox.size_x)
@register_post_processor("octagon_table")
class OctagonTablePostProcessor(TaskPostProcessor):
"""Range every table detection from the table's width."""
def process(self, detections, rgb_image, depth_image, camera_info, model_masks=None):
for det in detections.detections:
det.distance_m = _width_distance_m(TABLE_WIDTH_M, det.detection.bbox, camera_info)
return detections

That’s the distance formula from above. Other post-processors do more:

Task Its post-processor
gate Works out which side of the gate is which, reads the icons on it to find its role, and estimates its angle
slalom Keeps only the nearest red pipe, and checks that it really is red
path_marker Works out which way a marker on the floor points, using OpenCV
torp Works out the board’s exact position and angle (using a technique called PnP) and picks the hole to aim for

Post-processors describes each one, and Adding a new mission task shows how to add your own.

The full path from an image to the robot moving:

front_camera/image_raw
→ sub_vision: YOLO network, then the task's post-processor
→ vision/detections (class, score, bearing, distance)
→ sub_mission: a step like AlignToDetection
→ motion_setpoint
→ sub_control and the thrusters

Align, orient and sweep defines the words we use in mission XML.

  1. Record. Run with record:=true to save the cameras to a bag. Bags from pool tests are the most valuable.
  2. Label. remora samples images from a bag, proposes labels with another neural network, and lets people fix them. It is private: ask a lead for access.
  3. Train. remora trains a YOLO network and exports an .onnx file.
  4. Publish. Upload the file to a GitHub release, and update weights/manifest.toml. Model weights has the steps.