-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCVpaint.py
55 lines (47 loc) · 1.86 KB
/
CVpaint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from flask import Flask, render_template, request, Response
from opencv_camera import Camera
import time
import numpy as np
from mini_project_4 import Model, Controller, View, process_frame
import os
import cv2
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home_screen.html')
@app.route('/intro')
def introduction():
return render_template('intro.html')
@app.route('/rules')
def rules():
return render_template('rules.html')
@app.route('/draw')
def draw():
model.calibration_start = time.time()
return render_template('stream.html')
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen(Camera()),
mimetype='multipart/x-mixed-replace; boundary=frame') # continuously get frames from the camera
def gen(camera):
"""Video streaming generator function."""
while True:
frame = camera.get_frame() # get the frame in binary from Opencv
frame = np.frombuffer(frame, np.uint8) # turn the binary into an array
frame = cv2.imdecode(frame, cv2.IMREAD_UNCHANGED) # turn the array into an image
model.frame = frame # give that frame to the object that keeps track of everything
process_frame(model, controller, view) # run MP4 on the image
frame = cv2.imencode('.jpg', model.frame)[1].tobytes() # turn the image back into binary
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # send the binary to the web app
if __name__ == '__main__':
"""
All this function does is initialize everything we need for the web app to run.
"""
model = Model()
view = View(model)
controller = Controller(model)
HOST = '0.0.0.0' if 'PORT' in os.environ else '127.0.0.1'
PORT = int(os.environ.get('PORT', 5000))
app.run(host=HOST, port=PORT)