Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -148,7 +148,8 @@ def run_yolo_generic(img_rgb, model_path, target_classes, color):
|
|
| 148 |
|
| 149 |
out = apply_mask_overlay(img_rgb, combined_mask, color=color)
|
| 150 |
out = draw_boxes(out, boxes, labels, color=color)
|
| 151 |
-
|
|
|
|
| 152 |
|
| 153 |
def run_sam_generic(img_rgb, yolo_model_path, target_classes, color):
|
| 154 |
try:
|
|
@@ -190,14 +191,15 @@ def run_sam_generic(img_rgb, yolo_model_path, target_classes, color):
|
|
| 190 |
elapsed = time.time() - t0
|
| 191 |
out = apply_mask_overlay(img_rgb, combined_mask, color=color)
|
| 192 |
out = draw_boxes(out, boxes_list, labels, color=color)
|
| 193 |
-
|
|
|
|
| 194 |
|
| 195 |
except ImportError:
|
| 196 |
h, w = img_rgb.shape[:2]
|
| 197 |
placeholder = img_rgb.copy()
|
| 198 |
cv2.putText(placeholder, "SAM: pip install segment-anything",
|
| 199 |
(20, h//2), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,80,160), 2)
|
| 200 |
-
return placeholder, "Error: segment-anything not installed"
|
| 201 |
|
| 202 |
def run_maskrcnn(img_rgb):
|
| 203 |
import torchvision
|
|
@@ -231,7 +233,8 @@ def run_maskrcnn(img_rgb):
|
|
| 231 |
labels.append(f"car {score:.2f}")
|
| 232 |
out = apply_mask_overlay(img_rgb, combined_mask, color=(80, 120, 255))
|
| 233 |
out = draw_boxes(out, boxes, labels, color=(80, 120, 255))
|
| 234 |
-
|
|
|
|
| 235 |
|
| 236 |
def run_segformer(img_rgb):
|
| 237 |
from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation
|
|
@@ -256,14 +259,15 @@ def run_segformer(img_rgb):
|
|
| 256 |
car_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
|
| 257 |
)
|
| 258 |
out = cv2.drawContours(out, contours, -1, (255, 180, 50), 2)
|
| 259 |
-
|
|
|
|
| 260 |
|
| 261 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 262 |
# Gradio Process Function
|
| 263 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 264 |
def process_image(img_rgb, model_name):
|
| 265 |
if img_rgb is None:
|
| 266 |
-
return None, "Please upload an image."
|
| 267 |
try:
|
| 268 |
if model_name == "YOLOv8x-seg (Custom Mirror)":
|
| 269 |
return run_yolo_generic(img_rgb, "best.pt", target_classes=[0, 1], color=(50, 220, 100))
|
|
@@ -278,22 +282,65 @@ def process_image(img_rgb, model_name):
|
|
| 278 |
elif model_name == "SegFormer (Pretrained Car)":
|
| 279 |
return run_segformer(img_rgb)
|
| 280 |
else:
|
| 281 |
-
return img_rgb, "Model not recognized."
|
| 282 |
except Exception as e:
|
| 283 |
-
return img_rgb, f"Error: {str(e)}"
|
| 284 |
|
| 285 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 286 |
# Gradio UI
|
| 287 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 288 |
theme = gr.themes.Soft(primary_hue="blue", secondary_hue="indigo")
|
| 289 |
|
| 290 |
-
with gr.Blocks(theme=theme, title="Car
|
| 291 |
gr.Markdown("""
|
| 292 |
-
#
|
|
|
|
| 293 |
""")
|
| 294 |
|
| 295 |
-
# ββ TAB 1:
|
| 296 |
-
with gr.Tab(" Test
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
with gr.Row():
|
| 298 |
with gr.Column(scale=1):
|
| 299 |
input_image_car = gr.Image(type="numpy", label="Upload Car Image")
|
|
@@ -308,16 +355,19 @@ with gr.Blocks(theme=theme, title="Car and Windows Segmentation") as demo:
|
|
| 308 |
label="Select Pretrained Model",
|
| 309 |
info="These models are pretrained from the internet to detect full cars."
|
| 310 |
)
|
| 311 |
-
submit_btn_car = gr.Button(" Run Segmentation", variant="primary", size="lg")
|
| 312 |
with gr.Column(scale=1):
|
| 313 |
output_image_car = gr.Image(label="Segmentation Result", interactive=False)
|
|
|
|
| 314 |
output_stats_car = gr.Textbox(label="Detection Statistics", interactive=False)
|
| 315 |
|
| 316 |
if car_examples:
|
| 317 |
-
gr.Markdown("###
|
| 318 |
car_gallery = gr.Gallery(
|
| 319 |
value=car_examples,
|
| 320 |
columns=10,
|
|
|
|
|
|
|
| 321 |
allow_preview=False,
|
| 322 |
show_label=False
|
| 323 |
)
|
|
@@ -330,46 +380,7 @@ with gr.Blocks(theme=theme, title="Car and Windows Segmentation") as demo:
|
|
| 330 |
submit_btn_car.click(
|
| 331 |
fn=process_image,
|
| 332 |
inputs=[input_image_car, model_dropdown_car],
|
| 333 |
-
outputs=[output_image_car, output_stats_car]
|
| 334 |
-
)
|
| 335 |
-
|
| 336 |
-
# ββ TAB 2: Car Mirrors (Custom) ββ
|
| 337 |
-
with gr.Tab(" Test Car Mirrors (Custom Models)"):
|
| 338 |
-
with gr.Row():
|
| 339 |
-
with gr.Column(scale=1):
|
| 340 |
-
input_image_mirror = gr.Image(type="numpy", label="Upload Mirror Image")
|
| 341 |
-
model_dropdown_mirror = gr.Dropdown(
|
| 342 |
-
choices=[
|
| 343 |
-
"YOLOv8x-seg (Custom Mirror)",
|
| 344 |
-
"SAM + YOLO (Custom Mirror)"
|
| 345 |
-
],
|
| 346 |
-
value="YOLOv8x-seg (Custom Mirror)",
|
| 347 |
-
label="Select Custom Model",
|
| 348 |
-
info="These models are specifically trained to detect car mirrors."
|
| 349 |
-
)
|
| 350 |
-
submit_btn_mirror = gr.Button("π Run Segmentation", variant="primary", size="lg")
|
| 351 |
-
with gr.Column(scale=1):
|
| 352 |
-
output_image_mirror = gr.Image(label="Segmentation Result", interactive=False)
|
| 353 |
-
output_stats_mirror = gr.Textbox(label="Detection Statistics", interactive=False)
|
| 354 |
-
|
| 355 |
-
if mirror_examples:
|
| 356 |
-
gr.Markdown("### Click any mirror image below to load it")
|
| 357 |
-
mirror_gallery = gr.Gallery(
|
| 358 |
-
value=mirror_examples,
|
| 359 |
-
columns=10,
|
| 360 |
-
allow_preview=False,
|
| 361 |
-
show_label=False
|
| 362 |
-
)
|
| 363 |
-
|
| 364 |
-
def load_mirror_img(evt: gr.SelectData):
|
| 365 |
-
return mirror_examples[evt.index]
|
| 366 |
-
|
| 367 |
-
mirror_gallery.select(fn=load_mirror_img, inputs=None, outputs=input_image_mirror)
|
| 368 |
-
|
| 369 |
-
submit_btn_mirror.click(
|
| 370 |
-
fn=process_image,
|
| 371 |
-
inputs=[input_image_mirror, model_dropdown_mirror],
|
| 372 |
-
outputs=[output_image_mirror, output_stats_mirror]
|
| 373 |
)
|
| 374 |
|
| 375 |
if __name__ == "__main__":
|
|
|
|
| 148 |
|
| 149 |
out = apply_mask_overlay(img_rgb, combined_mask, color=color)
|
| 150 |
out = draw_boxes(out, boxes, labels, color=color)
|
| 151 |
+
bw_mask = (combined_mask * 255).astype(np.uint8)
|
| 152 |
+
return out, bw_mask, f"Found: {len(boxes)} | Inference Time: {elapsed:.2f}s"
|
| 153 |
|
| 154 |
def run_sam_generic(img_rgb, yolo_model_path, target_classes, color):
|
| 155 |
try:
|
|
|
|
| 191 |
elapsed = time.time() - t0
|
| 192 |
out = apply_mask_overlay(img_rgb, combined_mask, color=color)
|
| 193 |
out = draw_boxes(out, boxes_list, labels, color=color)
|
| 194 |
+
bw_mask = (combined_mask * 255).astype(np.uint8)
|
| 195 |
+
return out, bw_mask, f"Found: {len(boxes_list)} | Inference Time: {elapsed:.2f}s"
|
| 196 |
|
| 197 |
except ImportError:
|
| 198 |
h, w = img_rgb.shape[:2]
|
| 199 |
placeholder = img_rgb.copy()
|
| 200 |
cv2.putText(placeholder, "SAM: pip install segment-anything",
|
| 201 |
(20, h//2), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,80,160), 2)
|
| 202 |
+
return placeholder, None, "Error: segment-anything not installed"
|
| 203 |
|
| 204 |
def run_maskrcnn(img_rgb):
|
| 205 |
import torchvision
|
|
|
|
| 233 |
labels.append(f"car {score:.2f}")
|
| 234 |
out = apply_mask_overlay(img_rgb, combined_mask, color=(80, 120, 255))
|
| 235 |
out = draw_boxes(out, boxes, labels, color=(80, 120, 255))
|
| 236 |
+
bw_mask = (combined_mask * 255).astype(np.uint8)
|
| 237 |
+
return out, bw_mask, f"Cars found: {len(boxes)} | Inference Time: {elapsed:.2f}s"
|
| 238 |
|
| 239 |
def run_segformer(img_rgb):
|
| 240 |
from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation
|
|
|
|
| 259 |
car_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
|
| 260 |
)
|
| 261 |
out = cv2.drawContours(out, contours, -1, (255, 180, 50), 2)
|
| 262 |
+
bw_mask = (car_mask * 255).astype(np.uint8)
|
| 263 |
+
return out, bw_mask, f"Car regions: {len(contours)} | Inference Time: {elapsed:.2f}s"
|
| 264 |
|
| 265 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 266 |
# Gradio Process Function
|
| 267 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 268 |
def process_image(img_rgb, model_name):
|
| 269 |
if img_rgb is None:
|
| 270 |
+
return None, None, "Please upload an image."
|
| 271 |
try:
|
| 272 |
if model_name == "YOLOv8x-seg (Custom Mirror)":
|
| 273 |
return run_yolo_generic(img_rgb, "best.pt", target_classes=[0, 1], color=(50, 220, 100))
|
|
|
|
| 282 |
elif model_name == "SegFormer (Pretrained Car)":
|
| 283 |
return run_segformer(img_rgb)
|
| 284 |
else:
|
| 285 |
+
return img_rgb, None, "Model not recognized."
|
| 286 |
except Exception as e:
|
| 287 |
+
return img_rgb, None, f"Error: {str(e)}"
|
| 288 |
|
| 289 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 290 |
# Gradio UI
|
| 291 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 292 |
theme = gr.themes.Soft(primary_hue="blue", secondary_hue="indigo")
|
| 293 |
|
| 294 |
+
with gr.Blocks(theme=theme, title="Car vs Mirror Segmentation") as demo:
|
| 295 |
gr.Markdown("""
|
| 296 |
+
# π Car vs Mirror Segmentation Comparison
|
| 297 |
+
Compare your custom trained **Car Mirror** models against pretrained **Full Car** models.
|
| 298 |
""")
|
| 299 |
|
| 300 |
+
# ββ TAB 1: Car Mirrors (Custom) - NOW THE DEFAULT TAB ββ
|
| 301 |
+
with gr.Tab("πͺ Test Car Mirrors (Custom Models)"):
|
| 302 |
+
with gr.Row():
|
| 303 |
+
with gr.Column(scale=1):
|
| 304 |
+
input_image_mirror = gr.Image(type="numpy", label="Upload Mirror Image")
|
| 305 |
+
model_dropdown_mirror = gr.Dropdown(
|
| 306 |
+
choices=[
|
| 307 |
+
"YOLOv8x-seg (Custom Mirror)",
|
| 308 |
+
"SAM + YOLO (Custom Mirror)"
|
| 309 |
+
],
|
| 310 |
+
value="YOLOv8x-seg (Custom Mirror)",
|
| 311 |
+
label="Select Custom Model",
|
| 312 |
+
info="These models are specifically trained to detect car mirrors."
|
| 313 |
+
)
|
| 314 |
+
submit_btn_mirror = gr.Button("π Run Segmentation", variant="primary", size="lg")
|
| 315 |
+
with gr.Column(scale=1):
|
| 316 |
+
output_image_mirror = gr.Image(label="Segmentation Result", interactive=False)
|
| 317 |
+
output_mask_mirror = gr.Image(label="Binary Mask (White=Object, Black=Background)", interactive=False)
|
| 318 |
+
output_stats_mirror = gr.Textbox(label="Detection Statistics", interactive=False)
|
| 319 |
+
|
| 320 |
+
if mirror_examples:
|
| 321 |
+
gr.Markdown("### πΈ Click any mirror image below to load it")
|
| 322 |
+
mirror_gallery = gr.Gallery(
|
| 323 |
+
value=mirror_examples,
|
| 324 |
+
columns=10,
|
| 325 |
+
height=120,
|
| 326 |
+
object_fit="cover",
|
| 327 |
+
allow_preview=False,
|
| 328 |
+
show_label=False
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
def load_mirror_img(evt: gr.SelectData):
|
| 332 |
+
return mirror_examples[evt.index]
|
| 333 |
+
|
| 334 |
+
mirror_gallery.select(fn=load_mirror_img, inputs=None, outputs=input_image_mirror)
|
| 335 |
+
|
| 336 |
+
submit_btn_mirror.click(
|
| 337 |
+
fn=process_image,
|
| 338 |
+
inputs=[input_image_mirror, model_dropdown_mirror],
|
| 339 |
+
outputs=[output_image_mirror, output_mask_mirror, output_stats_mirror]
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
# ββ TAB 2: Full Car (Pretrained) ββ
|
| 343 |
+
with gr.Tab("π Test Full Cars (Pretrained Models)"):
|
| 344 |
with gr.Row():
|
| 345 |
with gr.Column(scale=1):
|
| 346 |
input_image_car = gr.Image(type="numpy", label="Upload Car Image")
|
|
|
|
| 355 |
label="Select Pretrained Model",
|
| 356 |
info="These models are pretrained from the internet to detect full cars."
|
| 357 |
)
|
| 358 |
+
submit_btn_car = gr.Button("π Run Segmentation", variant="primary", size="lg")
|
| 359 |
with gr.Column(scale=1):
|
| 360 |
output_image_car = gr.Image(label="Segmentation Result", interactive=False)
|
| 361 |
+
output_mask_car = gr.Image(label="Binary Mask (White=Object, Black=Background)", interactive=False)
|
| 362 |
output_stats_car = gr.Textbox(label="Detection Statistics", interactive=False)
|
| 363 |
|
| 364 |
if car_examples:
|
| 365 |
+
gr.Markdown("### πΈ Click any car image below to load it")
|
| 366 |
car_gallery = gr.Gallery(
|
| 367 |
value=car_examples,
|
| 368 |
columns=10,
|
| 369 |
+
height=120,
|
| 370 |
+
object_fit="cover",
|
| 371 |
allow_preview=False,
|
| 372 |
show_label=False
|
| 373 |
)
|
|
|
|
| 380 |
submit_btn_car.click(
|
| 381 |
fn=process_image,
|
| 382 |
inputs=[input_image_car, model_dropdown_car],
|
| 383 |
+
outputs=[output_image_car, output_mask_car, output_stats_car]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
)
|
| 385 |
|
| 386 |
if __name__ == "__main__":
|