'Flask Streaming Server

I have a problem with a debug streaming server that I want to use to get better insights into my project in Python.

I set up the following flask server, but I do not want to just stream a video directly from the camera to the browser but edit the image on the main thread and after that update the web server to display the new image.

I am trying to achieve that by calling the update method in the main loop. But the image in my browser does not change. Maybe because the methods are running in different threads?

from flask import Response
from flask import Flask
from flask import render_template
from flask import request
from multiprocessing import Process
import datetime
import time
import cv2
import queue

global outputFrame

class Webstream():

def __init__(self):
    self.t1 = None
    # initialize a flask object
    self.app = Flask(__name__)
    self.app.add_url_rule('/', 'index', self.index)
    self.app.add_url_rule('/videofeed', 'videofeed', self.videofeed)

def shutdown_server(self):
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Not running with the Werkzeug Server')
    func()

def startInternally(self):
    self.app.run(host='0.0.0.0', port='5000', debug=False)

def start(self):
    self.t1 = Process(target=self.startInternally)
    self.t1.start()

def stop(self):
    self.t1.terminate()
    self.t1.join()

@staticmethod
def update(image):
    global outputFrame
    outputFrame = image

@staticmethod
def getFrame():
    global outputFrame
    return outputFrame

def gen(self):
    while True:
        time.sleep(0.01)
        frame = Webstream.getFrame()                #This never changes, even when #the update method is called
        if(frame.any()):
            ret, buffer = cv2.imencode('.jpg', frame)
            yield (b'--frame\r\n'
                b'Content-Type: image/jpeg\r\n\r\n' + bytearray(buffer) + b'\r\n\r\n')
        else:
            yield (b'--frame\r\n'
                b'Content-Type: image/jpeg\r\n\r\n\r\n\r\n')

def index(self):
    # return the rendered template
    return render_template('index.html')

def videofeed(self):
    # return the response generated along with the specific media
    # type (mime type)
    return Response(self.gen(), mimetype='multipart/x-mixed-replace; boundary=frame')

I cannot find any relevant answers on google but only the standard tutorials on using this setup with a regular generator. Please help me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source