|
5 | 5 | import datetime as dt |
6 | 6 | import json |
7 | 7 | import logging |
8 | | -from PIL import Image |
9 | 8 | import requests |
10 | 9 | from shapely import wkt |
11 | 10 | from shapely.geometry import Polygon |
12 | 11 | from typing import Any, Dict, Sequence |
13 | 12 |
|
14 | 13 | from labelbox.exceptions import UnknownFormatError |
| 14 | +from PIL import Image |
15 | 15 |
|
16 | 16 |
|
17 | 17 | def from_json(labeled_data, coco_output, label_format='WKT'): |
@@ -78,7 +78,8 @@ def add_label( |
78 | 78 | label_id: ID for the instance to write |
79 | 79 | image_url: URL to download image file from |
80 | 80 | 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". |
82 | 83 |
|
83 | 84 | Returns: |
84 | 85 | The updated COCO export represented as a dictionary. |
@@ -118,25 +119,29 @@ def add_label( |
118 | 119 | coco['categories'].append(category) |
119 | 120 |
|
120 | 121 | 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) |
140 | 145 |
|
141 | 146 |
|
142 | 147 | def _get_polygons(label_format, label_data): |
|
0 commit comments