-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
216 lines (170 loc) · 6.45 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import matplotlib
matplotlib.use('Agg')
# Importing major libraries
from flask import Flask, render_template, url_for,request, jsonify
from flask_socketio import SocketIO
import base64
import os
# Importing from external python file
from audio_model import init_cough_mask,extract_features
from check_mask import Check_Mask
from capture import Capture
from check_mask import mask_count,re_init
#Remove logging
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
#Hide warnings
import warnings
warnings.filterwarnings("ignore")
import shutil
from engineio.payload import Payload
Payload.max_decode_packets = 500
#Function to handle unrequired pages
def page_not_found(e):
print('Not found')
return render_template('404.html'), 404
# Creating flask app and initialize the socket
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.register_error_handler(404, page_not_found)
app.config.update(
SESSION_COOKIE_SECURE=True,
SESSION_COOKIE_HTTPONLY=True,
SESSION_COOKIE_SAMESITE='Lax',
)
# Socket
socketio = SocketIO(app, cors_allowed_origins="*",ping_interval=60000, ping_timeout=120000, async_mode = "threading")
#Global Variables
sockets = {}
#Load the models
cough_model = init_cough_mask()
class Socket:
def __init__(self, sid):
self.sid = sid
self.connected = True
self.count = "0"
self.icount = "0"
self.lname = "breathing"
self.lprob = 0
self.dirpath =""
self.Hrisk = 0
self.Mrisk = 0
self.Lrisk = 0
self.capture = None
# Emits data to a socket's unique room
def emit(self, event, data):
socketio.emit(event, data, room=self.sid)
#Reinitialise the count when refreshed or on a new connection
def re_initialise():
global Hrisk,Mrisk,Lrisk
Hrisk = 0
Mrisk = 0
Lrisk = 0
capture = None
# Load the html page as homepage
@app.route('/', methods=['GET'])
def index():
# Main page
return render_template('index.html')
# Return message on successful connection
@socketio.on('connect')
def connection():
currentSocketId = request.sid
sockets[request.sid] = Socket(request.sid)
sockets[request.sid].emit('message', 'connected established')
#When the user clicks on start
@socketio.on('started')
def started_feed(message):
print(message)
re_initialise()
sockets[request.sid].capture = Capture(Check_Mask())
currentSocketId = request.sid
folder = os.path.join(os.getcwd(),"static")
sockets[request.sid].dirpath = os.path.join(folder,currentSocketId)
if not os.path.isdir(sockets[request.sid].dirpath):
os.mkdir(sockets[request.sid].dirpath)
#Receive the blob and process it
@socketio.on('blob_event')
def handle_blob(message):
currentSocketId = request.sid
#Declaring filename and videoname)
filename = "video" + sockets[request.sid].count + ".webm"
filepath = os.path.join(sockets[request.sid].dirpath,filename)
data = base64.b64decode(message)
#write the videofile to disk
try:
f = open(filepath, 'wb')
f.write(data)
f.close()
except:
# print("Stoppped feed")
return
#Updating information
sockets[request.sid].count = str(int(sockets[request.sid].count)+1)
print ("Processing now: ", filepath)
#Path to save the melspectrogram image
img_name = sockets[request.sid].count + ".png"
img_savepath = (os.sep).join(filepath.split(os.sep)[:-1])
img_savepath = os.path.join(img_savepath,img_name)
#function for audio processing
sockets[request.sid].lname,sockets[request.sid].lprob = extract_features(cough_model,filepath,img_savepath)
print("Predicted class:",sockets[request.sid].lname)
sockets[request.sid].emit('label_event',{"label":sockets[request.sid].lname,"prob":str(sockets[request.sid].lprob),"video":filename})
#Process the frames from the video feed
@socketio.on('input_image')
def test_message(image):
#Creating image path
image = image.split(",")[1]
iname = sockets[request.sid].icount + ".jpg"
currentSocketId = request.sid
if not os.path.isdir(sockets[request.sid].dirpath):
os.mkdir(sockets[request.sid].dirpath)
# Creating folder to save images
ipath = os.path.join(sockets[request.sid].dirpath,"images")
if not os.path.isdir(ipath):
os.mkdir(ipath)
ipath = os.path.join(ipath,iname)
# Saving image
with open(ipath, "wb") as f:
f.write(base64.b64decode(image))
# Send image for processing
sockets[request.sid].capture.enqueue_input(ipath,sockets[request.sid].lname,sockets[request.sid].lprob)
sockets[request.sid].icount = str(int(sockets[request.sid].icount)+1)
imagepath = os.path.join(currentSocketId,"images")
imagepath = os.path.join(imagepath,iname)
frame = sockets[request.sid].capture.get_frame()
#To solve Windows path error issues
if os.sep == '\\':
imagepath = imagepath.replace('\\','/')
#Sending the processed frame to frontend
sockets[request.sid].emit("response_back",url_for("static",filename = imagepath))
#Send analytics every five seconds
if int(sockets[request.sid].icount) % 5 ==0:
hrisk,mrisk,lrisk = mask_count()
re_init()
sockets[request.sid].Hrisk += hrisk
sockets[request.sid].Lrisk += lrisk
sockets[request.sid].Mrisk += mrisk
print("High Risk: ",sockets[request.sid].Hrisk,"Moderate Risk: ",sockets[request.sid].Mrisk,"Low Risk: ",sockets[request.sid].Lrisk)
sockets[request.sid].emit('stopped',{"High Risk": sockets[request.sid].Hrisk,"Moderate Risk":sockets[request.sid].Mrisk,"Low Risk":sockets[request.sid].Lrisk})
@socketio.on('stopped')
def stopped_function(message):
print(message)
shutil.rmtree(sockets[request.sid].dirpath, ignore_errors = True)
socketio.sleep(1)
@socketio.on('disconnect')
def disconnect_function():
try:
shutil.rmtree(sockets[request.sid].dirpath)
except:
pass
print("Disconnected the socket: ")
sockets[request.sid].emit('disconnect',"Manual Disconnect")
@socketio.on('uncaughtException')
def exception_func():
# handle or ignore error
console.log(exception);
if __name__ == '__main__':
print ("socket listening on 8000")
socketio.run(app, port = 8000, host='0.0.0.0', debug = True)