Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
@@ -134,7 +135,7 @@ def perform_object_detection(image, model, device):
|
|
134 |
if scores[i] >= 0.75:
|
135 |
x1, y1, x2, y2 = boxes[i]
|
136 |
if (int(labels[i])-1) == 1 or (int(labels[i])-1) == 0:
|
137 |
-
color = (0,
|
138 |
label_text = f'Region {i}'
|
139 |
|
140 |
# Scale coordinates to original image size
|
@@ -149,16 +150,16 @@ def perform_object_detection(image, model, device):
|
|
149 |
# Check if image has 4 channels (RGBA), convert to RGB
|
150 |
if cropped_image.shape[-1] == 4:
|
151 |
cropped_image = cv2.cvtColor(cropped_image, cv2.COLOR_RGBA2RGB)
|
152 |
-
|
|
|
153 |
# Resize cropped image
|
154 |
resized_crop = resize_to_square(cropped_image)
|
155 |
-
cropped_images.append(resized_crop)
|
156 |
detected_boxes.append((x1, y1, x2, y2))
|
157 |
|
158 |
# Draw bounding box
|
159 |
-
cv2.rectangle(result_image, (x1, y1), (x2, y2), color,
|
160 |
-
cv2.putText(result_image, label_text, (x1, y1 -
|
161 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
162 |
|
163 |
return Image.fromarray(result_image), cropped_images, detected_boxes
|
164 |
|
@@ -216,26 +217,26 @@ def main():
|
|
216 |
# Process each detected region
|
217 |
class_names = ['10', '6.5', '7', '7.5', '8', '8.5', '9', '9.2', '9.5', '9.7']
|
218 |
all_predictions = []
|
219 |
-
|
220 |
-
for idx, cropped_image in
|
221 |
processed_image = preprocess_image(cropped_image)
|
222 |
prediction = classification_model.predict(
|
223 |
np.expand_dims(processed_image, axis=0)
|
224 |
)
|
225 |
top_predictions = get_top_predictions(prediction, class_names)
|
226 |
-
all_predictions.append(top_predictions)
|
227 |
-
|
228 |
# Store in session state
|
229 |
st.session_state.predictions = all_predictions
|
230 |
-
|
231 |
except Exception as e:
|
232 |
st.error(f"Error during processing: {str(e)}")
|
233 |
|
234 |
with col2:
|
235 |
st.subheader("Classification Results")
|
236 |
if st.session_state.predictions is not None:
|
237 |
-
for idx, predictions in
|
238 |
-
st.markdown(f"### Region {idx
|
239 |
|
240 |
# Display main prediction
|
241 |
top_class, top_confidence = predictions[0]
|
@@ -278,12 +279,16 @@ def main():
|
|
278 |
key=f"selectbox_correct_grade_{idx}"
|
279 |
)
|
280 |
|
281 |
-
#
|
282 |
-
if
|
|
|
|
|
|
|
|
|
283 |
st.info(f"Đã chọn màu đúng: {correct_grade}")
|
284 |
|
285 |
# Resize hình ảnh xuống 256x256
|
286 |
-
resized_image = Image.fromarray(
|
287 |
temp_image_path = generate_random_filename()
|
288 |
|
289 |
# Lưu tệp resize tạm thời
|
@@ -295,8 +300,11 @@ def main():
|
|
295 |
if isinstance(cloudinary_result, dict):
|
296 |
st.success(f"Hình ảnh đã được tải lên thành công cho màu {correct_grade}")
|
297 |
st.write(f"URL công khai: {cloudinary_result['upload_result']['secure_url']}")
|
|
|
|
|
298 |
else:
|
299 |
st.error(cloudinary_result)
|
|
|
300 |
else:
|
301 |
st.info("Upload an image to see detection and classification results")
|
302 |
|
|
|
1 |
+
%%writefile app.py
|
2 |
import streamlit as st
|
3 |
import tensorflow as tf
|
4 |
import numpy as np
|
|
|
135 |
if scores[i] >= 0.75:
|
136 |
x1, y1, x2, y2 = boxes[i]
|
137 |
if (int(labels[i])-1) == 1 or (int(labels[i])-1) == 0:
|
138 |
+
color = (0, 255, 0) # Green bounding box
|
139 |
label_text = f'Region {i}'
|
140 |
|
141 |
# Scale coordinates to original image size
|
|
|
150 |
# Check if image has 4 channels (RGBA), convert to RGB
|
151 |
if cropped_image.shape[-1] == 4:
|
152 |
cropped_image = cv2.cvtColor(cropped_image, cv2.COLOR_RGBA2RGB)
|
153 |
+
else:
|
154 |
+
cropped_image = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)
|
155 |
# Resize cropped image
|
156 |
resized_crop = resize_to_square(cropped_image)
|
157 |
+
cropped_images.append([i,resized_crop])
|
158 |
detected_boxes.append((x1, y1, x2, y2))
|
159 |
|
160 |
# Draw bounding box
|
161 |
+
cv2.rectangle(result_image, (x1, y1), (x2, y2), color, 1)
|
162 |
+
cv2.putText(result_image, label_text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 255), 1, cv2.LINE_AA) # Yellow text, smaller font
|
|
|
163 |
|
164 |
return Image.fromarray(result_image), cropped_images, detected_boxes
|
165 |
|
|
|
217 |
# Process each detected region
|
218 |
class_names = ['10', '6.5', '7', '7.5', '8', '8.5', '9', '9.2', '9.5', '9.7']
|
219 |
all_predictions = []
|
220 |
+
all_image=[]
|
221 |
+
for idx, cropped_image in cropped_images:
|
222 |
processed_image = preprocess_image(cropped_image)
|
223 |
prediction = classification_model.predict(
|
224 |
np.expand_dims(processed_image, axis=0)
|
225 |
)
|
226 |
top_predictions = get_top_predictions(prediction, class_names)
|
227 |
+
all_predictions.append([idx,top_predictions])
|
228 |
+
all_image.append(cropped_image)
|
229 |
# Store in session state
|
230 |
st.session_state.predictions = all_predictions
|
231 |
+
st.session_state.image = all_image
|
232 |
except Exception as e:
|
233 |
st.error(f"Error during processing: {str(e)}")
|
234 |
|
235 |
with col2:
|
236 |
st.subheader("Classification Results")
|
237 |
if st.session_state.predictions is not None:
|
238 |
+
for idx, predictions in st.session_state.predictions:
|
239 |
+
st.markdown(f"### Region {idx}")
|
240 |
|
241 |
# Display main prediction
|
242 |
top_class, top_confidence = predictions[0]
|
|
|
279 |
key=f"selectbox_correct_grade_{idx}"
|
280 |
)
|
281 |
|
282 |
+
# Kiểm tra xem đã tải lên hay chưa
|
283 |
+
if f"uploaded_{idx}" not in st.session_state:
|
284 |
+
st.session_state[f"uploaded_{idx}"] = False
|
285 |
+
|
286 |
+
# Chỉ thực hiện khi người dùng đã chọn giá trị và chưa tải lên
|
287 |
+
if correct_grade and not st.session_state[f"uploaded_{idx}"]:
|
288 |
st.info(f"Đã chọn màu đúng: {correct_grade}")
|
289 |
|
290 |
# Resize hình ảnh xuống 256x256
|
291 |
+
resized_image = Image.fromarray(st.session_state.image[idx]).resize((256, 256))
|
292 |
temp_image_path = generate_random_filename()
|
293 |
|
294 |
# Lưu tệp resize tạm thời
|
|
|
300 |
if isinstance(cloudinary_result, dict):
|
301 |
st.success(f"Hình ảnh đã được tải lên thành công cho màu {correct_grade}")
|
302 |
st.write(f"URL công khai: {cloudinary_result['upload_result']['secure_url']}")
|
303 |
+
# Đánh dấu trạng thái đã tải lên
|
304 |
+
st.session_state[f"uploaded_{idx}"] = True
|
305 |
else:
|
306 |
st.error(cloudinary_result)
|
307 |
+
|
308 |
else:
|
309 |
st.info("Upload an image to see detection and classification results")
|
310 |
|