Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,25 +3,25 @@ import cv2
|
|
3 |
from streamlit_drawable_canvas import st_canvas
|
4 |
from keras.models import load_model
|
5 |
import numpy as np
|
|
|
6 |
|
7 |
-
# Sidebar
|
8 |
-
drawing_mode = st.sidebar.selectbox("Drawing tool:", ("freedraw",
|
9 |
-
stroke_width = st.sidebar.slider("Stroke width: ",
|
10 |
-
stroke_color =
|
11 |
-
bg_color =
|
12 |
-
bg_image = st.sidebar.file_uploader("Background image:", type=["png", "jpg"])
|
13 |
realtime_update = st.sidebar.checkbox("Update in realtime", True)
|
14 |
|
15 |
-
# Load
|
16 |
@st.cache_resource
|
17 |
def load_mnist_model():
|
18 |
return load_model("mnist.keras")
|
19 |
|
20 |
model = load_mnist_model()
|
21 |
|
22 |
-
#
|
23 |
canvas_result = st_canvas(
|
24 |
-
fill_color="rgba(
|
25 |
stroke_width=stroke_width,
|
26 |
stroke_color=stroke_color,
|
27 |
background_color=bg_color,
|
@@ -32,31 +32,52 @@ canvas_result = st_canvas(
|
|
32 |
key="canvas",
|
33 |
)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
|
42 |
-
#
|
43 |
-
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
# Resize
|
46 |
-
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
#
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
# Normalize
|
52 |
-
|
53 |
|
54 |
-
#
|
55 |
-
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
st.image(img_thresh, caption="Preprocessed (28x28 Thresholded)")
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
3 |
from streamlit_drawable_canvas import st_canvas
|
4 |
from keras.models import load_model
|
5 |
import numpy as np
|
6 |
+
from scipy.ndimage import interpolation
|
7 |
|
8 |
+
# Sidebar: Canvas controls
|
9 |
+
drawing_mode = st.sidebar.selectbox("Drawing tool:", ("freedraw",))
|
10 |
+
stroke_width = st.sidebar.slider("Stroke width: ", 10, 25, 20)
|
11 |
+
stroke_color = "#000000" # Black
|
12 |
+
bg_color = "#FFFFFF" # White
|
|
|
13 |
realtime_update = st.sidebar.checkbox("Update in realtime", True)
|
14 |
|
15 |
+
# Load MNIST model
|
16 |
@st.cache_resource
|
17 |
def load_mnist_model():
|
18 |
return load_model("mnist.keras")
|
19 |
|
20 |
model = load_mnist_model()
|
21 |
|
22 |
+
# Streamlit drawable canvas
|
23 |
canvas_result = st_canvas(
|
24 |
+
fill_color="rgba(0,0,0,0)", # Transparent fill
|
25 |
stroke_width=stroke_width,
|
26 |
stroke_color=stroke_color,
|
27 |
background_color=bg_color,
|
|
|
32 |
key="canvas",
|
33 |
)
|
34 |
|
35 |
+
def preprocess(img):
|
36 |
+
# Convert to grayscale
|
37 |
+
gray = cv2.cvtColor(img.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
38 |
+
|
39 |
+
# Invert (black bg, white digit)
|
40 |
+
gray = 255 - gray
|
41 |
|
42 |
+
# Apply threshold
|
43 |
+
_, thresh = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)
|
44 |
|
45 |
+
# Crop the digit (remove empty rows/cols)
|
46 |
+
if np.sum(thresh) == 0:
|
47 |
+
return None # blank canvas
|
48 |
+
coords = cv2.findNonZero(thresh)
|
49 |
+
x, y, w, h = cv2.boundingRect(coords)
|
50 |
+
cropped = thresh[y:y+h, x:x+w]
|
51 |
|
52 |
+
# Resize keeping aspect ratio
|
53 |
+
h, w = cropped.shape
|
54 |
+
if h > w:
|
55 |
+
resized = cv2.resize(cropped, (int(20 * w / h), 20))
|
56 |
+
else:
|
57 |
+
resized = cv2.resize(cropped, (20, int(20 * h / w)))
|
58 |
|
59 |
+
# Add padding to get 28x28
|
60 |
+
h, w = resized.shape
|
61 |
+
top = (28 - h) // 2
|
62 |
+
bottom = 28 - h - top
|
63 |
+
left = (28 - w) // 2
|
64 |
+
right = 28 - w - left
|
65 |
+
padded = cv2.copyMakeBorder(resized, top, bottom, left, right, cv2.BORDER_CONSTANT, value=0)
|
66 |
|
67 |
# Normalize
|
68 |
+
norm = padded / 255.0
|
69 |
|
70 |
+
# Final reshape
|
71 |
+
return norm.reshape(1, 28, 28, 1)
|
72 |
+
|
73 |
+
if canvas_result.image_data is not None:
|
74 |
+
st.image(canvas_result.image_data, caption="Original Drawing")
|
75 |
|
76 |
+
processed = preprocess(canvas_result.image_data)
|
|
|
77 |
|
78 |
+
if processed is not None:
|
79 |
+
st.image(processed.reshape(28, 28), caption="Processed Input (28x28)")
|
80 |
+
pred = model.predict(processed)
|
81 |
+
st.subheader(f"Prediction: {np.argmax(pred)}")
|
82 |
+
else:
|
83 |
+
st.warning("Please draw a digit!")
|