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

Beginners • Re: How to send two camera streams from RPi 5 over TCP connection with PiCamera2

$
0
0
There are many ways to go about this, so I'm slightly hesitant to make a suggestion. You could certainly put a "capture_array()" in a loop where you could (compress?) and send the frames. The outline code below might give you some ideas too.

Code:

from picamera2 import Picamera2from picamera2.encoders import MJPEGEncoderfrom picamera2.outputs import Outputfrom queue import Queuecams = [Picamera2(0), Picamera2(1)]queue = Queue()class QueueOutput(Output):    def __init__(self, cam_id, *args):        super().__init__(*args)        self.cam_id = cam_id    def outputframe(self, frame, keyframe=True, timestamp=None):        queue.put((self.cam_id, timestamp, frame))for i, cam in enumerate(cams):    config = cam.create_video_configuration({'format': 'RGB888', 'size': (640, 480)})    cam.configure(config)    cam.start_recording(MJPEGEncoder(), QueueOutput(i))while True:    cam_id, timestamp, frame = queue.get()    # Collect a frame frome each camera at a matching timestamp, then send them.
Here, both cameras should dump encoded frames and timestamps into a common queue. The loop at the end will need to collect corresponding frames, one from each camera, and send them to the socket connection you had earlier.
Doing it this way might be a better way than to send numpy arrays too since a numpy array containing an image will certainly be larger than the image itself, therefore it will be slower to receive the entire thing.

Also, look into how frequently you need to get new data. If every 1 second is enough, then sending the entire numpy array may not be an issue at all.

Statistics: Posted by memjr — Tue Jul 30, 2024 3:43 pm



Viewing all articles
Browse latest Browse all 5851

Trending Articles