Spaces:
Sleeping
Sleeping
Update tasks/image.py
Browse files- tasks/image.py +21 -11
tasks/image.py
CHANGED
@@ -67,6 +67,16 @@ def compute_max_iou(true_boxes, pred_box):
|
|
67 |
max_iou = max(max_iou, iou)
|
68 |
return max_iou
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
@router.post(ROUTE, tags=["Image Task"],
|
71 |
description=DESCRIPTION)
|
72 |
async def evaluate_image(request: ImageEvaluationRequest):
|
@@ -100,6 +110,11 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
100 |
# Update the code below to replace the random baseline with your model inference
|
101 |
#--------------------------------------------------------------------------------------------
|
102 |
|
|
|
|
|
|
|
|
|
|
|
103 |
predictions = []
|
104 |
true_labels = []
|
105 |
pred_boxes = []
|
@@ -111,25 +126,20 @@ async def evaluate_image(request: ImageEvaluationRequest):
|
|
111 |
has_smoke = len(annotation) > 0
|
112 |
true_labels.append(int(has_smoke))
|
113 |
|
114 |
-
|
115 |
-
pred_has_smoke =
|
116 |
predictions.append(int(pred_has_smoke))
|
117 |
|
118 |
# If there's a true box, parse it and make random box prediction
|
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 |
-
random.random(), # x_center
|
128 |
-
random.random(), # y_center
|
129 |
-
random.random() * 0.5, # width (max 0.5)
|
130 |
-
random.random() * 0.5 # height (max 0.5)
|
131 |
-
]
|
132 |
-
pred_boxes.append(random_box)
|
133 |
|
134 |
#--------------------------------------------------------------------------------------------
|
135 |
# YOUR MODEL INFERENCE STOPS HERE
|
|
|
67 |
max_iou = max(max_iou, iou)
|
68 |
return max_iou
|
69 |
|
70 |
+
def load_model(path_to_model, model_type="YOLO"):
|
71 |
+
if model_type == "YOLO":
|
72 |
+
model = YOLO(path_to_model)
|
73 |
+
else:
|
74 |
+
raise NotImplementedError
|
75 |
+
return model
|
76 |
+
|
77 |
+
def get_boxes_list(predictions):
|
78 |
+
return [box.tolist() for box in predictions.boxes.xywhn]
|
79 |
+
|
80 |
@router.post(ROUTE, tags=["Image Task"],
|
81 |
description=DESCRIPTION)
|
82 |
async def evaluate_image(request: ImageEvaluationRequest):
|
|
|
110 |
# Update the code below to replace the random baseline with your model inference
|
111 |
#--------------------------------------------------------------------------------------------
|
112 |
|
113 |
+
|
114 |
+
PATH_TO_MODEL = "models/best_YOLOv8n.pt"
|
115 |
+
model = load_model(PATH_TO_MODEL)
|
116 |
+
|
117 |
+
print(f"Model info: {model.info()}")
|
118 |
predictions = []
|
119 |
true_labels = []
|
120 |
pred_boxes = []
|
|
|
126 |
has_smoke = len(annotation) > 0
|
127 |
true_labels.append(int(has_smoke))
|
128 |
|
129 |
+
model_preds = model(example['image'])[0]
|
130 |
+
pred_has_smoke = len(model_preds) > 0
|
131 |
predictions.append(int(pred_has_smoke))
|
132 |
|
133 |
# If there's a true box, parse it and make random box prediction
|
134 |
if has_smoke:
|
135 |
+
|
136 |
# Parse all true boxes from the annotation
|
137 |
image_true_boxes = parse_boxes(annotation)
|
138 |
true_boxes_list.append(image_true_boxes)
|
139 |
|
140 |
+
pred_box_list = get_boxes_list(model_preds)[0] # With one bbox to start with (as in the random baseline)
|
141 |
+
pred_boxes.append(pred_box_list)
|
142 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
#--------------------------------------------------------------------------------------------
|
145 |
# YOUR MODEL INFERENCE STOPS HERE
|