Skip to content

Commit 2cfcc16

Browse files
committed
More updates to the examples
Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
1 parent a52e4a2 commit 2cfcc16

14 files changed

+118
-16
lines changed

examples/capture_average.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
import numpy as np
88
from PIL import Image
99

10-
from picamera2 import Picamera2, Preview
10+
from picamera2 import MappedArray, Picamera2, Preview
1111

1212
picam2 = Picamera2()
1313
picam2.start_preview(Preview.NULL)
14-
capture_config = picam2.create_still_configuration()
14+
# Buffer count of 2 will reduce frame drops (default is 1 otherwise)
15+
capture_config = picam2.create_still_configuration(buffer_count=2)
1516
picam2.configure(capture_config)
1617

1718
picam2.start()
@@ -25,12 +26,15 @@
2526
imgs = 20 # Capture 20 images to average
2627
sumv = None
2728
for _ in range(imgs):
28-
if sumv is None:
29-
sumv = np.longdouble(picam2.capture_array())
30-
img = Image.fromarray(np.uint8(sumv))
31-
img.save("original.tif")
32-
else:
33-
sumv += np.longdouble(picam2.capture_array())
29+
with picam2.captured_request() as request:
30+
# Using MappedArray saves an array copy
31+
with MappedArray(request, 'main') as m:
32+
if sumv is None:
33+
sumv = np.longdouble(m.array)
34+
img = Image.fromarray(np.uint8(m.array))
35+
img.save("original.tif")
36+
else:
37+
sumv += m.array
3438

3539
img = Image.fromarray(np.uint8(sumv / imgs))
3640
img.save("averaged.tif")

examples/capture_dng_to_buffer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/python3
22

3+
# Capture a DNG to a BytesIO.
4+
35
import io
46
import time
57

examples/capture_mjpeg.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
from picamera2 import Picamera2
55
from picamera2.encoders import JpegEncoder
66

7+
# This is using the software JPEG encoder. On lower powered devices, the
8+
# MJPEGEncoder would be better as it uses the hardware encoder. See
9+
# capture_mjpeg_v4l2.py.
10+
11+
712
picam2 = Picamera2()
813
video_config = picam2.create_video_configuration(main={"size": (1920, 1080)})
914
picam2.configure(video_config)

examples/capture_mjpeg_timestamp.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
from picamera2 import Picamera2
55
from picamera2.encoders import JpegEncoder
66

7+
# This is using the software JPEG encoder. On lower powered devices, the
8+
# MJPEGEncoder would be better as it uses the hardware encoder. See
9+
# capture_mjpeg_v4l2.py.
10+
11+
712
picam2 = Picamera2()
813
video_config = picam2.create_video_configuration(main={"size": (1920, 1080)})
914
picam2.configure(video_config)

examples/capture_motion.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/usr/bin/python3
22

3+
# This example writes unformatted H.264 bitstream using the FileOutput. Mostly
4+
# we would now recommend using the PyavOutput to write mp4 files directly. See
5+
# capture_motion_improved.py.
6+
37
import time
48

59
import numpy as np
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/python3
2+
3+
import time
4+
5+
import numpy as np
6+
7+
from picamera2 import Picamera2
8+
from picamera2.encoders import H264Encoder
9+
from picamera2.outputs import PyavOutput
10+
11+
lsize = (320, 240)
12+
picam2 = Picamera2()
13+
main = {"size": (1280, 720), "format": "RGB888"}
14+
lores = {"size": lsize, "format": "YUV420"}
15+
video_config = picam2.create_video_configuration(main, lores=lores)
16+
picam2.configure(video_config)
17+
encoder = H264Encoder(bitrate=1000000)
18+
picam2.start()
19+
20+
w, h = lsize
21+
prev = None
22+
encoding = False
23+
ltime = 0
24+
25+
while True:
26+
cur = picam2.capture_array("lores")[:h, :w]
27+
if prev is not None:
28+
# Measure pixels differences between current and
29+
# previous frame
30+
mse = np.square(np.subtract(cur, prev)).mean()
31+
if mse > 7:
32+
if not encoding:
33+
encoder.output = PyavOutput(f"{int(time.time())}.mp4")
34+
picam2.start_encoder(encoder)
35+
encoding = True
36+
print("New Motion", mse)
37+
ltime = time.time()
38+
else:
39+
if encoding and time.time() - ltime > 2.0:
40+
picam2.stop_encoder()
41+
encoding = False
42+
prev = cur

examples/capture_stream_udp.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/usr/bin/python3
22

3+
# Send an H.264 bitstream over a UDP socket to a client. Mostly we would
4+
# recommend using PyavOutput so that you can send more widely supported
5+
# formats. See capture_stream_udp_improved.py.
6+
37
import socket
48
import time
59

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/python3
2+
3+
# Send an MPEG2 transport stream over a socket using PyavOutput.
4+
5+
import socket
6+
import time
7+
8+
from picamera2 import Picamera2
9+
from picamera2.encoders import H264Encoder
10+
from picamera2.outputs import PyavOutput
11+
12+
picam2 = Picamera2()
13+
video_config = picam2.create_video_configuration({"size": (1280, 720)})
14+
picam2.configure(video_config)
15+
encoder = H264Encoder(1000000)
16+
17+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
18+
sock.connect(("REMOTEIP", 10001))
19+
picam2.start_recording(encoder, PyavOutput(f"pipe:{sock.fileno()}", format="mpegts")) # noqa
20+
time.sleep(20)
21+
picam2.stop_recording()

examples/capture_video.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
from picamera2 import Picamera2
55
from picamera2.encoders import H264Encoder
66

7+
# This example saves an unformatted H.264 file. Mostly we would recommend
8+
# using the PyavOutput so that an mp4 file could be saved directly instead.
9+
10+
711
picam2 = Picamera2()
812
video_config = picam2.create_video_configuration()
913
picam2.configure(video_config)

examples/easy_capture.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/usr/bin/python3
22

3+
# This is the most high level way to capture images and save them. Howwever,
4+
# the API gives you less control over the camera system so we would recommend
5+
# it only to folks with the simplest use-cases.
6+
37
from picamera2 import Picamera2
48

59
picam2 = Picamera2()

0 commit comments

Comments
 (0)