Skip to main content

Object Detection

Object detection is a computer vision technique that allows a computer to identify and locate objects within an image. For our applications, this often means identifying game pieces or field elements.

A camera picture of a practice field with yellow game pieces, each one boxed and labeled by an object detector with its confidence

For every object it finds, a detector gives back three things:

  • a class: what it is ("game piece", "robot", ...),
  • a bounding box: the smallest rectangle around it, in pixels,
  • a confidence: how sure it is, from 0 to 1.

The picture above is a real detection: a rendered practice field run through a YOLOv8 nano model (a general model trained on everyday objects, which calls our balls "sports ball"). A team model is trained on our own game piece pictures instead.

Kinds of vision tasks​

Object detection is one of several related jobs a vision model can do. They differ in how much detail they give back about where things are:

The same picture four ways: a classification model gives one label for the whole picture, a detector draws a box around each ball, semantic segmentation colors every ball pixel the same, and instance segmentation colors each ball separately

  • Image classification gives one answer for the whole picture: "this picture shows a tennis ball", with a confidence. It says nothing about where the object is or how many there are. (Here a general model trained on everyday pictures guessed "tennis ball" at 0.38.) Useful for questions like "is there a game piece in the intake?"
  • Object detection finds every object, with a class and a box for each one. This is what we use most, because the center of a box is enough to steer toward a piece.
  • Semantic segmentation labels every pixel with a class ("ball", "carpet", "robot"), but doesn't separate one object from the next: two touching balls are one patch of "ball" pixels.
  • Instance segmentation does both: an outline (a mask) of the exact pixels of each separate object. It is the most detailed and takes the most computing. It helps when a box isn't precise enough, for example to find the bottom edge of a piece on the floor or to split pieces that are piled together.

There are also pose (or keypoint) models, which find specific points on an object, like the corners of a game piece or the joints of a person, and oriented detectors, whose boxes can be rotated to fit long objects lying at an angle.

Our Jetson's detector can run either detection or instance segmentation models (the "segment" versions of YOLOv8 and RF-DETR). The pictures above come from general YOLOv8 nano models, not a team model; the semantic segmentation panel was made by merging the instance masks.

There are two common approaches that can be used for object detection: traditional computer vision techniques and deep learning.

Traditional Computer Vision Techniques​

The traditional way is to write the rules yourself, as a pipeline: a chain of simple image operations, each one feeding the next. Here is a classic pipeline for bright yellow balls, run step by step on a picture where we added a strip of yellow tape, a yellow number plate on the robot and some camera noise, to show what each step is for:

The traditional pipeline in six steps: the camera picture, its hue, the yellow pixels, the cleaned up mask, the outlines of 12 blobs, and the 10 round blobs kept as targets with the tape and plate rejected

  1. Camera picture. Each pixel is three numbers: red, green and blue (RGB).
  2. Convert to HSV (hue, saturation, value). In RGB, a ball in shadow and the same ball in light have very different numbers. In HSV, hue is the color itself (yellow, red, blue...), saturation is how strong the color is, and value is how bright it is, so "yellow" is roughly one range of hue whether the pixel is lit or shaded.
  3. Color threshold. Keep only pixels whose hue, saturation and value are all inside the "yellow" ranges (white), and drop everything else (black). The result is a black and white mask. Notice it also keeps the tape, the plate and hundreds of noise specks: they are yellow too.
  4. Clean up. An opening shrinks every white region by a few pixels and then grows it back. Specks smaller than the shrink disappear completely and don't come back; big regions return to their shape.
  5. Find contours. Trace the outline of every white region that's left. Each outline is one candidate blob: here, 12 of them.
  6. Filter. Keep blobs that look like a game piece. Here the rule is "big enough, and round": the roundness 4π⋅areaperimeter2\frac{4\pi \cdot \text{area}}{\text{perimeter}^2} is 1 for a perfect circle and gets smaller for long or jagged shapes. The tape (0.09) and the plate (0.43) fail it. For each blob kept, the center of its circle is the target, and from there it is turned into angles exactly like a detector's box (see From a box to the robot).

Every number in this pipeline (the hue range, the opening size, the minimum area, the roundness cutoff) is a setting someone tuned by hand while looking at camera pictures. This is fast and easy to understand, and it works well when the lighting and the scene match what it was tuned for. But it breaks when they don't:

The same color rule finds all 10 balls under practice lighting but only 1 when the lights are dimmer and warmer. A YOLOv8 nano model finds 8 of them in that dim picture.

In the middle picture, the same rule, tuned on the practice field picture, runs on the same scene lit dimmer and warmer, like a different venue: the balls turn orange-brown, fall outside the "yellow" range, and the rule finds just 1 of them. A neural network that was never tuned for this lighting still finds 8.

Lighting is only one way rules break: a yellow bumper or a team shirt can pass both the color and shape tests, two touching pieces can merge into one blob, and a piece half hidden behind a robot isn't round anymore. Each fix is another hand-tuned rule.

Deep Learning Techniques​

Deep learning involves training a model on a large set of labelled example pictures, called a dataset. The model uses the examples to learn the features and patterns that identify each object by itself, instead of being told them. That handles the lighting, partly hidden pieces and look-alikes that break hand-written rules, as long as the training pictures include them.

For computer vision applications, most of the deep learning models use a foundational concept called Convolutional Neural Networks (CNNs). CNNs use a mathematical operation called convolution to extract features such as edges, textures, and shapes, and stack many of these layers to learn more complex patterns.

New to neural networks?

Deep Dive: How Neural Networks Work explains neurons, training, and convolution from the ground up, with pictures of a small network learning.

Over the past few years we have used several different deep learning models for object detection, including:

Our Jetson software, Aquila Vision, currently runs two families of detectors: YOLOv8 and RF-DETR. Each can either draw boxes (detection) or also outline the exact pixels of each object (segmentation).

How YOLO finds objects​

The You Only Look Once (YOLO) family of models are real-time object detection models designed to be fast and accurate. They look at the whole picture once, in a single pass through the network, and predict every object at the same time. This is the model architecture we have selected for the 2024 and 2025 seasons, due to its speed and accuracy.

Here is what happens to each camera picture in our YOLOv8 detector:

  1. Straighten the picture. The camera's calibration is used to undo the lens distortion first, so straight lines are straight and angles to objects come out right.
  2. Resize to 640 × 640. The network always takes a 640 × 640 picture. The camera picture is shrunk to fit and the leftover space is filled with black bars (letterboxing), so objects aren't stretched.
  3. Run the network. The network divides the picture into grids at three sizes: 80 × 80 cells (for small, far-away objects), 40 × 40 and 20 × 20 (for big, close ones). Every cell predicts a box and a score for each class. That is 6400+1600+400=84006400 + 1600 + 400 = 8400 candidate boxes for every picture.
  4. Drop unlikely boxes. Each candidate keeps its most likely class. Candidates whose score is below the confidence threshold are thrown away.
  5. Remove duplicates (non-maximum suppression). Neighboring cells often find the same object, so one ball can have a dozen overlapping boxes. Non-maximum suppression (NMS) keeps the most confident box and removes every other box of the same class that overlaps it too much, then repeats with the next most confident box that's left.

Left: before NMS, every ball has 7 to 11 nearly identical candidate boxes stacked on top of each other. Right: after NMS, one box per ball.

(For this picture the confidence threshold was set very low, 0.05, to show all the candidates.)

"Overlaps too much" is measured with Intersection over Union (IoU): the area where two boxes overlap, divided by the area they cover together. Two identical boxes have an IoU of 1, two boxes that don't touch have 0:

IoU is the overlap area divided by the total area covered by both boxes

Both thresholds are set per camera on the Pipelines tab of the camera web page:

  • Confidence threshold: raise it to get fewer false detections, lower it to find more of the hard pieces (far away, half hidden).
  • IoU threshold: the overlap above which two boxes count as the same object. Too high and one piece gets two boxes; too low and two pieces touching each other merge into one.

RF-DETR​

RF-DETR is a newer kind of detector, built on a transformer (the same building block as the language models behind chatbots) instead of only convolutions. Instead of a grid of cells, it has a fixed number of queries, and each query learns to "claim" one object in the picture. Because each object is claimed by only one query, RF-DETR doesn't need non-maximum suppression. Transformers look at the whole picture at once, which helps with crowded scenes and objects partly hidden behind each other, at the cost of more computing per picture.

From a box to the robot​

The robot doesn't need pixels, it needs directions. For each detection, the center of its box is turned into two angles from the camera, using the same pinhole camera model as for AprilTags:

θx=arctan⁡ ⁣(u−cxfx)θy=arctan⁡ ⁣(v−cyfy)\theta_x = \arctan\!\left(\frac{u - c_x}{f_x}\right) \qquad \theta_y = \arctan\!\left(\frac{v - c_y}{f_y}\right)

where (u,v)(u, v) is the center of the box in pixels. Those angles, in radians, are sent to the roboRIO as piece detection packets along with the class and the time the picture was taken. With the camera's height and tilt, the vertical angle also tells the robot how far away a piece on the floor is.

Model Training​

For our deep learning models, we use a two step process to train the models. First we create a dataset in RoboFlow, which is a tool that allows us to create a labelled dataset for training. We have a RoboFlow organization set up for our team. Once the dataset is created, we then use that dataset to train the model using the Ultralytics YOLO training framework. This framework provides a simple interface for training YOLO models from our RoboFlow dataset.

A few things make the biggest difference to how well a model works on the field:

  • Pictures from our own cameras, at the resolution and exposure we run in matches, on real carpet under event lighting.
  • Hard examples: pieces far away, half hidden, touching each other, blurred by motion, and pictures with look-alikes (yellow bumpers, shirts, signs) but no pieces at all.
  • A held-back validation set to measure the model on pictures it never trained on.

To compare models we look at:

  • Precision: of the boxes the model drew, how many were really game pieces?
  • Recall: of the game pieces really in the pictures, how many did the model find?
  • mAP (mean average precision): one number that combines both over all the confidence thresholds. Higher is better.

Model Deployment​

Once the model is trained, we then deploy the model to our Nvidia Jetson vision computer. Since the Jetson is not as powerful as a desktop computer, we often need to optimize the model for inference on the Jetson. To do this, we use a tool called TensorRT, which is a high-performance deep learning optimizer and runtime library.

First the model must be exported to the ONNX format, which is a standard format for trained deep learning models. ONNX encodes both the model architecture and the learned weights. Once encoded, the model is then fed to TensorRT for optimization. TensorRT Converts the model to a highly optimized runtime that is specifically designed for inference on Nvidia hardware like the Jetson.

On our Jetson, you upload the .onnx file on the Models tab of the camera web page. The first time the detector loads it, it builds the TensorRT engine and saves it next to the model as a .trt file, so later starts are fast. (The detector also accepts a .trt engine directly, but an engine only works on the same Jetson model and TensorRT version it was built with, so the .onnx file is the one to keep.) Building the engine can take a few minutes, so do it in the pits, not right before a match.

Try It: Object Detection Lab​

Tune a real color filter on camera pictures and see where it breaks, then adjust the confidence, NMS and model size of a simulated YOLO detector. A robot then plays 15 seconds of auto on your settings, so you can see what a wrong box costs.

Open the Object Detection Lab in a new tab ↗