Spaces:
Sleeping
Sleeping
File size: 2,040 Bytes
cd9ab2f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
model_path = "transferlearning_tools.keras"
model = tf.keras.models.load_model(model_path)
# Define the core prediction function
def predict_tools(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
image = np.array(image)
image = np.expand_dims(image, axis=0) # Expand dimensions to match the model input shape
# Predict
prediction = model.predict(image)
# Print the shape of the prediction to debug
print(f"Prediction shape: {prediction.shape}")
# Assuming the output is already softmax probabilities
probabilities = prediction[0]
# Print the probabilities array to debug
print(f"Probabilities: {probabilities}")
# Assuming your model was trained with these class names
class_names = ['Gasoline can', 'Hammer', 'Rope', 'Screw driver', 'Wrench']
# Create a dictionary of class probabilities
result = {class_names[i]: float(probabilities[i]) for i in range(len(class_names))}
return result
# Create the Gradio interface
input_image = gr.Image()
iface = gr.Interface(
fn=predict_tools,
inputs=input_image,
outputs=gr.Label(),
examples=["tools_examples/Gasoline can 1.jpg",
"tools_examples/Gasoline can 2.jpg",
"tools_examples/Hammer 1.jpg",
"tools_examples/Hammer 2.jpg",
"tools_examples/Rope 1.jpg",
"tools_examples/Rope 2.jpg",
"tools_examples/Screw driver 1.jpg",
"tools_examples/Screw driver 2.jpg",
"tools_examples/Wrench 1.jpg",
"tools_examples/Wrench 2.jpg"],
description="A simple mlp classification model for image classification using the mnist dataset.")
iface.launch()
|