OpenCV无法调用内置摄像头及外接Logitech C270摄像头的问题求助
OpenCV无法调用内置摄像头及外接Logitech C270摄像头的问题求助
我现在在测试用OpenCV推流摄像头画面,笔记本上连了两个摄像头:
- 内置摄像头
- 外接的Logitech C270摄像头
这两个摄像头在VLC或者Windows相机应用里都能正常工作,但在OpenCV里遇到了问题:
- 用
cv2.VideoCapture(0)时,直接报错:Error: Could not open webcam. - 用
cv2.VideoCapture(1)时没有报错,但画面全黑。我怀疑索引1对应的是外接摄像头,应该是正确的索引。
另外还有这些细节:
- 用索引1运行代码时,外接摄像头会被其他程序占用,这倒是正常的,毕竟OpenCV会接管设备
- 我已经在Windows设置里给Python 3.11开了摄像头权限,而且摄像头的使用指示灯也亮着,证明确实被调用了
- 这个问题和VS Code没关系,直接在终端运行脚本也会出现同样的情况
我搞不懂两个点:
- 为什么内置摄像头(索引0)无法访问?
- 为什么外接摄像头(索引1)在OpenCV里显示黑画面?
下面是我最初用的代码:
import cv2 def main(): print("Press 'q' to quit the program.") # Open a connection to the webcam (0 is the default camera) cap = cv2.VideoCapture(0) if not cap.isOpened(): print("Error: Could not open webcam.") return # Allow the camera to warm up cv2.waitKey(1000) while True: # Capture frame-by-frame ret, frame = cap.read() if not ret: print("Error: Could not read frame.") break # Display the resulting frame cv2.imshow('Webcam Stream', frame) # Break the loop on 'q' key press if cv2.waitKey(1) == ord('q'): break # When everything is done, release the capture cap.release() cv2.destroyAllWindows() if __name__ == "__main__": main()
希望能得到一些调试或者解决的建议,有没有什么配置我漏了?或者有没有更好的方法测试这两个摄像头?
补充调试信息
我试着用ffmpeg获取摄像头的更多信息,安装了python-lib之后运行了这个脚本:
from imageio_ffmpeg import get_ffmpeg_exe import os ffmpeg_path = get_ffmpeg_exe() os.system(f'"{ffmpeg_path}" -list_devices true -f dshow -i dummy')
得到的输出是:
libavutil 56. 31.100 / 56. 31.100 libavcodec 58. 54.100 / 58. 54.100 libavformat 58. 29.100 / 58. 29.100 libavdevice 58. 8.100 / 58. 8.100 libavfilter 7. 57.100 / 7. 57.100 libswscale 5. 5.100 / 5. 5.100 libswresample 3. 5.100 / 3. 5.100 libpostproc 55. 5.100 / 55. 5.100 [dshow @ 00000211f34a91c0] DirectShow video devices (some may be both video and audio devices) [dshow @ 00000211f34a91c0] "Integrated Webcam" [dshow @ 00000211f34a91c0] Alternative name "@device_pnp_\\?\usb#vid_0c45&pid_6d13&mi_00#6&18c8350f&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global" [dshow @ 00000211f34a91c0] "Logi C270 HD WebCam" [dshow @ 00000211f34a91c0] Alternative name "@device_pnp_\\?\usb#vid_046d&pid_0825&mi_00#7&1ef939b1&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global" [dshow @ 00000211f34a91c0] "OBS Virtual Camera" [dshow @ 00000211f34a91c0] Alternative name "@device_sw_{860BB310-5D01-11D0-BD3B-00A0C911CE86}\{A3FCE0F5-3493-419F-958A-ABA1250EC20B}" [dshow @ 00000211f34a91c0] DirectShow audio devices [dshow @ 00000211f34a91c0] "Mikrofon (3- Logi C270 HD WebCam)" [dshow @ 00000211f34a91c0] Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{2839ECB9-3054-4631-9391-CE0C2E6CADD4}" [dshow @ 00000211f34a91c0] "Microphone Array (Realtek(R) Audio)" [dshow @ 00000211f34a91c0] Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{EC1466BD-0BFF-46FD-960E-B7EAAD2B6787}"
之后我又运行了这个脚本来获取摄像头支持的模式:
import subprocess from imageio_ffmpeg import get_ffmpeg_exe def list_camera_modes(camera_name): ffmpeg_path = get_ffmpeg_exe() command = f'"{ffmpeg_path}" -f dshow -list_options true -i video="{camera_name}"' subprocess.run(command, shell=True, check=True) if __name__ == "__main__": camera_name = "Logi C270 HD WebCam" list_camera_modes(camera_name)
输出结果是:
libavutil 56. 31.100 / 56. 31.100 libavcodec 58. 54.100 / 58. 54.100 libavformat 58. 29.100 / 58. 29.100 libavdevice 58. 8.100 / 58. 8.100 libavfilter 7. 57.100 / 7. 57.100 libswscale 5. 5.100 / 5. 5.100 libswresample 3. 5.100 / 3. 5.100 libpostproc 55. 5.100 / 55. 5.100 [dshow @ 0000023f579391c0] DirectShow video device options (from video devices) [dshow @ 0000023f579391c0] Pin "Capture" (alternative pin name "0") [dshow @ 0000023f579391c0] vcodec=mjpeg min s=1280x960 fps=5 max s=1280x960 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=1280x960 fps=5 max s=1280x960 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=1280x720 fps=5 max s=1280x720 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=1280x720 fps=5 max s=1280x720 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=1024x768 fps=5 max s=1024x768 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=1024x768 fps=5 max s=1024x768 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=800x600 fps=5 max s=800x600 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=800x600 fps=5 max s=800x600 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=640x480 fps=5 max s=640x480 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=640x480 fps=5 max s=640x480 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=640x360 fps=5 max s=640x360 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=640x360 fps=5 max s=640x360 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=320x240 fps=5 max s=320x240 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=320x240 fps=5 max s=320x240 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=1280x960 fps=5 max s=1280x960 fps=30 [dshow @ 0000023f579391c0] vcodec=mjpeg min s=1280x960 fps=5 max s=1280x960 fps=30 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=1280x960 fps=5 max s=1280x960 fps=5 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=1280x960 fps=5 max s=1280x960 fps=5 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=1280x720 fps=5 max s=1280x720 fps=5 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=1280x720 fps=5 max s=1280x720 fps=5 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=1024x768 fps=5 max s=1024x768 fps=5 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=1024x768 fps=5 max s=1024x768 fps=5 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=800x600 fps=20 max s=800x600 fps=20 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=800x600 fps=20 max s=800x600 fps=20 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=640x480 fps=5 max s=640x480 fps=30 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=640x480 fps=5 max s=640x480 fps=30 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=320x240 fps=30 max s=320x240 fps=30 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=320x240 fps=30 max s=320x240 fps=30 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=1280x960 fps=5 max s=1280x960 fps=5 [dshow @ 0000023f579391c0] pixel_format=yuyv422 min s=1280x960 fps=5 max s=1280x960 fps=5
我觉得摄像头本身应该没问题,所以调整了OpenCV的代码,匹配摄像头支持的模式:
import cv2 cap = cv2.VideoCapture(1) width, height = 1280, 960 cap.set(cv2.CAP_PROP_FRAME_WIDTH, width) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height) fps = 30 cap.set(cv2.CAP_PROP_FPS, fps) cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG')) if not cap.isOpened(): print("Kamera konnte nicht geöffnet werden") else: print(f"Kamera erfolgreich geöffnet mit {width}x{height} @ {fps} FPS") while True: ret, frame = cap.read() if not ret: print("Frame konnte nicht gelesen werden") break cv2.imshow("Kamera", frame) if cv2.waitKey(1) & 0xFF == ord('q'): # 'q' zum Beenden break cap.release() cv2.destroyAllWindows()
但结果
相关产品推荐
相关产品推荐

