Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,26 +3,35 @@ import tensorflow as tf
|
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
|
6 |
-
|
7 |
# Klassennamen, sollten Ihrem Dataset entsprechen
|
8 |
class_names = ['Apple__black_rot', 'Apple__healthy', 'Apple__rust', 'Apple__scab']
|
9 |
|
10 |
def predict_figure(uploaded_file):
|
11 |
if uploaded_file is None:
|
12 |
-
return "
|
13 |
|
14 |
model = tf.keras.models.load_model('apple_classification.keras')
|
15 |
|
16 |
-
# Load the image from the file path
|
17 |
with Image.open(uploaded_file).convert('RGB') as img:
|
18 |
img = img.resize((150, 150))
|
19 |
img_array = np.array(img)
|
20 |
-
|
21 |
prediction = model.predict(np.expand_dims(img_array, axis=0))
|
22 |
|
23 |
# Identify the most confident prediction
|
24 |
confidences = {class_names[i]: np.round(float(prediction[0][i]), 2) for i in range(len(class_names))}
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Define example images
|
28 |
examples = [
|
@@ -32,17 +41,13 @@ examples = [
|
|
32 |
["images/Apple__scab.JPG"]
|
33 |
]
|
34 |
|
35 |
-
|
36 |
# Define the Gradio interface
|
37 |
iface = gr.Interface(
|
38 |
-
fn=predict_figure,
|
39 |
-
inputs=gr.File(label="
|
40 |
-
outputs=["image", "text"
|
41 |
-
title="
|
42 |
-
description="
|
43 |
-
examples=examples
|
44 |
)
|
45 |
-
|
46 |
iface.launch()
|
47 |
-
|
48 |
-
|
|
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
|
|
|
6 |
# Klassennamen, sollten Ihrem Dataset entsprechen
|
7 |
class_names = ['Apple__black_rot', 'Apple__healthy', 'Apple__rust', 'Apple__scab']
|
8 |
|
9 |
def predict_figure(uploaded_file):
|
10 |
if uploaded_file is None:
|
11 |
+
return "Keine Datei hochgeladen.", None, "Keine Vorhersage"
|
12 |
|
13 |
model = tf.keras.models.load_model('apple_classification.keras')
|
14 |
|
|
|
15 |
with Image.open(uploaded_file).convert('RGB') as img:
|
16 |
img = img.resize((150, 150))
|
17 |
img_array = np.array(img)
|
|
|
18 |
prediction = model.predict(np.expand_dims(img_array, axis=0))
|
19 |
|
20 |
# Identify the most confident prediction
|
21 |
confidences = {class_names[i]: np.round(float(prediction[0][i]), 2) for i in range(len(class_names))}
|
22 |
+
max_confidence_class = max(confidences, key=confidences.get)
|
23 |
+
response = generate_response(max_confidence_class)
|
24 |
+
|
25 |
+
return img, confidences, response
|
26 |
+
|
27 |
+
def generate_response(predicted_class):
|
28 |
+
responses = {
|
29 |
+
'Apple__black_rot': "Die Pflanze zeigt Anzeichen von Schwarzfäule. Erhöhen Sie die Luftzirkulation und vermeiden Sie Überwässerung.",
|
30 |
+
'Apple__healthy': "Die Pflanze ist gesund. Weiter so!",
|
31 |
+
'Apple__rust': "Rost wurde erkannt. Entfernen Sie infizierte Blätter und behandeln Sie mit einem Fungizid.",
|
32 |
+
'Apple__scab': "Schorf ist sichtbar. Beschneiden Sie betroffene Bereiche und verwenden Sie Fungizide."
|
33 |
+
}
|
34 |
+
return responses.get(predicted_class, "Unbekannte Krankheit")
|
35 |
|
36 |
# Define example images
|
37 |
examples = [
|
|
|
41 |
["images/Apple__scab.JPG"]
|
42 |
]
|
43 |
|
|
|
44 |
# Define the Gradio interface
|
45 |
iface = gr.Interface(
|
46 |
+
fn=predict_figure,
|
47 |
+
inputs=gr.File(label="Datei hochladen"),
|
48 |
+
outputs=["image", "text", "text"],
|
49 |
+
title="Klassifikator für Apfelblattkrankheiten",
|
50 |
+
description="Lade ein Bild eines Apfelblatts hoch, um den Gesundheitszustand und die Vertrauenswürdigkeit des Modells zu sehen.",
|
51 |
+
examples=examples
|
52 |
)
|
|
|
53 |
iface.launch()
|
|
|
|