Skip to content

Commit fc6b762

Browse files
committed
Simplify path by default, change API to num points
1 parent a67791d commit fc6b762

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

labelbox/predictions/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
def vectorize_to_v4_label(
1010
segmentation_map,
1111
legend: Dict[int, str],
12-
epsilon: Optional[float] = None) -> DefaultDict[str, List[dict]]:
12+
max_num_points: Optional[int] = 50) -> DefaultDict[str, List[dict]]:
1313
"""Converts a segmentation map into polygons.
1414
1515
Given a raster pixel wise array of predictions in `segmentation_map`,
@@ -25,10 +25,8 @@ def vectorize_to_v4_label(
2525
legend: A dictonary mapping pixel values
2626
used in `segmentation_map` to semantic
2727
class names.
28-
epsilon: An optional argument, if present
29-
controls the amount of path simplification.
30-
Start with 1.0 and decrease to 0.01, 0.001.
31-
No simplification occurs if absent.
28+
max_num_points: The maximum number of points in the simplified path.
29+
If `None`, then no path simplification is performed.
3230
3331
Returns:
3432
A dictionary suitable for use as a `prediction`
@@ -43,8 +41,12 @@ class names.
4341
if pixel_value in legend and pixel_value is not 0:
4442
xy_list = polygon['coordinates'][0]
4543

46-
if epsilon:
44+
if max_num_points:
45+
epsilon = 0.001
4746
xy_list = simplify_coords(xy_list, epsilon)
47+
while len(xy_list) > max_num_points:
48+
epsilon *= 2
49+
xy_list = simplify_coords(xy_list, epsilon)
4850

4951
geometry = []
5052
for point in xy_list:

tests/test_predictions.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,23 @@ def test_vectorize_simplify():
5252
}
5353
segmentation_map = np.asarray(segmentation_map)
5454

55-
label = lbpreds.vectorize_to_v4_label(segmentation_map, legend, epsilon=None)
56-
label_simple = lbpreds.vectorize_to_v4_label(segmentation_map, legend, epsilon=1.0)
55+
label = lbpreds.vectorize_to_v4_label(segmentation_map, legend, max_num_points=None)
56+
label_simple = lbpreds.vectorize_to_v4_label(segmentation_map, legend, max_num_points=10)
5757

5858
# simplification reduces the number of points
5959
assert len(label['CLASS'][0]['geometry']) > len(label_simple['CLASS'][0]['geometry'])
60+
61+
def test_vectorize_simplify_defaults(datadir):
62+
with open(datadir.join('dog_prediction.png'), 'rb') as fp:
63+
im = np.array(Image.open(fp))
64+
legend = {
65+
65535: 'Dog',
66+
}
67+
label = lbpreds.vectorize_to_v4_label(im, legend, max_num_points=None)
68+
69+
# a huge label with no simplification
70+
assert len(label['Dog'][0]['geometry']) > 5000
71+
72+
# by default, simplifies to <= 50 points
73+
label_simple = lbpreds.vectorize_to_v4_label(im, legend)
74+
assert len(label_simple['Dog'][0]['geometry']) <= 50
47.9 KB
Loading

0 commit comments

Comments
 (0)