Skip to content

Commit 8d14334

Browse files
committed
Merge branch 'master' of ssh://github.com/Labelbox/labelbox-python
* 'master' of ssh://github.com/Labelbox/labelbox-python: Fix ENG-495 and ENG-549 Failing tests for ENG-549 and ENG-495
2 parents 0fb3a04 + f7ae35d commit 8d14334

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

labelbox/exporters/coco_exporter.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
from labelbox.exceptions import UnknownFormatError
1616

1717

18+
LOGGER = logging.getLogger(__name__)
19+
20+
1821
def from_json(labeled_data, coco_output, label_format='WKT'):
1922
"Writes labelbox JSON export into MS COCO format."
2023
# read labelbox JSON output
@@ -29,10 +32,10 @@ def from_json(labeled_data, coco_output, label_format='WKT'):
2932
try:
3033
add_label(coco, data['ID'], data['Labeled Data'], data['Label'], label_format)
3134
except requests.exceptions.MissingSchema as exc:
32-
logging.exception(exc)
35+
LOGGER.warning(exc)
3336
continue
3437
except requests.exceptions.ConnectionError:
35-
logging.exception('Failed to fetch image from %s', data['Labeled Data'])
38+
LOGGER.warning('Failed to fetch image from %s, skipping', data['Labeled Data'])
3639
continue
3740

3841
with open(coco_output, 'w+') as file_handle:
@@ -155,18 +158,20 @@ def _get_polygons(label_format, label_data):
155158
xy_list = xy_list['geometry']
156159

157160
# V2 and V3
158-
assert isinstance(xy_list, list), \
159-
'Expected list in "geometry" key but got {}'.format(xy_list)
161+
if not isinstance(xy_list, list):
162+
LOGGER.warning('Could not get an point list to construct polygon, skipping')
163+
continue
160164
else: # V2, or non-list
161165
if not isinstance(xy_list, list) or not xy_list or 'x' not in xy_list[0]:
162166
# skip non xy lists
167+
LOGGER.warning('Could not get an point list to construct polygon, skipping')
163168
continue
164169

165170
if len(xy_list) > 2: # need at least 3 points to make a polygon
166171
polygons.append(Polygon(map(lambda p: (p['x'], p['y']), xy_list)))
167172
else:
168173
exc = UnknownFormatError(label_format=label_format)
169-
logging.exception(exc.message)
174+
LOGGER.exception(exc.message)
170175
raise exc
171176

172177
return polygons

labelbox/exporters/voc_exporter.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
from labelbox.exporters.pascal_voc_writer import Writer as PascalWriter
1616

1717

18+
LOGGER = logging.getLogger(__name__)
19+
20+
1821
def from_json(labeled_data, annotations_output_dir, images_output_dir,
1922
label_format='WKT'):
2023
"""Convert Labelbox JSON export to Pascal VOC format.
@@ -26,18 +29,15 @@ def from_json(labeled_data, annotations_output_dir, images_output_dir,
2629
images_output_dir (str): File path of directory to write images.
2730
label_format (str): Format of the labeled data.
2831
Valid options are: "WKT" and "XY", default is "WKT".
29-
30-
Todo:
31-
* Add functionality to allow use of local copy of an image instead of
32-
downloading it each time.
3332
"""
3433

3534
# make sure annotation output directory is valid
3635
try:
3736
annotations_output_dir = os.path.abspath(annotations_output_dir)
3837
assert os.path.isdir(annotations_output_dir)
39-
except AssertionError:
40-
logging.exception('Annotation output directory does not exist')
38+
except AssertionError as exc:
39+
LOGGER.exception('Annotation output directory does not exist, please create it first.')
40+
raise exc
4141

4242
# read labelbox JSON output
4343
with open(labeled_data, 'r') as file_handle:
@@ -55,10 +55,10 @@ def from_json(labeled_data, annotations_output_dir, images_output_dir,
5555
annotations_output_dir)
5656

5757
except requests.exceptions.MissingSchema as exc:
58-
logging.exception(exc)
58+
LOGGER.warning(exc)
5959
continue
6060
except requests.exceptions.ConnectionError:
61-
logging.exception('Failed to fetch image from %s', data['Labeled Data'])
61+
LOGGER.warning('Failed to fetch image from %s, skipping', data['Labeled Data'])
6262
continue
6363

6464

@@ -141,7 +141,7 @@ def _add_pascal_object_from_xy(xml_writer, img_height, polygons, label):
141141
polygon = polygon['geometry']
142142
if not isinstance(polygon, list) \
143143
or not all(map(lambda p: 'x' in p and 'y' in p, polygon)):
144-
# couldn't make a list of points, give up
144+
LOGGER.warning('Could not get an point list to construct polygon, skipping')
145145
return xml_writer
146146

147147
xy_coords = []

labelbox/lbx.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import numpy as np
3434
from PIL import Image
3535

36+
LOGGER = logging.getLogger(__name__)
37+
3638
# NOTE: image-segmentation front-end requires background pixels to be white to
3739
# render them transparent
3840
BACKGROUND_RGBA = np.array([255, 255, 255, 255], dtype=np.uint8)
@@ -92,9 +94,9 @@ def _color_to_key(color):
9294
offset += 4
9395
count = 0
9496
except KeyError as exc:
95-
logging.error('Could not find color %s in colormap', pixel_words[i])
97+
LOGGER.error('Could not find color %s in colormap', pixel_words[i])
9698
if np.all(pixel_words[i] == np.array([0, 0, 0, 0])):
97-
logging.error('Did you remember to set background pixels to `BACKGROUND_RGBA`?')
99+
LOGGER.error('Did you remember to set background pixels to `BACKGROUND_RGBA`?')
98100
raise exc
99101

100102
# write header

tests/test_exporters.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def test_empty_skipped(self, tmpfile, datadir):
4141
labeled_data = datadir.join('empty_skipped.json')
4242
lb2co.from_json(labeled_data=labeled_data, coco_output=tmpfile)
4343

44+
def test_non_polygons(self, tmpfile, datadir):
45+
labeled_data = datadir.join('non_polygon.json')
46+
lb2co.from_json(labeled_data=labeled_data, coco_output=tmpfile, label_format='XY')
47+
4448

4549
class TestVocExporter(object):
4650
def test_wkt_1(self, tmpdir, datadir):
@@ -85,3 +89,7 @@ def test_bad_label_format(self, tmpdir, datadir):
8589
labeled_data = datadir.join('labelbox_xy_1.json')
8690
with pytest.raises(lb2pa.UnknownFormatError):
8791
lb2pa.from_json(labeled_data, tmpdir, tmpdir, label_format='INVALID')
92+
93+
def test_non_polygons(self, tmpdir, datadir):
94+
labeled_data = datadir.join('non_polygon.json')
95+
lb2pa.from_json(labeled_data, tmpdir, tmpdir, label_format='XY')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"ID":"cjlmqp9dnw28w0771zeasfqie","Labeled Data":"https://storage.googleapis.com/labelbox-example-datasets/tesla/2018-Tesla-Model-3-front-three-quarter-in-motion.jpg","Label":{"box":[{"geometry":{"x":1259,"y":1411}}],"select_the_cup_models":["model_3"]},"Created By":"feynman@labelbox.com","Project Name":"seg tesla v3","Created At":"2018-09-03T20:30:51.000Z","Seconds to Label":3.111,"External ID":null,"Agreement":null,"Dataset Name":"Example Tesla Dataset","Reviews":[],"View Label":"https://image-segmentation-v3.labelbox.com?project=cjl8ss8h83bep0715vux0nugp&label=cjlmqp9dnw28w0771zeasfqie"}]

0 commit comments

Comments
 (0)