diff --git a/MANIFEST.in b/MANIFEST.in index 9209ed065..4bfa6f178 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -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 diff --git a/labelbox/__init__.py b/labelbox/__init__.py index c201ceba4..704a5efcc 100644 --- a/labelbox/__init__.py +++ b/labelbox/__init__.py @@ -1,3 +1,3 @@ "The Labelbox python package." -__version__ = '0.0.2' +__version__ = '0.0.3' diff --git a/labelbox/predictions/__init__.py b/labelbox/predictions/__init__.py index 409b914dd..a3651a150 100644 --- a/labelbox/predictions/__init__.py +++ b/labelbox/predictions/__init__.py @@ -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`, @@ -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` @@ -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: diff --git a/tests/test_predictions.py b/tests/test_predictions.py index 415577627..0e21d8b20 100644 --- a/tests/test_predictions.py +++ b/tests/test_predictions.py @@ -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 diff --git a/tests/test_predictions/dog_prediction.png b/tests/test_predictions/dog_prediction.png new file mode 100644 index 000000000..aeda58e1c Binary files /dev/null and b/tests/test_predictions/dog_prediction.png differ