Created
November 16, 2025 17:19
-
-
Save maxpromer/d5462161e95b84adf4bbddbc6e4b01a7 to your computer and use it in GitHub Desktop.
Raspberry Pi Camera Module 3 autofocus settings with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from picamera2 import Picamera2 | |
| import cv2 | |
| picam2 = Picamera2() | |
| # Set up Auto Focus | |
| config = picam2.create_preview_configuration() | |
| picam2.configure(config) | |
| picam2.start() | |
| # Enable Continuous Auto Focus | |
| picam2.set_controls({"AfMode": 2, "AfTrigger": 0}) | |
| try: | |
| while True: | |
| # Capture frame from the camera | |
| frame = picam2.capture_array() | |
| # Convert color from RGB to BGR for OpenCV | |
| frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) | |
| # Display the frame | |
| cv2.imshow("Auto Focus Camera", frame) | |
| # Press 'q' to exit | |
| if cv2.waitKey(1) & 0xFF == ord('q'): | |
| break | |
| finally: | |
| # Close all OpenCV windows | |
| cv2.destroyAllWindows() | |
| # Stop the camera | |
| picam2.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment