簡體   English   中英

Python OpenCv 可以打開外部網絡攝像頭但無法顯示圖像

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

我正在嘗試從 Mac 上的外部網絡攝像頭讀取 VideoCapture 和圖像,但我無法顯示圖像。 這是我正在使用的:

  • Python:3.9.1
  • opencv-contrib-python:4.5.1.48
  • MacOS 大蘇爾 11.2.3
  • 外接網絡攝像頭:羅技 C905

我從命令行運行腳本,我很確定我提供了所有需要的權限。 使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM