You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

  1. 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.
  2. Install the evdev library:
    pip install evdev
    
  3. 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:

  1. Identify the button control ID:
    Run v4l2-ctl --list-controls and look for a control related to your camera's button (e.g., under "User Controls" you might see a "Button" entry with an ID like 0x00980901).
  2. Install python-v4l2:
    pip install v4l2
    
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:31:55