OpenCV 在尝试 运行 VideoCapture(0) 时冻结

OpenCV freezes while trying to run VideoCapture(0)

我正在玩 OpenCV。我正在关注文档示例 (link)

我在 Ubuntu 上安装了 GTK 网络摄像头应用程序以验证我的网络摄像头是否正常工作。我可以启动网络摄像头并在 GTK 中看到视频反馈。

我在教程代码中添加了一些打印消息以查看我得到了什么。 我在这一行之前和之后添加了一个打印:cap = cv2.VideoCapture(0)

当 运行 Python 文件时,我得到的只是我在 cap = cv2.VideoCapture(0) 之前添加的印刷品,没有别的。

我尝试将 waitKey 增加到 20、40、100,但没有帮助。

有谁知道为什么它没有进一步显示框架?

我的代码:

import numpy as np 
import cv2 

videoFeed = cv2.VideoCapture(0) 
while (True): 
    ret, frame = videoFeed.read() 
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    cv2.imshow('Feed', frame_gray) 
    if cv2.waitKey(10) & 0xFF = ord("q"): 
        break 
videoFeed.release() 
cv2.destroyAllWindows() 

我的设置:

您在 if cv2.waitKey(10) & 0xFF = ord("q"): 的代码中有错误。不过你应该在这里遇到了语法错误。

import numpy as np 
import cv2 

videoFeed = cv2.VideoCapture(0) 
while (True): 
    ret, frame = videoFeed.read()
    if ret == False:
        print("Failed to retrieve frame")
        break 
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    cv2.imshow('Feed', frame_gray) 
    if cv2.waitKey(10) & 0xFF == ord("q"): 
        break 
videoFeed.release() 
cv2.destroyAllWindows() 

测试了您的代码。工作正常。唯一的其他建议是检查您的 Ubuntu 访客是否有权访问您的网络摄像头。如果您使用的是 VirtualBox,我记得在界面中看到了一个选项