Create openCV VideoCapture from interface name instead of camera numbers

后端 未结 5 935
眼角桃花
眼角桃花 2020-12-17 20:13

The normal way to create a videocapture is this:

cam = cv2.VideoCapture(n)

where n corresponds to the number of /dev/video0,

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 20:31

    If you know the model of the camera, you can look it up in /dev/v4l/by-id/.... We are using an HDMI-USB video converter, and we connect to it like this:

    #! /usr/bin/env python
    import os
    import re
    import cv2
    
    DEFAULT_CAMERA_NAME = '/dev/v4l/by-id/usb-AVerMedia_Technologies__Inc._Live_Gamer_Portable_2_Plus_5500114600612-video-index0'
    
    device_num = 0
    if os.path.exists(DEFAULT_CAMERA_NAME):
        device_path = os.path.realpath(DEFAULT_CAMERA_NAME)
        device_re = re.compile("\/dev\/video(\d+)")
        info = device_re.match(device_path)
        if info:
            device_num = int(info.group(1))
            print("Using default video capture device on /dev/video" + str(device_num))
    cap = cv2.VideoCapture(device_num)
    

    This follows the device name symlink to the /dev/video name, then parses that for the device number.

提交回复
热议问题