Skip to content

Commit e5f7dc2

Browse files
authored
Merge pull request #3 from Labelbox/ENG-480
[ENG-480] Simplify path by default, change API to num points
2 parents a67791d + b085640 commit e5f7dc2

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ recursive-include docs *.rst
1313
recursive-include labelbox *.py
1414
include labelbox/exporters/pascal_voc_writer/templates/annotation.xml
1515
recursive-include tests *.json
16+
recursive-include tests *.png
1617
recursive-include tests *.py

labelbox/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"The Labelbox python package."
22

3-
__version__ = '0.0.2'
3+
__version__ = '0.0.3'

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: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,24 @@ 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+
62+
def test_vectorize_simplify_defaults(datadir):
63+
with open(datadir.join('dog_prediction.png'), 'rb') as fp:
64+
im = np.array(Image.open(fp))
65+
legend = {
66+
65535: 'Dog',
67+
}
68+
label = lbpreds.vectorize_to_v4_label(im, legend, max_num_points=None)
69+
70+
# a huge label with no simplification
71+
assert len(label['Dog'][0]['geometry']) > 5000
72+
73+
# by default, simplifies to <= 50 points
74+
label_simple = lbpreds.vectorize_to_v4_label(im, legend)
75+
assert len(label_simple['Dog'][0]['geometry']) <= 50
47.9 KB
Loading

0 commit comments

Comments
 (0)