Mpavan45 commited on
Commit
9b0af88
·
verified ·
1 Parent(s): a5750ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -117,31 +117,35 @@ with col2:
117
  st.image(canvas_result.image_data, use_column_width=True)
118
 
119
  # === Image preprocessing and prediction ===
120
- # Initialize session state for draw count
 
 
 
 
 
 
121
  if "draw_count" not in st.session_state:
122
  st.session_state.draw_count = 0
123
  st.session_state.last_image = None
124
 
125
- # === Image preprocessing and prediction ===
126
  if canvas_result.image_data is not None:
127
  current_image = canvas_result.image_data
128
 
129
- # Compare current image with previous
130
  if st.session_state.last_image is None or not np.array_equal(current_image, st.session_state.last_image):
131
  st.session_state.draw_count += 1
132
  st.session_state.last_image = current_image
133
 
134
  st.markdown(f"### ✏️ Draw Count: {st.session_state.draw_count}")
135
-
136
  st.subheader("Preprocessed Image & Prediction")
137
 
138
- # Preprocess image
139
  img = cv2.cvtColor(current_image.astype("uint8"), cv2.COLOR_RGBA2GRAY)
140
  img = 255 - img # Invert colors
141
 
142
- # === Model Selection Based on Draw Count ===
143
  if st.session_state.draw_count == 1:
144
- # Assume single-digit drawing
145
  img_resized = cv2.resize(img, (28, 28))
146
  img_normalized = img_resized / 255.0
147
  final_img = img_normalized.reshape(1, 28, 28, 1)
@@ -149,7 +153,7 @@ if canvas_result.image_data is not None:
149
  preds = model_to_use.predict(final_img)
150
  predicted_str = str(np.argmax(preds))
151
  else:
152
- # Assume multi-digit drawing
153
  img_resized = cv2.resize(img, (100, 28))
154
  img_normalized = img_resized / 255.0
155
  final_img = img_normalized.reshape(1, 28, 100, 1)
@@ -158,5 +162,5 @@ if canvas_result.image_data is not None:
158
  predicted_digits = [np.argmax(p[0]) for p in preds]
159
  predicted_str = ''.join([str(d) for d in predicted_digits])
160
 
161
- # Show result
162
  st.markdown(f"### 🧠 Predicted Number: **{predicted_str}**")
 
117
  st.image(canvas_result.image_data, use_column_width=True)
118
 
119
  # === Image preprocessing and prediction ===
120
+ # === Sidebar reset ===
121
+ if st.sidebar.button("🔄 Reset"):
122
+ st.session_state.draw_count = 0
123
+ st.session_state.last_image = None
124
+ st.experimental_rerun()
125
+
126
+ # === Initialize draw counter ===
127
  if "draw_count" not in st.session_state:
128
  st.session_state.draw_count = 0
129
  st.session_state.last_image = None
130
 
131
+ # === Preprocess and predict ===
132
  if canvas_result.image_data is not None:
133
  current_image = canvas_result.image_data
134
 
135
+ # Check if drawing has changed
136
  if st.session_state.last_image is None or not np.array_equal(current_image, st.session_state.last_image):
137
  st.session_state.draw_count += 1
138
  st.session_state.last_image = current_image
139
 
140
  st.markdown(f"### ✏️ Draw Count: {st.session_state.draw_count}")
 
141
  st.subheader("Preprocessed Image & Prediction")
142
 
143
+ # Convert and preprocess
144
  img = cv2.cvtColor(current_image.astype("uint8"), cv2.COLOR_RGBA2GRAY)
145
  img = 255 - img # Invert colors
146
 
 
147
  if st.session_state.draw_count == 1:
148
+ # Single digit
149
  img_resized = cv2.resize(img, (28, 28))
150
  img_normalized = img_resized / 255.0
151
  final_img = img_normalized.reshape(1, 28, 28, 1)
 
153
  preds = model_to_use.predict(final_img)
154
  predicted_str = str(np.argmax(preds))
155
  else:
156
+ # Multi digit
157
  img_resized = cv2.resize(img, (100, 28))
158
  img_normalized = img_resized / 255.0
159
  final_img = img_normalized.reshape(1, 28, 100, 1)
 
162
  predicted_digits = [np.argmax(p[0]) for p in preds]
163
  predicted_str = ''.join([str(d) for d in predicted_digits])
164
 
165
+ # Output
166
  st.markdown(f"### 🧠 Predicted Number: **{predicted_str}**")