Soundarmsd-7's picture
1
53f137e verified
Raw
History Blame Contribute Delete
918 Bytes
import gradio as gr
import numpy as np
import cv2
from tensorflow.keras.models import load_model
model = load_model("saved_models/model.h5")
def predict_digit(image):
img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
img = cv2.resize(img, (28, 28))
img = cv2.bitwise_not(img)
_, img = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)
img = img.astype("float32") / 255.0
img = np.expand_dims(img, axis=-1)
img = np.expand_dims(img, axis=0)
prediction = model.predict(img)
digit = np.argmax(prediction)
confidence = np.max(prediction) * 100
return f"πŸ”’ Predicted Digit: {digit}\nπŸ“Š Confidence: {confidence:.2f}%"
gr.Interface(
fn=predict_digit,
inputs=gr.Image(shape=(200, 200), tool="editor", label="Draw a digit"),
outputs="text",
title="🧠 Handwritten Digit Recognizer",
description="Draw a digit (0–9) and let the model predict it"
).launch()