from imutils.video import VideoStream, FPS import cv2 # initialize the webcam stream vs = VideoStream(src=0).start() # start the FPS counter fps = FPS().start() # loop over frames from the video stream while True: # read the next frame from the webcam frame = vs.read() # do some processing on the frame (e.g. detect objects) # update the FPS counter fps.update() # display the processed frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the 'q' key is pressed, break from the loop if key == ord("q"): break # stop the FPS counter and display the results fps.stop() print("FPS:", fps.fps()) # cleanup cv2.destroyAllWindows() vs.stop()
from imutils.video import FileVideoStream, FPS import cv2 # initialize the video stream from a file vs = FileVideoStream("my_video.mp4").start() # start the FPS counter fps = FPS().start() # loop over frames from the video stream while vs.more(): # read the next frame from the file frame = vs.read() # do some processing on the frame (e.g. detect objects) # update the FPS counter fps.update() # display the processed frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the 'q' key is pressed, break from the loop if key == ord("q"): break # stop the FPS counter and display the results fps.stop() print("FPS:", fps.fps()) # cleanup cv2.destroyAllWindows() vs.stop()This example is similar to the previous one, but instead of opening a webcam stream it opens a video file using the FileVideoStream class. The rest of the code is the same. Package library: OpenCV (cv2)