Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,40 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
|
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
9 |
|
|
|
10 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
demo.launch()
|
|
|
1 |
+
import cv2
|
2 |
import gradio as gr
|
3 |
+
import tensorflow as tf
|
4 |
+
import numpy as np
|
5 |
|
6 |
+
# Load the trained model
|
7 |
+
model = tf.keras.models.load_model("number_recognition_model_colab.keras")
|
8 |
|
9 |
+
# Image size and labels
|
10 |
+
img_size = 28
|
11 |
+
labels = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
|
12 |
|
13 |
+
# Prediction function
|
14 |
+
def predict(img):
|
15 |
+
try:
|
16 |
+
# Ensure the image is a NumPy array and grayscale
|
17 |
+
img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY)
|
18 |
+
img = cv2.resize(img, (img_size, img_size))
|
19 |
+
img = img.astype("float32") / 255.0
|
20 |
+
img = img.reshape(1, img_size, img_size, 1)
|
21 |
|
22 |
+
# Make predictions
|
23 |
+
preds = model.predict(img)[0]
|
24 |
+
return {label: float(pred) for label, pred in zip(labels, preds)}
|
25 |
+
except Exception as e:
|
26 |
+
return {"Error": str(e)}
|
27 |
|
28 |
+
# Gradio interface
|
29 |
if __name__ == "__main__":
|
30 |
+
demo = gr.Interface(
|
31 |
+
fn=predict, # Function to call
|
32 |
+
inputs=gr.Sketchpad(label="Draw a number"), # Sketchpad input
|
33 |
+
outputs=gr.Label(num_top_classes=3), # Label output to show top 3 predictions
|
34 |
+
title="Number Recognition App",
|
35 |
+
description=(
|
36 |
+
"The model was trained to classify numbers (from 0 to 9). "
|
37 |
+
"Draw a number in the sketchpad below and see the prediction!"
|
38 |
+
)
|
39 |
+
)
|
40 |
demo.launch()
|