Mpavan45 commited on
Commit
d1b0437
·
verified ·
1 Parent(s): c71845f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -87
app.py CHANGED
@@ -1,68 +1,4 @@
1
- # import streamlit as st
2
- # import cv2
3
- # from streamlit_drawable_canvas import st_canvas
4
- # from keras.models import load_model
5
- # import numpy as np
6
-
7
- # # Sidebar controls
8
- # st.sidebar.title("Canvas Settings")
9
- # drawing_mode = st.sidebar.selectbox("Drawing tool:", ("freedraw", "line", "rect", "circle", "transform"))
10
- # stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 10)
11
- # stroke_color = st.sidebar.color_picker("Stroke color hex: ", "#000000") # black
12
- # bg_color = st.sidebar.color_picker("Background color hex: ", "#FFFFFF") # white
13
- # bg_image = st.sidebar.file_uploader("Background image:", type=["png", "jpg"])
14
- # realtime_update = st.sidebar.checkbox("Update in realtime", True)
15
-
16
- # # Load model with caching
17
- # @st.cache_resource
18
- # def load_mnist_model():
19
- # return load_model("mnist_model.keras")
20
-
21
- # model = load_mnist_model()
22
-
23
- # st.title("🖌️ Mindist: Draw a Number, Predict Instantly")
24
-
25
- # # Create a two-column layout
26
- # col1, col2 = st.columns([1, 1])
27
-
28
- # with col1:
29
- # st.subheader("Draw Here 👇")
30
- # canvas_result = st_canvas(
31
- # fill_color="rgba(255, 165, 0, 0.3)",
32
- # stroke_width=stroke_width,
33
- # stroke_color=stroke_color,
34
- # background_color=bg_color,
35
- # update_streamlit=realtime_update,
36
- # height=280,
37
- # width=280,
38
- # drawing_mode=drawing_mode,
39
- # key="canvas",
40
- # )
41
-
42
- # with col2:
43
- # if canvas_result.image_data is not None:
44
- # st.subheader("Original Drawing")
45
- # st.image(canvas_result.image_data, use_column_width=True)
46
-
47
- # # Below the two columns: Show preprocessing and prediction
48
- # if canvas_result.image_data is not None:
49
- # st.markdown("---")
50
- # st.subheader("Preprocessed Image & Prediction")
51
-
52
- # img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
53
- # img = 255 - img # Invert colors
54
- # img_resized = cv2.resize(img, (28, 28))
55
- # img_normalized = img_resized / 255.0
56
- # final_img = img_normalized.reshape(1, 28, 28, 1)
57
-
58
- # col3, col4 = st.columns([1, 1])
59
- # with col3:
60
- # st.image(img_resized, caption="28x28 Preprocessed", clamp=True, channels="GRAY")
61
- # with col4:
62
- # prediction = model.predict(final_img)
63
- # predicted_digit = np.argmax(prediction)
64
- # st.markdown(f"### 🧠 Predicted Digit: **{predicted_digit}**")
65
- import streamlit as st
66
  import cv2
67
  from streamlit_drawable_canvas import st_canvas
68
  from keras.models import load_model
@@ -112,30 +48,17 @@ with col2:
112
  if canvas_result.image_data is not None:
113
  st.markdown("---")
114
  st.subheader("Preprocessed Image & Prediction")
115
-
116
  img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
117
  img = 255 - img # Invert colors
118
- _, thresh_img = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY)
119
-
120
- # Find contours of digits
121
- contours, _ = cv2.findContours(thresh_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
122
- contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0]) # Sort left-to-right
123
-
124
- predictions = []
125
-
126
  col3, col4 = st.columns([1, 1])
127
  with col3:
128
- st.image(img, caption="Thresholded Image", clamp=True, channels="GRAY")
129
-
130
  with col4:
131
- for cnt in contours:
132
- x, y, w, h = cv2.boundingRect(cnt)
133
- if w > 5 and h > 5: # Filter out noise/small contours
134
- digit_roi = thresh_img[y:y+h, x:x+w]
135
- digit_resized = cv2.resize(digit_roi, (28, 28))
136
- digit_normalized = digit_resized / 255.0
137
- input_img = digit_normalized.reshape(1, 28, 28, 1)
138
- pred = np.argmax(model.predict(input_img))
139
- predictions.append(str(pred))
140
-
141
- st.markdown(f"### 🧠 Predicted Digits: **{''.join(predictions) if predictions else 'No digits found'}**")
 
1
+ import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import cv2
3
  from streamlit_drawable_canvas import st_canvas
4
  from keras.models import load_model
 
48
  if canvas_result.image_data is not None:
49
  st.markdown("---")
50
  st.subheader("Preprocessed Image & Prediction")
51
+
52
  img = cv2.cvtColor(canvas_result.image_data.astype("uint8"), cv2.COLOR_RGBA2GRAY)
53
  img = 255 - img # Invert colors
54
+ img_resized = cv2.resize(img, (28, 28))
55
+ img_normalized = img_resized / 255.0
56
+ final_img = img_normalized.reshape(1, 28, 28, 1)
57
+
 
 
 
 
58
  col3, col4 = st.columns([1, 1])
59
  with col3:
60
+ st.image(img_resized, caption="28x28 Preprocessed", clamp=True, channels="GRAY")
 
61
  with col4:
62
+ prediction = model.predict(final_img)
63
+ predicted_digit = np.argmax(prediction)
64
+ st.markdown(f"### 🧠 Predicted Digit: **{predicted_digit}**")