This code resulted in errors:Try adding a short delay before starting the recording.We need only do something about that 1st frame being stretched.
As per this version, which also has a more generic way of defining the ScalerCrop to allow for different camera versions.Code:
#!/usr/bin/python3# Encode and store a pair of side by side 1500x1500 image streams to give 3000x1500 video at ~15fps.# Borrows callback techniques from stereo_preview.py example in Picamera github repository.from picamera2 import Picamera2, MappedArrayfrom picamera2.encoders import H264Encoderfrom picamera2.outputs import FfmpegOutputfrom libcamera import Transformimport timefps=15duration=20height=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[binned2x2ffmode]# 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("Test_left_right_combining.mp4")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) # Record 20 seconds worthlcam.start_recording(encoder,output)time.sleep(duration) lcam.stop_recording()
Code:
p1/i2c@80000/imx477@1a - Selected sensor format: 2028x1520-SRGGB12_1X12 - Selected CFE format: 2028x1520-PC1RException during process_requests()Traceback (most recent call last): File "/usr/lib/python3/dist-packages/picamera2/previews/null_preview.py", line 85, in handle_request picam2.process_requests(self) File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1273, in process_requests self.pre_callback(req) File "/pop/fusion2.py", line 31, in mergeviews 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 0Exception in thread Thread-2 (thread_func):Traceback (most recent call last): File "/usr/lib/python3.11/threading.py", line 1038, in _bootstrap_inner self.run() File "/usr/lib/python3.11/threading.py", line 975, in runRuntimeError: CompletedRequest: acquiring lock with ref_count 0
Statistics: Posted by MRV — Mon Oct 21, 2024 5:38 am