Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import cv2
|
| 5 |
+
|
| 6 |
+
# Load the trained model
|
| 7 |
+
model = tf.keras.models.load_model("Handwritten_model.h5") # Adjust this if you use the SavedModel format
|
| 8 |
+
|
| 9 |
+
def predict_digit(image):
|
| 10 |
+
# Preprocess the image
|
| 11 |
+
img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # Convert to grayscale
|
| 12 |
+
img = cv2.resize(img, (28, 28)) # Resize to 28x28 pixels
|
| 13 |
+
img = np.invert(img) # Invert the image colors
|
| 14 |
+
img = img.astype("float32") / 255 # Normalize to [0, 1]
|
| 15 |
+
img = img.reshape(1, 28, 28) # Reshape for the model input
|
| 16 |
+
|
| 17 |
+
# Make prediction
|
| 18 |
+
prediction = model.predict(img)
|
| 19 |
+
predicted_class = np.argmax(prediction)
|
| 20 |
+
return predicted_class
|
| 21 |
+
|
| 22 |
+
# Define the Gradio interface
|
| 23 |
+
gr.Interface(
|
| 24 |
+
fn=predict_digit,
|
| 25 |
+
inputs=gr.inputs.Image(type="numpy", label="Upload a digit image"),
|
| 26 |
+
outputs="text",
|
| 27 |
+
title="Handwritten Digit Recognition",
|
| 28 |
+
description="Upload an image of a handwritten digit, and the model will predict which digit it is."
|
| 29 |
+
).launch()
|