Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4140

Camera board • Re: "Or Better"?

$
0
0
So, I'm running the following script:
Basically 3k resolution at 15fps at 10mbit

Code:

#!/usr/bin/python3# Encode and store a pair of side-by-side 1500x1500 image streams to give 3000x1500 video at ~15fps.from picamera2 import Picamera2, MappedArrayfrom picamera2.encoders import H264Encoderfrom picamera2.outputs import FfmpegOutputfrom libcamera import Transformimport timeimport osimport sys# Ensure the "pop" directory existsoutput_dir = "pop"# Get the name from the argumentsif len(sys.argv) > 1:    video_name = sys.argv[1]else:    video_name = "fusion.mp4"  # fallback name# Set the full path including the "pop" foldervideo_name = os.path.join(output_dir, video_name)fps = 15height = 1500leftsize = (2 * height, height)  # define 2:1 format for left main streamrightsize = (height, height)     # define 1:1 right main streamFrameTime = int(1000000 / fps)# select fastest full format sensor mode# which is 5 for V2 camera: 1 for v3: 2 for HQbinned2x2ffmode = 2# adopt a mixture of callback methods to repackage downsampled left image# into left half of stream, and also package 1:1 right image into the other half. def mergeviews(request):    request_2 = cam2_request    if request_2 is None:        return    request_2.acquire()    with MappedArray(request, "main") as m1, MappedArray(request_2, "main") as m2:        # fill left half of original left array with half-width downsampled left camera view        m1.array[:, :height] = m1.array[:, ::2]        # fill right half of left array with 1:1 right camera view        m1.array[:, height:] = m2.array    request_2.release()def save_request(request):    # Store most recent request for use by other camera    global cam2_request    if cam2_request is not None:        cam2_request.release()    request.acquire()    cam2_request = requestlcam = Picamera2(0)rcam = Picamera2(1)# Select best modemode = lcam.sensor_modes[2]# Calculate dimensions for crop of central square full height region of sensorsensorsize = lcam.camera_properties['PixelArraySize']sensorwidth = sensorsize[0]sensorheight = sensorsize[1]x = (sensorwidth - sensorheight) // 2crop = (x, 0, sensorheight, sensorheight)# Configure both cameras to full format binned 2x2 mode and set the main stream to be 2:1 wide in left cam,# filled with the stretched central square zone of the sensor, right cam stream is set for equivalent square zone at 1:1.lconfig = lcam.create_video_configuration(sensor={"output_size": mode['size'], 'bit_depth': mode['bit_depth']},                                          main={"format": "RGB888", "size": leftsize},                                          controls={"ScalerCrop": crop, "FrameDurationLimits": (FrameTime, FrameTime)},                                          transform=Transform(hflip=True, vflip=True),                                          queue=False)rconfig = lcam.create_video_configuration(sensor={"output_size": mode['size'], 'bit_depth': mode['bit_depth']},                                          main={"format": "RGB888", "size": rightsize},                                          controls={"ScalerCrop": crop, "FrameDurationLimits": (FrameTime, FrameTime)},                                          transform=Transform(hflip=True, vflip=True),                                          )lcam.configure(lconfig)rcam.configure(rconfig)# Prepare to encode and output to an mp4 fileencoder = H264Encoder(10000000)output = FfmpegOutput(video_name)lcam.start()rcam.start()cam2_request = Nonercam.pre_callback = save_requestlcam.pre_callback = mergeviews# Short delay to allow Ae and AWB to settle, and should also avoid recording unprocessed first frametime.sleep(1)# Start recording indefinitely until stopped manuallylcam.start_recording(encoder, output)# Infinite loop to keep recordingtry:    while True:        time.sleep(1)except KeyboardInterrupt:    # Stop recording when interrupted (Ctrl + C)    lcam.stop_recording()
This issue is that I can only record approximately 15-30 seconds of video before it stops with an error, probably memory related. I'm running the 2gb model of the pi5. Is there I way to manage the memory to keep the video recording perpetually?

Code:

Exception during process_requests()    request_2.acquire()  File "/usr/lib/python3/dist-packages/picamera2/request.py", line 120, in acquire    raise RuntimeError("CompletedRequest: acquiring lock with ref_count 0")RuntimeError: CompletedRequest: acquiring lock with ref_count 0^Ccc^CTraceback (most recent call last):  File "/var/www/html/fusion.py", line 111, in <module>    time.sleep(1)KeyboardInterruptDuring handling of the above exception, another exception occurred:Traceback (most recent call last):  File "/var/www/html/fusion.py", line 114, in <module>    lcam.stop_recording()  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1811, in stop_recording    self.stop()  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1228, in stop    self.dispatch_functions([self.stop_], wait=True, immediate=True)  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1354, in dispatch_functions    return job.get_result(timeout=timeout) if wait else job           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/usr/lib/python3/dist-packages/picamera2/job.py", line 79, in get_result    return self._future.result(timeout=timeout)           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/usr/lib/python3.11/concurrent/futures/_base.py", line 451, in result    self._condition.wait(timeout)  File "/usr/lib/python3.11/threading.py", line 320, in wait    waiter.acquire()KeyboardInterruptError writing trailer of pop/fusion.mp4: Immediate exit requestedError closing file pop/fusion.mp4: Immediate exit requested

Statistics: Posted by MRV — Fri Oct 25, 2024 8:21 am



Viewing all articles
Browse latest Browse all 4140

Trending Articles