Ubuntu 22.04 LTS下Webcam频繁闪烁问题求助
Hey there, sorry to hear you're stuck with this frustrating webcam flickering issue on Ubuntu! It's super weird that it works fine on Windows and apps like Zoom/Discord but acts up with OpenCV, face-recognition, and Cheese. Let's go through some practical fixes that have helped others resolve similar problems:
强制设置固定的帧率和分辨率
OpenCV often defaults to auto-resizing/auto-framerate which can cause the camera to "jump" between settings, leading to flickering. Try hardcoding a stable resolution and FPS in your code. For example:import cv2 cap = cv2.VideoCapture(0) # Set fixed resolution (adjust to your cam's supported specs) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) # Set fixed FPS (30 is standard for most webcams) cap.set(cv2.CAP_PROP_FPS, 30) while True: ret, frame = cap.read() if not ret: break cv2.imshow('Webcam', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()You can check your camera's supported resolutions/FPS using
v4l2-ctl --list-formats-ext -d /dev/video0(installv4l-utilsfirst if needed withsudo apt install v4l-utils).调整V4L2摄像头控制参数
Auto-exposure, auto-gain, or auto-whitebalance can trigger flickering in low-light or unstable lighting. Let's lock these settings manually:- First list all available controls:
v4l2-ctl --list-ctrls -d /dev/video0 - Disable auto-exposure and set a fixed value (replace
100with a value that works for your environment):sudo v4l2-ctl -d /dev/video0 --set-ctrl=exposure_auto=1 sudo v4l2-ctl -d /dev/video0 --set-ctrl=exposure_absolute=100 - You can also try disabling auto-whitebalance:
sudo v4l2-ctl -d /dev/video0 --set-ctrl=white_balance_temperature_auto=0 sudo v4l2-ctl -d /dev/video0 --set-ctrl=white_balance_temperature=4000
- First list all available controls:
修复UVCCamera驱动兼容性
Many USB webcams use theuvcvideodriver on Ubuntu, which can have quirks. Try reloading the driver or adding compatibility flags:- Reload the driver:
sudo rmmod uvcvideo && sudo modprobe uvcvideo - Add a quirk flag to fix flickering (this persists after reboot):
echo "options uvcvideo quirks=128" | sudo tee /etc/modprobe.d/uvcvideo.conf sudo update-initramfs -u reboot
The
128quirk disables auto-exposure for some problematic cams—you can try other values like64or256if this doesn't work.- Reload the driver:
关闭USB电源节能模式
Ubuntu默认的电源节能设置可能会限制摄像头所在USB端口的供电,导致间歇性闪烁。关闭对应端口的电源管理:- 找到摄像头的USB设备路径(在输出中寻找带"Camera"的条目):
lsusb -t - 修改电源控制设置(替换为你自己的设备路径):
sudo echo "on" | sudo tee /sys/bus/usb/devices/1-2/power/control
想要永久生效的话,可以把这条命令添加到开机启动项里。
- 找到摄像头的USB设备路径(在输出中寻找带"Camera"的条目):
检查Cheese应用设置
打开Cheese,点击右上角三个点进入偏好设置,尝试将分辨率和帧率设为固定值而非"自动"。同时关闭可能会处理画面的"特效"功能,避免因额外渲染导致闪烁。
可以逐个尝试这些步骤——通常固定分辨率/帧率或者锁定曝光设置就能解决大部分人的问题。如果有任何步骤生效或者遇到新问题,随时反馈哦!
备注:内容来源于stack exchange,提问作者Nayem Sarwar

