1

I've written a website written in python by using the Flask framework. I tried to show a live video stream on the website but it seems like only one client can stream the video and even that stream fails after 30 seconds...

The website is deployed on a raspberry pi and has 3 workers, so I understand that it is not possible for them to read frames from the camera simultanously. That's why I used a synchronization method that I found online, though with the results mentioned above.

This is the relevant part of my code:

from flask import Flask, render_template, url_for, request, escape, Response
import picamera 
from camera import CameraStream
import cv2

app = Flask(__name__)

cap = CameraStream().start()

def gen_frame():
"""Video streaming generator function."""
    while cap:
        frame = cap.read()
        convert = cv2.imencode('.jpg', frame)[1].tobytes()
        yield (b'--frame\r\n'
           b'Content-Type: image/jpeg\r\n\r\n' + convert + b'\r\n') # concate frame one by one and show result


@app.route("/video_feed")
def video_feed():
    return Response(gen_frame(), mimetype='multipart/x-mixed-replace; boundary=frame')

The camera module is a copy from the synchronized method that I mentioned (link)

The logs I get after letting several client connect and try to stream my camera:

Jan 03 17:46:41 raspberrypi gunicorn[1567]:   File "/usr/lib/python2.7/dist-
packages/werkzeug/wrappers.py", line 82, in _iter_encoded
Jan 03 17:46:41 raspberrypi gunicorn[1567]:     for item in iterable:
Jan 03 17:46:41 raspberrypi gunicorn[1567]:   File "/home/pi/Desktop/python_scripts/internetdisplay/app.py", line 23, in gen_frame
Jan 03 17:46:41 raspberrypi gunicorn[1567]:     frame = cap.read()
Jan 03 17:46:41 raspberrypi gunicorn[1567]:   File "/home/pi/Desktop/python_scripts/internetdisplay/camera.py", line 31, in read
Jan 03 17:46:41 raspberrypi gunicorn[1567]:     frame = self.frame.copy()
Jan 03 17:46:41 raspberrypi gunicorn[1567]: AttributeError: 'NoneType' object has no attribute 'copy'
Jan 03 17:46:54 raspberrypi gunicorn[1567]: [2020-01-03 17:46:54 +0000] [1567] [CRITICAL] WORKER TIMEOUT (pid:1576)
Jan 03 17:46:54 raspberrypi gunicorn[1567]: [2020-01-03 17:46:54 +0000] [1576] [INFO] Worker exiting (pid: 1576)
Jan 03 17:46:55 raspberrypi gunicorn[1567]: [2020-01-03 17:46:55 +0000] [1593] [INFO] Booting worker with pid: 1593

I tried adjusting the read-method from the module and return None if the frame is None. But the results were similar.

  • did you ever find out how to do it? – bherbruck Feb 10 '21 at 02:45
  • Off hand I believe the approach would be to separate the camera capture portion into a separate process; that process would capture the frames and put them in a shared location (buffer?) A – John Blacker Mar 09 '23 at 12:36

0 Answers0