AI_DL_Assignment / ListCamera.py
Prince-1's picture
Add files using upload-large-folder tool
0182da2 verified
raw
history blame
1.77 kB
# 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}")