-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.py
309 lines (278 loc) · 17.2 KB
/
ui.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
import math
import copy
import random
import camera
import image_identif
import webbrowser
from cmu_112_graphics import *
def appStarted(app):
pages = {"homepage", "about", "learnMore", "webcam", "recycle",
"compost", "landfill"}
app.currentPage = "homepage"
app.aboutUnderline = ''
app.learnMoreUnderline = ''
app.webCamUnderline = ''
app.homepageUnderline = ''
app.object = None
app.accuracy = None
def redrawAll(app, canvas):
canvas.create_rectangle(0, 0, 1440, 777, fill="#3A7B48")
if app.currentPage == "homepage":
drawHomepage(app, canvas)
elif app.currentPage == 'about':
drawAbout(app, canvas)
elif app.currentPage == 'learnMore':
drawLearnMore(app, canvas)
elif app.currentPage == 'webcam':
drawWebcam(app, canvas)
elif app.currentPage == 'compost':
drawCompost(app, canvas)
elif app.currentPage == 'recycling':
drawRecycle(app, canvas)
elif app.currentPage == 'landfill':
drawLandfill(app, canvas)
if app.currentPage != "homepage":
drawHomeButton(app, canvas)
else:
drawLogo(app, canvas)
def drawLogo(app, canvas):
from PIL import Image, ImageTk
display = ImageTk.PhotoImage(Image.open('images/logo.png'))
canvas.create_image(163,150, anchor=NW,image=display)
def drawHomeButton(app, canvas):
display = ImageTk.PhotoImage(Image.open('images/homebutton.png'))
canvas.create_image(168,26, anchor=NW,image=display)
def drawHomepage(app, canvas):
canvas.create_text(182, 306,
text="OATS",
font="Inter 112 bold", fill="white", anchor="nw")
canvas.create_text(182, 444,
text="Open CV AI Trash Sorter", font="Inter 25 italic", fill="white", anchor="nw")
canvas.create_text(182, 484,
text="about",
font=f"Inter 45{app.aboutUnderline}", fill="white", anchor="nw")
canvas.create_text(182, 538,
text="learn more about sustainability",
font=f"Inter 45{app.learnMoreUnderline}", fill="white", anchor="nw")
canvas.create_text(182, 592,
text="try it yourself", font=f"Inter 45 bold{app.webCamUnderline}", anchor="nw")
def drawAbout(app, canvas):
canvas.create_text(181, 250, text = "about",
font = "Inter 80 bold", fill = "white", anchor = "nw")
canvas.create_text(182, 370, text = "A small project made during the 2022 Hack112 Hackathon that uses a PyTorch machine ",
font = "Inter 16", fill = "white", anchor = "nw")
canvas.create_text(182, 390, text = "learning model to categorize a given piece of trash captured by the webcam into either",
font = "Inter 16", fill = "white", anchor = "nw")
canvas.create_text(182, 410, text = "compost, recycling, or landfill.",
font = "Inter 16", fill = "white", anchor = "nw")
canvas.create_text(182, 450, text = "Authors",
font = "Inter 16 underline", fill = "white", anchor = "nw")
canvas.create_text(182, 470, text = "Orelia Pi (CS)",
font = "Inter 16", fill = "white", anchor = "nw")
canvas.create_text(182, 490, text = "Anna Shi (IS)",
font = "Inter 16", fill = "white", anchor = "nw")
canvas.create_text(182, 510, text = "Tim Wang (IS)",
font = "Inter 16", fill = "white", anchor = "nw")
canvas.create_text(182, 530, text = "Suanna Zhong (BCSA)",
font = "Inter 16", fill = "white", anchor = "nw")
# canvas.create_rectangle(168, 26, 246, 137, fill = "pink", outline = "pink")
def drawLearnMore(app, canvas):
canvas.create_text(181, 259, text="learn more about",
font="Inter 80 bold", fill="white", anchor="nw")
canvas.create_text(181, 356, text="sustainability",
font="Inter 80 bold", fill="white", anchor="nw")
canvas.create_text(182, 460, text="Sustainability is based on a simple principle: Everything that we need for our survival and well-",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 480, text="being depends, either directly or indirectly, on our natural environment. To pursue",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 500, text="sustainability is to create and maintain the conditions under which humans and nature can ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 520, text="exist in productive harmony to support present and future generations.",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 560, text="Read more about sustainability at epa.gov",
font="Inter 16 underline", fill="white", anchor="nw")
canvas.create_text(182, 600, text="To see what you can do to address sustainability at a smaller scale in Pittsburgh, PA,",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 620, text="read about Pittsburgh’s goals for sustainability here.",
font="Inter 16 underline", fill="white", anchor="nw")
from PIL import Image, ImageTk
display = ImageTk.PhotoImage(Image.open('images/learnmoreimage.jpg'))
canvas.create_image(930,260, anchor=NW,image=display)
def drawWebcam(app, canvas):
canvas.create_text(182, 226, text = "let's get sorting!", font = "Inter 80 bold", fill = "white", anchor = "nw")
# canvas.create_rectangle(168, 26, 246, 137, fill = "pink", outline = "pink")
# numbers
canvas.create_text(182, 377, text = "1.", font = "Inter 30 bold", fill = "white", anchor = "nw")
canvas.create_text(182, 521, text = "2.", font = "Inter 30 bold", fill = "white", anchor = "nw")
canvas.create_text(182, 593, text = "3.", font = "Inter 30 bold", fill = "white", anchor = "nw")
# instructions
canvas.create_text(222, 377, text = "Click anywhere on the screen to start the ", font = "Inter 30", fill = "white", anchor = "nw")
canvas.create_text(222, 413, text = "webcam. It may take a few moments for the ", font = "Inter 30", fill = "white", anchor = "nw")
canvas.create_text(222, 449, text = "pop-up window to show up.", font = "Inter 30", fill = "white", anchor = "nw")
canvas.create_text(222, 521, text = "Press the spacebar key to take a photo.", font = "Inter 30", fill = "white", anchor = "nw")
canvas.create_text(222, 593, text = "Press the escape key to close the window.", font = "Inter 30", fill = "white", anchor = "nw")
def drawRecycle(app, canvas):
display = ImageTk.PhotoImage(Image.open('images/recycle.jpg'))
canvas.create_image(930,260, anchor=NW,image=display)
canvas.create_text(182, 226, text="recycle this one!",
font="Inter 80 bold", fill="white", anchor="nw")
canvas.create_text(182, 341, text=f"Accuracy: {app.accuracy}",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 370, text="Recycling is the process of collecting and processing materials that would otherwise be ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 390, text="thrown away as trash and turning them into new products. Recycling can benefit your",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 430, text="Read more about recycling at epa.gov",
font="Inter 16 underline", fill="white", anchor="nw")
canvas.create_text(182, 470, text="Did you know that recycling is mandatory in the City of Pittsburgh? All residents of single-",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 490, text="family homes and small apartments (five or fewer) must separate recyclable items from ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 510, text="household trash and package them for bi-weekly recycling curbside collection, per City",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 530, text="Code §619.03(a). The City practices single-stream recycling, which means all recyclables ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 550, text="are combined and placed into one truck and taken to a Materials Recovery Facility, ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 570, text="Recycle Source, for further processing.",
font="Inter 16", fill="white", anchor="nw")
# canvas.create_rectangle(168, 26, 246, 137, fill = "pink", outline = "pink")
def drawCompost(app, canvas):
display = ImageTk.PhotoImage(Image.open('images/compost.jpg'))
canvas.create_image(930,260, anchor=NW,image=display)
canvas.create_text(182, 226, text="compost this one!",
font="Inter 80 bold", fill="white", anchor="nw")
canvas.create_text(182, 341, text=f"Accuracy: {app.accuracy}",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 370, text="Composting is the natural process of recycling organic matter, such as leaves and food",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 390, text="scraps, into a valuable fertilizer that can enrich soil and plants. Anything that grows",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 410, text="decomposes eventually; composting simply speeds up the process by providing an ideal ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 430, text="environment for bacteria, fungi, and other decomposing organisms (such as worms, ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 450, text="sowbugs, and nematodes) to do their work. The resulting decomposed matter, which often ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 470, text="ends up looking like fertile garden soil, is called compost. Fondly referred to by farmers as ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 490, text="“black gold,” compost is rich in nutrients and can be used for gardening, horticulture, and ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 510, text="agriculture.",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 550, text="Organic discards can be processed in industrial-scale composting facilities, in smaller-",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 590, text="scale community composting systems, and in anaerobic digesters, among other options. ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 610, text="This guide focuses primarily on home composting, which is a great way to keep your ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 630, text="organic discards out of the waste stream and produce a valuable soil amendment for your",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 650, text="own use.",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 690, text="Read more at nrdc.org",
font="Inter 16 underline", fill="white", anchor="nw")
# canvas.create_rectangle(168, 26, 246, 137, fill = "pink", outline = "pink")
def drawLandfill(app, canvas):
display = ImageTk.PhotoImage(Image.open('images/landfill.jpg'))
canvas.create_image(930,260, anchor=NW,image=display)
canvas.create_text(182, 226, text="throw this one out.",
font="Inter 80 bold", fill="white", anchor="nw")
canvas.create_text(182, 341, text=f"Accuracy: {app.accuracy}",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 370, text="The most effective way to reduce waste is to not create it in the first place. Making a new ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 390, text="product emits greenhouse gases that contribute to climate change and requires a lot of ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 410, text="materials and energy - raw materials must be extracted from the earth, and the product",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 430, text="must be fabricated then transported to wherever it will be sold. As a result, reduction and",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 450, text="reuse are the most effective ways you can save natural resources, protect the",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 470, text="environment and save money.",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 510, text="One person's trash is another person's treasure. Instead of discarding unwanted ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 530, text="appliances, tools or clothes, try selling or donating them. Not only will you be reducing ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 550, text="waste, you'll be helping others. Local churches, community centers, thrift stores, schools ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 590, text="and nonprofit organizations may accept a variety of donated items, including used books, ",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 610, text="working electronics and unneeded furniture.",
font="Inter 16", fill="white", anchor="nw")
canvas.create_text(182, 650, text="Read more at ways to reuce waste at epa.gov",
font="Inter 16 underline", fill="white", anchor="nw")
# canvas.create_rectangle(168, 26, 246, 137, fill = "pink", outline = "pink")
def mouseMoved(app, event):
x, y = event.x, event.y
app.aboutUnderline = ''
app.learnMoreUnderline = ''
app.webCamUnderline = ''
app.homepageUnderline = ''
if app.currentPage == 'homepage':
if (182 <= x <= 310) and (484 <= y <= 538):
app.aboutUnderline = " underline"
elif (182 <= x <= 830) and (538 <= y <= 590):
app.learnMoreUnderline = " underline"
elif (182 <= x <= 455) and (592 <= y <= 650):
app.webCamUnderline = " underline"
elif app.currentPage == 'about' or app.currentPage == 'learnMore' or app.currentPage == "webcam" or app.currentPage == 'recycle' or app.currentPage == 'compost' or app.currentPage == 'landfill':
if (180 <= x <= 366) and (40 <= y <= 90):
app.homepageUnderline = " underline"
def mousePressed(app, event):
x, y = event.x, event.y
if app.currentPage == 'homepage':
if (182 <= x <= 310) and (484 <= y <= 538):
app.currentPage = 'about'
elif (182 <= x <= 830) and (538 <= y <= 590):
app.currentPage = 'learnMore'
elif (182 <= x <= 540) and (592 <= y <= 650):
app.currentPage = 'webcam'
elif app.currentPage == 'learnMore':
if (168 <= x <= 245) and (26 <= y <= 137):
app.currentPage = 'homepage'
elif (183 <= x <= 488) and (560 <= y <= 580):
webbrowser.open("https://www.epa.gov/sustainability/learn-about-sustainability")
elif (183 <= x <= 563) and (624 <= y <= 638):
webbrowser.open("https://pittsburghpa.gov/dcp/sustainability-resilience")
elif app.currentPage == 'webcam':
camera.takePicture()
waste_types = ["cardboard", "glass", "metal", "paper", "plastic", "trash"]
wasteType, confidence = image_identif.identify(Image.open('opencv_frame_0.png'))
app.object = waste_types[wasteType]
app.accuracy = f'{round(confidence, 2)}%'
if waste_types[wasteType] == "cardboard":
app.currentPage = 'compost'
elif waste_types[wasteType] in {"glass", "metal", "paper"}:
app.currentPage = 'recycling'
else:
app.currentPage = 'landfill'
if (182<=x<=517) and (652<=y<=668):
webbrowser.open("hi")
if app.currentPage == 'recycling':
if (182<=x<=460) and (434<=y<=449):
webbrowser.open("https://www.epa.gov/recycle")
elif app.currentPage == 'landfill':
if (182<=x<=515) and (654<=y<=668):
webbrowser.open('https://www.epa.gov/landfills')
elif app.currentPage == 'compost':
if (182<=x<=350) and (690<=y<=710):
webbrowser.open('https://www.nrdc.org/stories/composting-101')
if app.currentPage != 'homepage':
if (170 <= x <= 250) and (30 <= y <= 140):
app.currentPage = 'homepage'
def ifInsideCircle(x, y):
# radians is 35 px
a = (x - 725)**2
b = (y - 615)**2
return (a + b)**0.5 <= 35
def display():
width = 1440
height = 777
runApp(width=width, height=height)
def main():
display()
if __name__ == '__main__':
main()