摘要:YOLO 模型的类别数量取决于训练时使用的模型配置和数据集。例如,yolov8m.pt 是基于 COCO 数据集训练的,它包含 80 个类别。以下是 COCO 数据集中所有类别的完整列表:
YOLO 模型的类别数量取决于训练时使用的模型配置和数据集。例如,yolov8m.pt 是基于 COCO 数据集训练的,它包含 80 个类别。以下是 COCO 数据集中所有类别的完整列表:
class_names = { 0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorbike', 4: 'aeroplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'sofa', 58: 'pottedplant', 59: 'bed', 60: 'diningtable', 61: 'toilet', 62: 'tvmonitor', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush',}如果已经安装了yolov8或11的环境可以用下代码检测视频或摄像头中的人或物体
import cv2import timefrom ultralytics import YOLO# 手动设置设备 (cuda 或 cpu)# device = 'cuda' # 修改为 'cpu' 使用 CPUdevice = 'cpu' # 初始化YOLO模型model = YOLO('yolov8n.pt')# 将模型移动到指定设备model.to(device)# 创建类别映射字典class_names = { 0: 'person', 2: 'car', 3: 'motorbike', 5: 'bus', 7: 'truck', # 这里可以继续添加其他类别的映射}# 打开视频文件或摄像头cap = cv2.VideoCapture('nfs.mp4') # 替换为 0 使用摄像头if not cap.isOpened: print("Error: Could not open video.") exit# 获取视频帧率和尺寸fps = cap.get(cv2.CAP_PROP_FPS)width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))# 初始化视频写入器fourcc = cv2.VideoWriter_fourcc(*'XVID')out = cv2.VideoWriter('output.avi', fourcc, fps, (width, height))while True: ret, frame = cap.read if not ret: break start_time = time.time # YOLO进行目标检测 results = model(frame) # 使用模型进行推理 detections = results[0].boxes.xyxy.cpu.numpy # 获取检测结果并转换为numpy数组 confidences = results[0].boxes.conf.cpu.numpy # 获取置信度 class_ids = results[0].boxes.cls.cpu.numpy # 获取类别ID # 绘制检测框和类别 for det, conf, cls in zip(detections, confidences, class_ids): x1, y1, x2, y2 = map(int, det) label = class_names.get(int(cls), f'Class {int(cls)}') # 获取类别名称,默认显示为 Class cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(frame, f'{label} {conf:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # 计算FPS并显示在窗口上 end_time = time.time fps_display = 1 / (end_time - start_time) cv2.putText(frame, f'FPS: {fps_display:.2f}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) # 显示结果 cv2.imshow('Video', frame) # 写入视频文件 out.write(frame) if cv2.waitKey(1) & 0xFF == ord('q'): breakcap.releaseout.releasecv2.destroyAllWindows来源:新手村养牛
免责声明:本站系转载,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与本站联系,我们将在第一时间删除内容!