Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Camera Device Name AND Port for OpenCV VideoStream Python?

Tags:

python

opencv

I am currently trying to let users pick a camera for our front end software. We currently iterate through the camera devices like shown in this answer on StackOverflow. This will return us the Camera IDs:

index = 0
arr = []
while True:
    cap = cv2.VideoCapture(index)
    if not cap.read()[0]:
        break
    else:
        arr.append(index)
    cap.release()
    index += 1
return arr

which is fine, but it would be a lot better to get a friendly device name to show the user i.e: Logitech Webcam

Does anyone know how to get the actual names of these cameras to show to the users rather than displaying with IDs?

like image 767
ariagno Avatar asked Feb 15 '26 04:02

ariagno


1 Answers

Here you can directly, use this one.

pip install pygrabber

from pygrabber.dshow_graph import FilterGraph

def get_available_cameras() :

    devices = FilterGraph().get_input_devices()

    available_cameras = {}

    for device_index, device_name in enumerate(devices):
        available_cameras[device_index] = device_name

    return available_cameras

print(get_available_cameras())

Output on my device,

{0: 'HD Webcam', 1: 'FHD Camera', 2: 'Intel Virtual Camera', 3: 'OBS Virtual Camera'}

Additionally, this one is for microphones also,

pip install pyaudio

import pyaudio

def get_available_michrophones() :

    available_microphones = {}
    pyduo = pyaudio.PyAudio()
    devices_info = pyduo.get_host_api_info_by_index(0)
    number_of_devices = devices_info.get('deviceCount')
    for device_index in range(0, number_of_devices):
        if (pyduo.get_device_info_by_host_api_device_index(0, device_index).get('maxInputChannels')) > 0:
            available_microphones[device_index] = pyduo.get_device_info_by_host_api_device_index(0, device_index).get('name')

    return available_microphones

print(get_available_michrophones())

Output on my device,

{0: 'Microsoft Sound Mapper - Input', 1: 'Microphone (Pusat USB Broadcast', 2: 'Microphone (FHD Camera Micropho', 3: 'Microphone (Realtek(R) Audio)'}
like image 118
ECM Avatar answered Feb 16 '26 17:02

ECM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!