Research
Check valid webcams with cv2, python
Shakeratto
2020. 11. 18. 16:01
import cv2
def testDevice(device_id): cap = cv2.VideoCapture(device_id) if cap is None or not cap.isOpened(): print('Warning: unable to open camera:', device_id) return None else: print('here is your camera', device_id) return cap
valid_cam_ids = [] caps = [] for device_id in range(0,5): valid_cap = testDevice(device_id) if valid_cap: valid_cam_ids.append(device_id) caps.append(valid_cap)
while True: for cam_id in valid_cam_ids: ret, frame = caps[cam_id].read() cv2.imshow('cam_id: '+str(cam_id), frame) k = cv2.waitKey(1) if k == ord('q') or k == 27: break
|