gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
使用YOLOv11模型检测摄像头目标的python代码
- User
使用YOLOv11模型检测摄像头目标的python代码
- Assistant
要使用YOLOv11模型进行摄像头目标检测,你需要先设置好环境,并安装相应的依赖项。YOLOv11可能是一个假设的版本,因为直到我最后的知识更新,YOLO的最新版本是YOLOv5和YOLOv6。下面是一个基于YOLOv5的例子,如果YOLOv11存在,请根据YOLOv5的例子进行调整。
首先,确保你已经安装了以下库:
pip install torch torchvision pip install opencv-python接下来,你可以使用下面的代码使用YOLO模型通过摄像头检测目标:
import cv2 import torch # 加载YOLOv5模型 # 可以替换成YOLOv11模型的加载方式 model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # 使用YOLOv5小模型 # 打开摄像头 cap = cv2.VideoCapture(0) while cap.isOpened(): ret, frame = cap.read() if not ret: break # 进行目标检测 results = model(frame) # 显示结果 # 将结果渲染到帧上 frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 转换颜色 # 从结果中获取检测框 for xmin, ymin, xmax, ymax, conf, cls in results.xyxy[0]: cv2.rectangle(frame, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (255, 0, 0), 2) # 在框上显示类别和置信度 label = f'{model.names[int(cls)]} {conf:.2f}' cv2.putText(frame, label, (int(xmin), int(ymin)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2) # 将结果显示在窗口 cv2.imshow('YOLOv5 Detection', frame) # 按 'q' 键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头和关闭窗口 cap.release() cv2.destroyAllWindows() - User
import cv2 from ultralytics import YOLO
加载模型
model = YOLO("yolo11n.pt")
打开摄像头
cap = cv2.VideoCapture(0) # 0通常是默认摄像头
设置跳帧数,例如每5帧处理一次
skip_frames = 5 frame_count = 0
循环读取摄像头帧
while cap.isOpened(): success, frame = cap.read() if not success: break
# 跳过一些帧以降低帧率 frame_count += 1 if frame_count % skip_frames != 0: continue # 降低图像分辨率 scale_percent = 50 # 缩放比例 width = int(frame.shape[1] * scale_percent / 100) height = int(frame.shape[0] * scale_percent / 100) dim = (width, height) resized_frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA) # 使用模型进行检测 results = model(resized_frame) # print(results[0]) # 绘制检测结果 annotated_frame = results[0].plot() # 显示结果 cv2.imshow("YOLOv11物体检测", annotated_frame) # 按下'q'键退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break释放资源
cap.release() cv2.destroyAllWindows() 在上述代码中增加输出是否检测到目标
- Assistant
要在代码中增加输出以指示是否检测到目标,我们可以在处理检测结果后检查检测到的物体数量,并在控制台上打印相关信息。以下是您给出的代码的改进版本,增加了目标检测的输出:
import cv2 from ultralytics import YOLO # 加载模型 model = YOLO("yolo11n.pt") # 打开摄像头 cap = cv2.VideoCapture(0) # 0通常是默认摄像头 # 设置跳帧数,例如每5帧处理一次 skip_frames = 5 frame_count = 0 # 循环读取摄像头帧 while cap.isOpened(): success, frame = cap.read() if not success: break # 跳过一些帧以降低帧率 frame_count += 1 if frame_count % skip_frames != 0: continue # 降低图像分辨率 scale_percent = 50 # 缩放比例 width = int(frame.shape[1] * scale_percent / 100) height = int(frame.shape[0] * scale_percent / 100) dim = (width, height) resized_frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA) # 使用模型进行检测 results = model(resized_frame) # 绘制检测结果 annotated_frame = results[0].plot() # 检查检测到的目标 detections = results[0].boxes # 获取检测框信息 if detections is not None and detections.shape[0] > 0: print(f"检测到目标: {len(detections)}") # 输出检测到的目标数量 else: print("未检测到目标") # 显示结果 cv2.imshow("YOLOv11物体检测", annotated_frame) # 按下'q'键退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放资源 cap.release() cv2.destroyAllWindows()