Alessio Grancini
commited on
Update image_segmenter.py
Browse files- image_segmenter.py +17 -17
image_segmenter.py
CHANGED
|
@@ -7,17 +7,17 @@ import spaces
|
|
| 7 |
|
| 8 |
class ImageSegmenter:
|
| 9 |
def __init__(self, model_type="yolov8s-seg") -> None:
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
def get_cls_clr(self, cls_id):
|
| 23 |
if cls_id in self.cls_clr:
|
|
@@ -30,18 +30,18 @@ class ImageSegmenter:
|
|
| 30 |
self.cls_clr[cls_id] = (r, g, b)
|
| 31 |
return (r, g, b)
|
| 32 |
|
| 33 |
-
@spaces.GPU
|
| 34 |
def predict(self, image):
|
| 35 |
-
# Load model if not loaded
|
| 36 |
if self.model is None:
|
|
|
|
| 37 |
self.model = YOLO('models/' + self.model_type + '.pt')
|
| 38 |
-
self.model.to(
|
|
|
|
| 39 |
|
| 40 |
# params
|
| 41 |
objects_data = []
|
| 42 |
image = image.copy()
|
| 43 |
-
|
| 44 |
-
# Run prediction
|
| 45 |
predictions = self.model.predict(image)
|
| 46 |
|
| 47 |
cls_ids = predictions[0].boxes.cls.cpu().numpy()
|
|
@@ -73,7 +73,7 @@ class ImageSegmenter:
|
|
| 73 |
if image.shape[:2] != seg_mask[id].shape[:2]:
|
| 74 |
colored_mask = cv2.resize(colored_mask, (image.shape[1], image.shape[0]))
|
| 75 |
|
| 76 |
-
# filling the
|
| 77 |
masked = np.ma.MaskedArray(image, mask=colored_mask, fill_value=cls_clr)
|
| 78 |
image_overlay = masked.filled()
|
| 79 |
image = cv2.addWeighted(image, 1 - alpha, image_overlay, alpha, 0)
|
|
|
|
| 7 |
|
| 8 |
class ImageSegmenter:
|
| 9 |
def __init__(self, model_type="yolov8s-seg") -> None:
|
| 10 |
+
# Store parameters but don't initialize CUDA
|
| 11 |
+
self.model_type = model_type
|
| 12 |
+
self.is_show_bounding_boxes = True
|
| 13 |
+
self.is_show_segmentation_boundary = False
|
| 14 |
+
self.is_show_segmentation = False
|
| 15 |
+
self.confidence_threshold = 0.5
|
| 16 |
+
self.cls_clr = {}
|
| 17 |
+
self.bb_thickness = 2
|
| 18 |
+
self.bb_clr = (255, 0, 0)
|
| 19 |
+
self.masks = {}
|
| 20 |
+
self.model = None # Model will be loaded in predict
|
| 21 |
|
| 22 |
def get_cls_clr(self, cls_id):
|
| 23 |
if cls_id in self.cls_clr:
|
|
|
|
| 30 |
self.cls_clr[cls_id] = (r, g, b)
|
| 31 |
return (r, g, b)
|
| 32 |
|
| 33 |
+
@spaces.GPU
|
| 34 |
def predict(self, image):
|
| 35 |
+
# Load model if not loaded
|
| 36 |
if self.model is None:
|
| 37 |
+
print("Loading YOLO model...")
|
| 38 |
self.model = YOLO('models/' + self.model_type + '.pt')
|
| 39 |
+
self.model.to('cuda')
|
| 40 |
+
print("Model loaded successfully")
|
| 41 |
|
| 42 |
# params
|
| 43 |
objects_data = []
|
| 44 |
image = image.copy()
|
|
|
|
|
|
|
| 45 |
predictions = self.model.predict(image)
|
| 46 |
|
| 47 |
cls_ids = predictions[0].boxes.cls.cpu().numpy()
|
|
|
|
| 73 |
if image.shape[:2] != seg_mask[id].shape[:2]:
|
| 74 |
colored_mask = cv2.resize(colored_mask, (image.shape[1], image.shape[0]))
|
| 75 |
|
| 76 |
+
# filling the mased area with class color
|
| 77 |
masked = np.ma.MaskedArray(image, mask=colored_mask, fill_value=cls_clr)
|
| 78 |
image_overlay = masked.filled()
|
| 79 |
image = cv2.addWeighted(image, 1 - alpha, image_overlay, alpha, 0)
|