Rather than capture stills repeatedly it will be quicker to run the camera in a video mode and pass the captured frames to your OpenCV code.
This snippet uses the Picamera2 library to do the video capturing.Result with a V2 camera on a Pi5 with the default framerate of the video configuration of 30 fps.Isn't saving the files or doing any processing only capturing and counting. But much faster that stills.
PS Search for some of the work by HermannSW if you need to explore very high framerate captures.
This snippet uses the Picamera2 library to do the video capturing.
Code:
import cv2import osfrom picamera2 import Picamera2from time import time, sleep# Configurationcapture_duration = 10 # Total duration to capture frames in secondsresolution = (1332, 990) # Desired resolutionsize=tuple(resolution)filename = "/dev/shm/frame.jpg" # Use the same file name for all capturescam = Picamera2()cam.configure(cam.create_video_configuration(main={"format": 'RGB888', "size": size}))cam.start()# Capture and process framesstart_time = time()frame_count = 0while time() - start_time < capture_duration: # Check if the file was created successfully #if os.path.exists(filename): # Read the frame frame = cam.capture_array() if frame is not None: # Process the frame # Example: Convert to grayscale #gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Increment frame counter frame_count += 1# Calculate and display the average FPSend_time = time()duration = end_time - start_timeaverage_fps = frame_count / durationprint(f"Total number of frames captured: {frame_count}")print(f"Average FPS: {average_fps:.2f}")Code:
Total number of frames captured: 293Average FPS: 29.21PS Search for some of the work by HermannSW if you need to explore very high framerate captures.
Statistics: Posted by sandyol — Wed Apr 24, 2024 7:45 pm