Linux下如何用Python获取USB摄像头(/dev/video0)按键触发信号?
Hey there! Let's tackle this problem step by step. Since your camera has a physical button that triggers captures on Windows, on Linux it's likely exposing that button either as a HID input device or via the UVC (USB Video Class) control interface—here are two solid approaches that don't require spinning up a new video window with OpenCV/GStreamer:
Approach 1: Listen for HID Input Events (Most Direct)
Many USB cameras with physical buttons register themselves as standard input devices (like a keyboard or mouse) on Linux. You can listen for button presses directly using the evdev library.
Steps:
- Find your camera's input device:
- Run
ls /dev/input/by-id/to look for a device named after your camera (e.g.,usb-<Camera_Model>-event-if00). - Or install
evtest(sudo apt install evtest), run it, and test each input device by pressing your camera's button—you'll see event logs when the correct device is selected.
- Run
- Install the
evdevlibrary:pip install evdev - Python code to listen for button presses:
from evdev import InputDevice, categorize, ecodes # Replace this path with your camera's input device (e.g., /dev/input/event5) camera_input = InputDevice('/dev/input/event5') print(f"Listening for button presses on: {camera_input.name}") for event in camera_input.read_loop(): if event.type == ecodes.EV_KEY: key_event = categorize(event) # Trigger only on button press (not release) if key_event.keystate == key_event.key_down: print("Camera button pressed!") # Add your custom logic here (no capture needed per your request)
Note:
You might need to run this script with sudo, or add your user to the input group to avoid permission issues:
sudo usermod -aG input $USER
Log out and back in for the group change to take effect.
Approach 2: Poll V4L2 UVC Control State
Some cameras expose the button as a UVC control instead of a HID device. You can read this control's state using V4L2 utilities and Python bindings.
Steps:
- Identify the button control ID:
Runv4l2-ctl --list-controlsand look for a control related to your camera's button (e.g., under "User Controls" you might see a "Button" entry with an ID like0x00980901). - Install
python-v4l2:pip install v4l2 - Python code to monitor button state:
import v4l2 import fcntl import time # Open the video device video_dev = open('/dev/video0', 'rb+') # Replace with your button's control ID from v4l2-ctl output BUTTON_CTRL_ID = 0x00980901 prev_button_state = None print("Monitoring camera button state...") try: while True: # Query the current button state ctrl = v4l2.v4l2_control() ctrl.id = BUTTON_CTRL_ID fcntl.ioctl(video_dev, v4l2.VIDIOC_G_CTRL, ctrl) current_state = ctrl.value # Detect a press event (adjust values based on your camera: 1=press, 0=release) if prev_button_state is not None and current_state == 1 and prev_button_state == 0: print("Camera button pressed!") prev_button_state = current_state time.sleep(0.05) # Adjust polling interval for responsiveness finally: video_dev.close()
Extra Tips:
- If one approach doesn't work, try the other—camera manufacturers implement button handling differently.
- Neither of these methods create a video window or process the video stream, so they fit your requirement perfectly.
内容的提问来源于stack exchange,提问作者peter

