Skip to main content

๐ŸŽฏ Advanced Usage โ€” Custom Objects

This section explains how to change the robot's object detection targets by creating custom datasets, training your own YOLO model, and optimizing detection performance for your LIMO system.


๐Ÿ›  Changing Object Detection Classesโ€‹

Your YOLO detector (object_detector.py) currently loads a custom model:

self.model = YOLO("/home/agilex/krish_ws/runs/detect/mycobot_final2/weights/best.pt")

To change classes:

  1. Train a new YOLO model with your desired objects.
  2. Replace the .pt file path in the detector node.
  3. Ensure self.model.names contains the correct class labels.
  4. Restart the system to load the new model.

๐Ÿ’ก Note: If you only want to filter detections to certain classes, you can check cls_name inside the YOLO callback and ignore others.


๐Ÿ“ธ Custom Dataset Creationโ€‹

1. Collect Imagesโ€‹

Use the LIMO's camera:

ros2 run rqt_image_view rqt_image_view

Take screenshots or record video, then extract frames.

2. Annotateโ€‹

Use LabelImg or Label Studio:

labelImg /path/to/images classes.txt

Export format: YOLO TXT format (one file per image).

3. Organize Datasetโ€‹

dataset/
โ”œโ”€โ”€ images/
โ”‚ โ”œโ”€โ”€ train/
โ”‚ โ”œโ”€โ”€ val/
โ”œโ”€โ”€ labels/
โ”‚ โ”œโ”€โ”€ train/
โ”‚ โ”œโ”€โ”€ val/
โ””โ”€โ”€ data.yaml

Example data.yaml:

train: /absolute/path/to/dataset/images/train
val: /absolute/path/to/dataset/images/val
nc: 3
names: ["objectA", "objectB", "objectC"]

๐Ÿง  Model Training Proceduresโ€‹

from ultralytics import YOLO

model = YOLO("yolov8n.pt") # or yolov8s.pt for more accuracy
model.train(
data="/path/to/data.yaml",
epochs=50,
imgsz=640,
batch=16,
device=0 # GPU ID
)

After training, your weights will be at:

runs/detect/trainX/weights/best.pt

Update the object_detector.py model path to this file.


โšก Detection Optimizationโ€‹

Confidence Threshold (conf)โ€‹

Increase to reduce false positives.

results = self.model.predict(source=color_img, conf=0.6, verbose=False)

Image Sizeโ€‹

Larger sizes improve accuracy, but increase inference time.

Model Variantโ€‹

  • yolov8n.pt โ†’ fastest, least accurate.
  • yolov8s.pt โ†’ balanced.
  • yolov8m.pt โ†’ most accurate, slower.

GPU Accelerationโ€‹

export CUDA_VISIBLE_DEVICES=0

Class Filteringโ€‹

if cls_name not in ["target_class"]:
return

๐Ÿงช Testing Your Modelโ€‹

After swapping the model:

ros2 run object_detector yolo_detector

Look for:

โœ… YOLO detector node initialized with synchronized inputs.
[๐Ÿ“ DETECTED] target_class 2D(320,240) 3D(0.42, 0.15, 0.80)

Verify:

  • Bounding boxes appear correctly in /yolo/annotated.
  • /target_pose publishes when your target is detected.
Pro Tip

If detection is unstable, collect more diverse training images with different lighting, angles, and distances.


Next: ๐Ÿ“ Customizing Waypoints