Spaces:
Sleeping
Sleeping
import gradio as gr | |
import tensorflow as tf | |
import numpy as np | |
from PIL import Image | |
model_path = "BMWXModelClassifier.keras" | |
model = tf.keras.models.load_model(model_path) | |
# Define the core prediction function | |
def predict_bmwX(image): | |
# Preprocess image | |
print(type(image)) | |
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image | |
image = image.resize((224, 224)) #resize the image to 224x224 | |
image = np.array(image) | |
image = np.expand_dims(image, axis=0) # same as image[None, ...] | |
# Predict | |
prediction = model.predict(image) | |
# Apply softmax to get probabilities for each class | |
prediction = tf.nn.softmax(prediction) | |
# Create a dictionary with the probabilities for each Pokemon | |
x1 = np.round(float(prediction[0][0]), 2) | |
x2 = np.round(float(prediction[0][1]), 2) | |
x3 = np.round(float(prediction[0][2]), 2) | |
x4 = np.round(float(prediction[0][3]), 2) | |
x5 = np.round(float(prediction[0][4]), 2) | |
x6 = np.round(float(prediction[0][5]), 2) | |
x7 = np.round(float(prediction[0][6]), 2) | |
return {'X1': x1, 'X2': x2, 'X3': x3, 'X4': x4, 'X5': x5, 'X6': x6, 'X7': x7} | |
input_image = gr.Image() | |
iface = gr.Interface( | |
fn=predict_bmwX, | |
inputs=input_image, | |
outputs=gr.Label(), | |
description="A simple mlp classification model for image classification using the mnist dataset.") | |
iface.launch(share=True) |