--- title: "🎬 Счётчик FPS в OpenCV" date: 2023-10-03T21:41:14+03:00 draft: false tags: [cv, tips, python] --- На примере работы с YOLOv8 показываю получение FPS. Необходим модуль `time` и две переменные: `prev_frame_time`, `new_frame_time`. ```python import time import cv2 from ultralytics import YOLO model = YOLO('best_s_640.pt') video_path = 'run.mp4' cap = cv2.VideoCapture(video_path) prev_frame_time = 0 new_frame_time = 0 while cap.isOpened(): success, frame = cap.read() new_frame_time = time.time() if success: results = model(frame) # Обработка кадра # ... fps = str(1 / (new_frame_time - prev_frame_time)) # Вывод FPS на кадр cv2.putText( annotated_frame, fps, (7, 70), cv2.FONT_HERSHEY_SIMPLEX, 3, (100, 255, 0), 3, cv2.LINE_AA, ) prev_frame_time = new_frame_time cv2.imshow('OpenCV', complete_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break print('FPS: {}'.format(fps)) cap.release() cv2.destroyAllWindows() ```