from flask import Flask, render_template, Response
import cv2

app = Flask(__name__)
camera = cv2.VideoCapture(0)

def generateFrames():
    while True:
        # Read the camera frame:
        success, frame = camera.read()
        # cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        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') # Alternative to return
            

@app.route('/')
def index():
    return render_template('index.html')

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



if __name__ == "__main__":
    # app.run(debug=True)
    app.run(host='0.0.0.0', port=2204, threaded=True)
