How To Use Raspberry Pi Camera Module 3 with Python Code
of raspberry pi have a lot of best accessories One thing that will definitely appear on that list is the new camera module 3. Raspberry Pi Camera Module 3 reviewsaid we love fast autofocus and HDR images and wanted to share these features in this how-to.
If you have never used a Raspberry Pi camera before, Picamera2 Beginner’s Guide A great introduction to taking great pictures with the Pi. In this how-to, explore different ways to use the Camera Module 3’s focus system with the Picamera2 and learn how to capture HDR images using a quick and simple script that automates the process. If you are not good with Python, you can also use Camera Module 3 to control it. Via terminal libcamera.
Know your autofocus
Autofocus has three modes of operation.
- manual: To change the focus of the lens, the user must specify a LensPosition control. A value of zero produces infinite focus. Values up to 10 are accepted, with 10 setting the focus to 1/10 meter (10CM).
- Automatic: Typical autofocus using AfTrigger to start the autofocus cycle.
- continue: The camera looks for a target and refocuses on it when the algorithm finds it.
Project 1: Using Continuous Focus
Continuous focus uses an algorithm to find the target in the image stream. Algorithms find and lock onto targets, whether they are near (about 10cm) or far away. But how do we use it? Let’s run a quick test. Use continuous focus mode to find the best possible focus for the target. It would be nice to have an object that can be held up to the camera. I used dollar bills.
1. From the main menu, open Programming >> Thonny.
2. Import Picamera2.
from picamera2 import Picamera2
3. Import the libcamera control class. This allows you to configure the camera to suit your requirements.
from libcamera import controls
Four. Create an object picam2. Use this as the link between your code and your camera.
picam2 = Picamera2()
Five. Start the preview window. Preview is where you see the output of your camera.
picam2.start(show_preview=True)
6. Set AfMode (autofocus mode) to continuous.
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})
7. Save the code as AFtest.py.
8. [実行]Click to start the code. A preview window appears. Move objects, select dollar bills around the frame, and watch the focus shift. Try moving the object closer to the lens. The closest focus is 10 cm.
complete code listing
from picamera2 import Picamera2
from libcamera import controls
picam2 = Picamera2()
picam2.start(show_preview=True)
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})
Project 2: Setting Focus Manually
Sometimes you need fixed focus to get great shots. After all, you don’t want to capture a blurry mess. Fixing the focus is relatively easy. In fact, it’s simple enough that most of the code from the previous example can be reused.
1. Use Save As In the previous example to Create a new file named ManualFocusTest.py
2. Change the last line to use LensPositionin which case we set the infinite focus value to 0.0.
picam2.set_controls({"AfMode": controls.AfModeEnum.Manual, "LensPosition": 0.0})
3. Run your code. Far objects are sharp in focus, but blurry in close-ups.
Four. Change the LensPosition value to 0.5. This gives a focal length of about 50 cm.
Five. Save and run your code. Move an object closer or further away from the camera. Notice how the focus sharpens around 50 cm.
complete code listing
from picamera2 import Picamera2
from libcamera import controls
picam2 = Picamera2()
picam2.start(show_preview=True)
picam2.set_controls({"AfMode": controls.AfModeEnum.Manual, "LensPosition": 0.5})
Project 3: Fast focus of multiple images
There are times when it is necessary to acquire clear images continuously, such as bird cams, school sports day, home security, etc. Luckily, you can configure the Camera Module 3 to take a series of high-speed images and set the autofocus to fast.
1. Create a new file named AfFastFocus.py.
2. Import Picamera2.
from picamera2 import Picamera2
3. Import the libcamera control class. This allows you to configure the camera to suit your requirements.
from libcamera import controls
Four. Create an object picam2. Use this as the link between your code and your camera.
picam2 = Picamera2()
Five. Start the preview window. Preview is where you see the output of your camera.
picam2.start(show_preview=True)
6. Set Autofocus Mode to Continuous and AfSpeed to Fast.
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous, "AfSpeed": controls.AfSpeedEnum.Fast})
7. Set your camera to capture three files. There is a 0.5 second delay between each shot. The filename “fastfocus.jpg” appends 0, 1, 2 to each file. Specify three files in numerical order.
picam2.start_and_capture_files("fastfocus{:d}.jpg", num_files=3, delay=0.5)
8. Close the preview window.
picam2.stop_preview()
9. Close the camera connection.
picam2.stop()
Ten. Save and run your code. Hold the object at three different distances to the camera and watch the focus change. The preview window freezes when the camera takes a shot, freeing up for the next shot. After 3 photos are taken, the preview window closes.
complete code listing
from picamera2 import Picamera2
from libcamera import controls
picam2 = Picamera2()
picam2.start(show_preview=True)
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous, "AfSpeed": controls.AfSpeedEnum.Fast})
picam2.start_and_capture_files("fastfocus-test{:d}.jpg", num_files=3, delay=0.5)
picam2.stop_preview()
picam2.stop()
Capturing HDR images with Picamera2
Easily capture HDR (High Dynamic Range) images. library cameraJust pass the –hdr argument when running the command. However, Picamera2 requires you to run a terminal command before running your Python code.
HDR extends the dynamic lightness range of an image. With HDR you get deeper darks and brighter images. It works by capturing multiple images of the same scene, each with a different exposure. These images are combined into a single image containing the entire range. Camera module 3 can capture HDR images, but not full 12MP resolution. Instead you get a 3MP image of him with a resolution of 2304 x 1296 pixels.
in our test, AfFastFocus.py Capture a project, a series of HDR images. It also uses Python’s OS library to run terminal commands to turn HDR settings on or off without user interaction. This means remembering to turn the HDR setting on and off.
1. Create a new file named HDRAfFastFocus.py.
2. Import Picamera2.
from picamera2 import Picamera2
3. Import the libcamera control class. This allows you to configure the camera to suit your requirements.
from libcamera import controls
Four. Import OS modulesThis allows your code to interact with the underlying operating system, in this case the Raspberry Pi OS (Linux).
import os
Five. Create an object picam2. Use this as the link between your code and your camera.
picam2 = Picamera2()
6. Configure the camera to use HDR using the system function of the os module. Picamera2 supports HDR, but not directly in the module. The issue is in V4L2, the kernel interface between the camera and the Linux video system. Currently, this camera is not ready to support HDR, so you will need to do this simple workaround to make it available for Picamera2.
7. Print messages to the Python shell Notifies you that HDR is on.
print("Setting HDR to ON")
8. Start the preview window. Preview is where you see the output of your camera.
picam2.start(show_preview=True)
9. Set autofocus mode to continuous When Set AfSpeed to Fast.
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous, "AfSpeed": controls.AfSpeedEnum.Fast})
Ten. Set the camera to capture 3 files, with a 1 second delay between each shot. The file name “HDRfastfocus.jpg” appends 0 to each file, then 1 and 2. Specify three files in numerical order. Double the delay between each shot to give the camera time to save the previous image and focus on the next image. I tested with a delay of 0.5 and sometimes shots were a little too blurry.
picam2.start_and_capture_files("HDRfastfocus{:d}.jpg", num_files=3, delay=1)
11. Close the preview window.
picam2.stop_preview()
12. Close the camera connection.
picam2.stop()
13. print a message to the user That HDR is turned off and I use os.system to run the command.
print("Setting HDR to OFF")
os.system("v4l2-ctl --set-ctrl wide_dynamic_range=0 -d /dev/v4l-subdev0")
14. Save and run your code. Hold the object at three different distances to the camera and watch the focus change. The preview window freezes when the camera takes a shot, freeing up for the next shot. After 3 photos are taken, the preview window closes.
complete code listing
from picamera2 import Picamera2
from libcamera import controls
import os
picam2 = Picamera2()
os.system("v4l2-ctl --set-ctrl wide_dynamic_range=1 -d /dev/v4l-subdev0")
print("Setting HDR to ON")
picam2.start(show_preview=True)
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous, "AfSpeed": controls.AfSpeedEnum.Fast})
picam2.start_and_capture_files("HDRfastfocus{:d}.jpg", num_files=3, delay=1)
picam2.stop_preview()
picam2.stop()
print("Setting HDR to OFF")
os.system("v4l2-ctl --set-ctrl wide_dynamic_range=0 -d /dev/v4l-subdev0")