| name | yolo-integration |
| description | OpenCV4 YOLO 集成技能 - Ultralytics YOLOv5/v8/v11、目标检测、分割、分类、姿态估计 |
| user-invocable | true |
| argument-hint | yolo OR ultralytics OR 目标检测 OR 实例分割 OR 姿态估计 OR 目标追踪 |
OpenCV4 YOLO Integration Skill
Ultralytics YOLO 集成完整指南
何时使用
当需要以下帮助时使用此技能:
- YOLOv5/v8/v10/v11 目标检测
- 实例分割(Segmentation)
- 姿态估计(Pose)
- 分类模型(Classification)
- 目标追踪(Tracking)
- ROS2 集成部署
快速参考
Ultralytics 安装
pip install ultralytics
Python YOLO 推理
from ultralytics import YOLO
model = YOLO('yolov8n.pt')
results = model.predict(source='image.jpg', conf=0.5, iou=0.4)
annotated = results[0].plot()
for result in results:
boxes = result.boxes
masks = result.masks
keypoints = result.keypoints
probs = result.probs
YOLO 目标检测
results = model.predict(source='bus.jpg', conf=0.5, show=True)
results = model.predict(source='video.mp4', conf=0.5, save=True)
results = model.predict(source=0, conf=0.5, show=True)
results = model.predict(source='images/*.jpg', conf=0.5)
for r in results:
print(r.boxes.xyxy)
print(r.boxes.xywh)
print(r.boxes.xyxyn)
print(r.boxes.conf)
print(r.boxes.cls)
YOLO 分割(Segmentation)
model = YOLO('yolov8n-seg.pt')
results = model.predict(source='image.jpg', conf=0.5)
for r in results:
masks = r.masks
if masks is not None:
for mask in masks:
mask_data = mask.data.cpu().numpy()
mask_norm = mask.data.cpu().numpy()
YOLO 姿态估计(Pose)
model = YOLO('yolov8n-pose.pt')
results = model.predict(source='person.jpg', conf=0.5)
for r in results:
kpts = r.keypoints
if kpts is not None:
all_kpts = kpts.data.cpu().numpy()
visible = kpts.conf
YOLO 分类(Classification)
model = YOLO('yolov8n-cls.pt')
results = model.predict(source='cat.jpg')
for r in results:
top5_probs, top5_idxs = torch.topk(torch.tensor(r.probs.data), 5)
print(f"Top 5: {top5_idxs.numpy()}, {top5_probs.numpy()}")
目标追踪(Tracking)
from ultralytics import YOLO
from ultralytics.utils.trackers import ByteTrack
model = YOLO('yolov8n.pt')
model.predict(source='video.mp4', conf=0.5, persist=True)
results = model.predict(source='video.mp4', tracker='bytetrack.yaml')
for r in results:
if r.boxes.id is not None:
track_ids = r.boxes.id.cpu().numpy()
boxes = r.boxes.xyxy.cpu().numpy()
classes = r.boxes.cls.cpu().numpy()
OpenCV DNN 部署 YOLO
ONNX 导出与加载
model = YOLO('yolov8n.pt')
model.export(format='onnx', dynamic=True, opset=12)
import cv2
import numpy as np
net = cv2.dnn.readNetFromONNX('yolov8n.onnx')
img = cv2.imread('image.jpg')
blob = cv2.dnn.blobFromImage(img, 1/255.0, (640, 640),
swapRB=True, crop=False)
net.setInput(blob)
output = net.forward()
def postprocess_yolov8(output, img_shape, conf_thresh=0.5, iou_thresh=0.4):
predictions = output[0].T
boxes = []
for pred in predictions:
cx, cy, w, h = pred[:4]
class_scores = pred[4:]
class_id = np.argmax(class_scores)
confidence = class_scores[class_id]
if confidence > conf_thresh:
x = int((cx - w/2) * img_shape[1])
y = int((cy - h/2) * img_shape[0])
w = int(w * img_shape[1])
h = int(h * img_shape[0])
boxes.append([x, y, w, h, confidence, class_id])
boxes
ROS2 集成
import cv2
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
from ultralytics import YOLO
class YOLONode(Node):
def __init__(self):
super().__init__('yolo_node')
self.bridge = CvBridge()
self.model = YOLO('yolov8n.pt')
self.subscription = self.create_subscription(
Image, '/camera/image_raw', self.image_callback, 10)
self.publisher = self.create_publisher(Image, '/yolo/detections', 10)
def image_callback(self, msg):
img = self.bridge.imgmsg_to_cv2(msg, 'bgr8')
results = self.model.predict(img, conf=0.5, verbose=False)
annotated = results[0].plot()
out_msg = self.bridge.cv2_to_imgmsg(annotated, 'bgr8')
self.publisher.publish(out_msg)
def main(args=None):
rclpy.init(args=args)
node = YOLONode()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
最佳实践
-
模型选择:
- 实时应用:
YOLOv8n 或 YOLOv11n
- 精度优先:
YOLOv8x 或 YOLOv11x
- 分割任务:
YOLOv8n-seg / YOLOv11n-seg
-
推理优化:
- 使用 TensorRT 加速(需导出)
- 半精度(FP16)加速
- 批量推理提高吞吐
-
参数调优:
conf:降低可提高召回率
iou:降低可减少重叠检测
max_det:限制最大检测数
-
ROS2 部署:
- 使用
image_transport 减少传输开销
- 考虑使用组件(Component)方式部署
- 预处理在 GPU 做
相关技能