import gradio as gr import tensorflow as tf import numpy as np from PIL import Image model_path = "BMW_transfer_learning.keras" model = tf.keras.models.load_model(model_path) # Define the core prediction function def predict_bmw(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 28x28 and converts it to gray scale image = np.array(image) image = np.expand_dims(image, axis=0) # same as image[None, ...] # Predict prediction = model.predict(image) # Because the output layer was dense(0) without an activation function, we need to apply sigmoid to get the probability # we could also change the output layer to dense(4, activation='sigmoid') prediction = np.round(prediction, 2) # Separate the probabilities for each class p_m2 = prediction[0][0] # Probability for class 'm2' p_m4 = prediction[0][1] # Probability for class 'm4' p_x6 = prediction[0][2] # Probability for class 'x6' p_i8 = prediction[0][3] # Probability for class 'i8' return {'M2': p_m2, 'M4': p_m4, 'X6': p_x6, 'i8': p_i8} # Create the Gradio interface input_image = gr.Image() iface = gr.Interface( fn=predict_bmw, inputs=input_image, outputs=gr.Label(), examples=["images/m2.jpg", "images/m2_1.jpg", "images/m4.jpg", "images/m4_1.jpg", "images/x6.jpg", "images/x6_1.jpg", "images/i8.jpg", "images/i8_1.jpg"], description="A simple mlp classification model for image classification using the mnist dataset.") iface.launch(share=True)