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

Python • Creating two video streams with two cameras

$
0
0
Hi,

I am looking for some help, as I am stuck.

I have a Raspberry Pi 5 with two PiCam 3 cameras. I am trying to stream from both cameras, as I want to be able to use either one or both from a web browser. Which is why I was looking at the code below.

I have used the following code, which is direct from the samples:

Code:

#!/usr/bin/python3# This is the same as mjpeg_server.py, but uses the h/w MJPEG encoder.import ioimport loggingimport socketserverfrom http import serverfrom threading import Conditionfrom picamera2 import Picamera2from picamera2.encoders import MJPEGEncoderfrom picamera2.outputs import FileOutputPAGE = """\<html><head><title>picamera2 MJPEG streaming demo</title></head><body><h1>Picamera2 MJPEG Streaming Demo</h1><img src="stream.mjpg" width="640" height="480" /></body></html>"""class StreamingOutput(io.BufferedIOBase):    def __init__(self):        self.frame = None        self.condition = Condition()    def write(self, buf):        with self.condition:            self.frame = buf            self.condition.notify_all()class StreamingHandler(server.BaseHTTPRequestHandler):    def do_GET(self):        if self.path == '/':            self.send_response(301)            self.send_header('Location', '/index.html')            self.end_headers()        elif self.path == '/index.html':            content = PAGE.encode('utf-8')            self.send_response(200)            self.send_header('Content-Type', 'text/html')            self.send_header('Content-Length', len(content))            self.end_headers()            self.wfile.write(content)        elif self.path == '/stream.mjpg':            self.send_response(200)            self.send_header('Age', 0)            self.send_header('Cache-Control', 'no-cache, private')            self.send_header('Pragma', 'no-cache')            self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')            self.end_headers()            try:                while True:                    with output.condition:                        output.condition.wait()                        frame = output.frame                    self.wfile.write(b'--FRAME\r\n')                    self.send_header('Content-Type', 'image/jpeg')                    self.send_header('Content-Length', len(frame))                    self.end_headers()                    self.wfile.write(frame)                    self.wfile.write(b'\r\n')            except Exception as e:                logging.warning(                    'Removed streaming client %s: %s',                    self.client_address, str(e))        else:            self.send_error(404)            self.end_headers()class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):    allow_reuse_address = True    daemon_threads = Truepicam2 = Picamera2()picam2.configure(picam2.create_video_configuration(main={"size": (640, 480)}))output = StreamingOutput()picam2.start_recording(MJPEGEncoder(), FileOutput(output))try:    address = ('', 8000)    server = StreamingServer(address, StreamingHandler)    server.serve_forever()finally:    picam2.stop_recording()
And edited and changed it ad infinitum. I can get either camera 0 or camera 1 to stream, but I cannot get them to stream together. The solution doesn't need to be based of of this code, it is just where I was working.

I have tried running the code twice, in two different windows and only one seems to work. In the double experiment, I had different ports and used the correct camera.

Your help and suggestions would be much appreciated.

Thanks

Katie

Statistics: Posted by Katie1348 — Mon May 06, 2024 6:27 pm



Viewing all articles
Browse latest Browse all 1251

Trending Articles