Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -98,31 +98,43 @@ st.checkbox("I agree to the terms and conditions", key="terms_accepted")
|
|
98 |
|
99 |
# Upload Image
|
100 |
uploaded_file = st.file_uploader("Upload an image of your food", type=["jpg", "png", "jpeg"])
|
|
|
101 |
if uploaded_file:
|
102 |
-
|
103 |
-
st.session_state["original_image"] =
|
104 |
-
|
105 |
-
# Resize image in Streamlit before sending to S3
|
106 |
-
def resize_image(image, new_width=512):
|
107 |
-
aspect_ratio = image.width / image.height
|
108 |
-
new_height = int(new_width / aspect_ratio)
|
109 |
-
resized_image = image.resize((new_width, new_height))
|
110 |
-
return resized_image
|
111 |
|
|
|
112 |
if "original_image" in st.session_state:
|
113 |
original_img = st.session_state["original_image"]
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
-
|
117 |
-
col1, col2 = st.columns(2) # Two columns for images
|
118 |
-
|
119 |
-
with col1:
|
120 |
-
st.subheader("📷 Original Image")
|
121 |
-
st.image(original_img, caption=f"Original ({original_img.width}x{original_img.height} pixels)", use_column_width=True)
|
122 |
|
123 |
-
|
124 |
-
st.
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
|
128 |
# Create a table-like structure for food annotation
|
|
|
98 |
|
99 |
# Upload Image
|
100 |
uploaded_file = st.file_uploader("Upload an image of your food", type=["jpg", "png", "jpeg"])
|
101 |
+
# Ensure an image is uploaded before displaying
|
102 |
if uploaded_file:
|
103 |
+
original_img = Image.open(uploaded_file)
|
104 |
+
st.session_state["original_image"] = original_img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
+
# If an image has been uploaded, process and display it
|
107 |
if "original_image" in st.session_state:
|
108 |
original_img = st.session_state["original_image"]
|
109 |
+
|
110 |
+
# Resize while maintaining aspect ratio (longest side = 512)
|
111 |
+
def resize_image(image, max_size=512):
|
112 |
+
aspect_ratio = image.width / image.height
|
113 |
+
if image.width > image.height:
|
114 |
+
new_width = max_size
|
115 |
+
new_height = int(max_size / aspect_ratio)
|
116 |
+
else:
|
117 |
+
new_height = max_size
|
118 |
+
new_width = int(max_size * aspect_ratio)
|
119 |
+
return image.resize((new_width, new_height))
|
120 |
|
121 |
+
processed_img = resize_image(original_img)
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
+
# UI Layout: Two Columns for Images
|
124 |
+
col1, col2 = st.columns(2)
|
125 |
+
|
126 |
+
with col1:
|
127 |
+
st.subheader("📷 Original Image")
|
128 |
+
st.image(original_img, caption=f"Original ({original_img.width}x{original_img.height} pixels)", use_column_width=True)
|
129 |
+
|
130 |
+
with col2:
|
131 |
+
st.subheader("🖼️ Processed Image")
|
132 |
+
st.image(processed_img, caption=f"Processed ({processed_img.width}x{processed_img.height} pixels)", use_column_width=True)
|
133 |
+
|
134 |
+
# Store processed image in session state for annotations
|
135 |
+
st.session_state["processed_image"] = processed_img
|
136 |
+
else:
|
137 |
+
st.warning("⚠️ Please upload an image first.")
|
138 |
|
139 |
|
140 |
# Create a table-like structure for food annotation
|