Sathwikchowdary commited on
Commit
bbd0349
ยท
verified ยท
1 Parent(s): 6721822

Update Home.py

Browse files
Files changed (1) hide show
  1. Home.py +26 -58
Home.py CHANGED
@@ -1,65 +1,32 @@
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,
@@ -70,23 +37,24 @@ canvas_result = st_canvas(
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.")
 
 
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
+ # ๐ŸŽฏ App title and intro
8
+ st.set_page_config(page_title="MNIST Digit Recognizer", layout="centered")
9
+ st.title("โœ๏ธ MNIST Handwritten Digit Recognizer")
10
+ st.markdown("Draw a digit (0-9) below and click **Predict** to see the result!")
11
 
12
+ # ๐ŸŽจ Sidebar controls
13
+ drawing_mode = st.sidebar.selectbox("Drawing tool:", ("freedraw", "line", "rect", "circle", "transform"))
14
+ stroke_width = st.sidebar.slider("Stroke width", 1, 25, 10)
15
+ stroke_color = st.sidebar.color_picker("Stroke color", "#000000")
16
+ bg_color = st.sidebar.color_picker("Background color", "#FFFFFF")
17
+ bg_image = st.sidebar.file_uploader("Background image (optional):", type=["png", "jpg"])
18
+ realtime_update = st.sidebar.checkbox("Update in realtime", True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ # ๐Ÿง  Load model from local path
 
 
 
 
 
 
 
 
 
 
 
 
21
  @st.cache_resource
22
+ def load_mnist_model():
23
  return load_model("digit_reco.keras")
24
 
25
+ model = load_mnist_model()
 
 
 
26
 
27
+ # ๐Ÿ–Œ๏ธ Canvas setup
28
  canvas_result = st_canvas(
29
+ fill_color="rgba(255, 165, 0, 0.3)", # Transparent fill
30
  stroke_width=stroke_width,
31
  stroke_color=stroke_color,
32
  background_color=bg_color,
 
37
  key="canvas",
38
  )
39
 
40
+ # ๐Ÿ“ค Predict button
41
  if st.button("๐Ÿ”ฎ Predict"):
42
  if canvas_result.image_data is not None:
43
+ st.image(canvas_result.image_data, caption="๐Ÿ–ผ๏ธ Your Drawing", use_container_width=True) # Updated
44
 
45
+ # Preprocess image
46
  img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
47
+ img = 255 - img # Invert colors
48
  img_resized = cv2.resize(img, (28, 28))
49
  img_normalized = img_resized / 255.0
50
  img_reshaped = img_normalized.reshape((1, 28, 28))
51
 
52
+ # Model prediction
53
  prediction = model.predict(img_reshaped)
54
  predicted_digit = np.argmax(prediction)
55
 
56
+ # Show result
57
+ st.markdown(f"## ๐Ÿง  Predicted Digit: **{predicted_digit}**")
58
  else:
59
+ st.warning("Please draw something before predicting!")
60
+