-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
274 lines (204 loc) · 7.63 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import base64
import datetime
import os
import uuid
from cloud_storage import CloudStorage
from db import Mysql
from dotenv import load_dotenv
from flask import Flask
from flask_mqtt import Mqtt
from model import Model
from structlog import get_logger
from video import Video
load_dotenv()
storage = CloudStorage(
project_id=os.getenv("GOOGLE_PROJECT_ID"),
credentials_path=os.getenv("GOOGLE_APPLICATION_CREDENTIALS"),
bucket_name=os.getenv("GOOGLE_STORAGE_BUCKET_NAME"),
)
db = Mysql(
host=os.getenv("MYSQL_HOST"),
user=os.getenv("MYSQL_USER"),
password=os.getenv("MYSQL_PASSWORD"),
database=os.getenv("MYSQL_DATABASE"),
)
video = Video()
ml_model = Model(os.getenv("MODEL_PATH"))
app = Flask(__name__)
app.logger = get_logger()
app.config["HTTP_URL"] = os.getenv("HTTP_URL")
app.config["HTTP_PORT"] = os.getenv("HTTP_PORT")
app.config["MQTT_BROKER_URL"] = os.getenv("MQTT_BROKER_URL")
app.config["MQTT_BROKER_PORT"] = int(os.getenv("MQTT_BROKER_PORT"))
mqtt_client = Mqtt(app, connect_async=True)
@mqtt_client.on_connect()
def handle_connect(client, userdata, flags, rc):
if rc != 0:
print(f"Connection failed with result code {rc}")
return
app.logger.info(
f"Connected to MQTT broker at {app.config['MQTT_BROKER_URL']}:{
app.config['MQTT_BROKER_PORT']}"
)
topics = [
"stream/#",
"open_stream",
"close_stream",
]
for topic in topics:
mqtt_client.subscribe(topic)
app.logger.info(f"{topic} subscribed")
@mqtt_client.on_message()
def handle_message(client, userdata, message):
try:
topic = message.topic
payload = message.payload
if topic.startswith("open_stream"):
open_stream(payload.decode("utf-8"))
elif topic.startswith("stream"):
vehicle_uuid = None
topic_parts = topic.split("/")
if len(topic_parts) > 1:
vehicle_uuid = topic_parts[1]
process_img(vehicle_uuid, payload)
elif topic.startswith("close_stream"):
close_stream(payload.decode("utf-8"))
except Exception as e:
app.logger.error(e)
return
def open_stream(vehicle_uuid):
if vehicle_uuid in video.frames:
app.logger.info(f"[open_stream] {vehicle_uuid}: stream already open")
return
app.logger.info(f"[open_stream] {vehicle_uuid}: opening stream")
video.frames[vehicle_uuid] = list()
ml_model.detections[vehicle_uuid] = list()
conn = db.get_connection()
cursor = conn.cursor(buffered=True)
cursor.execute(
"SELECT EXISTS(SELECT 1 FROM vehicles WHERE uuid = %s)", (vehicle_uuid,)
)
vehicle = cursor.fetchone()[0]
if not vehicle:
app.logger.info(
f"[open_stream] {vehicle_uuid}: adding vehicle to database"
)
cursor.execute(
"INSERT INTO vehicles (uuid) VALUES (%s)", (vehicle_uuid,)
)
conn.commit()
return
conn.commit()
conn.close()
app.logger.info(
f"[open_stream] {vehicle_uuid}: vehicle already exists in database"
)
def process_img(vehicle_uuid, payload):
if vehicle_uuid is None:
return
if vehicle_uuid not in video.frames and vehicle_uuid not in ml_model.detections:
return
mqtt_client.publish(f"base64/{vehicle_uuid}", base64.b64encode(payload))
decoded_img, object_detected, face_detection_results = ml_model.analyze(payload)
if (any(value is True for value in object_detected.values()) or face_detection_results["ear"] != 0):
for key, value in object_detected.items():
if value:
mqtt_client.publish(f"alert/{vehicle_uuid}", f"{key}")
if face_detection_results:
mqtt_client.publish(f"alert/{vehicle_uuid}", "not focus")
ml_model.detections[vehicle_uuid].append({
"object_detected": object_detected,
"face_detection_results": face_detection_results
})
video.write_to_buffer(vehicle_uuid, decoded_img)
def close_stream(vehicle_uuid):
if vehicle_uuid not in video.frames:
app.logger.info(
f"[close_stream] {vehicle_uuid}: stream already closed"
)
return
if len(video.frames[vehicle_uuid]) == 0:
app.logger.info(f"[close_stream] {vehicle_uuid}: no frames to compile")
if vehicle_uuid in video.frames:
del video.frames[vehicle_uuid]
if vehicle_uuid in ml_model.detections:
del ml_model.detections[vehicle_uuid]
app.logger.info(f"[close_stream] {vehicle_uuid}: stream closed")
return
app.logger.info(f"[close_stream] {vehicle_uuid}: saving video")
filename = video.save_to_file(vehicle_uuid)
app.logger.info(
f"[close_stream] {vehicle_uuid}: video saved to {filename}"
)
video_url = storage.upload(
filename,
f"{datetime.date.today()}/{vehicle_uuid}-{str(uuid.uuid4())}.mp4"
)
app.logger.info(
f"[close_stream] {vehicle_uuid}: video uploaded to {video_url}"
)
app.logger.info(f"[close_stream] {vehicle_uuid}: saving video & telemetry to database")
conn = db.get_connection()
cursor = conn.cursor(buffered=True)
cursor.execute(
"SELECT id FROM vehicles WHERE uuid = %s", (vehicle_uuid,)
)
vehicle_id = cursor.fetchone()[0]
if not vehicle_id:
app.logger.error(
f"[close_stream] {vehicle_uuid}: vehicle id not found"
)
return
cursor.execute(
"INSERT INTO videos (vehicle_id, uuid, url) VALUES (%s, %s, %s)",
(vehicle_id, str(uuid.uuid4()), video_url),
)
video_id = cursor.lastrowid
for detections in ml_model.detections[vehicle_uuid]:
if all(value is False for value in detections["object_detected"].values()) and detections["face_detection_results"]["ear"] == 0:
continue
cursor.execute(
"INSERT INTO alerts (video_id, uuid, ear, mar, sleep_duration, yawning_duration, focus_duration, time) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
(
video_id,
str(uuid.uuid4()),
detections["face_detection_results"]["ear"],
detections["face_detection_results"]["mar"],
detections["face_detection_results"]["sleep_duration"],
detections["face_detection_results"]["yawning_duration"],
detections["face_detection_results"]["focus_duration"],
detections["face_detection_results"]["time"],
),
)
alerts_id = cursor.lastrowid
if detections["object_detected"]:
detected_str = ""
for key, value in detections["object_detected"].items():
if value:
detected_str += f"{key}, "
match detected_str:
case "":
detected_str = None
case _:
detected_str = detected_str[:-2]
cursor.execute(
"UPDATE alerts SET object_detected = %s WHERE id = %s",
(detected_str, alerts_id),
)
conn.commit()
conn.close()
app.logger.info(f"[close_stream] {vehicle_uuid}: video & telemetry saved to database")
if vehicle_uuid in video.frames:
del video.frames[vehicle_uuid]
if vehicle_uuid in ml_model.detections:
del ml_model.detections[vehicle_uuid]
if os.path.exists(filename):
os.remove(filename)
app.logger.info(f"[close_stream] {vehicle_uuid}: stream closed")
def main():
app.run(
host=app.config["HTTP_URL"],
port=app.config["HTTP_PORT"],
)
if __name__ == "__main__":
main()