Skip to content

Commit 807b757

Browse files
committed
Linter
1 parent 5067c60 commit 807b757

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

labelbox/exporters/coco_exporter.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import datetime as dt
66
import json
77
import logging
8-
from PIL import Image
98
import requests
109
from shapely import wkt
1110
from shapely.geometry import Polygon
1211
from typing import Any, Dict, Sequence
1312

1413
from labelbox.exceptions import UnknownFormatError
14+
from PIL import Image
1515

1616

1717
def from_json(labeled_data, coco_output, label_format='WKT'):
@@ -78,7 +78,8 @@ def add_label(
7878
label_id: ID for the instance to write
7979
image_url: URL to download image file from
8080
labels: Labelbox formatted labels to use for generating annotation
81-
label_format: Format of the labeled data. Valid options are: "WKT" and "XY", default is "WKT".
81+
label_format: Format of the labeled data. Valid options are: "WKT" and
82+
"XY", default is "WKT".
8283
8384
Returns:
8485
The updated COCO export represented as a dictionary.
@@ -118,25 +119,29 @@ def add_label(
118119
coco['categories'].append(category)
119120

120121
polygons = _get_polygons(label_format, label_data)
121-
122-
for polygon in polygons:
123-
segmentation = []
124-
for x_val, y_val in polygon.exterior.coords:
125-
segmentation.extend([x_val, image['height'] - y_val])
126-
127-
annotation = {
128-
"id": len(coco['annotations']) + 1,
129-
"image_id": image['id'],
130-
"category_id": category_id,
131-
"segmentation": [segmentation],
132-
"area": polygon.area, # float
133-
"bbox": [polygon.bounds[0], polygon.bounds[1],
134-
polygon.bounds[2] - polygon.bounds[0],
135-
polygon.bounds[3] - polygon.bounds[1]],
136-
"iscrowd": 0
137-
}
138-
139-
coco['annotations'].append(annotation)
122+
_append_polygons_as_annotations(coco, image, category_id, polygons)
123+
124+
125+
def _append_polygons_as_annotations(coco, image, category_id, polygons):
126+
"Adds `polygons` as annotations in the `coco` export"
127+
for polygon in polygons:
128+
segmentation = []
129+
for x_val, y_val in polygon.exterior.coords:
130+
segmentation.extend([x_val, image['height'] - y_val])
131+
132+
annotation = {
133+
"id": len(coco['annotations']) + 1,
134+
"image_id": image['id'],
135+
"category_id": category_id,
136+
"segmentation": [segmentation],
137+
"area": polygon.area, # float
138+
"bbox": [polygon.bounds[0], polygon.bounds[1],
139+
polygon.bounds[2] - polygon.bounds[0],
140+
polygon.bounds[3] - polygon.bounds[1]],
141+
"iscrowd": 0
142+
}
143+
144+
coco['annotations'].append(annotation)
140145

141146

142147
def _get_polygons(label_format, label_data):

labelbox/exporters/voc_exporter.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import json
66
import logging
77
import os
8-
from PIL import Image
98
import requests
109
from shapely import wkt
1110
from typing import Any, Sequence
1211

1312
from labelbox.exceptions import UnknownFormatError
1413
from labelbox.exporters.pascal_voc_writer import Writer as PascalWriter
14+
from PIL import Image
1515

1616

1717
def from_json(labeled_data, annotations_output_dir, images_output_dir,
@@ -70,9 +70,10 @@ def write_label(
7070
label_id: ID for the instance to write
7171
image_url: URL to download image file from
7272
labels: Labelbox formatted labels to use for generating annotation
73-
label_format: Format of the labeled data. Valid options are: "WKT" and "XY", default is "WKT".
73+
label_format: Format of the labeled data. Valid options are: "WKT" and
74+
"XY", default is "WKT".
7475
annotations_output_dir: File path of directory to write Pascal VOC
75-
annotation files.
76+
annotation files.
7677
images_output_dir: File path of directory to write images.
7778
"""
7879
# Download image and save it

0 commit comments

Comments
 (0)