forked from ThomasGust/FlaskSocketIOVideoStreaming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
37 lines (30 loc) · 920 Bytes
/
app.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
import cv2
import base64
import io
from PIL import Image
import numpy as np
from flask_socketio import emit, SocketIO
from flask import Flask, render_template
app = Flask(__name__, template_folder="templates")
sio = SocketIO(app)
@sio.on('image')
def image(data_image):
sbuf = io.StringIO()
sbuf.write(data_image)
b = io.BytesIO(base64.b64decode(data_image))
pimg = Image.open(b)
# DO WHATEVER IMAGE PROCESSING HERE{
frame = cv2.cvtColor(np.array(pimg), cv2.COLOR_RGB2BGR)
frame = cv2.flip(frame, flipCode=0)
imgencode = cv2.imencode('.jpg', frame)[1]
#}
stringData = base64.b64encode(imgencode).decode('utf-8')
b64_src = 'data:image/jpeg;base64,'
stringData = b64_src + stringData
emit('response_back', stringData)
@app.route("/")
@app.route("/home")
def index():
return render_template("index.html")
if __name__ == "__main__":
sio.run(app, debug=True)