Update tasks/image.py
Browse files- tasks/image.py +29 -16
tasks/image.py
CHANGED
|
@@ -99,37 +99,50 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
| 99 |
# YOUR MODEL INFERENCE CODE HERE
|
| 100 |
# Update the code below to replace the random baseline with your model inference
|
| 101 |
#--------------------------------------------------------------------------------------------
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
predictions = []
|
| 104 |
true_labels = []
|
| 105 |
pred_boxes = []
|
| 106 |
true_boxes_list = [] # List of lists, each inner list contains boxes for one image
|
| 107 |
|
|
|
|
|
|
|
| 108 |
for example in test_dataset:
|
| 109 |
# Parse true annotation (YOLO format: class_id x_center y_center width height)
|
| 110 |
annotation = example.get("annotations", "").strip()
|
| 111 |
has_smoke = len(annotation) > 0
|
| 112 |
true_labels.append(int(has_smoke))
|
| 113 |
-
|
| 114 |
-
#
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
predictions.append(int(pred_has_smoke))
|
| 117 |
-
|
| 118 |
-
#
|
| 119 |
if has_smoke:
|
| 120 |
# Parse all true boxes from the annotation
|
| 121 |
image_true_boxes = parse_boxes(annotation)
|
| 122 |
true_boxes_list.append(image_true_boxes)
|
| 123 |
-
|
| 124 |
-
#
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
| 133 |
|
| 134 |
#--------------------------------------------------------------------------------------------
|
| 135 |
# YOUR MODEL INFERENCE STOPS HERE
|
|
|
|
| 99 |
# YOUR MODEL INFERENCE CODE HERE
|
| 100 |
# Update the code below to replace the random baseline with your model inference
|
| 101 |
#--------------------------------------------------------------------------------------------
|
| 102 |
+
from ultralytics import YOLO
|
| 103 |
+
|
| 104 |
+
# Load the trained YOLOv8 model
|
| 105 |
+
yolov8_model = YOLO("./../yolov8n.pt")
|
| 106 |
+
|
| 107 |
predictions = []
|
| 108 |
true_labels = []
|
| 109 |
pred_boxes = []
|
| 110 |
true_boxes_list = [] # List of lists, each inner list contains boxes for one image
|
| 111 |
|
| 112 |
+
|
| 113 |
+
# Inference loop
|
| 114 |
for example in test_dataset:
|
| 115 |
# Parse true annotation (YOLO format: class_id x_center y_center width height)
|
| 116 |
annotation = example.get("annotations", "").strip()
|
| 117 |
has_smoke = len(annotation) > 0
|
| 118 |
true_labels.append(int(has_smoke))
|
| 119 |
+
|
| 120 |
+
# Load the image
|
| 121 |
+
image_path = example.get("image_path") # Assuming access to image file path
|
| 122 |
+
|
| 123 |
+
# Perform YOLOv8 inference
|
| 124 |
+
results = yolov8_model.predict(image_path)
|
| 125 |
+
|
| 126 |
+
# Classification: Check if the model predicted any boxes (smoke presence)
|
| 127 |
+
pred_has_smoke = len(results[0].boxes) > 0
|
| 128 |
predictions.append(int(pred_has_smoke))
|
| 129 |
+
|
| 130 |
+
# Parse YOLO format true boxes
|
| 131 |
if has_smoke:
|
| 132 |
# Parse all true boxes from the annotation
|
| 133 |
image_true_boxes = parse_boxes(annotation)
|
| 134 |
true_boxes_list.append(image_true_boxes)
|
| 135 |
+
|
| 136 |
+
# Get predicted bounding boxes (YOLO format)
|
| 137 |
+
image_pred_boxes = []
|
| 138 |
+
for box in results[0].boxes:
|
| 139 |
+
x_center, y_center, width, height = box.xywh[0].tolist()
|
| 140 |
+
image_pred_boxes.append([x_center, y_center, width, height])
|
| 141 |
+
|
| 142 |
+
pred_boxes.append(image_pred_boxes)
|
| 143 |
+
else:
|
| 144 |
+
true_boxes_list.append([]) # No true boxes for this image
|
| 145 |
+
pred_boxes.append([]) # No predicted boxes for this image
|
| 146 |
|
| 147 |
#--------------------------------------------------------------------------------------------
|
| 148 |
# YOUR MODEL INFERENCE STOPS HERE
|