-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
653 lines (533 loc) · 19.1 KB
/
gui.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
from imports import *
# Define base directory
BASE_DIR = Path(__file__).resolve().parent
# Define paths
ASSETS_PATH = BASE_DIR / "assets" / "frame0" # assets directory path
MODEL_PATH = BASE_DIR / "best.pt" # YOLOv8 model path
CLASSES_PATH = BASE_DIR / "classes.txt" # 'classes' file path
def relative_to_assets(path: str) -> Path:
return BASE_DIR / "assets" / "frame0" / path
def select_video_file():
file_path = filedialog.askopenfilename(
title="Select Video File",
filetypes=[("Video files", "*.mp4 *.avi *.mov *.mkv")]
)
return file_path
def store_video_path():
global video_path
video_path = select_video_file()
if not video_path:
messagebox.showerror("Error", "No video file selected.")
return
train_model_and_play(video_path)
def store_live_video_path():
global video_path
video_path = "live"
train_model_and_play(video_path)
# count the number of resets
reset_counter = 0
def reset_entries():
global reset_counter, X1, Y1, X2, Y2, X3, Y3, X4, Y4
# Default coordinates for zoning(Area1)
X1 = 0 # 400
Y1 = 0 # 140
X2 = 0 # 24
Y2 = 600 # 477
X3 = 1500 # 950
Y3 = 600 # 477
X4 = 1500 # 650
Y4 = 0 # 140
reset_counter += 1
# Show info messsage when reset button is pressed(counter > 1)
if reset_counter > 1:
messagebox.showinfo("info", "The entry values have been reverted to their default settings.")
# Initialise default values at program startup
reset_entries()
# Create window
window = Tk()
window.title("Vulnerable Group Detection App")
window.geometry("1000x600")
window.configure(bg="#F5F4F4")
# set default toggle to 'off'
toggle_state = False
canvas = Canvas(
window,
bg="#F5F4F4",
height=600,
width=1000,
bd=0,
highlightthickness=0,
relief="ridge"
)
canvas.place(x=0, y=0)
canvas.create_rectangle(
0.0,
0.0,
500.0,
600.0,
fill="#387EF5",
outline="")
canvas.create_text(
39.0,
128.0,
anchor="nw",
text="Vulnerable Group Detection",
fill="#F5F4F4",
font=("Inter Bold", 36 * -1)
)
canvas.create_rectangle(
57.0,
184.0,
157.0,
197.0,
fill="#F5F4F4",
outline="")
canvas.create_text(
40.0,
230.0,
anchor="nw",
text="Discover a groundbreaking solution for\n enhanced accessibility through our app,\n harnessing the "
"cutting-edge YOLOV\n technology.\n\n By accurately detecting pedestrians,\n including wheelchair users, "
"it redefines\n safety and inclusivity, revolutionizing\n navigation experiences for all.",
fill="#FFFFFF",
font=("Inter", 20 * -1)
)
canvas.create_text(
625.0,
180.0,
anchor="nw",
text="Enter The Details",
fill="#625F80",
font=("Inter ExtraBold", 32 * -1)
)
canvas.create_text(
623.0,
245.0,
anchor="nw",
text="Transform Any Video By Simply\n"
" Selecting a Video File",
fill="#625F80",
font=("Inter", 16 * -1)
)
# Button for file selection
button_image_1 = PhotoImage(
# Change "button_1.png" to "button_2.png" for a rectangle button
file=relative_to_assets("button_2.png"))
# Update the button command to call play_video function
button_1 = Button(
image=button_image_1,
borderwidth=0,
highlightthickness=0,
command=store_video_path, # Call store_video_path function when button is clicked
relief="flat",
text="Select File",
compound="center",
fg="#FFFFFF",
font=("Inter Bold", 12)
)
button_1.place(
x=665.0,
y=325.0,
width=144.0,
height=55.0
)
# Button for live detection
button_image_2 = PhotoImage(
# Change "button_1.png" to "button_2.png" for a rectangle button
file=relative_to_assets("button_2.png"))
# Update the button command to call play_video function
button_2 = Button(
image=button_image_1,
borderwidth=0,
highlightthickness=0,
command=store_live_video_path, # Call store_video_path function when button is clicked
relief="flat",
text="Live Detection",
compound="center",
fg="#FFFFFF",
font=("Inter Bold", 12)
)
button_2.place(
x=665.0,
y=400.0,
width=144.0,
height=55.0
)
# Function to open the hyperlink in a web browser
def open_link(event):
webbrowser.open("https://github.com/Omskka/Wheelchair_detection/blob/main/README.md")
# Create a label for the hyperlink
info_label = Label(
window,
text="Click here for instructions",
fg="white", # Set text color to blue
bg="#387EF5", # Set background color to the same as the blue background
cursor="hand2", # Change cursor to a hand when hovering over the label
font=("Inter", 12, "underline") # Set font and underline the text
)
info_label.place(x=25, y=560)
# Bind <Button-1> event to open_link function
info_label.bind("<Button-1>", open_link)
# Create a label for the hyperlink
info_label = Label(
window,
text="For configuration, click here",
fg="white", # Set text color to blue
bg="#387EF5", # Set background color to the same as the blue background
cursor="hand2", # Change cursor to a hand when hovering over the label
font=("Inter", 12, "underline") # Set font and underline the text
)
info_label.place(x=38, y=460)
def validate_entries():
global X1, Y1, X2, Y2, X3, Y3, X4, Y4
# Get values from Entry widgets and convert them to integers
try:
X1 = int(entry1.get())
Y1 = int(entry2.get())
X2 = int(entry3.get())
Y2 = int(entry4.get())
X3 = int(entry5.get())
Y3 = int(entry6.get())
X4 = int(entry7.get())
Y4 = int(entry8.get())
except ValueError:
# If conversion fails, show an error message and return
messagebox.showwarning("Warning", "Please enter numerical values only.")
return
# If all entries are numerical, destroy the conf_window
conf_window.destroy()
# Restore the window after video ends
window.deiconify()
def config_window(event):
# Minimize the window
window.iconify()
# Create window
global conf_window, entry1, entry2, entry3, entry4, entry5, entry6, entry7, entry8, toggle_state, toggle_var
conf_window = Toplevel(window)
conf_window.geometry("1000x600")
conf_window.configure(bg="#F5F4F4")
canvas2 = Canvas(
conf_window,
bg="#F5F4F4",
height=600,
width=1000,
bd=0,
highlightthickness=0,
relief="ridge"
)
canvas2.place(x=0, y=0)
canvas2.create_rectangle(
0.0,
0.0,
500.0,
600.0,
fill="#387EF5",
outline=""
)
canvas2.create_text(
625.0,
114.0,
anchor="nw",
text="Enter The Details",
fill="#625F80",
font=("Inter ExtraBold", 32 * -1)
)
canvas2.create_text(
635.0,
182.0,
anchor="nw",
text="Please input the coordinates\ncorresponding to the area you\nwant to outline on the video.",
fill="#625F80",
font=("Inter", 16 * -1)
)
# Done button
done_button = Button(
conf_window,
text="Done",
command=validate_entries,
bg="#F5F4F4", # Set background color
width=15, # Set width (in characters)
height=2 # Set height (in lines)
)
done_button.place(x=660, y=300)
# Back button
back_button = Button(
conf_window,
text="Back",
command=go_back,
bg="#F5F4F4", # Set background color
width=15, # Set width (in characters)
height=2 # Set height (in lines)
)
back_button.place(x=660, y=360)
# Reset button
back_button = Button(
conf_window,
text="Reset",
command=reset_entries,
bg="#F5F4F4", # Set background color
width=15, # Set width (in characters)
height=2 # Set height (in lines)
)
back_button.place(x=210, y=525)
# Labels
label_x = 80
label_y = 40
label_spacing = 60
Label(conf_window, text="X1", bg="#387EF5", fg="#FFFFFF", font=("Inter", 16, "bold")).place(x=label_x, y=label_y)
label_y += label_spacing
Label(conf_window, text="Y1", bg="#387EF5", fg="#FFFFFF", font=("Inter", 16, "bold")).place(x=label_x, y=label_y)
label_y += label_spacing
Label(conf_window, text="X2", bg="#387EF5", fg="#FFFFFF", font=("Inter", 16, "bold")).place(x=label_x, y=label_y)
label_y += label_spacing
Label(conf_window, text="Y2", bg="#387EF5", fg="#FFFFFF", font=("Inter", 16, "bold")).place(x=label_x, y=label_y)
label_y += label_spacing
Label(conf_window, text="X3", bg="#387EF5", fg="#FFFFFF", font=("Inter", 16, "bold")).place(x=label_x, y=label_y)
label_y += label_spacing
Label(conf_window, text="Y3", bg="#387EF5", fg="#FFFFFF", font=("Inter", 16, "bold")).place(x=label_x, y=label_y)
label_y += label_spacing
Label(conf_window, text="X4", bg="#387EF5", fg="#FFFFFF", font=("Inter", 16, "bold")).place(x=label_x, y=label_y)
label_y += label_spacing
Label(conf_window, text="Y4", bg="#387EF5", fg="#FFFFFF", font=("Inter", 16, "bold")).place(x=label_x, y=label_y)
# Entries
entry_x = 200
entry_y = 40
spacing = 60
entry1 = Entry(conf_window)
entry1.place(x=entry_x, y=entry_y)
entry_y += spacing
entry2 = Entry(conf_window)
entry2.place(x=entry_x, y=entry_y)
entry_y += spacing
entry3 = Entry(conf_window)
entry3.place(x=entry_x, y=entry_y)
entry_y += spacing
entry4 = Entry(conf_window)
entry4.place(x=entry_x, y=entry_y)
entry_y += spacing
entry5 = Entry(conf_window)
entry5.place(x=entry_x, y=entry_y)
entry_y += spacing
entry6 = Entry(conf_window)
entry6.place(x=entry_x, y=entry_y)
entry_y += spacing
entry7 = Entry(conf_window)
entry7.place(x=entry_x, y=entry_y)
entry_y += spacing
entry8 = Entry(conf_window)
entry8.place(x=entry_x, y=entry_y)
# Toggle button
toggle_var = BooleanVar(value=toggle_state)
toggle_button = Checkbutton(conf_window, text="", variable=toggle_var, bg="#F5F4F4",
font=("Inter", 16))
toggle_button.place(x=850, y=550)
canvas2.create_text(
660.0,
553.0,
anchor="nw",
text="Traffic light visualization",
fill="#625F80",
font=("Inter ExtraBold", 15 * -1)
)
# Calculate the center coordinates of the screen
screen_width = conf_window.winfo_screenwidth()
screen_height = conf_window.winfo_screenheight()
x_coordinate = (screen_width - 1000) // 2
y_coordinate = (screen_height - 600) // 2
# Set the window geometry to center it on the screen
conf_window.geometry(f"1000x600+{x_coordinate}+{y_coordinate}")
conf_window.resizable(False, False)
conf_window.mainloop()
# Function to go back from config to main window
def go_back():
# Retain toggle state upon reopening
global toggle_state, toggle_var
toggle_state = toggle_var.get()
# Destroy the conf_window
conf_window.destroy()
# Restore the window
window.deiconify()
# Global variables to track time and traffic light state
duration = 2
green_time = duration + 10
yellow_time = duration
red_time = duration
extra_red_duration = 10
extra_green_duration = 10
total_time = green_time + yellow_time + red_time
def draw_traffic_light(current_phase):
# Define colors
GREY = (127, 127, 127)
RED = (0, 0, 255)
YELLOW = (0, 255, 255)
GREEN = (0, 255, 0)
# OpenCV window for the traffic light
cv2.namedWindow('Traffic Light', cv2.WINDOW_KEEPRATIO | cv2.WINDOW_GUI_NORMAL)
global object_detected
# Coordinates for circles
red_light = (50, 70)
yellow_light = (50, 150)
green_light = (50, 230)
# Radius for circles
radius = 30
# Create a black image
traffic_light = np.zeros((300, 100, 3), dtype=np.uint8)
if current_phase < green_time:
# Green phase
cv2.circle(traffic_light, red_light, radius, GREY, -1)
cv2.circle(traffic_light, yellow_light, radius, GREY, -1)
cv2.circle(traffic_light, green_light, radius, GREEN, -1)
if object_detected:
cv2.putText(traffic_light, "-10 secs", (10, 280), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
elif current_phase < green_time + yellow_time:
# Yellow phase
cv2.circle(traffic_light, red_light, radius, GREY, -1)
cv2.circle(traffic_light, yellow_light, radius, YELLOW, -1)
cv2.circle(traffic_light, green_light, radius, GREY, -1)
elif current_phase < green_time + yellow_time + red_time:
# Red phase
cv2.circle(traffic_light, red_light, radius, RED, -1)
cv2.circle(traffic_light, yellow_light, radius, GREY, -1)
cv2.circle(traffic_light, green_light, radius, GREY, -1)
if object_detected:
cv2.putText(traffic_light, "+10 secs", (10, 280), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
x_coordinate = 1220
y_coordinate = 150
# Show the traffic light
cv2.imshow('Traffic Light', traffic_light)
# Move the window
cv2.moveWindow('Traffic Light', x_coordinate, y_coordinate)
# Resize the window to 100x300
cv2.resizeWindow('Traffic Light', 100, 300)
# Bind <Button-1> event to open_link function
info_label.bind("<Button-1>", config_window)
def train_model_and_play(video_path):
global green_time, extra_green_duration, red_time, extra_red_duration, total_time, duration, red_time, green_time
model = YOLO(MODEL_PATH)
# Reset light duration
red_time = duration
green_time = duration + 10
# Read from webcam
if video_path == "live":
cap = cv2.VideoCapture(0)
# Read from videopath
else:
cap = cv2.VideoCapture(str(video_path))
if not cap.isOpened():
messagebox.showerror("error",
"Error: Could not open video file.")
return
my_file = open(CLASSES_PATH, "r")
data = my_file.read()
class_list = data.split("\n")
tracker = Tracker()
area1 = [(X1, Y1), (X2, Y2), (X3, Y3), (X4, Y4)]
global object_detected
object_detected = False
paused = False
# Minimize the window
window.iconify()
# ID arrays
wheelchairs_in_zone = []
prams_in_zone = []
crutches_in_zone = []
start_time = time.time()
while True:
current_time = time.time()
elapsed_time = current_time - start_time
# Update total_time for when red/green light duration is changed
total_time = green_time + yellow_time + red_time
if not paused:
ret, frame = cap.read()
if not ret:
break
frame = cv2.resize(frame, (1020, 500))
results = model.predict(frame, conf=0.52)
a = results[0].boxes.data
px = pd.DataFrame(a).astype("float")
list = []
# Extract confidence scores
confidence_scores = results[0].boxes.conf.cpu().numpy()
for index, row in px.iterrows():
x1 = int(row[0])
y1 = int(row[1])
x2 = int(row[2])
y2 = int(row[3])
d = int(row[5])
# Get the corresponding confidence score
confidence_score = confidence_scores[index]
c = class_list[d]
if ('wheelchair' in c or 'crutches' in c or 'prams' in c):
list.append([x1, y1, x2, y2])
bbox_idx = tracker.update(list)
wheelchairs_in_zone = []
crutches_in_zone = []
prams_in_zone = []
# Zip bbox_idx with confidence_scores
bbox_confidence_pairs = zip(bbox_idx, confidence_scores)
for bbox, confidence_score in bbox_confidence_pairs:
x3, y3, x4, y4, id = bbox
result = cv2.pointPolygonTest(np.array(area1, np.int32), (x4, y4), False)
if result >= 0:
# Increment counters based on object type
for r in results:
for c in r.boxes.cls:
class_label = model.names[int(c)]
# Increment counter based on class label
if class_label == "wheelchair":
wheelchairs_in_zone.append(id)
elif class_label == "crutches":
crutches_in_zone.append(id)
elif class_label == "prams":
prams_in_zone.append(id)
cv2.circle(frame, (x4, y4), 5, (0, 0, 255), -1)
cv2.rectangle(frame, (x3, y3), (x4, y4), (255, 255, 255), 2)
# Draw confidence score on the rectangle
confidence_str = f"{confidence_score:.3f}" # Convert confidence score to string
cvzone.putTextRect(frame, confidence_str, (x3, y3), 1, 1)
# If a wheelchair is detected(once), extend the duration of the red light.
if not object_detected:
# print("RED LIGHT EXTENDED FOR 10 SECS")
red_time += extra_red_duration
green_time -= extra_green_duration
object_detected = True
cv2.polylines(frame, [np.array(area1, np.int32)], True, (0, 255, 0), 2)
frame = cv2.resize(frame, (800, 600))
# counter bar
count_image = np.zeros((600, 200, 3), dtype=np.uint8)
count_image[:] = (245, 126, 56)
cv2.putText(count_image, f"Wheelchairs : {len(wheelchairs_in_zone)}", (13, 150), cv2.FONT_HERSHEY_SIMPLEX,
0.7, (255, 255, 255), 2)
cv2.putText(count_image, f"Crutches : {len(crutches_in_zone)}", (13, 300), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
(255, 255, 255), 2)
cv2.putText(count_image, f"Prams : {len(prams_in_zone)}", (13, 450), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
(255, 255, 255), 2)
display_frame = np.hstack((count_image, frame))
cv2.imshow('Detection Window', display_frame)
# Draw traffic lights if button is toggled
current_phase = elapsed_time % total_time
if toggle_state:
draw_traffic_light(current_phase)
frame_height, frame_width, _ = display_frame.shape
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x_coordinate = int((screen_width - frame_width) / 2)
y_coordinate = int((screen_height - frame_height) / 2)
cv2.moveWindow('Detection Window', x_coordinate, y_coordinate)
key = cv2.waitKey(1)
if key == ord('q') or not ret:
break
elif key == ord(' '):
paused = not paused
cap.release()
cv2.destroyAllWindows()
window.deiconify()
# Calculate the center coordinates of the screen
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x_coordinate = (screen_width - 1000) // 2
y_coordinate = (screen_height - 600) // 2
# Set the window geometry to center it on the screen
window.geometry(f"1000x600+{x_coordinate}+{y_coordinate}")
window.resizable(False, False)
window.mainloop()