Update detect_bubbles.py
Browse files- detect_bubbles.py +11 -4
detect_bubbles.py
CHANGED
|
@@ -1,18 +1,25 @@
|
|
| 1 |
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def detect_bubbles(model_path, image_path):
|
| 4 |
"""
|
| 5 |
Detects bubbles in an image using a YOLOv8 model.
|
| 6 |
-
|
| 7 |
Args:
|
| 8 |
model_path (str): The file path to the YOLO model.
|
| 9 |
image_path (str): The file path to the input image.
|
| 10 |
-
|
| 11 |
Returns:
|
| 12 |
-
list: A list containing the coordinates, score and class_id of
|
| 13 |
the detected bubbles.
|
| 14 |
"""
|
| 15 |
-
model =
|
| 16 |
bubbles = model(image_path)[0]
|
| 17 |
|
| 18 |
return bubbles.boxes.data.tolist()
|
|
|
|
| 1 |
from ultralytics import YOLO
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
def safe_load_model(model_path):
|
| 5 |
+
"""
|
| 6 |
+
Manually loads the YOLO model weights with 'weights_only' set to False
|
| 7 |
+
to avoid the security check issue with PyTorch.
|
| 8 |
+
"""
|
| 9 |
+
model = torch.load(model_path, map_location='cpu', weights_only=False)
|
| 10 |
+
return model
|
| 11 |
|
| 12 |
def detect_bubbles(model_path, image_path):
|
| 13 |
"""
|
| 14 |
Detects bubbles in an image using a YOLOv8 model.
|
|
|
|
| 15 |
Args:
|
| 16 |
model_path (str): The file path to the YOLO model.
|
| 17 |
image_path (str): The file path to the input image.
|
|
|
|
| 18 |
Returns:
|
| 19 |
+
list: A list containing the coordinates, score, and class_id of
|
| 20 |
the detected bubbles.
|
| 21 |
"""
|
| 22 |
+
model = safe_load_model(model_path)
|
| 23 |
bubbles = model(image_path)[0]
|
| 24 |
|
| 25 |
return bubbles.boxes.data.tolist()
|