Skip to content

Commit 17f364d

Browse files
committed
Exclude nested contours from prob computation
1 parent 7d03560 commit 17f364d

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

src/cpp/models/src/segmentation_model.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,17 +308,19 @@ std::vector<Contour> SegmentationModel::getContours(const ImageResultWithSoftPre
308308
continue;
309309
}
310310

311+
cv::Mat mask = cv::Mat::zeros(imageResult.resultImage.rows,
312+
imageResult.resultImage.cols,
313+
imageResult.resultImage.type());
314+
cv::drawContours(mask, contours, i, 255, -1);
315+
311316
std::vector<std::vector<cv::Point>> children;
312317
int next_child_idx = hierarchy[i][2];
313318
while (next_child_idx >= 0) {
314319
children.push_back(contours[next_child_idx]);
320+
cv::drawContours(mask, contours, next_child_idx, 0, -1);
315321
next_child_idx = hierarchy[next_child_idx][0];
316322
}
317323

318-
cv::Mat mask = cv::Mat::zeros(imageResult.resultImage.rows,
319-
imageResult.resultImage.cols,
320-
imageResult.resultImage.type());
321-
cv::drawContours(mask, contours, i, 255, -1);
322324
float probability = (float)cv::mean(current_label_soft_prediction, mask)[0];
323325
combined_contours.push_back({label, probability, contours[i], children});
324326
}

src/python/model_api/models/result/segmentation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ def rotated_rects(self, value):
149149

150150

151151
class Contour:
152+
"""Represents a contour object with label, probability, shape, and optional excluded shapes.
153+
Args:
154+
label (str): The label of the contour.
155+
probability (float): The probability associated with the contour.
156+
shape (np.ndarray | list[tuple[int, int]]): The shape of the contour.
157+
child_shapes (list[np.ndarray] | list[tuple[int, int]] | None, optional): Shapes of excluded contours. Defaults to None.
158+
"""
159+
152160
def __init__(
153161
self,
154162
label: str,

src/python/model_api/models/segmentation.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,6 @@ def get_contours(
219219
if hierarchy[i][3] >= 0:
220220
continue
221221

222-
children = []
223-
next_child_idx = hierarchy[i][2]
224-
while next_child_idx >= 0:
225-
children.append(contours[next_child_idx])
226-
next_child_idx = hierarchy[next_child_idx][0]
227-
228222
mask = np.zeros(prediction.resultImage.shape, dtype=np.uint8)
229223
cv2.drawContours(
230224
mask,
@@ -233,14 +227,20 @@ def get_contours(
233227
color=1,
234228
thickness=-1,
235229
)
236-
for c in children:
230+
231+
children = []
232+
next_child_idx = hierarchy[i][2]
233+
while next_child_idx >= 0:
234+
children.append(contours[next_child_idx])
237235
cv2.drawContours(
238236
mask,
239-
np.asarray([c]),
237+
np.asarray([contours[next_child_idx]]),
240238
contourIdx=-1,
241239
color=0,
242240
thickness=-1,
243241
)
242+
next_child_idx = hierarchy[next_child_idx][0]
243+
244244
probability = cv2.mean(current_label_soft_prediction, mask)[0]
245245
combined_contours.append(Contour(label, probability, contour, children))
246246

tests/python/accuracy/public_scope.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{
77
"image": "coco128/images/train2017/000000000074.jpg",
88
"reference": [
9-
"0: 0.537, 1: 0.463, [426,640,2], [0], [0]; object: 0.675, 508, 0, object: 0.527, 65, 1, object: 0.507, 18, 0, object: 0.624, 144, 0, object: 0.538, 67, 0, object: 0.507, 15, 0, object: 0.518, 41, 0, object: 0.507, 8, 0, object: 0.505, 14, 0, object: 0.885, 2138, 2, "
9+
"0: 0.537, 1: 0.463, [426,640,2], [0], [0]; object: 0.675, 508, 0, object: 0.530, 65, 1, object: 0.507, 18, 0, object: 0.624, 144, 0, object: 0.538, 67, 0, object: 0.507, 15, 0, object: 0.518, 41, 0, object: 0.507, 8, 0, object: 0.505, 14, 0, object: 0.885, 2138, 2, "
1010
]
1111
}
1212
]
@@ -55,7 +55,7 @@
5555
{
5656
"image": "coco128/images/train2017/000000000074.jpg",
5757
"reference": [
58-
"0: 0.561, 1: 0.439, [426,640,2], [0], [0]; object: 0.519, 26, 0, object: 0.531, 42, 0, object: 0.502, 21, 0, object: 0.505, 9, 0, object: 0.501, 4, 0, object: 0.509, 22, 0, object: 0.524, 85, 0, object: 0.520, 93, 0, object: 0.754, 2564, 3, "
58+
"0: 0.561, 1: 0.439, [426,640,2], [0], [0]; object: 0.519, 26, 0, object: 0.531, 42, 0, object: 0.502, 21, 0, object: 0.505, 9, 0, object: 0.501, 4, 0, object: 0.509, 22, 0, object: 0.524, 85, 0, object: 0.520, 93, 0, object: 0.757, 2564, 3, "
5959
]
6060
}
6161
]
@@ -67,7 +67,7 @@
6767
{
6868
"image": "coco128/images/train2017/000000000074.jpg",
6969
"reference": [
70-
"0: 0.944, 1: 0.056, [426,640,2], [0], [0]; object: 0.505, 2, 0, object: 0.518, 8, 0, object: 0.512, 5, 0, object: 0.506, 4, 0, object: 0.526, 8, 0, object: 0.529, 21, 0, object: 0.513, 12, 0, object: 0.536, 49, 0, object: 0.505, 2, 0, object: 0.512, 4, 0, object: 0.547, 6, 0, object: 0.511, 6, 0, object: 0.503, 1, 0, object: 0.539, 6, 0, object: 0.543, 39, 0, object: 0.529, 2, 0, object: 0.516, 9, 0, object: 0.565, 157, 4, object: 0.524, 6, 0, object: 0.528, 15, 0, object: 0.521, 18, 0, object: 0.503, 1, 0, object: 0.537, 73, 0, object: 0.513, 4, 0, object: 0.524, 27, 0, object: 0.513, 6, 0, object: 0.538, 65, 1, object: 0.501, 6, 0, object: 0.504, 1, 0, object: 0.507, 4, 0, object: 0.502, 1, 0, object: 0.518, 8, 0, object: 0.530, 11, 0, object: 0.502, 2, 0, object: 0.516, 2, 0, object: 0.506, 1, 0, object: 0.567, 17, 0, object: 0.502, 1, 0, object: 0.512, 7, 0, object: 0.538, 24, 0, object: 0.507, 1, 0, object: 0.534, 12, 0, object: 0.510, 5, 0, object: 0.537, 6, 0, object: 0.519, 13, 0, object: 0.505, 2, 0, object: 0.540, 6, 0, object: 0.517, 16, 0, object: 0.505, 5, 0, object: 0.506, 20, 0, object: 0.508, 6, 0, object: 0.519, 24, 0, object: 0.507, 4, 0, object: 0.506, 2, 0, object: 0.511, 4, 0, object: 0.556, 47, 0, object: 0.510, 10, 0, object: 0.500, 1, 0, object: 0.504, 5, 0, object: 0.501, 1, 0, object: 0.510, 6, 0, object: 0.549, 13, 0, object: 0.509, 2, 0, object: 0.510, 3, 0, object: 0.514, 1, 0, object: 0.529, 15, 0, object: 0.551, 110, 1, object: 0.504, 2, 0, object: 0.503, 3, 0, object: 0.518, 16, 0, object: 0.511, 14, 0, object: 0.502, 1, 0, object: 0.523, 1, 0, object: 0.533, 16, 0, object: 0.568, 65, 0, object: 0.582, 1793, 73, "
70+
"0: 0.944, 1: 0.056, [426,640,2], [0], [0]; object: 0.505, 2, 0, object: 0.518, 8, 0, object: 0.512, 5, 0, object: 0.506, 4, 0, object: 0.526, 8, 0, object: 0.529, 21, 0, object: 0.513, 12, 0, object: 0.536, 49, 0, object: 0.505, 2, 0, object: 0.512, 4, 0, object: 0.547, 6, 0, object: 0.511, 6, 0, object: 0.503, 1, 0, object: 0.539, 6, 0, object: 0.543, 39, 0, object: 0.529, 2, 0, object: 0.516, 9, 0, object: 0.571, 157, 4, object: 0.524, 6, 0, object: 0.528, 15, 0, object: 0.521, 18, 0, object: 0.503, 1, 0, object: 0.537, 73, 0, object: 0.513, 4, 0, object: 0.524, 27, 0, object: 0.513, 6, 0, object: 0.539, 65, 1, object: 0.501, 6, 0, object: 0.504, 1, 0, object: 0.507, 4, 0, object: 0.502, 1, 0, object: 0.518, 8, 0, object: 0.530, 11, 0, object: 0.502, 2, 0, object: 0.516, 2, 0, object: 0.506, 1, 0, object: 0.567, 17, 0, object: 0.502, 1, 0, object: 0.512, 7, 0, object: 0.538, 24, 0, object: 0.507, 1, 0, object: 0.534, 12, 0, object: 0.510, 5, 0, object: 0.537, 6, 0, object: 0.519, 13, 0, object: 0.505, 2, 0, object: 0.540, 6, 0, object: 0.517, 16, 0, object: 0.505, 5, 0, object: 0.506, 20, 0, object: 0.508, 6, 0, object: 0.519, 24, 0, object: 0.507, 4, 0, object: 0.506, 2, 0, object: 0.511, 4, 0, object: 0.556, 47, 0, object: 0.510, 10, 0, object: 0.500, 1, 0, object: 0.504, 5, 0, object: 0.501, 1, 0, object: 0.510, 6, 0, object: 0.549, 13, 0, object: 0.509, 2, 0, object: 0.510, 3, 0, object: 0.514, 1, 0, object: 0.529, 15, 0, object: 0.554, 110, 1, object: 0.504, 2, 0, object: 0.503, 3, 0, object: 0.518, 16, 0, object: 0.511, 14, 0, object: 0.502, 1, 0, object: 0.523, 1, 0, object: 0.533, 16, 0, object: 0.568, 65, 0, object: 0.603, 1793, 73, "
7171
]
7272
}
7373
]
@@ -427,7 +427,7 @@
427427
{
428428
"image": "coco128/images/train2017/000000000074.jpg",
429429
"reference": [
430-
"0: 0.272, 1: 0.728, [3500,3500,5], [0], [0]; background: 1.404, 311, 0, background: 1.397, 44, 0, background: 1.371, 34, 0, background: 1.377, 12, 0, background: 1.356, 155, 0, background: 1.345, 12, 0, background: 1.183, 219, 0, background: 1.524, 8, 0, background: 1.533, 4, 0, background: 1.519, 2, 0, background: 1.524, 4, 0, background: 1.530, 6, 0, background: 1.537, 2, 0, background: 1.514, 4, 0, background: 1.519, 8, 0, background: 1.529, 6, 0, background: 1.550, 6, 0, background: 1.558, 4, 0, background: 1.520, 2, 0, background: 1.529, 4, 0, background: 1.532, 6, 0, background: 1.535, 6, 0, background: 1.530, 2, 0, background: 1.529, 50, 0, background: 1.528, 22, 0, background: 1.527, 38, 0, background: 1.451, 1476, 0, background: 1.345, 2743, 9, background: 1.609, 2987, 1, background: 1.398, 20, 0, background: 1.636, 29909, 8, "
430+
"0: 0.272, 1: 0.728, [3500,3500,5], [0], [0]; background: 1.404, 311, 0, background: 1.397, 44, 0, background: 1.371, 34, 0, background: 1.377, 12, 0, background: 1.356, 155, 0, background: 1.345, 12, 0, background: 1.183, 219, 0, background: 1.524, 8, 0, background: 1.533, 4, 0, background: 1.519, 2, 0, background: 1.524, 4, 0, background: 1.530, 6, 0, background: 1.537, 2, 0, background: 1.514, 4, 0, background: 1.519, 8, 0, background: 1.529, 6, 0, background: 1.550, 6, 0, background: 1.558, 4, 0, background: 1.520, 2, 0, background: 1.529, 4, 0, background: 1.532, 6, 0, background: 1.535, 6, 0, background: 1.530, 2, 0, background: 1.529, 50, 0, background: 1.528, 22, 0, background: 1.527, 38, 0, background: 1.451, 1476, 0, background: 1.344, 2743, 9, background: 1.609, 2987, 1, background: 1.398, 20, 0, background: 1.644, 29909, 8, "
431431
]
432432
}
433433
]

0 commit comments

Comments
 (0)