Skip to content

Instantly share code, notes, and snippets.

@alex-luxonis
Created May 19, 2021 00:31
Show Gist options
  • Save alex-luxonis/8e5dd446b1af7c14a7549f6156c8fed6 to your computer and use it in GitHub Desktop.
Save alex-luxonis/8e5dd446b1af7c14a7549f6156c8fed6 to your computer and use it in GitHub Desktop.
Generic UVC camera preview and FPS measure
#!/usr/bin/env python3
# Key controls:
# q - quit
# c - capture
import cv2
import numpy as np
import time
# Configurable flags. Note: to get an accurate FPS measurement, disable 'show_frame'
calc_fps = True
show_frame = True
# TODO: change device number here
cam = cv2.VideoCapture(0)
time_start = time.monotonic()
fps_interval = 1
counter = 0
while True:
ret, frame = cam.read()
if calc_fps:
time_now = time.monotonic()
time_diff = time_now - time_start
counter += 1
if time_diff > fps_interval:
print("FPS: ", counter / time_diff)
counter = 0
time_start = time_now
if show_frame:
cv2.imshow("camera", frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
elif key == ord('c'):
name = 'cap-' + time.strftime("%H:%M:%S", time.localtime()) + '.png'
cv2.imwrite(name, frame)
print("Saved to:", name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment