I need to stream my desktop using flask but I'm confused. I saw here - https://blog.miguelgrinberg.com/post/video-streaming-with-flask that I can "fool" flask to think I have a webcam and stream another images. I tried to get the desktop image using the gen(camera) method and made a few changes to the Camera class but I still get a blank screen and couldn't find a solution online. My code:
import mss
import numpy
class Camera(object):
def __init__(self):
with mss.mss() as sct:
# Part of the screen to capture
monitor = {"top": 40, "left": 0, "width": 800, "height": 640}
self.frames = sct.grab(monitor)
def get_frame(self):
return self.frames
from flask import Response
from flask import Flask
from flask import render_template
import threading
import numpy
import time
import cv2
import mss
from camera import Camera
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed')
def video_feed():
return Response(gen(Camera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)