Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import tensorflow as tf | |
| from PIL import Image | |
| import numpy as np | |
| # Laden des vortrainierten Blumen-Modells | |
| model_path = "Flower_Classifier_ResNet50V2.h5" | |
| model = tf.keras.models.load_model(model_path) | |
| # Labels für die Blumen | |
| labels = [ | |
| 'Daisy', 'Dandelion', 'Lavender', 'Lilly', 'Lotus', 'Orchid', 'Rose', 'Sunflower', 'Tulip' | |
| ] | |
| def predict_flower(image): | |
| # Preprocess image | |
| image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image | |
| image = image.resize((224, 224)) | |
| image = np.array(image) | |
| image = np.expand_dims(image, axis=0) # same as image[None, ...] | |
| # Predict | |
| predictions = model.predict(image) | |
| prediction = np.argmax(predictions, axis=1)[0] | |
| confidence = np.max(predictions) | |
| # Vorbereiten der Ausgabe | |
| result = f"Predicted Flower: {labels[prediction]} with confidence: {confidence:.2f}" | |
| return result | |
| # Erstellen der Gradio-Oberfläche | |
| input_image = gr.Image() | |
| output_label = gr.Label() | |
| interface = gr.Interface(fn=predict_flower, | |
| inputs=input_image, | |
| outputs=output_label, | |
| examples=["Daisy.jpg", "Dandelion1.jpg", "Dandelion2.jpg", "Lavender.jpg", "Lilly.jpg", "Lotus.jpg","Orchid.jpg", "Rose.jpg", "Sunflower.jpg", "Tulip.jpg"], | |
| title="Flower Classifier", | |
| description="Drag and drop an image or select an example below to predict the Flower.") | |
| # Interface starten | |
| interface.launch() |