简体   繁体   中英

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. Here what I'm using:

  • Python: 3.9.1
  • opencv-contrib-python: 4.5.1.48
  • MacOS Big Sur 11.2.3
  • External webcam: Logitech 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. I'm able to see the webcam image when i'm using FaceTime or 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

Thank you for your help!

If your code works with main webcam and on linux, that the problem is on Mac permission, not 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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