import gradio as gr import tensorflow as tf print(tf.__version__) import numpy as np from PIL import Image import os model_path = "apple_model_transferlearning.keras" model = tf.keras.models.load_model(model_path) def predict_apple(image): # Preprocess image print(type(image)) image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image image = image.resize((150, 150)) # Resize the image to 150x150 pixels image = np.array(image) image = np.expand_dims(image, axis=0) # Add batch dimension # Predict prediction = model.predict(image) # Convert the probabilities to rounded values prediction = np.round(prediction, 2) # Make sure the indices are correct according to your model's training p_schorf = prediction[0][0] # Probability for "Schorf" p_schwarzfaeule = prediction[0][1] # Probability for "Schwarzfaeule" p_zederapfel = prediction[0][2] # Probability for "Zederapfel" p_gesund = prediction[0][3] # Probability for "Gesund" return {'Gesund': p_gesund, 'Schorf': p_schorf, 'Schwarzfaeule': p_schwarzfaeule, 'Zederapfel': p_zederapfel} # Create the Gradio interface input_image = gr.Image() iface = gr.Interface( fn=predict_apple, inputs=input_image, outputs=gr.Label(), examples=["images/Gesund1.JPG", "images/Gesund2.JPG", "images/Gesund3.JPG", "images/Schorf1.JPG", "images/Schorf2.JPG", "images/Schorf3.JPG", "images/Schwarzfaeule1.JPG", "images/Schwarzfaeule2.JPG", "images/Schwarzfaeule3.JPG", "images/Zederapfel1.JPG", "images/Zederapfel2.JPG", "images/Zederapfel3.JPG"], description="Applikation zur Überwachung der Gesundheit von Apfelbäumen") iface.launch()