OpenCV:无法从网络摄像头进行流式传输(内置和外部 Logitech C270)

问题描述 投票:0回答:1

我正在尝试测试是否可以在 OpenCV 中传输相机信号。我的笔记本电脑连接了两个摄像头:

  1. 内置网络摄像头。
  2. 外部 Logitech C270 网络摄像头。

当我在 VLC 或 Windows 相机应用程序中使用这两款相机时,它们都工作正常。然而,我在使用 OpenCV 时遇到了问题:

  • 当我使用
    cv2.VideoCapture(0)
    时,我收到错误: 错误:无法打开网络摄像头。
  • 当我使用
    cv2.VideoCapture(1)
    时,我没有看到任何错误,但显示的框架是全黑的。我怀疑索引 1 应该是我的外部摄像头的正确索引。

另外:

  • 当我运行索引为 1 的代码时,外部摄像头在其他程序中变得无法访问,这是有道理的,因为 OpenCV 控制了设备。
  • 我已确保Python 3.11在Windows设置下有权访问相机,并且相机使用指示器确认相机正在使用。
  • 该问题似乎与 VS Code 无关。直接从终端运行脚本时,我得到了相同的行为。

我想不通:

  1. 为什么我无法访问内置网络摄像头(索引 0)。
  2. 为什么我的外部摄像头(索引 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

我相信相机应该可以正常工作。我调整了我的 cv2 脚本以匹配我的相机支持的模式:

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()

结果还是一样。

python windows opencv camera webcam
1个回答
0
投票

事实证明,某些隐私措施(Avast Security)屏蔽了摄像头信息。

© www.soinside.com 2019 - 2024. All rights reserved.