import gradio as gr import tensorflow as tf from PIL import Image import numpy as np # Load your custom regression model model_path = "freshwaterfish_classifier_model_BasicKeras.keras" model = tf.keras.models.load_model(model_path) labels = ['Catfish', 'Freshwater Eel', 'Goby', 'Perch'] # Define regression function def predict_regression(image): # Preprocess image image = Image.fromarray(image.astype('uint8')) image = image.resize((224, 224)).convert('RGB') image = np.array(image) print(image.shape) # Predict prediction = model.predict(image[None, ...]) # Assuming single regression value confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))} return confidences # Define the file paths for the test images test_images = ["myImages/Egli1.jpeg", "myImages/Egli2.jpeg", "myImages/Wels1.jpeg", "myImages/Wels2.jpeg"] # Load and preprocess the test images example_images = [np.array(Image.open(image_path).resize((224, 224)).convert('RGB')) / 255.0 for image_path in test_images] # Create Gradio interface input_image = gr.Image() output_text = gr.Textbox(label="Predicted Value") interface = gr.Interface(fn=predict_regression, inputs=input_image, outputs=gr.Label(), title="Freshwater Fish Classifier", description="This model predicts four species of a freshwater fish from the lake of constance.", examples=example_images ) interface.launch()