So the story goes like this: I want to process the images from a video stream and display modified ones occasionally using a Flask API. The processing consists in counting the objects in image and keeping an average in ram and also drawing a box in a modified image that is then yielded as bytes to be displayed in a html page (something like this):
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result
The run() function from below once is run takes care to calculate average and yield modified images. But I want to run it in main to compute the average all the time (to send notification if average is too big or something) and occasionally to display the yielded images to html. How would you do that without calling it twice and without code duplicate, essentially to catch those yields when needed.
from flask import Flask, render_template, Response
import cv2
from detect1 import run
app = Flask(__name__)
@app.route('/video_feed')
def video_feed():
#Video streaming route. Put this in the src attribute of an img tag
return Response(run(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/')
def index():
"""Video streaming home page."""
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)