ariagno
ariagno

Reputation: 601

Get Camera Device Name AND Port for OpenCV VideoStream Python?

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?

Upvotes: 3

Views: 8379

Answers (2)

ECM
ECM

Reputation: 51

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)'}

Upvotes: 4

dmatos2012
dmatos2012

Reputation: 93

There are two options, with the MSMF backend, you can do it here CV-camera finder, just download the .pyd file on windows (I think only works with python 3.7), or with DSHOW, which is shown on that repo's README.

Upvotes: 1

Related Questions