from imutils.video import FPS import cv2 camera = cv2.VideoCapture(0) # initialize camera fps = FPS().start() # start FPS counter while True: ret, frame = camera.read() # read frames from camera if not ret: break # process frame here fps.update() cv2.imshow("Frame", frame) if cv2.waitKey(1) & 0xFF == ord("q"): break fps.stop() # stop the timer and display FPS information print("FPS: {:.2f}".format(fps.fps())) camera.release() cv2.destroyAllWindows()
from imutils.video import FPS import cv2 video = cv2.VideoCapture("myvideo.mp4") fps = FPS().start() while True: ret, frame = video.read() if not ret: break # process frame here fps.update() cv2.imshow("Frame", frame) if cv2.waitKey(1) & 0xFF == ord("q"): break fps.stop() print("FPS: {:.2f}".format(fps.fps())) video.release() cv2.destroyAllWindows()This example is similar to the previous one, but instead of using the default camera, it is reading frames from a video file called "myvideo.mp4". Package Library: Imutils (python package)