Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,47 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from torchvision import models, transforms
|
| 5 |
-
from PIL import Image
|
| 6 |
|
| 7 |
-
# 1.
|
| 8 |
-
|
| 9 |
-
model = models.resnet18(weights=None)
|
| 10 |
-
model.fc = nn.Linear(model.fc.in_features, 10) # Adjust head to 10 classes
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
if image is None: return None
|
| 32 |
-
img_tensor = transform(image).unsqueeze(0)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
| 37 |
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
outputs=gr.Label(num_top_classes=3),
|
| 45 |
title="Handwritten Digit Recognizer",
|
| 46 |
-
description="
|
| 47 |
)
|
| 48 |
|
|
|
|
| 49 |
if __name__ == "__main__":
|
| 50 |
-
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# 1. Load the trained model
|
| 7 |
+
model = tf.keras.models.load_model('digit_recognizer.keras')
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# 2. Define the classification function
|
| 10 |
+
def classify_digit(image):
|
| 11 |
+
if image is None:
|
| 12 |
+
return None
|
| 13 |
+
|
| 14 |
+
# Preprocessing to match MNIST data format
|
| 15 |
+
# Convert to grayscale if it isn't already
|
| 16 |
+
if len(image.shape) == 3:
|
| 17 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 18 |
+
|
| 19 |
+
# Resize the image to 28x28 pixels
|
| 20 |
+
image = cv2.resize(image, (28, 28))
|
| 21 |
+
|
| 22 |
+
# Reshape to (1, 28, 28, 1) to match model input shape
|
| 23 |
+
# The '1' indicates a batch size of 1
|
| 24 |
+
image = image.reshape(1, 28, 28, 1)
|
| 25 |
+
|
| 26 |
+
# Normalize pixel values (0 to 1) just like in the training notebook
|
| 27 |
+
image = image / 255.0
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Predict
|
| 30 |
+
prediction = model.predict(image).flatten()
|
|
|
|
| 31 |
|
| 32 |
+
# Return dictionary for Gradio Label output
|
| 33 |
+
return {str(i): float(prediction[i]) for i in range(10)}
|
| 34 |
|
| 35 |
+
# 3. Build the Gradio Interface
|
| 36 |
+
# We use Sketchpad so users can draw the digit
|
| 37 |
+
interface = gr.Interface(
|
| 38 |
+
fn=classify_digit,
|
| 39 |
+
inputs=gr.Sketchpad(label="Draw a Digit"),
|
| 40 |
outputs=gr.Label(num_top_classes=3),
|
| 41 |
title="Handwritten Digit Recognizer",
|
| 42 |
+
description="Draw a digit (0-9) on the canvas to see if the Neural Network recognizes it."
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# 4. Launch
|
| 46 |
if __name__ == "__main__":
|
| 47 |
+
interface.launch()
|