Spaces:
Sleeping
Sleeping
Update Home.py
Browse files
Home.py
CHANGED
@@ -1,109 +1,92 @@
|
|
1 |
import streamlit as st
|
2 |
from streamlit_drawable_canvas import st_canvas
|
3 |
-
from keras.models import load_model
|
4 |
import numpy as np
|
5 |
import cv2
|
6 |
|
7 |
-
#
|
8 |
-
st.set_page_config(page_title="
|
9 |
|
10 |
-
# Custom CSS
|
11 |
-
st.markdown(
|
12 |
-
"""
|
13 |
<style>
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
.prediction-text {
|
38 |
-
font-size: 3em;
|
39 |
-
font-weight: bold;
|
40 |
-
color: #ff4d4d;
|
41 |
-
text-shadow: 2px 2px 8px #ff0000;
|
42 |
-
}
|
43 |
-
.canvas-title {
|
44 |
-
text-align: center;
|
45 |
-
color: #00ccff;
|
46 |
-
font-size: 1.2em;
|
47 |
-
margin-bottom: 10px;
|
48 |
-
}
|
49 |
-
.sidebar .sidebar-content {
|
50 |
-
background-color: #2c2c2c;
|
51 |
-
}
|
52 |
</style>
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
""",
|
59 |
-
unsafe_allow_html=True
|
60 |
-
)
|
61 |
|
62 |
-
# Sidebar
|
63 |
-
st.sidebar.header("
|
64 |
-
drawing_mode = st.sidebar.selectbox("
|
65 |
-
stroke_width = st.sidebar.slider("
|
66 |
-
stroke_color = st.sidebar.color_picker("
|
67 |
-
bg_color = st.sidebar.color_picker("Canvas background
|
68 |
-
realtime_update = st.sidebar.checkbox("
|
69 |
|
70 |
-
# Load
|
71 |
@st.cache_resource
|
72 |
-
def
|
73 |
-
return load_model("
|
74 |
|
75 |
-
model =
|
76 |
|
77 |
-
#
|
78 |
-
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
key="canvas",
|
92 |
-
)
|
93 |
|
94 |
-
|
|
|
95 |
if canvas_result.image_data is not None:
|
96 |
-
st.image(canvas_result.image_data, caption="Your Drawing",
|
97 |
-
|
|
|
98 |
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
99 |
-
img = 255 - img
|
100 |
img_resized = cv2.resize(img, (28, 28))
|
101 |
img_normalized = img_resized / 255.0
|
102 |
img_reshaped = img_normalized.reshape((1, 28, 28))
|
|
|
|
|
103 |
prediction = model.predict(img_reshaped)
|
|
|
104 |
|
105 |
-
# Display
|
106 |
-
st.markdown(
|
107 |
-
|
108 |
-
|
109 |
-
)
|
|
|
1 |
import streamlit as st
|
2 |
from streamlit_drawable_canvas import st_canvas
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
import numpy as np
|
5 |
import cv2
|
6 |
|
7 |
+
# App configuration
|
8 |
+
st.set_page_config(page_title="DigitSketch - AI Digit Classifier", layout="centered")
|
9 |
|
10 |
+
# Custom styling with CSS
|
11 |
+
st.markdown("""
|
|
|
12 |
<style>
|
13 |
+
.stApp {
|
14 |
+
background-color: #121212;
|
15 |
+
color: #f0f0f0;
|
16 |
+
}
|
17 |
+
h1 {
|
18 |
+
color: #00ffff;
|
19 |
+
text-align: center;
|
20 |
+
text-shadow: 1px 1px 8px #00ffff;
|
21 |
+
}
|
22 |
+
.digit-result {
|
23 |
+
text-align: center;
|
24 |
+
font-size: 2.5em;
|
25 |
+
font-weight: bold;
|
26 |
+
color: #ff4d4d;
|
27 |
+
text-shadow: 1px 1px 10px #ff4d4d;
|
28 |
+
margin-top: 20px;
|
29 |
+
}
|
30 |
+
.canvas-title {
|
31 |
+
text-align: center;
|
32 |
+
color: #80dfff;
|
33 |
+
font-size: 1.2em;
|
34 |
+
margin-bottom: 10px;
|
35 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
</style>
|
37 |
+
""", unsafe_allow_html=True)
|
38 |
+
|
39 |
+
# App title and description
|
40 |
+
st.title("๐จ DigitSketch: AI Handwritten Digit Classifier")
|
41 |
+
st.markdown("Draw a digit between **0โ9** below, then click **๐ฎ Predict** to see what the AI thinks it is!")
|
|
|
|
|
|
|
42 |
|
43 |
+
# Sidebar: Drawing settings
|
44 |
+
st.sidebar.header("๐ ๏ธ Drawing Controls")
|
45 |
+
drawing_mode = st.sidebar.selectbox("Choose a drawing tool:", ("freedraw", "line", "rect", "circle", "transform"))
|
46 |
+
stroke_width = st.sidebar.slider("Pen thickness", 1, 25, 10)
|
47 |
+
stroke_color = st.sidebar.color_picker("Pen color", "#FFFFFF")
|
48 |
+
bg_color = st.sidebar.color_picker("Canvas background", "#000000")
|
49 |
+
realtime_update = st.sidebar.checkbox("Live update", True)
|
50 |
|
51 |
+
# Load the trained model
|
52 |
@st.cache_resource
|
53 |
+
def load_digit_model():
|
54 |
+
return load_model("digit_reco.keras")
|
55 |
|
56 |
+
model = load_digit_model()
|
57 |
|
58 |
+
# Canvas drawing area
|
59 |
+
st.markdown('<div class="canvas-title">โ๏ธ Draw your digit below</div>', unsafe_allow_html=True)
|
60 |
|
61 |
+
canvas_result = st_canvas(
|
62 |
+
fill_color="rgba(255, 255, 255, 0.0)", # Transparent fill
|
63 |
+
stroke_width=stroke_width,
|
64 |
+
stroke_color=stroke_color,
|
65 |
+
background_color=bg_color,
|
66 |
+
update_streamlit=realtime_update,
|
67 |
+
height=280,
|
68 |
+
width=280,
|
69 |
+
drawing_mode=drawing_mode,
|
70 |
+
key="canvas",
|
71 |
+
)
|
|
|
|
|
72 |
|
73 |
+
# Predict Button
|
74 |
+
if st.button("๐ฎ Predict"):
|
75 |
if canvas_result.image_data is not None:
|
76 |
+
st.image(canvas_result.image_data, caption="๐ผ๏ธ Your Drawing", use_container_width=True)
|
77 |
+
|
78 |
+
# Image preprocessing
|
79 |
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
80 |
+
img = 255 - img # Invert for white digit on black
|
81 |
img_resized = cv2.resize(img, (28, 28))
|
82 |
img_normalized = img_resized / 255.0
|
83 |
img_reshaped = img_normalized.reshape((1, 28, 28))
|
84 |
+
|
85 |
+
# Predict
|
86 |
prediction = model.predict(img_reshaped)
|
87 |
+
predicted_digit = np.argmax(prediction)
|
88 |
|
89 |
+
# Display result
|
90 |
+
st.markdown(f'<div class="digit-result">Predicted Digit: {predicted_digit}</div>', unsafe_allow_html=True)
|
91 |
+
else:
|
92 |
+
st.warning("โ ๏ธ Please draw something before clicking Predict.")
|
|