diff --git a/src/lane_visualize/yolo_det.py b/src/lane_visualize/yolo_det.py index d37aa118aa..1d9a794176 100644 --- a/src/lane_visualize/yolo_det.py +++ b/src/lane_visualize/yolo_det.py @@ -2,59 +2,33 @@ import cv2 import numpy as np - class ObjectDetector: def __init__(self, model_name='yolov8n.pt'): - """ - 初始化 YOLO 模型 - :param model_name: 模型名称,'yolov8n.pt' 是最快最小的模型,适合CPU - """ - print(f"正在加载 YOLO 模型: {model_name} (首次运行会自动下载)...") self.model = YOLO(model_name) - - # 定义我们关心的类别ID (COCO数据集) - # 2: car, 3: motorcycle, 5: bus, 7: truck + # 目标: 2=Car, 3=Motor, 5=Bus, 7=Truck self.target_classes = [2, 3, 5, 7] def detect(self, frame): """ - 输入一帧图像,返回画好框的图像 + 返回: (original_frame, detections_list) """ - # 运行推理,stream=True 节省内存,verbose=False 关闭打印日志 results = self.model(frame, stream=True, verbose=False) - - # 在原图的副本上绘图 - annotated_frame = frame.copy() + detections = [] for result in results: boxes = result.boxes for box in boxes: - # 获取类别ID cls_id = int(box.cls[0]) - - # 只处理车辆相关的类别 if cls_id in self.target_classes: - # 获取坐标 (x1, y1, x2, y2) - x1, y1, x2, y2 = box.xyxy[0] - x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) - - # 获取置信度 + x1, y1, x2, y2 = map(int, box.xyxy[0]) conf = float(box.conf[0]) - if conf < 0.3: continue # 过滤掉置信度低的 - - # 获取类别名称 - cls_name = self.model.names[cls_id] - - # 绘制矩形框 (红色) - cv2.rectangle(annotated_frame, (x1, y1), (x2, y2), (0, 0, 255), 2) - - # 绘制标签背景 - label = f"{cls_name} {conf:.2f}" - (w, h), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1) - cv2.rectangle(annotated_frame, (x1, y1 - 20), (x1 + w, y1), (0, 0, 255), -1) - - # 绘制文字 - cv2.putText(annotated_frame, label, (x1, y1 - 5), - cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) - - return annotated_frame \ No newline at end of file + if conf < 0.4: continue + + detections.append({ + 'box': [x1, y1, x2, y2], + 'class': self.model.names[cls_id], + 'conf': conf, + 'width': x2 - x1, + 'center': ((x1+x2)//2, (y1+y2)//2) + }) + return frame, detections \ No newline at end of file