Skip to content

Instantly share code, notes, and snippets.

@RealOrangeOne
Created July 9, 2023 11:19
Show Gist options
  • Save RealOrangeOne/1f2456d28f7d20e530ba78c4bc57778d to your computer and use it in GitHub Desktop.
Save RealOrangeOne/1f2456d28f7d20e530ba78c4bc57778d to your computer and use it in GitHub Desktop.
Script to test connected webcams
#!/usr/bin/env bash
set -e
# Remove annoyingly verbose logging from OpenCV
export OPENCV_LOG_LEVEL=FATAL
python3 ./webcam_test.py
import cv2
def get_cameras():
cameras = []
for i in range(8):
capture = cv2.VideoCapture(i)
if capture.isOpened():
cameras.append(i)
capture.release()
return cameras
def main():
initial_cameras = set(get_cameras())
while True:
print("Insert a camera")
while not (new_cameras := set(get_cameras()) - initial_cameras):
cv2.waitKey(100)
for camera_id in new_cameras:
camera = cv2.VideoCapture(camera_id)
ret, frame = camera.read()
cv2.imshow("frame", frame)
cv2.waitKey(200)
success = (input("Is the image clear? [Y/n]") or "y").lower() == "y"
camera.release()
cv2.destroyAllWindows()
print("Success. Disconnect cameras")
while set(get_cameras()) != initial_cameras:
cv2.waitKey(100)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment