Update handler.py
Browse files- handler.py +24 -26
handler.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
from ultralyticsplus import YOLO
|
2 |
from typing import List, Dict, Any
|
3 |
from sahi import ObjectPrediction
|
4 |
-
import torch, torchvision
|
5 |
|
6 |
|
7 |
DEFAULT_CONFIG = {'conf': 0.25, 'iou': 0.45, 'agnostic_nms': False, 'max_det': 1000}
|
@@ -22,32 +21,31 @@ class EndpointHandler():
|
|
22 |
Return:
|
23 |
object_predictions
|
24 |
"""
|
25 |
-
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
return torch.__version__, torchvision.__version__
|
53 |
|
|
|
1 |
from ultralyticsplus import YOLO
|
2 |
from typing import List, Dict, Any
|
3 |
from sahi import ObjectPrediction
|
|
|
4 |
|
5 |
|
6 |
DEFAULT_CONFIG = {'conf': 0.25, 'iou': 0.45, 'agnostic_nms': False, 'max_det': 1000}
|
|
|
21 |
Return:
|
22 |
object_predictions
|
23 |
"""
|
24 |
+
config = DEFAULT_CONFIG
|
25 |
+
# Set model parameters
|
26 |
+
self.model.overrides['conf'] = config.get('conf')
|
27 |
+
self.model.overrides['iou'] = config.get('iou')
|
28 |
+
self.model.overrides['agnostic_nms'] = config.get('agnostic_nms')
|
29 |
+
self.model.overrides['max_det'] = config.get('max_det')
|
30 |
|
31 |
+
# perform inference
|
32 |
+
inputs = data.pop("inputs", data)
|
33 |
+
result = self.model.predict(inputs['image'])[0]
|
34 |
|
35 |
+
names = self.model.model.names
|
36 |
+
boxes = result.boxes
|
37 |
|
38 |
+
object_predictions = []
|
39 |
+
if boxes is not None:
|
40 |
+
det_ind = 0
|
41 |
+
for xyxy, conf, cls in zip(boxes.xyxy, boxes.conf, boxes.cls):
|
42 |
+
object_prediction = ObjectPrediction(
|
43 |
+
bbox=xyxy.tolist(),
|
44 |
+
category_name=names[int(cls)],
|
45 |
+
category_id=int(cls),
|
46 |
+
score=conf,
|
47 |
+
)
|
48 |
+
object_predictions.append(object_prediction)
|
49 |
+
det_ind += 1
|
50 |
+
return object_predictions
|
|
|
51 |
|