Spaces:
Running
Running
File size: 1,551 Bytes
d70585e 044b146 20d3dff 90c9e9d 044b146 d70585e d9323d9 7b7ada0 9e8c390 d9323d9 dc05c78 2fb41bc 7b7ada0 d9323d9 f89da7b d9323d9 d70585e 2fb41bc fabe0a3 8d28437 3a813cf d70585e 8d28437 d70585e 5620b07 |
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 |
import gradio as gr
import tensorflow as tf
import numpy as np
# Load the trained model denis_mnist_cnn_model_v4.h5
model = tf.keras.models.load_model("denis_mnist_cnn_model_v4.h5")
# Preprocessing function for images
def preprocess_image(image):
# Resize the image to 28x28 as expected by the model
image = tf.image.resize(image, (28, 28)) # Resize to 28x28
print(f"Image shape after resizing: {image.shape}")
# Convert the image to grayscale (1 channel) directly
image = tf.image.rgb_to_grayscale(image) # Convert RGB to grayscale
print(f"Image shape after converting to grayscale: {image.shape}")
# Normalize pixel values to [0, 1]
image = tf.cast(image, tf.float32) / 255.0
# Add batch dimension (model expects batch of images)
image = tf.expand_dims(image, axis=0)
print(f"Image shape after adding batch dimension: {image.shape}")
return image
# Function to make predictions
def predict(image):
image = preprocess_image(image)
prediction = model.predict(image) # Predict
predicted_class = np.argmax(prediction, axis=-1)[0] # Get the predicted class
class_confidence = np.max(prediction, axis=-1)[0] # Get the confidence score
#return {"prediction": int(predicted_class), "confidence": class_confidence}
return {"prediction": int(predicted_class)}
# Create a Gradio interface
interface = gr.Interface(fn=predict, inputs="image", outputs="json")
# Launch the Gradio interface
if __name__ == "__main__":
interface.launch(share=True)
|