Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import tensorflow as tf | |
| from PIL import Image | |
| import numpy as np | |
| # Klassennamen, sollten Ihrem Dataset entsprechen | |
| class_names = ['Apple__black_rot', 'Apple__healthy', 'Apple__rust', 'Apple__scab'] | |
| def predict_figure(uploaded_file): | |
| if uploaded_file is None: | |
| return "Keine Datei hochgeladen.", None, "Keine Vorhersage" | |
| model = tf.keras.models.load_model('apple_classification-6.keras') | |
| with Image.open(uploaded_file).convert('RGB') as img: | |
| img = img.resize((150, 150)) | |
| img_array = np.array(img) | |
| prediction = model.predict(np.expand_dims(img_array, axis=0)) | |
| # Identify the most confident prediction | |
| confidences = {class_names[i]: np.round(float(prediction[0][i]), 2) for i in range(len(class_names))} | |
| max_confidence_class = max(confidences, key=confidences.get) | |
| response = generate_response(max_confidence_class) | |
| return img, confidences, response | |
| def generate_response(predicted_class): | |
| responses = { | |
| 'Apple__black_rot': "Die Pflanze zeigt Anzeichen von Schwarzfäule. Erhöhen Sie die Luftzirkulation und vermeiden Sie Überwässerung.", | |
| 'Apple__healthy': "Die Pflanze ist gesund. Weiter so!", | |
| 'Apple__rust': "Rost wurde erkannt. Entfernen Sie infizierte Blätter und behandeln Sie mit einem Fungizid.", | |
| 'Apple__scab': "Schorf ist sichtbar. Beschneiden Sie betroffene Bereiche und verwenden Sie Fungizide." | |
| } | |
| return responses.get(predicted_class, "Unbekannte Krankheit") | |
| # Define example images | |
| examples = [ | |
| ["images/Apple__black_rot.JPG"], | |
| ["images/Apple__healthy.JPG"], | |
| ["images/Apple__rust.JPG"], | |
| ["images/Apple__scab.JPG"] | |
| ] | |
| # Define the Gradio interface | |
| iface = gr.Interface( | |
| fn=predict_figure, | |
| inputs=gr.File(label="Datei hochladen"), | |
| outputs=["image", "text", "text"], | |
| title="Klassifikator für Apfelblattkrankheiten", | |
| description="Lade ein Bild eines Apfelblatts hoch, um den Gesundheitszustand und die Vertrauenswürdigkeit des Modells zu sehen.", | |
| examples=examples | |
| ) | |
| iface.launch() |