Update app.py
Browse files
app.py
CHANGED
@@ -81,36 +81,43 @@ drawing_mode = st.sidebar.selectbox(
|
|
81 |
"Drawing tool:", ("freedraw", "line", "rect", "circle", "transform", "polygon")
|
82 |
)
|
83 |
realtime_update = st.sidebar.checkbox("Update in realtime", True)
|
84 |
-
|
85 |
-
# Create a canvas component
|
86 |
canvas_result = st_canvas(
|
87 |
-
fill_color="
|
88 |
stroke_width=stroke_width,
|
89 |
stroke_color=stroke_color,
|
90 |
-
background_color=bg_color,
|
91 |
-
background_image=Image.open(bg_image) if bg_image else None,
|
92 |
update_streamlit=realtime_update,
|
93 |
-
height=
|
|
|
94 |
drawing_mode=drawing_mode,
|
95 |
display_toolbar=st.sidebar.checkbox("Display toolbar", True),
|
96 |
key="full_app",
|
97 |
)
|
98 |
|
99 |
-
# Do something interesting with the image data and paths
|
100 |
# Do something interesting with the image data and paths
|
101 |
if canvas_result.image_data is not None:
|
102 |
-
#
|
103 |
image = canvas_result.image_data
|
104 |
image1 = image.copy()
|
105 |
-
image1 = image1.astype('
|
106 |
-
|
107 |
-
|
108 |
-
image1 =
|
109 |
|
110 |
-
|
111 |
-
image1.resize(
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
113 |
|
|
|
|
|
114 |
|
|
|
|
|
|
|
115 |
if canvas_result.json_data is not None:
|
116 |
-
st.dataframe(pd.json_normalize(canvas_result.json_data["objects"]))
|
|
|
81 |
"Drawing tool:", ("freedraw", "line", "rect", "circle", "transform", "polygon")
|
82 |
)
|
83 |
realtime_update = st.sidebar.checkbox("Update in realtime", True)
|
84 |
+
#create canvas component
|
|
|
85 |
canvas_result = st_canvas(
|
86 |
+
fill_color="white", # Set the canvas background color to white
|
87 |
stroke_width=stroke_width,
|
88 |
stroke_color=stroke_color,
|
|
|
|
|
89 |
update_streamlit=realtime_update,
|
90 |
+
height=28, # Set the canvas height to 28 pixels
|
91 |
+
width=28, # Set the canvas width to 28 pixels
|
92 |
drawing_mode=drawing_mode,
|
93 |
display_toolbar=st.sidebar.checkbox("Display toolbar", True),
|
94 |
key="full_app",
|
95 |
)
|
96 |
|
|
|
97 |
# Do something interesting with the image data and paths
|
98 |
if canvas_result.image_data is not None:
|
99 |
+
# Preprocess the drawn image
|
100 |
image = canvas_result.image_data
|
101 |
image1 = image.copy()
|
102 |
+
image1 = image1.astype('uint8')
|
103 |
+
|
104 |
+
# Assuming the background is white, you can invert the colors if necessary
|
105 |
+
image1 = 255 - image1
|
106 |
|
107 |
+
# Resize the image to match the input size of your CNN model (28x28)
|
108 |
+
image1 = cv2.resize(image1, (28, 28))
|
109 |
+
|
110 |
+
# Normalize the image to values between 0 and 1
|
111 |
+
image1 = image1 / 255.0
|
112 |
+
|
113 |
+
# Convert the image to the expected shape for the model
|
114 |
+
image1 = image1.reshape(1, 1, 28, 28).astype('float32')
|
115 |
|
116 |
+
# Pass the preprocessed image to your model for prediction
|
117 |
+
prediction = np.argmax(cnn.predict(image1))
|
118 |
|
119 |
+
# Display the prediction result
|
120 |
+
st.title(f"Predicted Digit: {prediction}")
|
121 |
+
|
122 |
if canvas_result.json_data is not None:
|
123 |
+
st.dataframe(pd.json_normalize(canvas_result.json_data["objects"]))
|