YOLOv8 Object Detection Example
- YOLOv8 Object Detection Example
- Package and Code Structure
- 📦 Box Recognition
- 🎯 Detection Call for a Trained YOLOv8 Model
- 📁 Model Path and Description
- 📡 Box Recognition ROS Topic Subscriptions
- 💻 yolo_box_object_detection Package Code Description (Head NUC)
- 🔧 Detected Pose Quaternion Description
- GPU Acceleration Instructions
Package and Code Structure
-
Package:
yolo_box_object_detection, the YOLO ROS package for box recognition- Package path:
~/kuavo_ros_application/src/ros_vision/detection_industrial_yolo/yolo_box_object_detection
- Package path:
-
Example code
- Path:
/home/lab/kuavo-ros-opensource/src/demo/examples_code/yolo_detect/yolo_detect_info.py yolo_detect_info.py: obtains the pose of a single detection result from/object_yolo_box_tf2_torso_resultrelative to the robot base frame
- Path:
📦 Box Recognition
If you need the position of the delivery box relative to the robot, that is, to subscribe to the
object_yolo_box_tf2_torso_resulttopic, you must first make the robot stand up. The camera must be turned on before use
cd ~/kuavo_ros_application
source /opt/ros/noetic/setup.bash
source ~/kuavo_ros_application/devel/setup.bash
# 旧版4代, 4Pro
roslaunch dynamic_biped load_robot_head.launch
# 标准版, 进阶版, 展厅版, 展厅算力版
roslaunch dynamic_biped load_robot_head.launch use_orbbec:=true
# MaxA版, MaxB版
roslaunch dynamic_biped load_robot_head.launch use_orbbec:=true enable_wrist_camera:=true🎯 Detection Call for a Trained YOLOv8 Model
- 🔍 yolo_box_object_detection -- YOLO ROS package for box recognition
📁 Model Path and Description
- 📂 Path:
~/kuavo_ros_application/src/ros_vision/detection_industrial_yolo/yolo_box_object_detection
- 📄 Model format:
.pt - 🗂️ Model path:
~/kuavo_ros_application/src/ros_vision/detection_industrial_yolo/yolo_box_object_detection/scripts/models/
Start the detection program
cd ~/kuavo_ros_application
source /opt/ros/noetic/setup.bash
source ~/kuavo_ros_application/devel/setup.bash
roslaunch yolo_box_object_detection yolo_segment_detect.launch - If the host computer is an AGX or NX, the following error may occur:

Enter the following command in the terminal:
echo 'export LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libgomp.so.1:$LD_PRELOAD' >> ~/.bashrcOpen a new terminal and start the detection program again.
If you want to visualize the detection results, open rqt_image_view or rviz (a monitor or remote desktop is required to view it), and subscribe to the object_yolo_box_segment_image topic to see the results.

📡 Box Recognition ROS Topic Subscriptions
/object_yolo_box_segment_result # 基于相机坐标系下的箱子中心点的3D位置
/object_yolo_box_segment_image # 识别箱子的绘制结果
/object_yolo_box_tf2_torso_result # 基于机器人基坐标系下的箱子中心点的3D位置💻 yolo_box_object_detection Package Code Description (Head NUC)
yolo_box_segment_ros.py:- Calls the model for detection and obtains the 3D coordinates of the center point of the detection box
- Publishes to
/object_yolo_box_segment_result - Filters out results with confidence below 0.6
yolo_box_transform_torso.py:- Subscribes to
/object_yolo_box_segment_result - Transforms the coordinates into the robot base frame
- Publishes the transformed result to
/object_yolo_box_tf2_torso_result
- Subscribes to
🔧 Detected Pose Quaternion Description
- 📄 See lines 71-74 of
yolo_box_transform_torso.py. Since detection only obtains the spatial position of the detected target's center point and provides no orientation information, the quaternion is a fixed value rather than an actual one.
GPU Acceleration Instructions
⚠️⚠️⚠️ This section applies only to Advanced Edition and Max Edition users ⚠️⚠️⚠️ This section applies only to JetPack 5.1.4 with CUDA 11.4
- How to check
- Run
sudo -H pip install -U jetson-statsin the terminal to install the tool - Run
jetson_releasein the terminal to view the local environment - If the part in the red box in the figure below matches the terminal output, you can follow this document to use the GPU for acceleration

- Run
Installing PyTorch, Torchvision and Ultralytics
- New images come with the above dependencies preinstalled. Only devices running an older image that require GPU-accelerated detection need to additionally install PyTorch, Torchvision, and Ultralytics.
⚠️ The installation order is very important:
torch -> torchvision -> ultralytics
Reasons:
- torch is the core foundation
- torchvision depends on torch
- ultralytics depends on torch and can call torchvision transforms
- CUDA + cuDNN + JetPack are already compiled inside Jetson, so torch must match CUDA and JetPack; otherwise the automatic installation may fail
1. Check Whether torch Is Already Installed
- Open a new terminal
pip3 show torch- not found -> install torch
- version displayed -> determine from the following steps whether the torch is usable; if not, uninstall and reinstall it as described below
- Determine the version of the Jetson NX/AGX host:
dpkg-query --show nvidia-l4t-coreSee the NVIDIA developer website. In this example it shows 35.6.0, which corresponds to JetPack 5.1.4
- Confirm the python3 version:
python3 --versionIn this example the Python version is 3.8.10
- Remove the old torch to prevent conflicts:
sudo pip3 uninstall torch torchvision torchaudio -y - Refer to the official NVIDIA forum to find a suitable torch version (JetPack 5.1.4 corresponds to PyTorch 2.0.0)
2. Install PyTorch 2.0.0 (JetPack 5.1.4 / CUDA 11.4)
- Download the whl file (it can be transferred to the Jetson NX/AGX host via SSH):
wget https://nvidia.box.com/shared/static/i8pukc49h3lhak4kkn67tg9j4goqm0m7.whl \ -O torch-2.0.0+nv23.05-cp38-cp38-linux_aarch64.whl - Install the dependencies:
sudo apt-get install python3-pip libopenblas-base libopenmpi-dev libomp-dev pip3 install 'Cython<3' pip3 install numpy torch-2.0.0+nv23.05-cp38-cp38-linux_aarch64.whl - Verify the installation:
python3 -c "import torch; print(torch.__version__)"Example output:
2.0.0+nv23.05, installation succeeded
3. Install Torchvision (corresponding to 0.15.1)
-
Refer to the documentation link and install the corresponding version of torchvision; here, as shown in the figure, it corresponds to 0.15.1

-
Install the dependencies:
sudo apt-get install libjpeg-dev zlib1g-dev libpython3-dev libopenblas-dev \ libavcodec-dev libavformat-dev libswscale-dev -
Clone the source code and install:
git clone --branch v0.15.1 https://github.com/pytorch/vision torchvision cd torchvision git fetch --tags git tag | grep v0.15.1 git checkout tags/v0.15.1 export BUILD_VERSION=0.15.1 pip install 'pillow<7' python3 setup.py install --user -
Verify the installation:
pip install "urllib3<2" python3 -c "import torchvision; print(torchvision.__version__)"Example output:
0.15.1, installation succeeded
4. Install Ultralytics
- Install:
pip3 install ultralytics==8.0.196 -i https://pypi.tuna.tsinghua.edu.cn/simple python3 -c "import ultralytics; print(ultralytics.__version__)"If the version information is printed, the installation succeeded
Modifying the Code and Running It
Notes
- For YOLOv8, the program calls
ultralytics, andultralyticsautomatically depends on thetorchin the current environment. - The image ships with the GPU version of
torch, so the program automatically uses GPU acceleration. - In a GPU environment, if you want to test how it runs on CPU, you need to manually specify the device as CPU. The methods for explicitly specifying CPU/GPU detection are provided below.
Code Location
- Edit the file
~/kuavo_ros_application/src/ros_vision/detection_industrial_yolo/yolo_box_object_detection/scripts/yolo_box_segment_ros.pyInsidedef main(), find the following code block:
# 使用 Ultralytics 加载模型
model_path = os.path.join(rospkg.RosPack().get_path('yolo_box_object_detection'), 'scripts/models/best.pt')
model = YOLO(model_path)Explicitly Set the Running Device to CPU
-
Modify the code:
# 使用 Ultralytics 加载模型 model_path = os.path.join( rospkg.RosPack().get_path('yolo_box_object_detection'), 'scripts/models/best.pt' ) model = YOLO(model_path).to('cpu') -
The running behavior is shown in the figure:

- GPU utilization barely increases
- The frame rate is in the single digits
- ✅ Confirms that CPU mode works properly
Explicitly Set the Running Device to GPU
-
Modify the code:
# 使用 Ultralytics 加载模型 model_path = os.path.join( rospkg.RosPack().get_path('yolo_box_object_detection'), 'scripts/models/best.pt' ) model = YOLO(model_path).to('cuda') -
Running behavior:

- GPU utilization increases noticeably
- The frame rate can reach 30+ FPS
- ✅ Confirms that GPU mode accelerates properly