简体   繁体   English

Python OpenCv 可以打开外部网络摄像头但无法显示图像

[英]Python OpenCv can open external webcam but can't show image

I'm trying to read and image from VideoCapture from an external webcam on my mac, but I'm not able to display the image.我正在尝试从 Mac 上的外部网络摄像头读取 VideoCapture 和图像,但我无法显示图像。 Here what I'm using:这是我正在使用的:

  • Python: 3.9.1 Python:3.9.1
  • opencv-contrib-python: 4.5.1.48 opencv-contrib-python:4.5.1.48
  • MacOS Big Sur 11.2.3 MacOS 大苏尔 11.2.3
  • External webcam: Logitech C905外接网络摄像头:罗技 C905

I'm running the script from the command line, and I'm pretty sure that I gave all the permissions needed.我从命令行运行脚本,我很确定我提供了所有需要的权限。 Everything is working fine on my friend's laptop using Linux.使用 Linux 在我朋友的笔记本电脑上一切正常。 I'm able to see the webcam image when i'm using FaceTime or Photo Booth.当我使用 FaceTime 或 Photo Booth 时,我可以看到网络摄像头图像。

Here's the code:这是代码:

 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 return True, but this is the image I'm getting: image open_successfully 返回 True,但这是我得到的图像:图像

Thank you for your help!谢谢您的帮助!

If your code works with main webcam and on linux, that the problem is on Mac permission, not python.如果您的代码适用于主网络摄像头和 linux,则问题出在 Mac 权限上,而不是 python。

Try instead of running the script on an iDE open it up Terminal on Mac into the folder that contained.py file and run it.尝试不要在 iDE 上运行脚本,而是在 Mac 上打开终端,进入包含 .py 文件的文件夹并运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM