Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions examples/capture_video_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,34 @@
from picamera2 import Picamera2
from picamera2.encoders import Encoder

size = (2592, 1944)
size = (640, 360)
picam2 = Picamera2()
video_config = picam2.create_video_configuration(raw={"format": 'SGBRG10', 'size': size})
raw = {"format": 'SGBRG10', 'size': size}
controls = {'FrameRate': 15}
video_config = picam2.create_video_configuration(raw=raw, controls=controls)
picam2.configure(video_config)
size = video_config['raw']['size']
stride = video_config['raw']['stride']
print("Size after config:", size, "stride", stride)
picam2.encode_stream_name = "raw"
encoder = Encoder()

picam2.start_recording(encoder, 'test.raw', pts='timestamp.txt')
time.sleep(5)
picam2.start_recording(encoder, '/dev/shm/test.raw', pts='/dev/shm/timestamp.txt')
time.sleep(3)
picam2.stop_recording()

buf = open("test.raw", "rb").read(size[0] * size[1] * 2)
arr = np.frombuffer(buf, dtype=np.uint16).reshape((size[1], size[0]))
buf = open("/dev/shm/test.raw", "rb").read(stride * size[1])
arr = np.frombuffer(buf, dtype=np.uint8).reshape((size[1], stride))
arr = arr[:, :2 * size[0]].view(np.uint16)

# Scale 10 bit / channel to 8 bit per channel
im = Image.fromarray((arr * ((2**8 - 1) / (2**10 - 1))).astype(np.uint8))
im.save("test-8bit.tif")
im.save("/dev/shm/test-8bit.tif")

# Note - this will look very dark, because it's 10 bit colour depth of image, in
# a 16 bit / channel tiff
im2 = Image.fromarray(arr)
im2.save("test-16bit.tif")
im2.save("/dev/shm/test-16bit.tif")

# Create DNG file from frame, based on https://github.com/schoolpost/PiDNG/blob/master/examples/raw2dng.py
# Tested loading of DNG in darktable
Expand All @@ -50,4 +56,4 @@
t.set(Tag.DNGVersion, DNGVersion.V1_4)
t.set(Tag.DNGBackwardVersion, DNGVersion.V1_2)
r.options(t, path="", compress=False)
r.convert(arr, filename="test")
r.convert(arr, filename="/dev/shm/test")
4 changes: 3 additions & 1 deletion picamera2/encoders/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,11 @@ def _start(self):
"RGB888": "bgr24",
"XBGR8888": "rgba",
"XRGB8888": "bgra"}
pix_fmt = FORMAT_TABLE[self._format]
pix_fmt = FORMAT_TABLE.get(self._format)
rate = Fraction(1000000, self._framerate)
for out in self._output:
if pix_fmt is None and out.needs_add_stream:
raise RuntimeError(f"Output does not support {self._format}")
out._add_stream("video", "rawvideo", pix_fmt=pix_fmt, rate=rate,
width=self.width, height=self.height)

Expand Down
1 change: 1 addition & 0 deletions picamera2/outputs/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(self, pts=None):
self.recording = False
self.ptsoutput = pts
self.needs_pacing = False
self.needs_add_stream = False

def start(self):
"""Start recording"""
Expand Down
1 change: 1 addition & 0 deletions picamera2/outputs/pyavoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self, output_name, format=None, pts=None, options=None):
self._options = options
# A user can set this to get notifications of failures.
self.error_callback = None
self.needs_add_stream = True

def _add_stream(self, encoder_stream, codec_name, **kwargs):
# The output container that does the muxing needs to know about the streams for which packets
Expand Down
1 change: 1 addition & 0 deletions picamera2/outputs/splittableoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self, output=None, *args, **kwargs):
self._new_output = None
self._split_done = Event()
self._streams = []
self.needs_add_stream = True

def split_output(self, new_output, wait_for_keyframe=True):
"""
Expand Down
1 change: 1 addition & 0 deletions tests/test_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ examples/capture_to_buffer.py
examples/capture_video.py
examples/capture_video_multiple.py
examples/capture_video_multiple_2.py
examples/capture_video_raw.py
examples/controls.py
examples/controls_2.py
examples/display_transform_qtgl.py
Expand Down