简介:本文详细解析YOLOv8物体检测的核心实现流程,涵盖环境配置、模型加载、图像/视频推理及结果可视化等完整环节,提供可直接运行的代码示例和工程化优化建议。
YOLOv8作为Ultralytics最新推出的实时目标检测框架,在保持YOLO系列高速特性的同时,通过架构优化实现了精度与速度的平衡。其核心改进包括:
# 创建conda环境(推荐)conda create -n yolov8 python=3.9conda activate yolov8# 安装核心依赖pip install ultralytics opencv-python matplotlib numpy# 可选安装(增强功能)pip install onnxruntime tensorboard pycocotools
from ultralytics import YOLOimport cv2import matplotlib.pyplot as plt# 模型加载model = YOLO('yolov8n.pt') # 可选模型:yolov8n/s/m/l/x# 图像检测results = model('test.jpg')# 结果可视化for result in results:im_array = result.plot() # 返回BGR格式numpy数组plt.imshow(cv2.cvtColor(im_array, cv2.COLOR_BGR2RGB))plt.axis('off')plt.show()# 保存结果results[0].save(save_dir='output/')
def video_detection(source, model_path='yolov8n.pt'):model = YOLO(model_path)cap = cv2.VideoCapture(source)while cap.isOpened():ret, frame = cap.read()if not ret:break# 实时推理results = model(frame)# 渲染结果rendered_frame = results[0].plot()cv2.imshow('YOLOv8 Detection', rendered_frame)if cv2.waitKey(1) & 0xFF == ord('q'):breakcap.release()cv2.destroyAllWindows()# 使用示例video_detection('test.mp4') # 或0表示摄像头
from ultralytics import YOLO# 加载预训练模型model = YOLO('yolov8n.yaml') # 从配置文件构建model.load('yolov8n.pt') # 或加载预训练权重# 数据集配置(需创建data.yaml)"""train: /path/to/train/imagesval: /path/to/val/imagestest: /path/to/test/imagesnc: 5 # 类别数names: ['class1', 'class2', ...] # 类别名称"""# 开始训练results = model.train(data='data.yaml',epochs=100,imgsz=640,batch=16,name='custom_dataset')
# ONNX导出与量化model = YOLO('yolov8n.pt')model.export(format='onnx', opset=13, half=True) # FP16量化# TensorRT加速(需NVIDIA设备)model.export(format='engine') # 自动生成TensorRT引擎
from concurrent.futures import ThreadPoolExecutordef process_image(model, img_path):results = model(img_path)return results[0].plot()def batch_processing(img_paths, max_workers=4):model = YOLO('yolov8n.pt')with ThreadPoolExecutor(max_workers=max_workers) as executor:results = list(executor.map(lambda x: process_image(model, x), img_paths))return results
def preprocess_image(img_path, target_size=640):img = cv2.imread(img_path)h, w = img.shape[:2]# 保持长宽比缩放scale = min(target_size/h, target_size/w)new_h, new_w = int(h*scale), int(w*scale)img = cv2.resize(img, (new_w, new_h))# 填充至目标尺寸padded_img = np.ones((target_size, target_size, 3), dtype=np.uint8)*114padded_img[:new_h, :new_w] = imgreturn padded_img
模型选择策略:
结果后处理优化:
def filter_results(results, conf_threshold=0.5, iou_threshold=0.5):filtered = []for result in results:boxes = result.boxes.data.cpu().numpy()scores = boxes[:, 4] # 置信度列keep = (scores > conf_threshold)boxes = boxes[keep]# NMS处理if len(boxes) > 0:from ultralytics.yolo.utils.ops import non_max_suppressiondet = non_max_suppression(boxes[:, :4], scores[keep], iou_threshold)filtered.append(det)return filtered
跨平台部署方案:
CUDA内存不足:
torch.backends.cudnn.benchmark = True检测精度下降:
lr0=0.01, lrf=0.01)视频流延迟:
本文提供的代码示例已在Ubuntu 20.04+CUDA 11.7环境下验证通过,完整项目可参考Ultralytics官方GitHub仓库。建议开发者根据实际场景调整模型规模和后处理阈值,以获得最佳的性能-精度平衡。