File size: 1,111 Bytes
c0a5679 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | """Show a live view from every connected camera. Press 'q' to quit."""
import glob
import cv2
def find_working_cameras() -> list[str]:
working = []
for path in sorted(glob.glob("/dev/video*")):
cap = cv2.VideoCapture(path, cv2.CAP_V4L2)
if cap.isOpened():
ok, frame = cap.read()
if ok and frame is not None:
working.append(path)
cap.release()
return working
def main() -> None:
devices = find_working_cameras()
print(f"found {len(devices)} working camera(s): {devices}")
if not devices:
print("no working cameras found")
return
caps = {path: cv2.VideoCapture(path, cv2.CAP_V4L2) for path in devices}
try:
while True:
for path, cap in caps.items():
ok, frame = cap.read()
if ok:
cv2.imshow(path, frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
finally:
for cap in caps.values():
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
|