How to make efficient "broadcasting to localhost" with python-flask?

I have script that broadcasting my screen to localhost (later i ll try to use this idea as a baby monitor). My problem is sometimes connection goes down, browser is needed to be refreshed. other problem is FPS is little bit low. I decrease the resolution in order to fast broadcast but doesn't seem very efficient. So

  • What can I do for prevent connection cut ?
  • How can I increase FPS from other devices ?



from flask import Flask, render_template, Response
import cv2
from pyautogui import screenshot
from numpy import array
import argparse

app = Flask(__name__)

def gen_frames():  # generate frame by frame from camera
    while True:
        # Capture screenshots,
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        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

def video_feed():
    #Video streaming route. Put this in the src attribute of an img tag
    return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')

def index():
    """Video streaming home page."""
    return render_template('index.html')

if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--ip", type=str, required=True,
        help="ip address of the device")
    ap.add_argument("-o", "--port", type=int, required=True,
        help="ephemeral port number of the server (1024 to 65535)")
    args= vars(ap.parse_args())["ip"], port=args["port"], debug=True)

# run this script as an example | cmd > $python --ip --port 8000
# then other device can enter on browser
# all devices must be on same network
How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum