1import cv2
2
3def clearCapture(capture):
4 capture.release()
5 cv2.destroyAllWindows()
6
7def countCameras():
8 n = 0
9 for i in range(10):
10 try:
11 cap = cv2.VideoCapture(i)
12 ret, frame = cap.read()
13 cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
14 clearCapture(cap)
15 n += 1
16 except:
17 clearCapture(cap)
18 break
19 return n
20
21print countCameras()
22