rifatramadhani commited on
Commit
085be2c
·
1 Parent(s): bb51827
Files changed (1) hide show
  1. detection/object_detection.py +10 -9
detection/object_detection.py CHANGED
@@ -10,20 +10,21 @@ from ultralytics import YOLO
10
  # Local imports
11
  from utils.image_utils import load_image, preprocess_image
12
 
 
13
 
14
- # Load the YOLOv8 model globally to avoid reloading on each function call
15
- # Using a common pre-trained YOLOv8 nano model ('yolov8n.pt')
16
  try:
17
- model = YOLO('yolov8n.pt')
18
- print("YOLOv8 model loaded successfully.")
19
  except Exception as e:
20
- print(f"Error loading YOLOv8 model: {e}")
21
  model = None # Set model to None if loading fails
22
 
23
 
24
  def object_detection(input_type, uploaded_image, image_url, base64_string):
25
  """
26
- Performs object detection on the image from various input types using YOLOv8.
27
 
28
  Args:
29
  input_type (str): The selected input method ("Upload File", "Enter URL", "Enter Base64").
@@ -37,7 +38,7 @@ def object_detection(input_type, uploaded_image, image_url, base64_string):
37
  - dict: A dictionary containing the raw detection data (bounding boxes, classes, scores), or None.
38
  """
39
  if model is None:
40
- print("YOLOv8 model is not loaded. Cannot perform object detection.")
41
  return None, None # Return None for both outputs
42
 
43
  image = None
@@ -84,7 +85,7 @@ def object_detection(input_type, uploaded_image, image_url, base64_string):
84
  # box.xywh contains [x_center, y_center, width, height]
85
  # box.conf contains confidence score
86
  # box.cls contains class index
87
- x_center, y_center, width, height = [round(float(coord), 2) for coord in box.xywh[0].tolist()] # Changed to xywh
88
  confidence = round(float(box.conf[0]), 4)
89
  class_id = int(box.cls[0])
90
  class_name = model.names[class_id] if model.names else str(class_id) # Get class name if available
@@ -103,5 +104,5 @@ def object_detection(input_type, uploaded_image, image_url, base64_string):
103
  return result_image_np, raw_data # Return both the image and raw data
104
 
105
  except Exception as e:
106
- print(f"Error during YOLOv8 object detection: {e}")
107
  return None, None # Return None for both outputs
 
10
  # Local imports
11
  from utils.image_utils import load_image, preprocess_image
12
 
13
+ YOLO_MODEL = "yolo11n.pt"
14
 
15
+ # Load the YOLO model globally to avoid reloading on each function call
16
+ # Using a common pre-trained YOLO nano model ('yolov8n.pt')
17
  try:
18
+ model = YOLO(YOLO_MODEL)
19
+ print("YOLO model loaded successfully.")
20
  except Exception as e:
21
+ print(f"Error loading YOLO model: {e}")
22
  model = None # Set model to None if loading fails
23
 
24
 
25
  def object_detection(input_type, uploaded_image, image_url, base64_string):
26
  """
27
+ Performs object detection on the image from various input types using YOLO (YOLOv11 nano).
28
 
29
  Args:
30
  input_type (str): The selected input method ("Upload File", "Enter URL", "Enter Base64").
 
38
  - dict: A dictionary containing the raw detection data (bounding boxes, classes, scores), or None.
39
  """
40
  if model is None:
41
+ print("YOLO model is not loaded. Cannot perform object detection.")
42
  return None, None # Return None for both outputs
43
 
44
  image = None
 
85
  # box.xywh contains [x_center, y_center, width, height]
86
  # box.conf contains confidence score
87
  # box.cls contains class index
88
+ x_center, y_center, width, height = [round(float(coord)) for coord in box.xywh[0].tolist()] # Changed to xywh
89
  confidence = round(float(box.conf[0]), 4)
90
  class_id = int(box.cls[0])
91
  class_name = model.names[class_id] if model.names else str(class_id) # Get class name if available
 
104
  return result_image_np, raw_data # Return both the image and raw data
105
 
106
  except Exception as e:
107
+ print(f"Error during YOLO object detection: {e}")
108
  return None, None # Return None for both outputs