Spaces:
Sleeping
Sleeping
Update Home.py
Browse files
Home.py
CHANGED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Page configuration
|
8 |
+
st.set_page_config(page_title="DigitGlow - AI Digit Identifier", layout="centered")
|
9 |
+
|
10 |
+
# Custom CSS Styling
|
11 |
+
st.markdown(
|
12 |
+
"""
|
13 |
+
<style>
|
14 |
+
.stApp {
|
15 |
+
background-color: #1e1e1e;
|
16 |
+
font-family: 'Segoe UI', sans-serif;
|
17 |
+
}
|
18 |
+
.title-box {
|
19 |
+
text-align: center;
|
20 |
+
padding: 20px;
|
21 |
+
}
|
22 |
+
.main-title {
|
23 |
+
font-size: 3em;
|
24 |
+
font-weight: bold;
|
25 |
+
color: #00ffcc;
|
26 |
+
text-shadow: 1px 1px 5px #00ffff;
|
27 |
+
}
|
28 |
+
.sub-title {
|
29 |
+
font-size: 1.2em;
|
30 |
+
color: #ccc;
|
31 |
+
font-style: italic;
|
32 |
+
}
|
33 |
+
.prediction-box {
|
34 |
+
text-align: center;
|
35 |
+
margin-top: 30px;
|
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 |
+
<div class="title-box">
|
54 |
+
<div class="main-title">DigitGlow</div>
|
55 |
+
<div class="sub-title">AI-Powered Handwritten Digit Recognition</div>
|
56 |
+
</div>
|
57 |
+
<hr style="border-top: 1px solid #555;">
|
58 |
+
""",
|
59 |
+
unsafe_allow_html=True
|
60 |
+
)
|
61 |
+
|
62 |
+
# Sidebar - Drawing Settings
|
63 |
+
st.sidebar.header("✏️ Canvas Controls")
|
64 |
+
drawing_mode = st.sidebar.selectbox("Drawing tool:", ("freedraw", "line", "rect", "circle", "transform"))
|
65 |
+
stroke_width = st.sidebar.slider("Stroke width:", 1, 25, 10)
|
66 |
+
stroke_color = st.sidebar.color_picker("Stroke color:", "#FFFFFF")
|
67 |
+
bg_color = st.sidebar.color_picker("Canvas background:", "#000000")
|
68 |
+
realtime_update = st.sidebar.checkbox("Update in real-time", True)
|
69 |
+
|
70 |
+
# Load Model
|
71 |
+
@st.cache_resource
|
72 |
+
def load_mnist_model():
|
73 |
+
return load_model("handwritten_digit_recognition.keras")
|
74 |
+
|
75 |
+
model = load_mnist_model()
|
76 |
+
|
77 |
+
# Layout columns
|
78 |
+
col1, col2 = st.columns([1, 1])
|
79 |
+
|
80 |
+
with col1:
|
81 |
+
st.markdown('<div class="canvas-title">🖌️ Draw a digit below:</div>', unsafe_allow_html=True)
|
82 |
+
canvas_result = st_canvas(
|
83 |
+
fill_color="rgba(255, 255, 255, 1)",
|
84 |
+
stroke_width=stroke_width,
|
85 |
+
stroke_color=stroke_color,
|
86 |
+
background_color=bg_color,
|
87 |
+
update_streamlit=realtime_update,
|
88 |
+
height=280,
|
89 |
+
width=250,
|
90 |
+
drawing_mode=drawing_mode,
|
91 |
+
key="canvas",
|
92 |
+
)
|
93 |
+
|
94 |
+
with col2:
|
95 |
+
if canvas_result.image_data is not None:
|
96 |
+
st.image(canvas_result.image_data, caption="Your Drawing", width=280)
|
97 |
+
# Preprocess image
|
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 Prediction
|
106 |
+
st.markdown(
|
107 |
+
f'<div class="prediction-box"><div class="prediction-text">Prediction: {np.argmax(prediction)}</div></div>',
|
108 |
+
unsafe_allow_html=True,
|
109 |
+
)
|