- 1 year ago
Get list of Video Device in python on Windows machine
Comment
1 / 1
To obtain a list of video devices connected to a Windows machine using Python, you can utilize the opencv-python
library. First, ensure you have installed it by running pip install opencv-python
in your Python environment. Once installed, you can use the following code snippet:
pythonimport cv2
def get_video_devices():
video_devices = []
index = 0
while True:
# Create a VideoCapture object
cap = cv2.VideoCapture(index, cv2.CAP_DSHOW)
# Check if the VideoCapture object is successfully opened
if not cap.isOpened():
break
# Read a frame from the video device
_, _ = cap.read()
# Get the video device's properties
device_name = cap.getBackendName()
device_id = cap.get(cv2.CAP_PROP_POS_MSEC)
# Append the video device to the list
video_devices.append({"id": index, "name": device_name, "properties": device_id})
# Release the VideoCapture object
cap.release()
# Increment the index to check the next video device
index += 1
return video_devices
# Call the function to get the list of video devices
devices = get_video_devices()
# Print the list of video devices
for device in devices:
print(f"Device ID: {device['id']}")
print(f"Device Name: {device['name']}")
print(f"Device Properties: {device['properties']}")
print()
This code iterates over the video devices connected to the system and retrieves their ID, name, and properties using the cv2.VideoCapture
class from OpenCV. The loop terminates when there are no more video devices to be found. Finally, it prints the details of each video device.