-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
89 lines (49 loc) · 1.46 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
import random
import cv2
import base64
from io import BytesIO
from PIL import Image
import io
import numpy as np
app = Flask(__name__)
socketio = SocketIO(app)
def resize_pyim(image, width=None, height=None, inter=cv2.INTER_AREA):
dim = None
(h, w) = image.shape[:2]
if width is None and height is None:
return image
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
resized = cv2.resize(image, dim, interpolation=inter)
return resized
@app.route('/')
def index():
return render_template('trex.html')
@app.route('/vid')
def index2():
return render_template('vid.html')
@socketio.on('my event')
def test_message(message):
print(message)
@socketio.on('catch-frame')
def catch_frame(data):
# print(data)
image=Image.open(BytesIO(base64.b64decode(data)))
frame=np.asarray(image)
# Process the image frame
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
imgencode = cv2.imencode('.png', frame)[1]
# base64 encode
stringData = base64.b64encode(imgencode).decode('utf-8')
b64_src = 'data:image/jpg;base64,'
stringData = b64_src + stringData
# emit the frame back
emit('response_back', stringData)
if __name__ == '__main__':
socketio.run(app, port='5000',debug=True)