Bhuvi20's picture
Update app.py
ae279d6 verified
import gradio as gr
import numpy as np
import tensorflow as tf
import cv2
# Load the trained model
model = tf.keras.models.load_model("Handwritten_model.h5") # Make sure the filename matches your uploaded model
def predict_digit(image):
# Preprocess the image
img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # Convert to grayscale
img = cv2.resize(img, (28, 28)) # Resize to 28x28 pixels
img = np.invert(img) # Invert the image colors
img = img.astype("float32") / 255 # Normalize to [0, 1]
img = img.reshape(1, 28, 28) # Reshape for the model input
# Make prediction
prediction = model.predict(img)
predicted_class = np.argmax(prediction)
return predicted_class
# Define the Gradio interface using the updated API
gr.Interface(
fn=predict_digit,
inputs=gr.Image(type="numpy", label="Upload a digit image"), # Updated to use gr.Image
outputs="text",
title="Handwritten Digit Recognition",
description="Upload an image of a handwritten digit, and the model will predict which digit it is."
).launch()