how to count how many cameras you have with python

Solutions on MaxInterview for how to count how many cameras you have with python by the best coders in the world

showing results for - "how to count how many cameras you have with python"
Ambrine
23 Jan 2016
1import cv2
2
3def camera_amount():
4    '''Returns int value of available camera devices connected to the host device'''
5    camera = 0
6    while True:
7        if (cv2.VideoCapture(camera).grab()) is True:
8            camera = camera + 1
9        else:
10            cv2.destroyAllWindows()
11            return(int(camera))
Alessia
03 Nov 2019
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