Skip to main content
 首页 » 编程设计

python之从网络摄像头按钮获取信号

2025年01月19日10虾米姐

我有一个网络摄像头,它有按钮。在 Windows 中,当我按下该按钮时,它会抓取图像快照。但现在我在 linux 上,我正在使用 VLC 从我的 usb 网络摄像头“/dev/video0”查看视频。我想在按下按钮后使用 python 从该按钮获取信号——我的意思是只是获取此信号(不会自动抓取快照)。 我试图谷歌它,但没有运气。我不想使用 opencv 或 gstreamer 将视频放入新窗口,我只需要在按下网络摄像头上的按钮时获取信号。知道如何获得这个信号吗?

请您参考如下方法:

使用 python-evdev,您可以捕获由鼠标、键盘……以及网络摄像头等输入设备触发的事件

evdev is a Linux-only generic protocol that the kernel uses to forward information and events about input devices to userspace. It's not just for mice and keyboards but any device that has any sort of axis, key or button, including things like webcams and remote controls. Each device is represented as a device node in the form of /dev/input/event0, with the trailing number increasing as you add more devices. The node numbers are re-used after you unplug a device, so don't hardcode the device node into a script. The device nodes are also only readable by root, thus you need to run any debugging tools as root too.

http://who-t.blogspot.de/2016/09/understanding-evdev.html

网络摄像头的按钮也分配了一个事件,你必须在哪个/dev/input/文件夹中找到

从 http://python-evdev.readthedocs.io/en/latest/tutorial.html 运行以下代码获取网络摄像头的 /dev/input 路径:

列出可访问的事件设备

 import evdev 
 
 devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()] 
 for device in devices: 
     print(device.fn, device.name, device.phys)