Create openCV VideoCapture from the interface name instead of camera numbers

The usual way to create a video capture is as follows:

cam = cv2.VideoCapture(n)

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

But since I am building a robot that uses several cameras for different things, I had to make sure that it was assigned to the correct camera, I created udev rules that created devices with symbolic links to the correct port whenever a certain camera was connected.

They work because when I look in a directory /dev, I see a link:

/dev/front_cam -> video1

However, I'm not sure how to actually use it now.

I thought I could just open it from the file name, as if it were a video, but it cam = cv2.VideoCapture('/dev/front_cam')doesn’t work.

Also cv2.VideoCapture('/dev/video1')

, VideoCapture, , (cam.isOpened() False).

+5
3
import re
import subprocess
import cv2
import os

device_re = re.compile("Bus\s+(?P<bus>\d+)\s+Device\s+(?P<device>\d+).+ID\s(?P<id>\w+:\w+)\s(?P<tag>.+)$", re.I)
df = subprocess.check_output("lsusb", shell=True)
for i in df.split('\n'):
    if i:
        info = device_re.match(i)
        if info:
            dinfo = info.groupdict()
            if "Logitech, Inc. Webcam C270" in dinfo['tag']:
                print "Camera found."
                bus = dinfo['bus']
                device = dinfo['device']
                break

device_index = None
for file in os.listdir("/sys/class/video4linux"):
    real_file = os.path.realpath("/sys/class/video4linux/" + file)
    print real_file
    print "/" + str(bus[-1]) + "-" + str(device[-1]) + "/"
    if "/" + str(bus[-1]) + "-" + str(device[-1]) + "/" in real_file:
        device_index = real_file[-1]
        print "Hurray, device index is " + str(device_index)


camera = cv2.VideoCapture(int(device_index))

while True:
    (grabbed, frame) = camera.read() # Grab the first frame
    cv2.imshow("Camera", frame)
    key = cv2.waitKey(1) & 0xFF

USB-. BUS DEVICE.

video4linux. realpath VideoCapture.

+1

, .

, , .

import subprocess

cmd = "readlink -f /dev/CAMC"
process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)

# output of form /dev/videoX
out = process.communicate()[0]

# parse for ints
nums = [int(x) for x in out if x.isdigit()]

cap = cv2.VideoCapture(nums[0])
+1

If you know the camera model, you can watch it at /dev/v4l/by-id/.... We use the HDMI-USB video converter and connect to it as follows:

#! /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 a symbolic link of the device name to the name /dev/video, and then analyzes it for the device number.

+1
source

All Articles