wjs/yolo_safehat_num_v8/ptDetcVesdio.py

31 lines
899 B
Python
Raw Permalink Normal View History

2024-05-15 14:46:56 +08:00
import time
import cv2
from ultralytics import YOLO
# 加载 YOLOv8 模型
model = YOLO(r"D:\1BNS_projects\yolo_safehat_num_v8\weights\best.pt")
# 获取摄像头内容,参数 0 表示使用默认的摄像头
cap = cv2.VideoCapture(0)
# frame_count = 0
# start_time = time.time()
while cap.isOpened():
success, frame = cap.read() # 读取摄像头的一帧图像
# 初始化帧数计数器和起始时间
frame_count = 0
start_time = time.time()
if success:
model.predict(source=frame, show=True) # 对当前帧进行目标检测并显示结果
frame_count += 1
# 通过按下 'q' 键退出循环
elapsed_time = time.time() - start_time
fps = frame_count / elapsed_time
print(f'FPS: {fps:.2f}'"")
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release() # 释放摄像头资源
cv2.destroyAllWindows() # 关闭OpenCV窗口