Sathwikchowdary commited on
Commit
d500bfb
ยท
verified ยท
1 Parent(s): f2c8234

Update Home.py

Browse files
Files changed (1) hide show
  1. Home.py +70 -87
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
- # 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
- )
 
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.")