YOLO Box-Moving Case
Feature Overview
This case implements the ability for the robot to visually recognize boxes and perform grasping operations. The main features include:
- Using a YOLO model to detect boxes in the image in real time
- Calculating the offset between the box and the image center
- Controlling robot movement (forward/backward/turning) based on the offset
- Performing the grasp action when the robot moves to a suitable position
- Supporting keyboard control (spacebar starts/stops robot motion)
Directory Structure of the roban2_move_boxes Package
roban2_move_boxes/
├── CMakeLists.txt # Build configuration file
├── package.xml # ROS package metadata file
├── model # Model files (YOLO)
│ └── best.pt # Trained box model weights
│ └── yolov8n.pt # YOLO pretrained model weights
├── launch # Launch file directory
│ └── get_boxes_image.launch # Launch file for starting the box image capture feature
│ └── roban_move_boxes.launch # Launch file for starting the move_boxes feature
│ └── yolo_auto_annotation.launch # Launch file for starting the auto-annotation feature
├── src # Source code directory
│ └── scripts # Executable script directory
│ └── generate_yolo_dataset.py# Converts annotated images into a YOLO dataset
│ └── get_boxes_image.py # Captures images via the camera and saves them to a specified folder
│ └── move_boxes_demo.py # Main program: robot autonomous movement and grasping logic
│ └── yolo_auto_annotation.py # Automatically annotates similar boxes using the trained box model
│ └── yolo_train.py # Trains on images using the YOLO training model
└── README.md # Package documentation1. Starting the Main Program:
cd ~/kuavo_ros_application
catkin build roban2_move_boxes kuavo_msgs
source devel/setup.bash
roslaunch roban_move_boxes roban_move_boxes.launchNote: When logged in to the robot via SSH, the image window cannot be displayed
- Configure the master/slave setup, then subscribe to the image topic in rviz or rqt
- Connect to the robot via VNC
2. Environment Setup
pip install labelimg
pip install ultralytics3. Model Training
1. Data Collection
- Launch the script:
roslaunch roban2_move_boxes get_boxes_image.launch - Usage: Press the spacebar to save the image in the image window to the
~/Desktop/yolo/imagesdirectory
2. Data Annotation
-
Run
labelimgin the terminal
-
Using labelimg
- Click
Open Dirand select the directory where the images were saved in the previous step - Click
Change Save Dirand select the directory for storing annotation files - Click
YOLOto change the annotation format toYOLO - Click
Create RectBoxand draw a bounding box around the box in the image, then assign a class label - Click
Saveafter annotating - Click
Next Imageto switch to the next image - Repeat steps 4-6 until all images have been annotated
- Click
-
Dataset generation
python3 generate_yolo_dataset.py --img_dir ~/Desktop/yolo/images --label_dir ~/Desktop/yolo/labels --output_dir ~/Desktop/yolo/dataset --split_ratio 0.8 --shuffleSet the directory containing the images
image_directorySet the directory containing the annotation fileslabel_directory
Set the output directoryoutput_directory
Set the training set ratiosplit_ratio
3. Model Training
python3 yolo_train.py --model_path yolov8n.pt --dataset_path dataset.yaml --epochs 200model_path: Path to the pretrained model file
dataset_path: Path to the YOLO dataset directory
epochs: Sets the number of training epochs
Automatic Data Annotation (Optional)
Run the script
python3 yolo_auto_annotation.py --model_name best.pt --base_dir my_dataset --yolo_conf 0.6-
Main features: Uses the new model obtained from training on part of the data in
Model Trainingto automatically annotate new data- Captures the video stream from the camera, performs real-time detection, and displays the results
- Saves detection results as images and annotation files via key press, for dataset construction
- Generates a classes.txt file containing all class names of the model
-
Parameter description: Load model:
--model_name best.ptspecifies the trained YOLO model file Specify dataset:--base_dir my_datasetsets the path to the directory containing the images to be annotated Set confidence:--yolo_conf 0.6keeps only detection results with confidence ≥ 0.6 (filters out low-quality predictions) -
Usage:
- After the script starts, check the image window; once the detection results meet expectations, click the save button to save the image and annotation information.
- After automatic annotation is complete, open
labelimgto review the dataset, check for missed or incorrect annotations, correct them, and save. - Use the
generate_yolo_dataset.pyscript to generate the YOLO dataset. - Train on the generated data using
yolo_train.py.
move_boxes_demo.py Documentation
The following are the ROS topics and services involved in the roban2_move_boxes node:
📡 ROS Topics
| Topic Name | Message Type | Publisher/Subscriber | Description |
|---|---|---|---|
/joy | sensor_msgs/Joy | Publisher | Publishes the Joy controller state, used to control robot gait switching. |
/key_pressed | std_msgs/String | Publisher / Subscriber | Publishes and subscribes to keyboard key events (e.g., spacebar triggers grasping). |
/camera/color/image_raw | sensor_msgs/Image | Subscriber | Subscribes to raw RGB image data for object detection and visual navigation. |
/camera/color/image_processed | sensor_msgs/Image | Publisher | Publishes processed images (with detection boxes, offset information, etc. drawn). |
/robot_head_motion_data | kuavo_msgs/robotHeadMotionData | Publisher | Controls the robot head rotation angle. |
/cmd_pose | geometry_msgs/Twist | Publisher | Controls the robot's crouching and standing actions (via z-axis linear velocity). |
/cmd_vel | geometry_msgs/Twist | Publisher | Controls the robot chassis motion (linear and angular velocity). |
⚙️ ROS Services
| Service Name | Service Type | Caller | Description |
|---|---|---|---|
/roban2_arm_action | kuavo_msgs/Roban2ArmAction | Client (self.grab_boxes) | Calls the robotic arm to perform the grasp action and returns the grasp result status. |
✅ Summary
🧠 Functional Logic
- Manual control:
- Spacebar toggles the movement mode (start/stop);
- Ctrl+C exits the program.
- Image recognition: Subscribes to
/camera/color/image_raw, uses YOLO to detect boxes, and calculates their position in the image. - Autonomous movement:
- Adjusts the robot's direction and speed based on the box offset;
- Slows down and stops as the box gets closer, then prepares to grasp.
- Grasping process:
- Stops robot motion;
- Crouches;
- Calls
/roban2_arm_actionto grasp the box; - Stands up;
- Returns
📌 Notes
-
Dependency checks:
- Ensure the
/roban2_arm_actionservice is available; - Ensure the camera node is started and
/camera/color/image_rawis publishing images properly; to start the camera:roslaunch realsense2_camera rs_camera.launch - Ensure [model/best.pt] exists and the path is correct.
- Run
roslaunch roban2_move_boxes roban2_move_boxes.launchin the upper-computer repository to start the box-moving program.
- Ensure the
-
Debugging tools:
- Use
rqt_image_viewto view/camera/color/image_processed; - Use
rostopic echo /key_pressedto monitor key events; - Use
rvizto view the motion trajectory corresponding to/cmd_vel.
- Use