Spaces:
Sleeping
Sleeping
Added files to hf space
Browse files- app.py +48 -0
- mnist_model.h5 +3 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
from PIL import Image
|
5 |
+
import tensorflow as tf
|
6 |
+
|
7 |
+
# Load the trained model
|
8 |
+
model = tf.keras.models.load_model('mnist_model.h5')
|
9 |
+
|
10 |
+
def cnn_predict_digit(image):
|
11 |
+
# Handle Gradio Sketchpad dictionary input
|
12 |
+
if isinstance(image, dict) and 'composite' in image:
|
13 |
+
image = image['composite']
|
14 |
+
|
15 |
+
# Convert to grayscale if RGB
|
16 |
+
if image.ndim == 3 and image.shape[2] == 3:
|
17 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
18 |
+
|
19 |
+
# Invert colors (white background → black background)
|
20 |
+
image = 255 - image
|
21 |
+
|
22 |
+
# Resize to 28x28
|
23 |
+
image = cv2.resize(image, (28, 28))
|
24 |
+
|
25 |
+
# Normalize and reshape
|
26 |
+
image = image.astype('float32') / 255.0
|
27 |
+
image = image.reshape(1, 28, 28, 1)
|
28 |
+
|
29 |
+
# Predict
|
30 |
+
prediction = model.predict(image)
|
31 |
+
pred_label = np.argmax(prediction, axis=1)[0]
|
32 |
+
|
33 |
+
return str(pred_label)
|
34 |
+
|
35 |
+
with gr.Blocks() as interface:
|
36 |
+
gr.Markdown(
|
37 |
+
"""
|
38 |
+
## ✍️ Digit Classification using Convolutional Neural Network
|
39 |
+
Draw a digit in the sketchpad below (0 to 9), then click **Submit** to see the prediction.
|
40 |
+
"""
|
41 |
+
)
|
42 |
+
with gr.Row():
|
43 |
+
sketchpad = gr.Sketchpad(image_mode='L')
|
44 |
+
output = gr.Label()
|
45 |
+
gr.Button("Submit").click(cnn_predict_digit, inputs=sketchpad, outputs=output)
|
46 |
+
gr.ClearButton([sketchpad, output])
|
47 |
+
|
48 |
+
interface.launch()
|
mnist_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2c435ee80c60965b62fc5bd5b47fb5ede1ea23ada102062cc46237904c67eb41
|
3 |
+
size 1168216
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow
|
2 |
+
gradio
|
3 |
+
opencv-python
|
4 |
+
numpy
|
5 |
+
Pillow
|