[英]Python OpenCv can open external webcam but can't show image
我正在尝试从 Mac 上的外部网络摄像头读取 VideoCapture 和图像,但我无法显示图像。 这是我正在使用的:
我从命令行运行脚本,我很确定我提供了所有需要的权限。 使用 Linux 在我朋友的笔记本电脑上一切正常。 当我使用 FaceTime 或 Photo Booth 时,我可以看到网络摄像头图像。
这是代码:
class Camera: def __init__(self, camera_index: int): self._camera_index = camera_index self._capture = None self._open_capture() def take_world_image(self): return self._get_camera_frame() def _get_camera_frame(self): print(self._capture.read) opened_successfully, current_frame = self._capture.read() if not opened_successfully: raise InvalidCameraConfigException return current_frame def _open_capture(self): self._capture = cv2.VideoCapture(self._camera_index) def _close_capture(self): self._capture.release() if __name__ == "__main__": camera = Camera(1) while True: image = camera.take_world_image() cv2.imshow("image", image) if cv2.waitKey(1) & 0xFF == ord("q"): break cv2.destroyAllWindows()
open_successfully 返回 True,但这是我得到的图像:图像
谢谢您的帮助!
如果您的代码适用于主网络摄像头和 linux,则问题出在 Mac 权限上,而不是 python。
尝试不要在 iDE 上运行脚本,而是在 Mac 上打开终端,进入包含 .py 文件的文件夹并运行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.