Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -31,7 +31,7 @@ def draw_boxes(img_rgb, boxes, labels, color=(0, 220, 100)):
|
|
| 31 |
return out
|
| 32 |
|
| 33 |
# βββ Model Functions ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
-
def run_yolo_generic(img_rgb, model_path,
|
| 35 |
from ultralytics import YOLO
|
| 36 |
t0 = time.time()
|
| 37 |
model = YOLO(model_path)
|
|
@@ -50,13 +50,13 @@ def run_yolo_generic(img_rgb, model_path, target_class, color):
|
|
| 50 |
result.boxes.cls,
|
| 51 |
result.boxes.conf
|
| 52 |
):
|
| 53 |
-
if int(cls)
|
| 54 |
continue
|
| 55 |
mask_np = mask.cpu().numpy()
|
| 56 |
mask_r = cv2.resize(mask_np, (w, h), interpolation=cv2.INTER_NEAREST)
|
| 57 |
combined_mask |= mask_r > 0.5
|
| 58 |
boxes.append(box.cpu().tolist())
|
| 59 |
-
class_name = "mirror" if
|
| 60 |
labels.append(f"{class_name} {conf:.2f}")
|
| 61 |
|
| 62 |
out = apply_mask_overlay(img_rgb, combined_mask, color=color)
|
|
@@ -64,7 +64,7 @@ def run_yolo_generic(img_rgb, model_path, target_class, color):
|
|
| 64 |
n = len(boxes)
|
| 65 |
return out, f"Found: {n} | Inference Time: {elapsed:.2f}s"
|
| 66 |
|
| 67 |
-
def run_sam_generic(img_rgb, yolo_model_path,
|
| 68 |
try:
|
| 69 |
from segment_anything import sam_model_registry, SamPredictor
|
| 70 |
import urllib.request, os
|
|
@@ -95,7 +95,7 @@ def run_sam_generic(img_rgb, yolo_model_path, target_class, color):
|
|
| 95 |
yolo_res.boxes.cls,
|
| 96 |
yolo_res.boxes.conf
|
| 97 |
):
|
| 98 |
-
if int(cls)
|
| 99 |
continue
|
| 100 |
box_np = box.cpu().numpy()
|
| 101 |
masks_sam, _, _ = predictor.predict(
|
|
@@ -104,7 +104,7 @@ def run_sam_generic(img_rgb, yolo_model_path, target_class, color):
|
|
| 104 |
)
|
| 105 |
combined_mask |= masks_sam[0]
|
| 106 |
boxes_list.append(box_np.tolist())
|
| 107 |
-
class_name = "mirror" if
|
| 108 |
labels.append(f"{class_name} {conf:.2f}")
|
| 109 |
|
| 110 |
elapsed = time.time() - t0
|
|
@@ -204,16 +204,16 @@ def process_image(img_rgb, model_name):
|
|
| 204 |
|
| 205 |
try:
|
| 206 |
if model_name == "YOLOv8x-seg (Custom Mirror)":
|
| 207 |
-
return run_yolo_generic(img_rgb, "best.pt",
|
| 208 |
|
| 209 |
elif model_name == "YOLOv8x (Pretrained Car)":
|
| 210 |
-
return run_yolo_generic(img_rgb, "yolov8x-seg.pt",
|
| 211 |
|
| 212 |
elif model_name == "SAM + YOLO (Custom Mirror)":
|
| 213 |
-
return run_sam_generic(img_rgb, "best.pt",
|
| 214 |
|
| 215 |
elif model_name == "SAM + YOLO (Pretrained Car)":
|
| 216 |
-
return run_sam_generic(img_rgb, "yolov8x-seg.pt",
|
| 217 |
|
| 218 |
elif model_name == "Mask R-CNN (Pretrained Car)":
|
| 219 |
return run_maskrcnn(img_rgb)
|
|
|
|
| 31 |
return out
|
| 32 |
|
| 33 |
# βββ Model Functions ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
+
def run_yolo_generic(img_rgb, model_path, target_classes, color):
|
| 35 |
from ultralytics import YOLO
|
| 36 |
t0 = time.time()
|
| 37 |
model = YOLO(model_path)
|
|
|
|
| 50 |
result.boxes.cls,
|
| 51 |
result.boxes.conf
|
| 52 |
):
|
| 53 |
+
if int(cls) not in target_classes:
|
| 54 |
continue
|
| 55 |
mask_np = mask.cpu().numpy()
|
| 56 |
mask_r = cv2.resize(mask_np, (w, h), interpolation=cv2.INTER_NEAREST)
|
| 57 |
combined_mask |= mask_r > 0.5
|
| 58 |
boxes.append(box.cpu().tolist())
|
| 59 |
+
class_name = "mirror" if 0 in target_classes or 1 in target_classes else "car"
|
| 60 |
labels.append(f"{class_name} {conf:.2f}")
|
| 61 |
|
| 62 |
out = apply_mask_overlay(img_rgb, combined_mask, color=color)
|
|
|
|
| 64 |
n = len(boxes)
|
| 65 |
return out, f"Found: {n} | Inference Time: {elapsed:.2f}s"
|
| 66 |
|
| 67 |
+
def run_sam_generic(img_rgb, yolo_model_path, target_classes, color):
|
| 68 |
try:
|
| 69 |
from segment_anything import sam_model_registry, SamPredictor
|
| 70 |
import urllib.request, os
|
|
|
|
| 95 |
yolo_res.boxes.cls,
|
| 96 |
yolo_res.boxes.conf
|
| 97 |
):
|
| 98 |
+
if int(cls) not in target_classes:
|
| 99 |
continue
|
| 100 |
box_np = box.cpu().numpy()
|
| 101 |
masks_sam, _, _ = predictor.predict(
|
|
|
|
| 104 |
)
|
| 105 |
combined_mask |= masks_sam[0]
|
| 106 |
boxes_list.append(box_np.tolist())
|
| 107 |
+
class_name = "mirror" if 0 in target_classes or 1 in target_classes else "car"
|
| 108 |
labels.append(f"{class_name} {conf:.2f}")
|
| 109 |
|
| 110 |
elapsed = time.time() - t0
|
|
|
|
| 204 |
|
| 205 |
try:
|
| 206 |
if model_name == "YOLOv8x-seg (Custom Mirror)":
|
| 207 |
+
return run_yolo_generic(img_rgb, "best.pt", target_classes=[0, 1], color=(50, 220, 100))
|
| 208 |
|
| 209 |
elif model_name == "YOLOv8x (Pretrained Car)":
|
| 210 |
+
return run_yolo_generic(img_rgb, "yolov8x-seg.pt", target_classes=[2], color=(0, 200, 255))
|
| 211 |
|
| 212 |
elif model_name == "SAM + YOLO (Custom Mirror)":
|
| 213 |
+
return run_sam_generic(img_rgb, "best.pt", target_classes=[0, 1], color=(255, 80, 160))
|
| 214 |
|
| 215 |
elif model_name == "SAM + YOLO (Pretrained Car)":
|
| 216 |
+
return run_sam_generic(img_rgb, "yolov8x-seg.pt", target_classes=[2], color=(200, 80, 255))
|
| 217 |
|
| 218 |
elif model_name == "Mask R-CNN (Pretrained Car)":
|
| 219 |
return run_maskrcnn(img_rgb)
|