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
在video4linux目录下找到符号链接。从
realpath中提取设备索引,并将其传递给VideoCapture方法。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)