从接口名称而不是摄像机编号创建openCV VideoCapture

从接口名称而不是摄像机编号创建openCV VideoCapture,第1张

从接口名称而不是摄像机编号创建openCV VideoCapture

import re
import subprocess
import cv2
import os

device_re = re.compile("Buss+(?P<bus>d+)s+Devices+(?P<device>d+).+IDs(?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']     breakdevice_index = Nonefor 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设备列表中搜索所需的字符串。获取总线和设备
号。

在video4linux目录下找到符号链接。从
realpath中提取设备索引,并将其传递给VideoCapture方法。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5661707.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存