File size: 1,772 Bytes
0182da2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# camlist.py
# Lists all available cameras attached to the computer
# Dependencies: pip install opencv-python
# Usage: python camlist.py

import cv2


class CamConfig:
    def __init__(self, cap: cv2.VideoCapture):
        self.width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        self.height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
        self.fps = cap.get(cv2.CAP_PROP_FPS)
        fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
        self.format_str = "".join([chr((fourcc >> (8 * i)) & 0xFF) for i in range(4)])

        # Additional camera properties
        self.brightness = cap.get(cv2.CAP_PROP_BRIGHTNESS)
        self.contrast = cap.get(cv2.CAP_PROP_CONTRAST)
        self.saturation = cap.get(cv2.CAP_PROP_SATURATION)
        self.device_id = cap.getBackendName()  # Get backend API name

    def __str__(self):
        return (
            f"Camera Details:\n"
            f"Resolution: {self.width}x{self.height}\n"
            f"FPS: {self.fps}\n"
            f"FourCC Format: {self.format_str}\n"
            f"Brightness: {self.brightness}\n"
            f"Contrast: {self.contrast}\n"
            f"Saturation: {self.saturation}\n"
            f"Device Backend: {self.device_id}\n"
        )


print(f"OpenCV version: {cv2.__version__}")

max_cameras = 10
available = []
details = {}

for i in range(max_cameras):
    cap = cv2.VideoCapture(i)

    if not cap.isOpened():  # Fix: Check if the camera opened successfully
        print(f"Camera index {i:02d} not found...")
        continue

    details[i] = CamConfig(cap)
    available.append(i)

    print(f"Camera index {i:02d} OK!")

    cap.release()  # Release AFTER capturing details

print(f"\nCameras found: {available}\n")

for index, config in details.items():
    print(f"Camera {index}:\n{config}")