aggiunte immagini
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- gradio_ui/app.py +122 -15
- gradio_ui/handlers.py +119 -2
- gradio_ui/tabs/compare.py +15 -0
- gradio_ui/tabs/draw_defects.py +15 -0
- gradio_ui/tabs/upload.py +16 -1
- sample_images/bottle/broken_large/000.png +3 -0
- sample_images/bottle/broken_large/001.png +3 -0
- sample_images/bottle/broken_small/000.png +3 -0
- sample_images/bottle/broken_small/001.png +3 -0
- sample_images/bottle/contamination/000.png +3 -0
- sample_images/bottle/contamination/001.png +3 -0
- sample_images/bottle/good/000.png +3 -0
- sample_images/cable/bent_wire/000.png +3 -0
- sample_images/cable/bent_wire/001.png +3 -0
- sample_images/cable/cable_swap/000.png +3 -0
- sample_images/cable/cable_swap/001.png +3 -0
- sample_images/cable/combined/000.png +3 -0
- sample_images/cable/combined/001.png +3 -0
- sample_images/cable/cut_inner_insulation/000.png +3 -0
- sample_images/cable/cut_inner_insulation/001.png +3 -0
- sample_images/cable/good/000.png +3 -0
- sample_images/capsule/crack/000.png +3 -0
- sample_images/capsule/crack/001.png +3 -0
- sample_images/capsule/faulty_imprint/000.png +3 -0
- sample_images/capsule/faulty_imprint/001.png +3 -0
- sample_images/capsule/good/000.png +3 -0
- sample_images/capsule/poke/000.png +3 -0
- sample_images/capsule/poke/001.png +3 -0
- sample_images/capsule/scratch/000.png +3 -0
- sample_images/capsule/scratch/001.png +3 -0
- sample_images/carpet/color/000.png +3 -0
- sample_images/carpet/color/001.png +3 -0
- sample_images/carpet/cut/000.png +3 -0
- sample_images/carpet/cut/001.png +3 -0
- sample_images/carpet/good/000.png +3 -0
- sample_images/carpet/hole/000.png +3 -0
- sample_images/carpet/hole/001.png +3 -0
- sample_images/carpet/metal_contamination/000.png +3 -0
- sample_images/carpet/metal_contamination/001.png +3 -0
- sample_images/grid/bent/000.png +3 -0
- sample_images/grid/bent/001.png +3 -0
- sample_images/grid/broken/000.png +3 -0
- sample_images/grid/broken/001.png +3 -0
- sample_images/grid/glue/000.png +3 -0
- sample_images/grid/glue/001.png +3 -0
- sample_images/grid/good/000.png +3 -0
- sample_images/grid/metal_contamination/000.png +3 -0
- sample_images/grid/metal_contamination/001.png +3 -0
- sample_images/hazelnut/crack/000.png +3 -0
- sample_images/hazelnut/crack/001.png +3 -0
gradio_ui/app.py
CHANGED
|
@@ -6,6 +6,8 @@ by assembling all tab components and wiring event handlers.
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import gradio as gr
|
|
|
|
|
|
|
| 9 |
|
| 10 |
from core import get_available_models, MVTEC_CATEGORIES
|
| 11 |
from gradio_ui.handlers import (
|
|
@@ -15,6 +17,7 @@ from gradio_ui.handlers import (
|
|
| 15 |
predict_compare,
|
| 16 |
update_categories,
|
| 17 |
update_compare_categories,
|
|
|
|
| 18 |
)
|
| 19 |
from gradio_ui.content import MAIN_HEADER
|
| 20 |
from gradio_ui.tabs import (
|
|
@@ -69,7 +72,7 @@ def create_app() -> gr.Blocks:
|
|
| 69 |
# =========================================================================
|
| 70 |
upload_components["model_dropdown"].change(
|
| 71 |
fn=update_categories,
|
| 72 |
-
inputs=[upload_components["model_dropdown"]],
|
| 73 |
outputs=[upload_components["category_dropdown"]]
|
| 74 |
)
|
| 75 |
|
|
@@ -86,18 +89,42 @@ def create_app() -> gr.Blocks:
|
|
| 86 |
]
|
| 87 |
)
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
)
|
| 102 |
|
| 103 |
# =========================================================================
|
|
@@ -105,7 +132,7 @@ def create_app() -> gr.Blocks:
|
|
| 105 |
# =========================================================================
|
| 106 |
sketch_components["model_dropdown"].change(
|
| 107 |
fn=update_categories,
|
| 108 |
-
inputs=[sketch_components["model_dropdown"]],
|
| 109 |
outputs=[sketch_components["category_dropdown"]]
|
| 110 |
)
|
| 111 |
|
|
@@ -122,12 +149,52 @@ def create_app() -> gr.Blocks:
|
|
| 122 |
]
|
| 123 |
)
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
# =========================================================================
|
| 126 |
# Event handlers for Tab 3 (Compare)
|
| 127 |
# =========================================================================
|
| 128 |
compare_components["model_checkboxes"].change(
|
| 129 |
fn=update_compare_categories,
|
| 130 |
-
inputs=[compare_components["model_checkboxes"]],
|
| 131 |
outputs=[compare_components["category_dropdown"]]
|
| 132 |
)
|
| 133 |
|
|
@@ -143,6 +210,46 @@ def create_app() -> gr.Blocks:
|
|
| 143 |
compare_components["summary"]
|
| 144 |
]
|
| 145 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
return app
|
| 148 |
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
+
import numpy as np
|
| 10 |
+
from PIL import Image
|
| 11 |
|
| 12 |
from core import get_available_models, MVTEC_CATEGORIES
|
| 13 |
from gradio_ui.handlers import (
|
|
|
|
| 17 |
predict_compare,
|
| 18 |
update_categories,
|
| 19 |
update_compare_categories,
|
| 20 |
+
get_category_from_sample,
|
| 21 |
)
|
| 22 |
from gradio_ui.content import MAIN_HEADER
|
| 23 |
from gradio_ui.tabs import (
|
|
|
|
| 72 |
# =========================================================================
|
| 73 |
upload_components["model_dropdown"].change(
|
| 74 |
fn=update_categories,
|
| 75 |
+
inputs=[upload_components["model_dropdown"], upload_components["category_dropdown"]],
|
| 76 |
outputs=[upload_components["category_dropdown"]]
|
| 77 |
)
|
| 78 |
|
|
|
|
| 89 |
]
|
| 90 |
)
|
| 91 |
|
| 92 |
+
# Sample gallery selection handler for Upload tab
|
| 93 |
+
def on_upload_sample_select(evt: gr.SelectData, current_model: str, gallery_value: list):
|
| 94 |
+
"""Handler for sample image selection in upload tab."""
|
| 95 |
+
if evt is None:
|
| 96 |
+
return None, gr.Dropdown()
|
| 97 |
+
|
| 98 |
+
# Get the selected index and retrieve original path from gallery data
|
| 99 |
+
idx = evt.index
|
| 100 |
+
if gallery_value and idx < len(gallery_value):
|
| 101 |
+
item = gallery_value[idx]
|
| 102 |
+
if isinstance(item, tuple):
|
| 103 |
+
image_path, label = item
|
| 104 |
+
category = get_category_from_sample(label)
|
| 105 |
+
else:
|
| 106 |
+
image_path = item
|
| 107 |
+
category = get_category_from_sample(str(image_path))
|
| 108 |
+
else:
|
| 109 |
+
return None, gr.Dropdown()
|
| 110 |
+
|
| 111 |
+
try:
|
| 112 |
+
img = np.array(Image.open(image_path).convert("RGB"))
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"Error loading image: {e}")
|
| 115 |
+
return None, gr.Dropdown()
|
| 116 |
+
|
| 117 |
+
trained = get_trained_categories(current_model)
|
| 118 |
+
if trained and category in trained:
|
| 119 |
+
return img, gr.Dropdown(choices=trained, value=category)
|
| 120 |
+
elif trained:
|
| 121 |
+
return img, gr.Dropdown(choices=trained, value=trained[0])
|
| 122 |
+
return img, gr.Dropdown(choices=MVTEC_CATEGORIES, value=category)
|
| 123 |
+
|
| 124 |
+
upload_components["sample_gallery"].select(
|
| 125 |
+
fn=on_upload_sample_select,
|
| 126 |
+
inputs=[upload_components["model_dropdown"], upload_components["sample_gallery"]],
|
| 127 |
+
outputs=[upload_components["image_input"], upload_components["category_dropdown"]]
|
| 128 |
)
|
| 129 |
|
| 130 |
# =========================================================================
|
|
|
|
| 132 |
# =========================================================================
|
| 133 |
sketch_components["model_dropdown"].change(
|
| 134 |
fn=update_categories,
|
| 135 |
+
inputs=[sketch_components["model_dropdown"], sketch_components["category_dropdown"]],
|
| 136 |
outputs=[sketch_components["category_dropdown"]]
|
| 137 |
)
|
| 138 |
|
|
|
|
| 149 |
]
|
| 150 |
)
|
| 151 |
|
| 152 |
+
# Sample gallery selection handler for Sketch tab
|
| 153 |
+
def on_sketch_sample_select(evt: gr.SelectData, current_model: str, gallery_value: list):
|
| 154 |
+
"""Handler for sample image selection in sketch tab."""
|
| 155 |
+
if evt is None:
|
| 156 |
+
return None, gr.Dropdown()
|
| 157 |
+
|
| 158 |
+
# Get the selected index and retrieve original path from gallery data
|
| 159 |
+
idx = evt.index
|
| 160 |
+
if gallery_value and idx < len(gallery_value):
|
| 161 |
+
item = gallery_value[idx]
|
| 162 |
+
if isinstance(item, tuple):
|
| 163 |
+
image_path, label = item
|
| 164 |
+
category = get_category_from_sample(label)
|
| 165 |
+
else:
|
| 166 |
+
image_path = item
|
| 167 |
+
category = get_category_from_sample(str(image_path))
|
| 168 |
+
else:
|
| 169 |
+
return None, gr.Dropdown()
|
| 170 |
+
|
| 171 |
+
try:
|
| 172 |
+
img = np.array(Image.open(image_path).convert("RGB"))
|
| 173 |
+
# For ImageEditor, return the image in the expected format
|
| 174 |
+
editor_value = {"background": img, "layers": [], "composite": img}
|
| 175 |
+
except Exception as e:
|
| 176 |
+
print(f"Error loading image: {e}")
|
| 177 |
+
return None, gr.Dropdown()
|
| 178 |
+
|
| 179 |
+
trained = get_trained_categories(current_model)
|
| 180 |
+
if trained and category in trained:
|
| 181 |
+
return editor_value, gr.Dropdown(choices=trained, value=category)
|
| 182 |
+
elif trained:
|
| 183 |
+
return editor_value, gr.Dropdown(choices=trained, value=trained[0])
|
| 184 |
+
return editor_value, gr.Dropdown(choices=MVTEC_CATEGORIES, value=category)
|
| 185 |
+
|
| 186 |
+
sketch_components["sample_gallery"].select(
|
| 187 |
+
fn=on_sketch_sample_select,
|
| 188 |
+
inputs=[sketch_components["model_dropdown"], sketch_components["sample_gallery"]],
|
| 189 |
+
outputs=[sketch_components["image_editor"], sketch_components["category_dropdown"]]
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
# =========================================================================
|
| 193 |
# Event handlers for Tab 3 (Compare)
|
| 194 |
# =========================================================================
|
| 195 |
compare_components["model_checkboxes"].change(
|
| 196 |
fn=update_compare_categories,
|
| 197 |
+
inputs=[compare_components["model_checkboxes"], compare_components["category_dropdown"]],
|
| 198 |
outputs=[compare_components["category_dropdown"]]
|
| 199 |
)
|
| 200 |
|
|
|
|
| 210 |
compare_components["summary"]
|
| 211 |
]
|
| 212 |
)
|
| 213 |
+
|
| 214 |
+
# Sample gallery selection handler for Compare tab
|
| 215 |
+
def on_compare_sample_select(evt: gr.SelectData, selected_models: list, gallery_value: list):
|
| 216 |
+
"""Handler for sample image selection in compare tab."""
|
| 217 |
+
if evt is None:
|
| 218 |
+
return None, gr.Dropdown()
|
| 219 |
+
|
| 220 |
+
# Get the selected index and retrieve original path from gallery data
|
| 221 |
+
idx = evt.index
|
| 222 |
+
if gallery_value and idx < len(gallery_value):
|
| 223 |
+
item = gallery_value[idx]
|
| 224 |
+
if isinstance(item, tuple):
|
| 225 |
+
image_path, label = item
|
| 226 |
+
category = get_category_from_sample(label)
|
| 227 |
+
else:
|
| 228 |
+
image_path = item
|
| 229 |
+
category = get_category_from_sample(str(image_path))
|
| 230 |
+
else:
|
| 231 |
+
return None, gr.Dropdown()
|
| 232 |
+
|
| 233 |
+
try:
|
| 234 |
+
img = np.array(Image.open(image_path).convert("RGB"))
|
| 235 |
+
except Exception as e:
|
| 236 |
+
print(f"Error loading image: {e}")
|
| 237 |
+
return None, gr.Dropdown()
|
| 238 |
+
|
| 239 |
+
# Get common categories for selected models
|
| 240 |
+
from gradio_ui.handlers import get_common_categories
|
| 241 |
+
common = get_common_categories(selected_models)
|
| 242 |
+
if common and category in common:
|
| 243 |
+
return img, gr.Dropdown(choices=common, value=category)
|
| 244 |
+
elif common:
|
| 245 |
+
return img, gr.Dropdown(choices=common, value=common[0])
|
| 246 |
+
return img, gr.Dropdown(choices=MVTEC_CATEGORIES, value=category)
|
| 247 |
+
|
| 248 |
+
compare_components["sample_gallery"].select(
|
| 249 |
+
fn=on_compare_sample_select,
|
| 250 |
+
inputs=[compare_components["model_checkboxes"], compare_components["sample_gallery"]],
|
| 251 |
+
outputs=[compare_components["image_input"], compare_components["category_dropdown"]]
|
| 252 |
+
)
|
| 253 |
|
| 254 |
return app
|
| 255 |
|
gradio_ui/handlers.py
CHANGED
|
@@ -256,33 +256,150 @@ def get_common_categories(model_names: list) -> list:
|
|
| 256 |
return list(common) if common else MVTEC_CATEGORIES
|
| 257 |
|
| 258 |
|
| 259 |
-
def update_categories(model_name: str):
|
| 260 |
"""
|
| 261 |
Updates available categories based on selected model.
|
|
|
|
| 262 |
|
| 263 |
Args:
|
| 264 |
model_name: Selected model name
|
|
|
|
| 265 |
|
| 266 |
Returns:
|
| 267 |
Updated Gradio Dropdown component
|
| 268 |
"""
|
| 269 |
trained = get_trained_categories(model_name)
|
| 270 |
if trained:
|
|
|
|
|
|
|
|
|
|
| 271 |
return gr.Dropdown(choices=trained, value=trained[0])
|
|
|
|
|
|
|
|
|
|
| 272 |
return gr.Dropdown(choices=MVTEC_CATEGORIES, value="bottle")
|
| 273 |
|
| 274 |
|
| 275 |
-
def update_compare_categories(selected_models: list):
|
| 276 |
"""
|
| 277 |
Update categories dropdown based on selected models.
|
|
|
|
| 278 |
|
| 279 |
Args:
|
| 280 |
selected_models: List of selected model names
|
|
|
|
| 281 |
|
| 282 |
Returns:
|
| 283 |
Updated Gradio Dropdown component
|
| 284 |
"""
|
| 285 |
common = get_common_categories(selected_models)
|
| 286 |
if common:
|
|
|
|
|
|
|
|
|
|
| 287 |
return gr.Dropdown(choices=common, value=common[0])
|
|
|
|
|
|
|
|
|
|
| 288 |
return gr.Dropdown(choices=MVTEC_CATEGORIES, value="bottle")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
return list(common) if common else MVTEC_CATEGORIES
|
| 257 |
|
| 258 |
|
| 259 |
+
def update_categories(model_name: str, current_category: str = None):
|
| 260 |
"""
|
| 261 |
Updates available categories based on selected model.
|
| 262 |
+
Keeps the current category if it's available for the new model.
|
| 263 |
|
| 264 |
Args:
|
| 265 |
model_name: Selected model name
|
| 266 |
+
current_category: Currently selected category to preserve if possible
|
| 267 |
|
| 268 |
Returns:
|
| 269 |
Updated Gradio Dropdown component
|
| 270 |
"""
|
| 271 |
trained = get_trained_categories(model_name)
|
| 272 |
if trained:
|
| 273 |
+
# Keep current category if it's available in the new model
|
| 274 |
+
if current_category and current_category in trained:
|
| 275 |
+
return gr.Dropdown(choices=trained, value=current_category)
|
| 276 |
return gr.Dropdown(choices=trained, value=trained[0])
|
| 277 |
+
# Keep current category if using all categories
|
| 278 |
+
if current_category and current_category in MVTEC_CATEGORIES:
|
| 279 |
+
return gr.Dropdown(choices=MVTEC_CATEGORIES, value=current_category)
|
| 280 |
return gr.Dropdown(choices=MVTEC_CATEGORIES, value="bottle")
|
| 281 |
|
| 282 |
|
| 283 |
+
def update_compare_categories(selected_models: list, current_category: str = None):
|
| 284 |
"""
|
| 285 |
Update categories dropdown based on selected models.
|
| 286 |
+
Keeps the current category if it's available for all selected models.
|
| 287 |
|
| 288 |
Args:
|
| 289 |
selected_models: List of selected model names
|
| 290 |
+
current_category: Currently selected category to preserve if possible
|
| 291 |
|
| 292 |
Returns:
|
| 293 |
Updated Gradio Dropdown component
|
| 294 |
"""
|
| 295 |
common = get_common_categories(selected_models)
|
| 296 |
if common:
|
| 297 |
+
# Keep current category if it's in common categories
|
| 298 |
+
if current_category and current_category in common:
|
| 299 |
+
return gr.Dropdown(choices=common, value=current_category)
|
| 300 |
return gr.Dropdown(choices=common, value=common[0])
|
| 301 |
+
# Keep current category if using all categories
|
| 302 |
+
if current_category and current_category in MVTEC_CATEGORIES:
|
| 303 |
+
return gr.Dropdown(choices=MVTEC_CATEGORIES, value=current_category)
|
| 304 |
return gr.Dropdown(choices=MVTEC_CATEGORIES, value="bottle")
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
def get_sample_images() -> list:
|
| 308 |
+
"""
|
| 309 |
+
Returns a list of sample images from the MVTecAD test set.
|
| 310 |
+
|
| 311 |
+
Returns:
|
| 312 |
+
List of tuples (image_path, label) for gallery display
|
| 313 |
+
"""
|
| 314 |
+
from core import DIR_DATASET
|
| 315 |
+
|
| 316 |
+
samples = []
|
| 317 |
+
|
| 318 |
+
for category in MVTEC_CATEGORIES:
|
| 319 |
+
test_dir = DIR_DATASET / category / "test"
|
| 320 |
+
if not test_dir.exists():
|
| 321 |
+
continue
|
| 322 |
+
|
| 323 |
+
# Get all defect types for this category
|
| 324 |
+
for defect_type in test_dir.iterdir():
|
| 325 |
+
if not defect_type.is_dir():
|
| 326 |
+
continue
|
| 327 |
+
|
| 328 |
+
# Get first image from each defect type
|
| 329 |
+
images = list(defect_type.glob("*.png"))
|
| 330 |
+
if images:
|
| 331 |
+
img_path = str(images[0])
|
| 332 |
+
label = f"{category}/{defect_type.name}"
|
| 333 |
+
samples.append((img_path, label))
|
| 334 |
+
|
| 335 |
+
return samples
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
def get_category_from_sample(image_path_or_label: str) -> str:
|
| 339 |
+
"""
|
| 340 |
+
Extracts category from a sample image path or gallery label.
|
| 341 |
+
|
| 342 |
+
Args:
|
| 343 |
+
image_path_or_label: Path to the sample image or gallery label (format: category/defect_type)
|
| 344 |
+
|
| 345 |
+
Returns:
|
| 346 |
+
Category name extracted from the path or label
|
| 347 |
+
"""
|
| 348 |
+
if image_path_or_label is None:
|
| 349 |
+
return "bottle"
|
| 350 |
+
|
| 351 |
+
path_str = str(image_path_or_label)
|
| 352 |
+
|
| 353 |
+
# First try: check if it's a label format "category/defect_type"
|
| 354 |
+
for category in MVTEC_CATEGORIES:
|
| 355 |
+
if path_str.startswith(f"{category}/") or path_str == category:
|
| 356 |
+
return category
|
| 357 |
+
|
| 358 |
+
# Second try: check if category is in the path
|
| 359 |
+
for category in MVTEC_CATEGORIES:
|
| 360 |
+
if f"/{category}/" in path_str or f"\\{category}\\" in path_str:
|
| 361 |
+
return category
|
| 362 |
+
|
| 363 |
+
return "bottle"
|
| 364 |
+
|
| 365 |
+
|
| 366 |
+
def on_sample_select(evt: gr.SelectData, current_model: str = None):
|
| 367 |
+
"""
|
| 368 |
+
Handler for when a sample image is selected from the gallery.
|
| 369 |
+
Returns the image and updates the category.
|
| 370 |
+
|
| 371 |
+
Args:
|
| 372 |
+
evt: Gradio SelectData event with image info
|
| 373 |
+
current_model: Currently selected model (for category validation)
|
| 374 |
+
|
| 375 |
+
Returns:
|
| 376 |
+
Tuple of (image, category_dropdown_update)
|
| 377 |
+
"""
|
| 378 |
+
if evt is None or evt.value is None:
|
| 379 |
+
return None, gr.Dropdown()
|
| 380 |
+
|
| 381 |
+
# Get the image path from the event
|
| 382 |
+
image_data = evt.value
|
| 383 |
+
if isinstance(image_data, dict):
|
| 384 |
+
image_path = image_data.get("image", {}).get("path", "")
|
| 385 |
+
else:
|
| 386 |
+
image_path = str(image_data)
|
| 387 |
+
|
| 388 |
+
# Extract category from path
|
| 389 |
+
category = get_category_from_sample(image_path)
|
| 390 |
+
|
| 391 |
+
# Load the image
|
| 392 |
+
try:
|
| 393 |
+
img = np.array(Image.open(image_path).convert("RGB"))
|
| 394 |
+
except Exception:
|
| 395 |
+
return None, gr.Dropdown()
|
| 396 |
+
|
| 397 |
+
# Get available categories for current model
|
| 398 |
+
if current_model:
|
| 399 |
+
trained = get_trained_categories(current_model)
|
| 400 |
+
if trained and category in trained:
|
| 401 |
+
return img, gr.Dropdown(choices=trained, value=category)
|
| 402 |
+
elif trained:
|
| 403 |
+
return img, gr.Dropdown(choices=trained, value=trained[0])
|
| 404 |
+
|
| 405 |
+
return img, gr.Dropdown(choices=MVTEC_CATEGORIES, value=category)
|
gradio_ui/tabs/compare.py
CHANGED
|
@@ -7,6 +7,7 @@ Provides the side-by-side model comparison interface.
|
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
from gradio_ui.content import COMPARE_MODELS_INSTRUCTIONS
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
def create_compare_tab(available_models: list, initial_categories: list):
|
|
@@ -67,6 +68,19 @@ def create_compare_tab(available_models: list, initial_categories: list):
|
|
| 67 |
)
|
| 68 |
|
| 69 |
compare_summary = gr.Markdown("*Select models and upload an image to compare*")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
return {
|
| 72 |
"image_input": compare_image_input,
|
|
@@ -75,4 +89,5 @@ def create_compare_tab(available_models: list, initial_categories: list):
|
|
| 75 |
"compare_btn": compare_btn,
|
| 76 |
"result_image": compare_result_image,
|
| 77 |
"summary": compare_summary,
|
|
|
|
| 78 |
}
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
from gradio_ui.content import COMPARE_MODELS_INSTRUCTIONS
|
| 10 |
+
from gradio_ui.handlers import get_sample_images
|
| 11 |
|
| 12 |
|
| 13 |
def create_compare_tab(available_models: list, initial_categories: list):
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
compare_summary = gr.Markdown("*Select models and upload an image to compare*")
|
| 71 |
+
|
| 72 |
+
# Sample Images Gallery
|
| 73 |
+
gr.Markdown("### 🖼️ Sample Images (click to select)")
|
| 74 |
+
compare_sample_gallery = gr.Gallery(
|
| 75 |
+
value=get_sample_images(),
|
| 76 |
+
label="Sample Images",
|
| 77 |
+
show_label=False,
|
| 78 |
+
columns=8,
|
| 79 |
+
rows=3,
|
| 80 |
+
height=350,
|
| 81 |
+
object_fit="cover",
|
| 82 |
+
allow_preview=False,
|
| 83 |
+
)
|
| 84 |
|
| 85 |
return {
|
| 86 |
"image_input": compare_image_input,
|
|
|
|
| 89 |
"compare_btn": compare_btn,
|
| 90 |
"result_image": compare_result_image,
|
| 91 |
"summary": compare_summary,
|
| 92 |
+
"sample_gallery": compare_sample_gallery,
|
| 93 |
}
|
gradio_ui/tabs/draw_defects.py
CHANGED
|
@@ -11,6 +11,7 @@ from gradio_ui.content import (
|
|
| 11 |
BRUSH_COLORS_INFO,
|
| 12 |
HEATMAP_INTERPRETATION,
|
| 13 |
)
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
def create_draw_defects_tab(available_models: list, default_model: str, initial_categories: list):
|
|
@@ -82,6 +83,19 @@ def create_draw_defects_tab(available_models: list, default_model: str, initial_
|
|
| 82 |
sketch_result_text = gr.Markdown(value="", label="Results")
|
| 83 |
|
| 84 |
gr.Markdown(HEATMAP_INTERPRETATION)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
return {
|
| 87 |
"image_editor": image_editor,
|
|
@@ -91,4 +105,5 @@ def create_draw_defects_tab(available_models: list, default_model: str, initial_
|
|
| 91 |
"result_image": sketch_result_image,
|
| 92 |
"result_image": sketch_result_image,
|
| 93 |
"result_text": sketch_result_text,
|
|
|
|
| 94 |
}
|
|
|
|
| 11 |
BRUSH_COLORS_INFO,
|
| 12 |
HEATMAP_INTERPRETATION,
|
| 13 |
)
|
| 14 |
+
from gradio_ui.handlers import get_sample_images
|
| 15 |
|
| 16 |
|
| 17 |
def create_draw_defects_tab(available_models: list, default_model: str, initial_categories: list):
|
|
|
|
| 83 |
sketch_result_text = gr.Markdown(value="", label="Results")
|
| 84 |
|
| 85 |
gr.Markdown(HEATMAP_INTERPRETATION)
|
| 86 |
+
|
| 87 |
+
# Sample Images Gallery
|
| 88 |
+
gr.Markdown("### 🖼️ Sample Images (click to load into editor)")
|
| 89 |
+
sketch_sample_gallery = gr.Gallery(
|
| 90 |
+
value=get_sample_images(),
|
| 91 |
+
label="Sample Images",
|
| 92 |
+
show_label=False,
|
| 93 |
+
columns=8,
|
| 94 |
+
rows=3,
|
| 95 |
+
height=350,
|
| 96 |
+
object_fit="cover",
|
| 97 |
+
allow_preview=False,
|
| 98 |
+
)
|
| 99 |
|
| 100 |
return {
|
| 101 |
"image_editor": image_editor,
|
|
|
|
| 105 |
"result_image": sketch_result_image,
|
| 106 |
"result_image": sketch_result_image,
|
| 107 |
"result_text": sketch_result_text,
|
| 108 |
+
"sample_gallery": sketch_sample_gallery,
|
| 109 |
}
|
gradio_ui/tabs/upload.py
CHANGED
|
@@ -5,6 +5,7 @@ Provides the standard image upload and analysis interface.
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import gradio as gr
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def create_upload_tab(available_models: list, default_model: str, initial_categories: list):
|
|
@@ -61,7 +62,20 @@ def create_upload_tab(available_models: list, default_model: str, initial_catego
|
|
| 61 |
height=300
|
| 62 |
)
|
| 63 |
|
| 64 |
-
result_text = gr.Markdown(value="", label="Results")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
|
| 67 |
return {
|
|
@@ -72,4 +86,5 @@ def create_upload_tab(available_models: list, default_model: str, initial_catego
|
|
| 72 |
"result_image": result_image,
|
| 73 |
"result_image": result_image,
|
| 74 |
"result_text": result_text,
|
|
|
|
| 75 |
}
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
+
from gradio_ui.handlers import get_sample_images
|
| 9 |
|
| 10 |
|
| 11 |
def create_upload_tab(available_models: list, default_model: str, initial_categories: list):
|
|
|
|
| 62 |
height=300
|
| 63 |
)
|
| 64 |
|
| 65 |
+
result_text = gr.Markdown(value="", label="Results")
|
| 66 |
+
|
| 67 |
+
# Sample Images Gallery
|
| 68 |
+
gr.Markdown("### 🖼️ Sample Images (click to select)")
|
| 69 |
+
sample_gallery = gr.Gallery(
|
| 70 |
+
value=get_sample_images(),
|
| 71 |
+
label="Sample Images",
|
| 72 |
+
show_label=False,
|
| 73 |
+
columns=8,
|
| 74 |
+
rows=3,
|
| 75 |
+
height=350,
|
| 76 |
+
object_fit="cover",
|
| 77 |
+
allow_preview=False,
|
| 78 |
+
)
|
| 79 |
|
| 80 |
|
| 81 |
return {
|
|
|
|
| 86 |
"result_image": result_image,
|
| 87 |
"result_image": result_image,
|
| 88 |
"result_text": result_text,
|
| 89 |
+
"sample_gallery": sample_gallery,
|
| 90 |
}
|
sample_images/bottle/broken_large/000.png
ADDED
|
Git LFS Details
|
sample_images/bottle/broken_large/001.png
ADDED
|
Git LFS Details
|
sample_images/bottle/broken_small/000.png
ADDED
|
Git LFS Details
|
sample_images/bottle/broken_small/001.png
ADDED
|
Git LFS Details
|
sample_images/bottle/contamination/000.png
ADDED
|
Git LFS Details
|
sample_images/bottle/contamination/001.png
ADDED
|
Git LFS Details
|
sample_images/bottle/good/000.png
ADDED
|
Git LFS Details
|
sample_images/cable/bent_wire/000.png
ADDED
|
Git LFS Details
|
sample_images/cable/bent_wire/001.png
ADDED
|
Git LFS Details
|
sample_images/cable/cable_swap/000.png
ADDED
|
Git LFS Details
|
sample_images/cable/cable_swap/001.png
ADDED
|
Git LFS Details
|
sample_images/cable/combined/000.png
ADDED
|
Git LFS Details
|
sample_images/cable/combined/001.png
ADDED
|
Git LFS Details
|
sample_images/cable/cut_inner_insulation/000.png
ADDED
|
Git LFS Details
|
sample_images/cable/cut_inner_insulation/001.png
ADDED
|
Git LFS Details
|
sample_images/cable/good/000.png
ADDED
|
Git LFS Details
|
sample_images/capsule/crack/000.png
ADDED
|
Git LFS Details
|
sample_images/capsule/crack/001.png
ADDED
|
Git LFS Details
|
sample_images/capsule/faulty_imprint/000.png
ADDED
|
Git LFS Details
|
sample_images/capsule/faulty_imprint/001.png
ADDED
|
Git LFS Details
|
sample_images/capsule/good/000.png
ADDED
|
Git LFS Details
|
sample_images/capsule/poke/000.png
ADDED
|
Git LFS Details
|
sample_images/capsule/poke/001.png
ADDED
|
Git LFS Details
|
sample_images/capsule/scratch/000.png
ADDED
|
Git LFS Details
|
sample_images/capsule/scratch/001.png
ADDED
|
Git LFS Details
|
sample_images/carpet/color/000.png
ADDED
|
Git LFS Details
|
sample_images/carpet/color/001.png
ADDED
|
Git LFS Details
|
sample_images/carpet/cut/000.png
ADDED
|
Git LFS Details
|
sample_images/carpet/cut/001.png
ADDED
|
Git LFS Details
|
sample_images/carpet/good/000.png
ADDED
|
Git LFS Details
|
sample_images/carpet/hole/000.png
ADDED
|
Git LFS Details
|
sample_images/carpet/hole/001.png
ADDED
|
Git LFS Details
|
sample_images/carpet/metal_contamination/000.png
ADDED
|
Git LFS Details
|
sample_images/carpet/metal_contamination/001.png
ADDED
|
Git LFS Details
|
sample_images/grid/bent/000.png
ADDED
|
Git LFS Details
|
sample_images/grid/bent/001.png
ADDED
|
Git LFS Details
|
sample_images/grid/broken/000.png
ADDED
|
Git LFS Details
|
sample_images/grid/broken/001.png
ADDED
|
Git LFS Details
|
sample_images/grid/glue/000.png
ADDED
|
Git LFS Details
|
sample_images/grid/glue/001.png
ADDED
|
Git LFS Details
|
sample_images/grid/good/000.png
ADDED
|
Git LFS Details
|
sample_images/grid/metal_contamination/000.png
ADDED
|
Git LFS Details
|
sample_images/grid/metal_contamination/001.png
ADDED
|
Git LFS Details
|
sample_images/hazelnut/crack/000.png
ADDED
|
Git LFS Details
|
sample_images/hazelnut/crack/001.png
ADDED
|
Git LFS Details
|