When Raspberry Pi OS moved from being primarily based on Debian Buster, to Bullseye, the transition wasnβt the smoothest. For a few years Raspberry Pi OS used three instruments to entry the official Raspberry Pi digital camera. The primary two have been raspistill / raspivid, which supplied management and entry to the digital camera through the Linux terminal.Β
It was a strong and versatile means to work with the digital camera, each might produce video results and stream video with no additional work. The opposite means was a group created challenge referred to as PiCamera. Initially created by Dave Jones, Picamera grew from a group challenge into a vital instrument. Picamera supplied a purely Python means to work together with the digital camera, and being primarily based on Python it additionally meant that we might combine the digital camera into our initiatives.
With the transfer to Bullseye, we noticed Picamera sadly break. Raspberry Pi LTD even went so far as to supply a βlegacyβ model of Buster with Picamera and safety updates. This was a stopgap measure whereas its builders labored on Picamera2. With the September 2022 launch of Raspberry Pi OS we now have a working Picamera2 module that we are able to use in our initiatives.
On this how-to we will learn to use Picamera2βs somewhat splendid API [pdf] to seize pictures, file video, and work with the GPIO to react to enter as a method to seize a picture.
Β For the initiatives you will wantΒ
Connecting your Raspberry Pi Digital camera
The Raspberry Pi digital camera has been a part of the greatest Raspberry Pi equipment for nearly so long as the Pi has been with us. Practically each mannequin of Raspberry Pi has a digital camera (CSI) connector (the exception being the primary mannequin of Raspberry Pi Zero) and this meant that the digital camera quickly turned the will need to have accent to your Pi. The identical remains to be true, due to the official HQ digital camera providing a lot better picture high quality and a collection of interchangeable lenses.
Connecting any official digital camera to the Raspberry Pi is straightforward to do, simply observe these steps.
3. Insert the cable with the blue tab dealing with the USB / Ethernet port.
4. Gently slide the tabs down to lock the cable in place.
5. Safe / mount the digital camera in order that it doesn’t flop over and contact the Pi or its GPIO. One technique is to make use of modelling clay / blu tack.Β
Putting in Picamera2 software program
1. Boot the Pi.
2. Open a terminal and replace the put in software program.
sudo apt replace
sudo apt improve -y
3. Set up the Picamera2 Python3 module. For the most recent Raspberry Pi OS releases (September 2022 onwards) it comes pre-installed, however this command will even replace your model to the most recent launch.
sudo apt set up -y python3-picamera2
Taking a {Photograph} with Picamera2
Taking {a photograph} with Picamera2 is essentially the most primary activity you can carry out with the module. By design, it has been created to be easy to make use of, however beneath the simplicity is a fancy module that we are able to tweak to swimsuit our wants.
On this challenge we will seize a picture, utilizing a preview to border the shot.
1. Open Thonny. You could find it on the primary menu.
2. In a brand new file, import the Picamera2 module, together with the preview class. On a brand new line, import the time module. The Picamera2 module will present us with management of the digital camera and time is used to regulate how lengthy the preview picture will stay on display screen.
from picamera2 import Picamera2, Preview
import time
3. Create an object, picam2, which is used to reference the Picamera2 module and management the digital camera.
picam2 = Picamera2()
4. Create a brand new object, camera_config and use it to set the nonetheless picture decision (major) to 1920 x 1080. and a βlowresβ picture with a dimension of 640 x 480. This lowres picture is used because the preview picture when framing a shot.
camera_config = picam2.create_still_configuration(major={"dimension": (1920, 1080)}, lores={"dimension": (640, 480)}, show="lores")
5. Load the configuration.
picam2.configure(camera_config)
6. Begin the preview window and then begin the digital camera.
picam2.start_preview(Preview.QTGL)
picam2.begin()
7. Pause the code for 2 seconds.
time.sleep(2)
8. Seize a picture and reserve it as check.jpg.
picam2.capture_file("check.jpg")
9. Save the code as camera-test.py and click on Run to start out. A preview window will seem, use this to border your shot. If two seconds is simply too quick a delay, change the delay to fulfill your wants.
10. Open the system File Supervisor and double click on on check.jpg to view the picture.
Full Code Itemizing
from picamera2 import Picamera2, Preview
import time
picam2 = Picamera2()
camera_config = picam2.create_still_configuration(major={"dimension": (1920, 1080)}, lores={"dimension": (640, 480)}, show="lores")
picam2.configure(camera_config)
picam2.start_preview(Preview.QTGL)
picam2.begin()
time.sleep(2)
picam2.capture_file("check.jpg")
Recording a Video with Picamera2
HD video recording is one thing that we now take with no consideration. The identical is true for the Raspberry Pi due to quite a few fashions of official (and unofficial) cameras. With Picamera2 we are able to file video at varied resolutions utilizing completely different encoders.
On this challenge we’ll present the right way to file a easy 1080P video stream, whereas previewing the stream in a decrease decision window.
1. Open Thonny and create a brand new file. You could find Thonny on the primary menu.
2. Import the H264 encoder from the Picamera2 module.
from picamera2.encoders import H264Encoder
3. Import the Picamera2 module, together with the preview class. Subsequent import the time module.
from picamera2 import Picamera2, Preview
import time
4. Create an object, picam2, which is used to reference the Picamera2 module and management the digital camera.
picam2 = Picamera2()
5. Create a brand new object, video_config and use it to set the nonetheless picture decision (major) to 1920 x 1080. and a βlowresβ picture with a dimension of 640 x 480. This lowres picture is used because the preview picture when framing a shot.
video_config = picam2.create_video_configuration(major={"dimension": (1920, 1080)}, lores={"dimension": (640, 480)}, show="lores")
6. Load the configuration.
picam2.configure(video_config)
7. Set the bitrate of the H264 encoder.
encoder = H264Encoder(bitrate=10000000)
8. Set the output file to check.h264. This can create a file containing the video.
output = "check.h264"
9. Begin the preview window, then begin recording utilizing the encoder settings and saving the video to the output file.
picam2.start_preview(Preview.QTGL)
picam2.start_recording(encoder, output)
10. Use a sleep to file ten seconds of video. The earlier recording command is just not a blocking line of code. Utilizing a sleep command, we hold the recording from stopping after a fraction of a second.
time.sleep(10)
11. Cease the digital camera recording and shut the preview window.
picam2.stop_recording()
picam2.stop_preview()
12. Save the code as video-test.py and click on Run to start out. The preview window will seem and you’ve got ten seconds to file a video.
13. View the video.You may get there within the File Supervisor by finding check.h264. And double-clicking on the video file to play it in VLC.Β
Full Code Itemizing
from picamera2.encoders import H264Encoder
from picamera2 import Picamera2, Preview
import time
picam2 = Picamera2()
video_config = picam2.create_video_configuration(major={"dimension": (1920, 1080)}, lores={"dimension": (640, 480)}, show="lores")
picam2.configure(video_config)
encoder = H264Encoder(bitrate=10000000)
output = "check.h264"
picam2.start_preview(Preview.QTGL)
picam2.start_recording(encoder, output)
time.sleep(10)
picam2.stop_recording()
picam2.stop_preview()
Utilizing a Set off to Take a Image on Raspberry Pi
Digital camera triggers are a traditional Raspberry Pi challenge. They’re used to seize pictures / video of animals, intruders or to prank unwilling relations. A set off generally is a sensor resembling a Passive Infrared (PIR) motion sensor, an ultrasonic sensor, or in our case, a easy push button.
On this challenge we’ll create a easy set off activated digital camera entice. We press the button, body the shot utilizing the preview window, and the file then mechanically saves to our Pi utilizing the present time and date as a filename.
The wiring for this challenge is easy. The button is related to GPIO17 and GND through a breadboard and two feminine to male wires.
1. Open Thonny and create a brand new file. You could find Thonny on the primary menu.Β
2. Import the Picamera2 module, together with the preview class. Subsequent import the time module.
from picamera2 import Picamera2, Preview
import time
3. Import the datetime, GPIO Zero and Sign modules. Datetime is used to generate a timestamp for our picture filenames. GPIO Zero is used for a easy button interface. Sign is used to cease the Python code from exiting.
from datetime import datetime
from gpiozero import Button
from sign import pause
4. Create an object, picam2, which is used to reference the Picamera2 module and management the digital camera.
picam2 = Picamera2()
5. Create an object, button, and use the thing to retailer the GPIO pin to which our button is related.
button = Button(17)
6. Create a brand new object, camera_config and use it to set the nonetheless picture decision (major) to 1920 x 1080. and a βlowresβ picture with a dimension of 640 x 480. This lowres picture is used because the preview picture when framing a shot.
camera_config = picam2.create_still_configuration(major={"dimension": (1920, 1080)}, lores={"dimension": (640, 480)}, show="lores")
7. Load the configuration.
picam2.configure(camera_config)
8. Create a operate, seize(), to retailer a collection of instructions that might be run when the set off is pressed. Code inside the operate is mechanically indented to point out that it belongs to the operate.
def seize():
9. Begin a preview window. This can allow us to border our picture.
picam2.start_preview(Preview.QTGL)
10. Create an object, timestamp, and use it to retailer the time and date of the set off occasion.
timestamp = datetime.now().isoformat()
11. Begin the digital camera, then pause for 2 seconds to permit time to border the picture.
picam2.begin()
time.sleep(2)
12. Set the seize file, in the end the picture file, to make use of the present timestamp because the filename.
picam2.capture_file('/dwelling/pi/%s.jpg' % timestamp)
13. Lastly within the operate cease the preview, and cease the digital camera.
picam2.stop_preview()
picam2.cease()
14. Use GPIO Zeroβs button class to react to a button press by calling our βseizeβ operate. Lastly use pause() to stop the code from ending.
button.when_pressed = seize
pause()
15. Save the code as trigger-test.py and click on Run to start out the code.
16. Press the button to start out the digital camera, and to take a picture.
17. Open the system File Supervisor and double click on on the picture to view.
Full Code Itemizing
from picamera2 import Picamera2, Preview
import time
from datetime import datetime
from gpiozero import Button
from sign import pause
picam2 = Picamera2()
button = Button(17)
camera_config = picam2.create_still_configuration(major={"dimension": (1920, 1080)}, lores={"dimension": (640, 480)}, show="lores")
picam2.configure(camera_config)
def seize():
picam2.start_preview(Preview.QTGL)
timestamp = datetime.now().isoformat()
picam2.begin()
time.sleep(2)
picam2.capture_file('/dwelling/pi/%s.jpg' % timestamp)
picam2.stop_preview()
picam2.cease()
button.when_pressed = seize
pause()