-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
475 lines (338 loc) · 14.5 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import streamlit as st
from detection import detect_people
from scipy.spatial import distance as dist
import numpy as np
import imutils
import cv2
import os
import datetime
import wget
from PIL import Image
import pandas as pd
import csv
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import time
import plotly.figure_factory as ff
#DB
import sqlite3
conn = sqlite3.connect('data.db')
c = conn.cursor()
# DB Functions for Video
def create_video_table():
c.execute('CREATE TABLE IF NOT EXISTS videotable(author TEXT,title TEXT,file_date DATE,path TEXT)')
def add_video(author,title,file_date,path):
c.execute('INSERT INTO videotable(author,title,file_date,path) VALUES (?,?,?,?)', (author,title,file_date,path))
conn.commit()
def view_all_videos():
c.execute("SELECT * FROM videotable")
data = c.fetchall()
return data
def view_by_video_author():
c.execute('SELECT DISTINCT author FROM videotable')
data = c.fetchall()
return data
def get_path_by_video_author(author):
c.execute('SELECT path FROM videotable WHERE author="{}"'.format(author))
data = c.fetchall()
return data
# DB Functions for Image
def create_image_table():
c.execute('CREATE TABLE IF NOT EXISTS imagetable(author TEXT,title TEXT,file_date DATE,path TEXT)')
def add_image(author,title,file_date,path):
c.execute('INSERT INTO imagetable(author,title,file_date,path) VALUES (?,?,?,?)', (author,title,file_date,path))
conn.commit()
def view_all_images():
c.execute("SELECT * FROM imagetable")
data = c.fetchall()
return data
def view_by_image_author():
c.execute('SELECT DISTINCT author FROM imagetable')
data = c.fetchall()
return data
def get_path_by_image_author(author):
c.execute('SELECT path FROM imagetable WHERE author="{}"'.format(author))
data = c.fetchall()
return data
def delete_image(path):
c.execute('DELETE FROM imagetable WHERE path="{}"'.format(path))
conn.commit()
def delete_video(path):
c.execute('DELETE FROM videotable WHERE path="{}"'.format(path))
conn.commit()
# save & upload helper function
def save_uploaded_image(uploaded_image):
with open(os.path.join("images",uploaded_image.name),"wb") as f:
f.write(uploaded_image.getbuffer())
def save_uploaded_video(uploaded_video):
with open(os.path.join("videos",uploaded_video.name),"wb") as f:
f.write(uploaded_video.getbuffer())
@st.cache
def load_image(image_file):
img = Image.open(image_file)
return img
def main():
f = open("data.csv", "w")
f.truncate()
f.close()
sidebar = st.sidebar.selectbox('Choose one of the following', ('Welcome', 'Add an Image','Add a Video','View All File','Image Analysis', 'Real Time Video Analysis'))
if sidebar == 'Welcome':
welcome()
if sidebar == 'Add an Image':
add_an_image()
if sidebar == 'Add a Video':
add_a_video()
if sidebar == 'View All File':
view_all_file()
if sidebar == 'Image Analysis':
image_analysis()
if sidebar == 'Real Time Video Analysis':
video_analysis()
def welcome():
st.title("Automated Social Distancing Monitoring System by AI4Life")
st.subheader('Team members:')
st.text('1. Lai Kok Wui (101211447)\n2. Lee Zhe Sheng (10215371)\n3. Didier Luther Ho Chih-Yuan (101214093)\n4. Abraham Tan Chiun Wu (101213825)')
st.subheader('A0 Poster for AI4LIFE video presentation')
st.image('images/poster.jpg',use_column_width=True)
def add_an_image():
st.title("Upload an image")
create_image_table()
file_author = st.text_input("Enter your name", max_chars=50)
file_title = st.text_input("Enter Desire File Name")
file_date = st.date_input("Created Date")
image_file = st.file_uploader("Upload An Image",type=['png', 'jpg', 'jpeg'])
if image_file is not None:
file_details = {"FileName":image_file.name,"FileType":image_file.type}
# st.write(file_details)
# st.write(type(image_file))
img = load_image(image_file)
st.image(img)
path = os.path.join("images", image_file.name)
save_uploaded_image(image_file)
if st.button("Add"):
add_image(file_author, file_title, file_date, path)
st.success("File: {} saved".format(file_title))
def add_a_video():
st.title("Upload a video")
create_video_table()
file_author = st.text_input("Enter your name", max_chars=50)
file_title = st.text_input("Enter Desire File Name")
file_date = st.date_input("Created Date")
video_file = st.file_uploader("Upload An Image",type=['mp4'])
if video_file is not None:
file_details = {"FileName":video_file.name,"FileType":video_file.type}
# st.write(file_details)
# st.write(type(video_file))
img = video_file.read()
st.video(img)
path = os.path.join("videos", video_file.name)
save_uploaded_video(video_file)
if st.button("Add"):
add_video(file_author, file_title, file_date, path)
st.success("File: {} saved".format(file_title))
def view_all_file():
st.header("View All Files")
images = view_all_images()
image_db = pd.DataFrame(images, columns=["Author", "Title","Created Date","File Path"])
st.subheader("Image Database")
st.dataframe(image_db)
all_images = [i[0] for i in view_by_image_author()]
image_option_1 = st.selectbox('Your Name for Image', all_images)
all_path = [i[0] for i in get_path_by_image_author(image_option_1)]
image_option_2 = st.selectbox("Select your uploaded image", all_path)
if st.button("Delete Image"):
delete_image(image_option_2)
st.warning("Deleted: '{}'".format(image_option_2))
videos = view_all_videos()
st.subheader("Video Database")
video_db = pd.DataFrame(videos, columns=["Author", "Title","Created Date","File Path"])
st.dataframe(video_db)
all_videos = [i[0] for i in view_by_video_author()]
video_option_1 = st.selectbox('Your Name for Video', all_videos)
all_video_path = [i[0] for i in get_path_by_video_author(video_option_1)]
video_option_2 = st.selectbox("Select your uploaded video", all_video_path)
if st.button("Delete Video"):
delete_video(video_option_2)
st.warning("Deleted: '{}'".format(video_option_2))
def image_analysis():
st.title('Real Time Social Distancing Monitor System with Image')
cuda = st.selectbox('NVIDIA CUDA GPU should be used?', ('True', 'False'))
st.subheader('Test Demo image')
all_titles = [i[0] for i in view_by_image_author()]
option = st.selectbox('Your Name', all_titles)
all_path = [i[0] for i in get_path_by_image_author(option)]
option2 = st.selectbox("Select your uploaded file", all_path)
USE_GPU = bool(cuda)
MIN_DISTANCE = 50
labelsPath = "yolo-coco/coco.names"
LABELS = open(labelsPath).read().strip().split("\n")
weightsPath = "yolo-coco/yolov4.weights"
configPath = "yolo-coco/yolov4.cfg"
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
if USE_GPU:
st.info("[INFO] setting preferable backend and target to CUDA...")
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
if st.button('Start'):
st.info("[INFO] loading YOLO from disk...")
st.info("[INFO] accessing image stream...")
for i in view_by_image_author():
if option == i[0]:
vs = cv2.VideoCapture(option2)
else:
vs = cv2.VideoCapture(0)
writer = None
image_placeholder = st.empty()
while True:
(grabbed, frame) = vs.read()
if not grabbed:
break
frame = imutils.resize(frame, width=700)
results = detect_people(frame, net, ln,
personIdx=LABELS.index("person"))
violate = set()
if len(results) >= 2:
centroids = np.array([r[2] for r in results])
D = dist.cdist(centroids, centroids, metric="euclidean")
for i in range(0, D.shape[0]):
for j in range(i + 1, D.shape[1]):
if D[i, j] < MIN_DISTANCE:
violate.add(i)
violate.add(j)
for (i, (prob, bbox, centroid)) in enumerate(results):
(startX, startY, endX, endY) = bbox
(cX, cY) = centroid
color = (0, 255, 0)
if i in violate:
color = (0, 0, 255)
cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2)
cv2.circle(frame, (cX, cY), 5, color, 1)
font = cv2.FONT_HERSHEY_SIMPLEX
datet = str(datetime.datetime.now())
frame = cv2.putText(frame, datet, (0, 35), font, 1,
(0, 255, 255), 2, cv2.LINE_AA)
text = "Social Distancing Violations: {}".format(len(violate))
cv2.putText(frame, text, (10, frame.shape[0] - 25),
cv2.FONT_HERSHEY_SIMPLEX, 0.85, (0, 0, 255), 3)
display = 1
if display > 0:
image_placeholder.image(
frame, caption='Live Social Distancing Monitor Running..!', channels="BGR")
if writer is not None:
writer.write(frame)
st.success("Design & Developed By AI4Life")
def video_analysis():
st.title('Real Time Social Distancing Monitor System with Video')
cuda = st.selectbox('NVIDIA CUDA GPU should be used?', ('True', 'False'))
st.subheader('Test Demo Video')
all_titles = [i[0] for i in view_by_video_author()]
option = st.selectbox('Your Name', all_titles)
all_path = [i[0] for i in get_path_by_video_author(option)]
option2 = st.selectbox("Select your uploaded file", all_path)
USE_GPU = bool(cuda)
MIN_DISTANCE = 50
labelsPath = "yolo-coco/coco.names"
LABELS = open(labelsPath).read().strip().split("\n")
weightsPath = "yolo-coco/yolov4.weights"
configPath = "yolo-coco/yolov4.cfg"
# Create a header for CSV file for category
header = ['time(seconds)','violate_count']
#CSV open and amend CSV file
with open('data.csv', 'a') as f:
writer_csv = csv.writer(f)
writer_csv.writerow(header)
f.close()
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
if USE_GPU:
st.info("[INFO] setting preferable backend and target to CUDA...")
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
st.write("The graph wil auto generate with a time interval of 3 seconds")
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
if st.button('Start'):
start = time.time()
st.info("[INFO] loading YOLO from disk...")
st.info("[INFO] accessing video stream...")
for i in view_by_video_author():
if option == i[0]:
vs = cv2.VideoCapture(option2)
else:
vs = cv2.VideoCapture(0)
writer = None
image_placeholder = st.empty()
while True:
(grabbed, frame) = vs.read()
if not grabbed:
break
frame = imutils.resize(frame, width=700)
results = detect_people(frame, net, ln,
personIdx=LABELS.index("person"))
violate = set()
if len(results) >= 2:
centroids = np.array([r[2] for r in results])
D = dist.cdist(centroids, centroids, metric="euclidean")
for i in range(0, D.shape[0]):
for j in range(i + 1, D.shape[1]):
if D[i, j] < MIN_DISTANCE:
violate.add(i)
violate.add(j)
for (i, (prob, bbox, centroid)) in enumerate(results):
(startX, startY, endX, endY) = bbox
(cX, cY) = centroid
color = (0, 255, 0)
if i in violate:
color = (0, 0, 255)
cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2)
cv2.circle(frame, (cX, cY), 5, color, 1)
font = cv2.FONT_HERSHEY_SIMPLEX
datet = str(datetime.datetime.now())
frame = cv2.putText(frame, datet, (0, 35), font, 1,
(0, 255, 255), 2, cv2.LINE_AA)
text = "Social Distancing Violations: {}".format(len(violate))
cv2.putText(frame, text, (10, frame.shape[0] - 25),
cv2.FONT_HERSHEY_SIMPLEX, 0.85, (0, 0, 255), 3)
count_violate = str(len(violate))
count_datet = str(datetime.datetime.now().strftime('%M:%S.%f')[:-4])
count_violate_list = []
count_datet_list = []
count_violate_list.append(count_violate)
end = time.time()
difference = (end - start)
count_datet_list.append(difference)
with open('data.csv', 'a') as f:
writer_csv = csv.writer(f)
writer_csv.writerows(zip(count_datet_list,count_violate_list))
f.close()
display = 1
if display > 0:
image_placeholder.image(
frame, caption='Live Social Distancing Monitor Running..!', channels="BGR")
if writer is not None:
writer.write(frame)
DATA_URL=('data.csv')
@st.cache(persist=True)
def load_data():
data=pd.read_csv(DATA_URL)
return data
df = pd.read_csv(DATA_URL)
chart_caption = st.text('Line chart of violated quantity & time in (s)')
linechart = st.line_chart(df)
countdown = st.text('Countdown: 3')
time.sleep(1)
countdown.empty()
countdown = st.text('Countdown: 2')
time.sleep(1)
countdown.empty()
countdown = st.text('Countdown: 1')
time.sleep(1)
countdown.empty()
linechart.empty()
chart_caption.empty()
st.success("Design & Developed By AI4Life")
if __name__ == "__main__":
main()