update UI
Browse files
app.py
CHANGED
|
@@ -86,6 +86,17 @@ def predict_flower(img):
|
|
| 86 |
|
| 87 |
return msg, results, gradcam_img
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
# --- GIAO DIỆN GRADIO (BOTANICAL SOFT STYLE) ---
|
| 90 |
custom_css = """
|
| 91 |
body { background-color: #fdfcf0 !important; }
|
|
@@ -111,6 +122,15 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="green", font=["
|
|
| 111 |
with gr.Column(scale=1):
|
| 112 |
gr.Markdown("### 📸 Tải ảnh hoa lên đây")
|
| 113 |
input_img = gr.Image(type="pil", label="Chọn một tấm ảnh thật đẹp nhé", height=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
with gr.Row():
|
| 115 |
clear_btn = gr.Button("LÀM MỚI", variant="secondary")
|
| 116 |
btn = gr.Button("NHẬN DIỆN NGAY", variant="primary", elem_classes="predict-btn")
|
|
@@ -120,10 +140,6 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="green", font=["
|
|
| 120 |
gr.Markdown("✨ **Mô hình:** EfficientNet-B0 (Chính xác & Nhẹ nhàng)\n"
|
| 121 |
"✨ **Công nghệ:** Grad-CAM (Giải thích vùng tập trung)\n"
|
| 122 |
"✨ **Đối tượng:** 5 loại hoa phổ biến")
|
| 123 |
-
gr.Markdown("### 💡 Giải thích kỹ thuật cho người dùng:")
|
| 124 |
-
gr.Markdown("- **Độ tin cậy > 60%:** Hệ thống tự tin đây là một trong 5 loại hoa đã học.\n"
|
| 125 |
-
"- **Độ tin cậy < 60%:** Hệ thống sẽ cảnh báo đây có thể không phải là hoa hoặc là một loại hoa chưa được huấn luyện.\n"
|
| 126 |
-
"- **Vùng AI tập trung (Grad-CAM):** Vùng màu đỏ trên ảnh cho biết những điểm ảnh quan trọng nhất mà mô hình đã dựa vào để đưa ra dự đoán.")
|
| 127 |
|
| 128 |
# Cột phải: Kết quả
|
| 129 |
with gr.Column(scale=1):
|
|
@@ -136,8 +152,13 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="green", font=["
|
|
| 136 |
with gr.Column(elem_classes="result-card"):
|
| 137 |
gradcam_output = gr.Image(label="Bản đồ nhiệt tập trung", height=300)
|
| 138 |
gr.Markdown("💡 *Vùng màu đỏ là nơi AI đang 'ngắm nhìn' kỹ nhất để nhận ra bông hoa của bạn.*")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
-
# Xử lý sự kiện
|
| 141 |
btn.click(fn=predict_flower, inputs=input_img, outputs=[output_text, output_label, gradcam_output])
|
| 142 |
input_img.upload(fn=predict_flower, inputs=input_img, outputs=[output_text, output_label, gradcam_output])
|
| 143 |
clear_btn.click(lambda: (None, None, None), None, [input_img, output_label, gradcam_output])
|
|
|
|
| 86 |
|
| 87 |
return msg, results, gradcam_img
|
| 88 |
|
| 89 |
+
# --- TẠO DANH SÁCH EXAMPLES TỰ ĐỘNG ---
|
| 90 |
+
example_list = []
|
| 91 |
+
if os.path.exists(DATA_DIR):
|
| 92 |
+
for flower_type in class_names:
|
| 93 |
+
folder_path = os.path.join(DATA_DIR, flower_type)
|
| 94 |
+
if os.path.exists(folder_path):
|
| 95 |
+
files = [f for f in os.listdir(folder_path) if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
|
| 96 |
+
if files:
|
| 97 |
+
# Lấy ảnh đầu tiên của mỗi loại làm ví dụ
|
| 98 |
+
example_list.append([os.path.join(folder_path, files[0])])
|
| 99 |
+
|
| 100 |
# --- GIAO DIỆN GRADIO (BOTANICAL SOFT STYLE) ---
|
| 101 |
custom_css = """
|
| 102 |
body { background-color: #fdfcf0 !important; }
|
|
|
|
| 122 |
with gr.Column(scale=1):
|
| 123 |
gr.Markdown("### 📸 Tải ảnh hoa lên đây")
|
| 124 |
input_img = gr.Image(type="pil", label="Chọn một tấm ảnh thật đẹp nhé", height=400)
|
| 125 |
+
|
| 126 |
+
# PHẦN EXAMPLES (ẢNH MẪU)
|
| 127 |
+
if example_list:
|
| 128 |
+
gr.Examples(
|
| 129 |
+
examples=example_list,
|
| 130 |
+
inputs=input_img,
|
| 131 |
+
label="Hoặc chọn một ảnh mẫu từ khu vườn:"
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
with gr.Row():
|
| 135 |
clear_btn = gr.Button("LÀM MỚI", variant="secondary")
|
| 136 |
btn = gr.Button("NHẬN DIỆN NGAY", variant="primary", elem_classes="predict-btn")
|
|
|
|
| 140 |
gr.Markdown("✨ **Mô hình:** EfficientNet-B0 (Chính xác & Nhẹ nhàng)\n"
|
| 141 |
"✨ **Công nghệ:** Grad-CAM (Giải thích vùng tập trung)\n"
|
| 142 |
"✨ **Đối tượng:** 5 loại hoa phổ biến")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
# Cột phải: Kết quả
|
| 145 |
with gr.Column(scale=1):
|
|
|
|
| 152 |
with gr.Column(elem_classes="result-card"):
|
| 153 |
gradcam_output = gr.Image(label="Bản đồ nhiệt tập trung", height=300)
|
| 154 |
gr.Markdown("💡 *Vùng màu đỏ là nơi AI đang 'ngắm nhìn' kỹ nhất để nhận ra bông hoa của bạn.*")
|
| 155 |
+
|
| 156 |
+
gr.Markdown("### 💡 Giải thích kỹ thuật:")
|
| 157 |
+
gr.Markdown("- **Độ tin cậy > 60%:** Hệ thống tự tin đây là hoa đã học.\n"
|
| 158 |
+
"- **Độ tin cậy < 60%:** Hệ thống cảnh báo ảnh lạ hoặc mờ.\n"
|
| 159 |
+
"- **Grad-CAM:** Trực quan hóa vùng quan trọng nhất AI dùng để dự đoán.")
|
| 160 |
|
| 161 |
+
# Xử lý sự kiện
|
| 162 |
btn.click(fn=predict_flower, inputs=input_img, outputs=[output_text, output_label, gradcam_output])
|
| 163 |
input_img.upload(fn=predict_flower, inputs=input_img, outputs=[output_text, output_label, gradcam_output])
|
| 164 |
clear_btn.click(lambda: (None, None, None), None, [input_img, output_label, gradcam_output])
|