Spaces:
Build error
Build error
Update Home.py
Browse files
Home.py
CHANGED
|
@@ -4,110 +4,112 @@ 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 |
-
#
|
| 11 |
st.markdown("""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
""", unsafe_allow_html=True)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
st.markdown("<h1>Digit Recognizer
|
| 59 |
-
st.markdown("<div
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
st.sidebar.title("
|
| 63 |
drawing_mode = st.sidebar.selectbox("Drawing Tool", ("freedraw", "line", "rect", "circle", "transform"))
|
| 64 |
stroke_width = st.sidebar.slider("Stroke Width", 1, 25, 10)
|
| 65 |
stroke_color = st.sidebar.color_picker("Stroke Color", "#FFFFFF")
|
| 66 |
bg_color = st.sidebar.color_picker("Background Color", "#000000")
|
| 67 |
realtime_update = st.sidebar.checkbox("Update in Realtime", True)
|
| 68 |
|
| 69 |
-
#
|
| 70 |
@st.cache_resource
|
| 71 |
def load_mnist_model():
|
| 72 |
return load_model("digit_recognization.keras")
|
| 73 |
|
| 74 |
model = load_mnist_model()
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
st.markdown("<div class='section'>", unsafe_allow_html=True)
|
| 92 |
-
|
| 93 |
-
col1, col2 = st.columns(2)
|
| 94 |
-
|
| 95 |
-
with col1:
|
| 96 |
-
st.image(canvas_result.image_data, caption="Your Drawing", use_container_width=True)
|
| 97 |
-
|
| 98 |
-
with col2:
|
| 99 |
-
# Preprocess
|
| 100 |
-
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
| 101 |
-
img_resized = cv2.resize(img, (28, 28))
|
| 102 |
-
img_normalized = img_resized / 255.0
|
| 103 |
-
img_reshaped = img_normalized.reshape((1, 28, 28))
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
| 113 |
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
| 6 |
|
| 7 |
+
# Set full-screen dark theme
|
| 8 |
+
st.set_page_config(page_title="🧠 Digit Recognizer", layout="centered")
|
| 9 |
|
| 10 |
+
# Inject custom glowing CSS styles
|
| 11 |
st.markdown("""
|
| 12 |
+
<style>
|
| 13 |
+
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@600&family=Poppins:wght@400;600&display=swap');
|
| 14 |
+
|
| 15 |
+
html, body, [class*="css"] {
|
| 16 |
+
font-family: 'Poppins', sans-serif;
|
| 17 |
+
background: linear-gradient(to right, #141e30, #243b55);
|
| 18 |
+
color: white;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
h1 {
|
| 22 |
+
font-family: 'Orbitron', sans-serif;
|
| 23 |
+
color: #00ffff;
|
| 24 |
+
text-align: center;
|
| 25 |
+
font-size: 3em;
|
| 26 |
+
text-shadow: 0px 0px 10px rgba(0, 255, 255, 0.9);
|
| 27 |
+
margin-bottom: 5px;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
.subtext {
|
| 31 |
+
text-align: center;
|
| 32 |
+
font-size: 18px;
|
| 33 |
+
color: #dddddd;
|
| 34 |
+
margin-bottom: 30px;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
.canvas-box {
|
| 38 |
+
background: rgba(255, 255, 255, 0.08);
|
| 39 |
+
padding: 20px;
|
| 40 |
+
border-radius: 20px;
|
| 41 |
+
box-shadow: 0 0 20px rgba(0,255,255,0.2);
|
| 42 |
+
text-align: center;
|
| 43 |
+
margin: auto;
|
| 44 |
+
width: fit-content;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
.prediction-box {
|
| 48 |
+
margin-top: 25px;
|
| 49 |
+
padding: 15px;
|
| 50 |
+
font-size: 2.2em;
|
| 51 |
+
font-weight: bold;
|
| 52 |
+
color: #00ffff;
|
| 53 |
+
background: rgba(255, 255, 255, 0.07);
|
| 54 |
+
border-radius: 12px;
|
| 55 |
+
text-align: center;
|
| 56 |
+
box-shadow: 0 0 12px rgba(0,255,255,0.4);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
.emoji {
|
| 60 |
+
text-align: center;
|
| 61 |
+
font-size: 3em;
|
| 62 |
+
margin-top: 15px;
|
| 63 |
+
}
|
| 64 |
+
</style>
|
| 65 |
""", unsafe_allow_html=True)
|
| 66 |
|
| 67 |
+
# Title & subtitle
|
| 68 |
+
st.markdown("<h1>Digit Recognizer</h1>", unsafe_allow_html=True)
|
| 69 |
+
st.markdown("<div class='subtext'>Draw a digit (0-9) below and let AI guess it!</div>", unsafe_allow_html=True)
|
| 70 |
|
| 71 |
+
# Sidebar - Drawing settings
|
| 72 |
+
st.sidebar.title("🎨 Drawing Settings")
|
| 73 |
drawing_mode = st.sidebar.selectbox("Drawing Tool", ("freedraw", "line", "rect", "circle", "transform"))
|
| 74 |
stroke_width = st.sidebar.slider("Stroke Width", 1, 25, 10)
|
| 75 |
stroke_color = st.sidebar.color_picker("Stroke Color", "#FFFFFF")
|
| 76 |
bg_color = st.sidebar.color_picker("Background Color", "#000000")
|
| 77 |
realtime_update = st.sidebar.checkbox("Update in Realtime", True)
|
| 78 |
|
| 79 |
+
# Load model
|
| 80 |
@st.cache_resource
|
| 81 |
def load_mnist_model():
|
| 82 |
return load_model("digit_recognization.keras")
|
| 83 |
|
| 84 |
model = load_mnist_model()
|
| 85 |
|
| 86 |
+
# Canvas Area
|
| 87 |
+
with st.container():
|
| 88 |
+
st.markdown("<div class='canvas-box'>", unsafe_allow_html=True)
|
| 89 |
+
canvas_result = st_canvas(
|
| 90 |
+
fill_color="rgba(255, 255, 255, 0.3)",
|
| 91 |
+
stroke_width=stroke_width,
|
| 92 |
+
stroke_color=stroke_color,
|
| 93 |
+
background_color=bg_color,
|
| 94 |
+
update_streamlit=realtime_update,
|
| 95 |
+
height=280,
|
| 96 |
+
width=280,
|
| 97 |
+
drawing_mode=drawing_mode,
|
| 98 |
+
key="canvas"
|
| 99 |
+
)
|
| 100 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
# Prediction
|
| 103 |
+
if canvas_result.image_data is not None:
|
| 104 |
+
img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
|
| 105 |
+
img_resized = cv2.resize(img, (28, 28))
|
| 106 |
+
img_normalized = img_resized / 255.0
|
| 107 |
+
img_reshaped = img_normalized.reshape((1, 28, 28))
|
| 108 |
|
| 109 |
+
prediction = model.predict(img_reshaped)
|
| 110 |
+
predicted_digit = np.argmax(prediction)
|
| 111 |
|
| 112 |
+
# Show prediction result
|
| 113 |
+
st.markdown(f"<div class='prediction-box'>Prediction: {predicted_digit}</div>", unsafe_allow_html=True)
|
| 114 |
+
st.markdown(f"<div class='emoji'>{['0️⃣','1️⃣','2️⃣','3️⃣','4️⃣','5️⃣','6️⃣','7️⃣','8️⃣','9️⃣'][predicted_digit]}</div>", unsafe_allow_html=True)
|
| 115 |
|