Skip to content

Commit a42a877

Browse files
committed
Revisit detections filtering
1 parent c4e2de9 commit a42a877

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

model_api/python/model_api/models/detection_model.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,54 @@ def _resize_detections(self, detections, meta):
105105
pad_top = (self.h - round(input_img_height / inverted_scale_y)) // 2
106106

107107
for detection in detections:
108-
detection.xmin = min(
109-
max(round((detection.xmin * self.w - pad_left) * inverted_scale_x), 0),
110-
input_img_widht,
108+
detection.xmin = round(
109+
min(
110+
max((detection.xmin * self.w - pad_left) * inverted_scale_x, 0),
111+
input_img_widht,
112+
)
111113
)
112-
detection.ymin = min(
113-
max(round((detection.ymin * self.h - pad_top) * inverted_scale_y), 0),
114-
input_img_height,
114+
detection.ymin = round(
115+
min(
116+
max((detection.ymin * self.h - pad_top) * inverted_scale_y, 0),
117+
input_img_height,
118+
)
115119
)
116-
detection.xmax = min(
117-
max(round((detection.xmax * self.w - pad_left) * inverted_scale_x), 0),
118-
input_img_widht,
120+
detection.xmax = round(
121+
min(
122+
max((detection.xmax * self.w - pad_left) * inverted_scale_x, 0),
123+
input_img_widht,
124+
)
119125
)
120-
detection.ymax = min(
121-
max(round((detection.ymax * self.h - pad_top) * inverted_scale_y), 0),
122-
input_img_height,
126+
detection.ymax = round(
127+
min(
128+
max((detection.ymax * self.h - pad_top) * inverted_scale_y, 0),
129+
input_img_height,
130+
)
123131
)
124132
return detections
125133

134+
def _filter_detections(self, detections, box_area_threshold=0):
135+
"""Filters detections by confidence threshold and box size threshold
136+
137+
Args:
138+
detections (List[Detection]): list of detections with coordinates in normalized form
139+
box_area_threshold (float): minimal area of the bounding to be considered
140+
141+
Returns:
142+
- list of detections with confidence above the threshold
143+
"""
144+
filtered_detections = []
145+
for detection in detections:
146+
if (
147+
detection.score < self.confidence_threshold
148+
or (detection.xmax - detection.xmin) * (detection.ymax - detection.ymin)
149+
< box_area_threshold
150+
):
151+
continue
152+
filtered_detections.append(detection)
153+
154+
return filtered_detections
155+
126156
def _add_label_names(self, detections):
127157
"""Adds labels names to detections if they are available
128158

model_api/python/model_api/models/ssd.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def preprocess(self, inputs):
4141
def postprocess(self, outputs, meta):
4242
detections = self._parse_outputs(outputs)
4343
detections = self._resize_detections(detections, meta)
44+
detections = self._filter_detections(detections, 1)
4445
detections = self._add_label_names(detections)
4546
return DetectionResult(
4647
detections,

0 commit comments

Comments
 (0)