2/02/2025

Find and search which webcam is online on your computer.

 python code:


.

import cv2
import time

def check_cameras():
print("\nChecking camera indices 0-9...")
print("----------------------------------------")
working_cameras = []
for i in range(10):
cap = cv2.VideoCapture(i)
if cap.isOpened():
# Try to read a frame
ret, frame = cap.read()
if ret:
# Get camera properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
# Get backend info
backend = cap.getBackendName()
print(f"\n✓ Camera {i} is ONLINE:")
print(f" Resolution: {width}x{height}")
print(f" FPS: {fps}")
print(f" Backend: {backend}")
print(f" Frame shape: {frame.shape}")
# Try to get more detailed format information
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
fourcc_str = "".join([chr((fourcc >> 8 * i) & 0xFF) for i in range(4)])
print(f" Format: {fourcc_str}")
working_cameras.append(i)
# Test a few more frames to ensure stability
frames_to_test = 5
success_count = 0
for _ in range(frames_to_test):
ret, frame = cap.read()
if ret:
success_count += 1
time.sleep(0.1)
print(f" Stability test: {success_count}/{frames_to_test} frames captured successfully")
else:
print(f"✗ Camera {i}: Device found but cannot read frames")
cap.release()
else:
print(f"✗ Camera {i}: Not available")
print("\n----------------------------------------")
print("Summary:")
if working_cameras:
print(f"Working camera indices: {working_cameras}")
else:
print("No working cameras found")
print("----------------------------------------")

def main():
print("Starting camera detection...")
check_cameras()
print("\nCamera check complete!")

if __name__ == "__main__":
main()

..



output is looks like:

Starting camera detection...


Checking camera indices 0-9...

----------------------------------------


✓ Camera 0 is ONLINE:

  Resolution: 640x480

  FPS: 30.0

  Backend: V4L2

  Frame shape: (480, 640, 3)

  Format: YUYV

  Stability test: 5/5 frames captured successfully

[ WARN:0@0.913] global cap_v4l.cpp:999 open VIDEOIO(V4L2:/dev/video1): can't open camera by index

[ERROR:0@0.972] global obsensor_uvc_stream_channel.cpp:158 getStreamChannelGroup Camera index out of range

✗ Camera 1: Not available


✓ Camera 2 is ONLINE:

  Resolution: 640x480

  FPS: 30.0

  Backend: V4L2

  Frame shape: (480, 640, 3)

  Format: YUYV

  Stability test: 5/5 frames captured successfully

[ WARN:0@1.818] global cap_v4l.cpp:999 open VIDEOIO(V4L2:/dev/video3): can't open camera by index

[ERROR:0@1.820] global obsensor_uvc_stream_channel.cpp:158 getStreamChannelGroup Camera index out of range

✗ Camera 3: Not available

[ WARN:0@1.820] global cap_v4l.cpp:999 open VIDEOIO(V4L2:/dev/video4): can't open camera by index

[ERROR:0@1.822] global obsensor_uvc_stream_channel.cpp:158 getStreamChannelGroup Camera index out of range

✗ Camera 4: Not available

[ WARN:0@1.822] global cap_v4l.cpp:999 open VIDEOIO(V4L2:/dev/video5): can't open camera by index

[ERROR:0@1.823] global obsensor_uvc_stream_channel.cpp:158 getStreamChannelGroup Camera index out of range

✗ Camera 5: Not available

[ WARN:0@1.824] global cap_v4l.cpp:999 open VIDEOIO(V4L2:/dev/video6): can't open camera by index

[ERROR:0@1.825] global obsensor_uvc_stream_channel.cpp:158 getStreamChannelGroup Camera index out of range

✗ Camera 6: Not available

[ WARN:0@1.825] global cap_v4l.cpp:999 open VIDEOIO(V4L2:/dev/video7): can't open camera by index

[ERROR:0@1.828] global obsensor_uvc_stream_channel.cpp:158 getStreamChannelGroup Camera index out of range

✗ Camera 7: Not available

[ WARN:0@1.828] global cap_v4l.cpp:999 open VIDEOIO(V4L2:/dev/video8): can't open camera by index

[ERROR:0@1.830] global obsensor_uvc_stream_channel.cpp:158 getStreamChannelGroup Camera index out of range

✗ Camera 8: Not available

[ WARN:0@1.830] global cap_v4l.cpp:999 open VIDEOIO(V4L2:/dev/video9): can't open camera by index

[ERROR:0@1.831] global obsensor_uvc_stream_channel.cpp:158 getStreamChannelGroup Camera index out of range

✗ Camera 9: Not available


----------------------------------------

Summary:

Working camera indices: [0, 2]

----------------------------------------


Camera check complete!



so you can know which one is online


Thank you!