Last active
July 14, 2021 05:25
-
-
Save khanhlvg/464fdd76702c94b15e9375e05a21798b to your computer and use it in GitHub Desktop.
Benchmark PiCamera vs. OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from picamera import PiCamera | |
from picamera.array import PiRGBArray | |
import time | |
import cv2 | |
WINDOW_NAME = 'benchmark' | |
FRAME_COUNT = 100 | |
FRAME_RATE = 30 | |
camera = PiCamera(framerate = FRAME_RATE) | |
time.sleep(2) | |
camera.resolution = (640,480) | |
rawCapture = PiRGBArray(camera, size=camera.resolution) | |
start = time.time() | |
for frame, i in zip(camera.capture_continuous(rawCapture, format="bgr", use_video_port=True), range(FRAME_COUNT)): | |
image = frame.array | |
rawCapture.truncate(0) | |
cv2.imshow(WINDOW_NAME, image) | |
cv2.waitKey(1) | |
camera.close() | |
print("Time for {0} frames: {1} seconds".format(FRAME_COUNT ,time.time()-start)) | |
time.sleep(2) | |
cap = cv2.VideoCapture(0) | |
cap.set(cv2.CAP_PROP_FRAME_WIDTH,640) | |
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,480) | |
cap.set(cv2.CAP_PROP_FPS, FRAME_RATE) | |
start = time.time() | |
for i in range(FRAME_COUNT): | |
success, image = cap.read() | |
if success: | |
cv2.imshow(WINDOW_NAME, image) | |
cv2.waitKey(1) | |
cap.release() | |
print("Time for {0} frames: {1} seconds".format(FRAME_COUNT, time.time() - start)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment