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

Camera board • Re: "Or Better"?

$
0
0
Next task. Live streaming the stereo video to a browser. I'm attempting to use flask but it doesn't work. (http://<pi ip>:5000/live)
Thoughts?

Code:

#!/usr/bin/python3from flask import Flask, Responsefrom picamera2 import Picamera2, MappedArrayfrom picamera2.encoders import H264Encoderfrom picamera2.outputs import FfmpegOutputfrom libcamera import Transformimport timefrom threading import Lockapp = Flask(__name__)fps = 15height = 1000  # Reduced heightwidth_left = 2 * height  # Left stream is 2:1width_right = height  # Right stream is 1:1FrameTime = int(1000000 / fps)lock = Lock()cam2_request = Nonedef mergeviews(request):    global cam2_request    with lock:        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:            m1.array[:, :height] = m1.array[:, ::2]  # Downsampled left view            m1.array[:, height:] = m2.array  # Right view        request_2.release()def save_request(request):    global cam2_request    with lock:        if cam2_request is not None:            cam2_request.release()        request.acquire()        cam2_request = requestlcam = Picamera2(0)rcam = Picamera2(1)# Select the best modemode = lcam.sensor_modes[2]sensorsize = lcam.camera_properties['PixelArraySize']sensorwidth = sensorsize[0]sensorheight = sensorsize[1]x = (sensorwidth - sensorheight) // 2crop = (x, 0, sensorheight, sensorheight)# Configure cameraslconfig = lcam.create_video_configuration(    sensor={"output_size": mode['size'], 'bit_depth': mode['bit_depth']},    main={"format": "RGB888", "size": (width_left, height)},    controls={"ScalerCrop": crop, "FrameDurationLimits": (FrameTime, FrameTime)},    transform=Transform(hflip=True, vflip=True),    queue=False)rconfig = rcam.create_video_configuration(    sensor={"output_size": mode['size'], 'bit_depth': mode['bit_depth']},    main={"format": "RGB888", "size": (width_right, height)},    controls={"ScalerCrop": crop, "FrameDurationLimits": (FrameTime, FrameTime)},    transform=Transform(hflip=True, vflip=True))lcam.configure(lconfig)rcam.configure(rconfig)# Prepare to encode for streamingencoder = H264Encoder(2000000)  # Set bitrate to 2,000,000output = FfmpegOutput()  # Use output directly for streaminglcam.start()rcam.start()cam2_request = Nonercam.pre_callback = save_requestlcam.pre_callback = mergeviews# Short delay to allow Ae and AWB to settletime.sleep(1)def generate_frames():    while True:        with lock:            if cam2_request is not None:                lcam.capture_request(cam2_request)                time.sleep(1 / fps)@app.route('/live')def live():    return Response(generate_frames(), mimetype='video/h264')if __name__ == '__main__':    try:        app.run(host='0.0.0.0', port=5000)    except KeyboardInterrupt:        lcam.stop_recording()

Statistics: Posted by MRV — Sun Oct 27, 2024 11:15 am



Viewing all articles
Browse latest Browse all 8044

Trending Articles