-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.py
More file actions
137 lines (112 loc) · 6.12 KB
/
interface.py
File metadata and controls
137 lines (112 loc) · 6.12 KB
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
from tkinter import Tk, Label, Canvas, Button, Frame
from threading import Thread
import time as t
SIZE = 16
COLOR = "white"
class Interface:
def __init__(self, rpi):
self.rpi = rpi
self.stream = Tk()
self.stream.attributes('-fullscreen', True)
self.stream.title("Trash Classifier System")
self.stream.minsize(width=300, height=400)
self.waiting = False
# this function for displaying the loading page after the capture button has been clicked
def display_loading_layout(self):
self.clear_frame()
self.waiting = True
loading = Label(text="Classifying...", font=(COLOR, SIZE))
loading.place(x=self.stream.winfo_width() * 0.45, y=self.stream.winfo_height() * 0.4)
for i in range(16):
Label(self.stream, bg="#525252", width=2, height=1).place(x=(i + 15) * 15,
y=self.stream.winfo_height() * 0.5)
self.stream.update()
self.play_animation()
# this function will play the animation of the bar in the loading layout
def play_animation(self):
i = 0
while (i < 3) or self.waiting:
for j in range(16):
# make block red:
Label(self.stream, bg="#FF2E2E", width=2, height=1).place(x=(j + 15) * 15,
y=self.stream.winfo_height() * 0.5)
t.sleep(0.06)
self.stream.update_idletasks()
# make block gray:
Label(self.stream, bg="#525252", width=2, height=1).place(x=(j + 15) * 15,
y=self.stream.winfo_height() * 0.5)
i += 1
self.show_final_layout()
# this function is for deleting all the widgets inside the window(which is the stream object in our case)
# before moving to the next layout
def clear_frame(self):
for widgets in self.stream.winfo_children():
widgets.destroy()
# this function will display the classification results after the item has been classified using the model
# it will display the name of the category and the percentage
def display_classification_results(self, dict):
self.waiting = False
self.dict = dict
def display_results(self, category, percentage):
category_displayed = Label(text=f"Item category is {category}", font=(COLOR, SIZE))
category_displayed.place(x=self.stream.winfo_width() * 0.32, y=self.stream.winfo_height() * 0.35)
percentage_displayed = Label(text=f"Percentage = {percentage}%", font=(COLOR, SIZE))
percentage_displayed.place(x=self.stream.winfo_width() * 0.32, y=self.stream.winfo_height() * 0.42)
def show_final_layout(self):
self.clear_frame()
if self.dict.get('General') == True:
category_displayed = Label(text=f"Item category is General", font=(COLOR, SIZE))
category_displayed.place(x=self.stream.winfo_width() * 0.32, y=self.stream.winfo_height() * 0.35)
percentage_displayed = Label(text=f"This category can't be classified", font=(COLOR, SIZE))
percentage_displayed.place(x=self.stream.winfo_width() * 0.32, y=self.stream.winfo_height() * 0.42)
else:
max_value = max(self.dict.values())
if self.dict.get('Plastic') == max_value:
self.display_results('Plastic', "%.5f" % max_value)
elif self.dict.get('Metal') == max_value:
self.display_results('Metal', "%.5f" % max_value)
elif self.dict.get('Paper') == max_value:
self.display_results('Paper', "%.5f" % max_value)
# buttons
retaking_button = Button(text="Retake Photo", command=self.display_first_layout)
retaking_button.place(x=self.stream.winfo_width() * 0.32, y=self.stream.winfo_height() * 0.5)
exit_button = Button(text="Exit", command=self.stream.destroy)
exit_button.place(x=self.stream.winfo_width() * 0.52, y=self.stream.winfo_height() * 0.5)
# this function will call the classify function in the rpi class, which will stop the camera from streaming
# after the 'Capture' button has been clicked on. Also, it will call the display loading layout function
# to display the loading layout
def call_classify_and_loading(self):
if self.rpi != None:
self.waiting = True
loading_thread = Thread(target=self.rpi.trigger_camera, name="Loading")
loading_thread.start()
self.display_loading_layout()
def exit(self):
if self.rpi != None:
self.rpi.stop_stream()
self.stream.destroy()
# this function is for displaying the first layout when you run the code.
# It will display some text at the top, display the camera streaming in its required position,
# and two buttons at the bottom one for capturing the photo and the other for stopping the program
def display_first_layout(self):
self.clear_frame()
welcome = Label(text="Welcome to our Trash Classifier System",
font=(COLOR, SIZE))
welcome.grid(row=0, column=2)
prompt = Label(text="Click on the Capture button to classify the trash", font=(COLOR, SIZE))
prompt.grid(row=1, column=2)
self.stream.update()
camera_stream = Canvas(self.stream, width=self.stream.winfo_width() * 0.8, height=self.stream.winfo_height() * 0.75)
camera_stream.grid(row=2, column=2)
# this function is for playing a real-live stream
if self.rpi != None:
self.rpi.make_stream()
# here where the two buttons "Capture" and "Exit" will be inside this frame to align with each other
f = Frame(self.stream)
f.grid(row=3, column=3)
capture_button = Button(f, text="Capture", command=self.call_classify_and_loading)
capture_button.pack(side="left")
exit_button = Button(f, text="Exit", command=self.exit)
exit_button.pack(side="right")
print(self.stream.winfo_height(),self.stream.winfo_width())
self.stream.mainloop()