Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +34 -28
src/streamlit_app.py
CHANGED
|
@@ -89,19 +89,22 @@ def fetch_random_image():
|
|
| 89 |
def preprocess_image(image, target_size=(64, 64)):
|
| 90 |
|
| 91 |
"""Resizes, normalizes, and ensures 3 channels for the image."""
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
return image
|
| 107 |
|
|
@@ -146,21 +149,24 @@ def create_model(input_shape=(64, 64, 3)):
|
|
| 146 |
def fetch_image():
|
| 147 |
print("entering fetch_image")
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
|
| 166 |
# ---------------------------
|
|
|
|
| 89 |
def preprocess_image(image, target_size=(64, 64)):
|
| 90 |
|
| 91 |
"""Resizes, normalizes, and ensures 3 channels for the image."""
|
| 92 |
+
try:
|
| 93 |
+
image = image.resize(target_size)
|
| 94 |
+
|
| 95 |
+
if image.mode == 'RGBA':
|
| 96 |
+
image = image.convert('RGB')
|
| 97 |
+
elif image.mode == 'P':
|
| 98 |
+
image = image.convert('RGB')
|
| 99 |
+
|
| 100 |
+
image = np.array(image) / 255.0 # Normalize to [0, 1]
|
| 101 |
+
|
| 102 |
+
if len(image.shape) == 2: # Convert grayscale to RGB
|
| 103 |
+
|
| 104 |
+
image = np.stack([image] * 3, axis=-1)
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f"Preprocessing error: {e}")
|
| 107 |
+
return None
|
| 108 |
|
| 109 |
return image
|
| 110 |
|
|
|
|
| 149 |
def fetch_image():
|
| 150 |
print("entering fetch_image")
|
| 151 |
|
| 152 |
+
try:
|
| 153 |
+
image = fetch_random_image()
|
| 154 |
+
|
| 155 |
+
if image is not None:
|
| 156 |
+
|
| 157 |
+
st.session_state.unprocessed_image = image
|
| 158 |
+
st.session_state.current_image = preprocess_image(image)
|
| 159 |
+
|
| 160 |
+
st.session_state.current_label = None
|
| 161 |
+
|
| 162 |
+
st.session_state.current_prediction = None
|
| 163 |
+
st.info("image ready")
|
| 164 |
+
|
| 165 |
+
else:
|
| 166 |
+
|
| 167 |
+
st.error("Failed to fetch an image. Please try again.")
|
| 168 |
+
except Exception as e:
|
| 169 |
+
st.error(f"An error occurred while fetching the image: {e}")
|
| 170 |
|
| 171 |
|
| 172 |
# ---------------------------
|