Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ recursive-include docs *.rst
recursive-include labelbox *.py
include labelbox/exporters/pascal_voc_writer/templates/annotation.xml
recursive-include tests *.json
recursive-include tests *.png
recursive-include tests *.py
2 changes: 1 addition & 1 deletion labelbox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"The Labelbox python package."

__version__ = '0.0.2'
__version__ = '0.0.3'
14 changes: 8 additions & 6 deletions labelbox/predictions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def vectorize_to_v4_label(
segmentation_map,
legend: Dict[int, str],
epsilon: Optional[float] = None) -> DefaultDict[str, List[dict]]:
max_num_points: Optional[int] = 50) -> DefaultDict[str, List[dict]]:
"""Converts a segmentation map into polygons.

Given a raster pixel wise array of predictions in `segmentation_map`,
Expand All @@ -25,10 +25,8 @@ def vectorize_to_v4_label(
legend: A dictonary mapping pixel values
used in `segmentation_map` to semantic
class names.
epsilon: An optional argument, if present
controls the amount of path simplification.
Start with 1.0 and decrease to 0.01, 0.001.
No simplification occurs if absent.
max_num_points: The maximum number of points in the simplified path.
If `None`, then no path simplification is performed.

Returns:
A dictionary suitable for use as a `prediction`
Expand All @@ -43,8 +41,12 @@ class names.
if pixel_value in legend and pixel_value is not 0:
xy_list = polygon['coordinates'][0]

if epsilon:
if max_num_points:
epsilon = 0.001
xy_list = simplify_coords(xy_list, epsilon)
while len(xy_list) > max_num_points:
epsilon *= 2
xy_list = simplify_coords(xy_list, epsilon)

geometry = []
for point in xy_list:
Expand Down
20 changes: 18 additions & 2 deletions tests/test_predictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,24 @@ def test_vectorize_simplify():
}
segmentation_map = np.asarray(segmentation_map)

label = lbpreds.vectorize_to_v4_label(segmentation_map, legend, epsilon=None)
label_simple = lbpreds.vectorize_to_v4_label(segmentation_map, legend, epsilon=1.0)
label = lbpreds.vectorize_to_v4_label(segmentation_map, legend, max_num_points=None)
label_simple = lbpreds.vectorize_to_v4_label(segmentation_map, legend, max_num_points=10)

# simplification reduces the number of points
assert len(label['CLASS'][0]['geometry']) > len(label_simple['CLASS'][0]['geometry'])


def test_vectorize_simplify_defaults(datadir):
with open(datadir.join('dog_prediction.png'), 'rb') as fp:
im = np.array(Image.open(fp))
legend = {
65535: 'Dog',
}
label = lbpreds.vectorize_to_v4_label(im, legend, max_num_points=None)

# a huge label with no simplification
assert len(label['Dog'][0]['geometry']) > 5000

# by default, simplifies to <= 50 points
label_simple = lbpreds.vectorize_to_v4_label(im, legend)
assert len(label_simple['Dog'][0]['geometry']) <= 50

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test. 👏

Binary file added tests/test_predictions/dog_prediction.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.