我正在尝试测试是否可以在 OpenCV 中传输相机信号。我的笔记本电脑连接了两个摄像头:
当我在 VLC 或 Windows 相机应用程序中使用这两款相机时,它们都工作正常。然而,我在使用 OpenCV 时遇到了问题:
cv2.VideoCapture(0)
时,我收到错误:
错误:无法打开网络摄像头。cv2.VideoCapture(1)
时,我没有看到任何错误,但显示的框架是全黑的。我怀疑索引 1 应该是我的外部摄像头的正确索引。另外:
我想不通:
这是我的代码:
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()
结果还是一样。
事实证明,某些隐私措施(Avast Security)屏蔽了摄像头信息。