|
| 1 | +# Import OpenCV |
| 2 | +import cv2 |
| 3 | + |
| 4 | +# Open a camera, similar to any other Python environment! In standard OpenCV, |
| 5 | +# you would use `cv2.VideoCapture(0)` or similar, and OpenCV would leverage the |
| 6 | +# host operating system to open a camera object and return it as a |
| 7 | +# `cv2.VideoCapture` object. However, we don't have that luxury in MicroPython, |
| 8 | +# so a camera driver is required instead. Any camera driver can be used, as long |
| 9 | +# as it implements the same methods as the standard OpenCV `cv2.VideoCapture` |
| 10 | +# class, such as `open()`, `read()`, and `release()` |
| 11 | +# |
| 12 | +# This example assumes a camera driver called `camera` has been initialized by a |
| 13 | +# `boot.py` script. See the example `boot.py` script for more details |
| 14 | +camera.open() |
| 15 | + |
| 16 | +# Loop to continuously read frames from the camera and display them |
| 17 | +while True: |
| 18 | + # Read a frame from the camera, just like any other Python environment! It |
| 19 | + # returns a tuple, where the first element is a boolean indicating success, |
| 20 | + # and the second element is the frame (NumPy array) read from the camera |
| 21 | + success, frame = camera.read() |
| 22 | + |
| 23 | + # Check if the frame was read successfully |
| 24 | + if success == False: |
| 25 | + print("Error reading frame from camera") |
| 26 | + break |
| 27 | + |
| 28 | + # Display the frame |
| 29 | + cv2.imshow(display, frame) |
| 30 | + |
| 31 | + # Check for key presses |
| 32 | + key = cv2.waitKey(1) |
| 33 | + |
| 34 | + # If the 'q' key is pressed, exit the loop |
| 35 | + if key == ord('q'): |
| 36 | + break |
| 37 | + |
| 38 | +# Release the camera, just like in any other Python environment! |
| 39 | +camera.release() |
0 commit comments