Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -31,35 +31,83 @@ transform = transforms.Compose([
|
|
31 |
|
32 |
# Define class labels
|
33 |
class_labels = [
|
34 |
-
"
|
35 |
-
"
|
36 |
-
"
|
37 |
-
"
|
38 |
-
"
|
39 |
-
"
|
40 |
-
"
|
41 |
-
"
|
|
|
|
|
42 |
]
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
# Prediction function
|
45 |
def predict_image(image):
|
46 |
if image.mode != "RGB":
|
47 |
image = image.convert("RGB")
|
48 |
-
input_tensor = transform(image).unsqueeze(0).to(device)
|
49 |
with torch.no_grad():
|
50 |
outputs = model(input_tensor)
|
51 |
_, predicted = torch.max(outputs, 1)
|
52 |
label = class_labels[predicted.item()]
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
# Gradio interface setup
|
56 |
interface = gr.Interface(
|
57 |
fn=predict_image,
|
58 |
-
inputs=gr.Image(type="pil", label="
|
59 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
|
60 |
live=True
|
61 |
)
|
62 |
|
63 |
-
# Launch Gradio app
|
64 |
interface.launch()
|
65 |
|
|
|
31 |
|
32 |
# Define class labels
|
33 |
class_labels = [
|
34 |
+
"Spraydosen", "Lebensmitteldosen_aus_Aluminium", "Getränkedosen_aus_Aluminium",
|
35 |
+
"Pappkartons", "Verpackungen_aus_Pappe", "Kleidung", "Kaffeekränzchen",
|
36 |
+
"Einweg_Besteck_aus_Kunststoff", "Eierschalen", "Lebensmittelabfälle",
|
37 |
+
"Getränkeflaschen_aus_Glas", "Kosmetikbehälter_aus_Glas", "Lebensmittelgläser_aus_Glas",
|
38 |
+
"Zeitschriften", "Zeitungen", "Buropapier", "Pappbecher", "Deckel_aus_Kunststoff",
|
39 |
+
"Waschmittelbehälter_aus_Kunststoff", "Lebensmittelbehälter_aus_Kunststoff",
|
40 |
+
"Plastiktüten", "Sodaflaschen_aus_Kunststoff", "Strohhalme_aus_Kunststoff",
|
41 |
+
"Mülltüten_aus_Kunststoff", "Wasserflaschen_aus_Kunststoff", "Schuhe",
|
42 |
+
"Lebensmitteldosen_aus_Stahl", "Styroporbecher", "Lebensmittelbehälter_aus_Styropor",
|
43 |
+
"Teebeutel"
|
44 |
]
|
45 |
|
46 |
+
class_to_tonne = {
|
47 |
+
"Spraydosen": "Gelbe Tonne",
|
48 |
+
"Lebensmitteldosen_aus_Aluminiumm": "Gelbe Tonne",
|
49 |
+
"Getränkedosen_aus_Aluminium": "Gelbe Tonne",
|
50 |
+
"Plastiktüten": "Gelbe Tonne",
|
51 |
+
"Wasserflaschen_aus_Kunststoff": "Gelbe Tonne",
|
52 |
+
"Sodaflaschen_aus_Kunststoff": "Gelbe Tonne",
|
53 |
+
"Lebensmittelbehälter_aus_Kunststoff": "Gelbe Tonne",
|
54 |
+
"Waschmittelbehälter_aus_Kunststoff": "Gelbe Tonne",
|
55 |
+
"Deckel_aus_Kunststoff": "Gelbe Tonne",
|
56 |
+
"Strohhalme_aus_Kunststoff": "Gelbe Tonne",
|
57 |
+
"Einweg_Besteck_aus_Kunststoff": "Gelbe Tonne",
|
58 |
+
|
59 |
+
"Pappkartons": "Papiertonne",
|
60 |
+
"Verpackungen_aus_Pappe": "Papiertonne",
|
61 |
+
"Zeitschriften": "Papiertonne",
|
62 |
+
"Zeitungen": "Papiertonne",
|
63 |
+
"Buropapier": "Papiertonne",
|
64 |
+
"Pappbecher": "Papiertonne",
|
65 |
+
|
66 |
+
"Kaffeekränzchen": "Biomüll",
|
67 |
+
"Lebensmittelabfälle": "Biomüll",
|
68 |
+
"Eierschalen": "Biomüll",
|
69 |
+
"Teebeutel": "Biomüll",
|
70 |
+
|
71 |
+
"Getränkeflaschen_aus_Glas": "Altglas",
|
72 |
+
"Lebensmittelgläser_aus_Glas": "Altglas",
|
73 |
+
"Losmetikbehälter_aus_Glas": "Altglas",
|
74 |
+
|
75 |
+
"Kleidung": "Restmüll",
|
76 |
+
"Schuhe": "Restmüll",
|
77 |
+
"Styroporbecher": "Restmüll",
|
78 |
+
"Lebensmittelbehälter_aus_Styropor": "Restmüll",
|
79 |
+
"Mülltüten_aus_Kunststoff": "Restmüll"
|
80 |
+
}
|
81 |
+
|
82 |
+
|
83 |
# Prediction function
|
84 |
def predict_image(image):
|
85 |
if image.mode != "RGB":
|
86 |
image = image.convert("RGB")
|
87 |
+
input_tensor = transform(image).unsqueeze(0).to(model.device)
|
88 |
with torch.no_grad():
|
89 |
outputs = model(input_tensor)
|
90 |
_, predicted = torch.max(outputs, 1)
|
91 |
label = class_labels[predicted.item()]
|
92 |
+
tonne = class_to_tonne.get(label, "Unbekannt")
|
93 |
+
|
94 |
+
return {
|
95 |
+
"Klasse (auf Deutsch)": label.replace("_", " ").capitalize(),
|
96 |
+
"Gehört in die": tonne
|
97 |
+
}
|
98 |
|
99 |
# Gradio interface setup
|
100 |
interface = gr.Interface(
|
101 |
fn=predict_image,
|
102 |
+
inputs=gr.Image(type="pil", label="Bild hochladen"),
|
103 |
+
outputs=[
|
104 |
+
gr.Textbox(label="Vorhersage (Klasse auf Deutsch)"),
|
105 |
+
gr.Textbox(label="Richtiger Abfallbehälter")
|
106 |
+
],
|
107 |
+
title="Abfallerkennung mit KI 🗑️",
|
108 |
+
description="Lade ein Bild hoch und erfahre, in welche Tonne der Abfall gehört.",
|
109 |
live=True
|
110 |
)
|
111 |
|
|
|
112 |
interface.launch()
|
113 |
|