Skip to content

Commit d2c4a2e

Browse files
authored
Merge pull request #45 from fastlabel/develop
Merge to main
2 parents e36179c + 680e3d7 commit d2c4a2e

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ _If you are using FastLabel prototype, please install version 0.2.2._
1919
- [COCO](#coco)
2020
- [YOLO](#yolo)
2121
- [Pascal VOC](#pascal-voc)
22+
- [labelme](#labelme)
2223

2324
## Installation
2425

@@ -815,6 +816,15 @@ tasks = client.get_image_tasks(project="YOUR_PROJECT_SLUG")
815816
client.export_pascalvoc(tasks)
816817
```
817818

819+
### labelme
820+
821+
- Get tasks and export as labelme format files.
822+
823+
```python
824+
tasks = client.get_image_tasks(project="YOUR_PROJECT_SLUG")
825+
client.export_labelme(tasks)
826+
```
827+
818828
## API Docs
819829

820830
Check [this](https://api.fastlabel.ai/docs/) for further information.

fastlabel/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,22 @@ def export_pascalvoc(self, tasks: list, output_dir: str = os.path.join("output",
509509
with open(file_path, 'w', encoding="utf8") as f:
510510
f.write(xml)
511511

512+
def export_labelme(self, tasks: list, output_dir: str = os.path.join("output", "labelme")) -> None:
513+
"""
514+
Convert tasks to labelme format as files.
515+
516+
tasks is a list of tasks. (Required)
517+
output_dir is output directory(default: output/labelme). (Optional)
518+
"""
519+
labelmes = converters.to_labelme(tasks)
520+
for labelme in labelmes:
521+
file_name = labelme["imagePath"]
522+
basename = utils.get_basename(file_name)
523+
file_path = os.path.join(output_dir, basename + ".json")
524+
os.makedirs(os.path.dirname(file_path), exist_ok=True)
525+
with open(file_path, 'w') as f:
526+
json.dump(labelme, f, indent=4, ensure_ascii=False)
527+
512528
# Annotation
513529

514530
def find_annotation(self, annotation_id: str) -> dict:

fastlabel/converters.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,60 @@ def to_pascalvoc(tasks: list) -> list:
220220
return pascalvoc
221221

222222

223+
# labelme
224+
225+
226+
def to_labelme(tasks: list) -> list:
227+
labelmes =[]
228+
for task in tasks:
229+
shapes = []
230+
for annotation in task["annotations"]:
231+
shape_type = __to_labelme_shape_type(annotation["type"])
232+
if not shape_type:
233+
continue
234+
points = annotation["points"]
235+
if len(points) == 0:
236+
continue
237+
238+
shape_points = []
239+
if annotation["type"] == "segmentation":
240+
for i in range(int(len(points[0][0]) / 2)):
241+
shape_points.append([points[0][0][i * 2], points[0][0][(i * 2) + 1]])
242+
else:
243+
for i in range(int(len(points) / 2)):
244+
shape_points.append([points[i * 2], points[(i * 2) + 1]])
245+
246+
shape = {
247+
"label": annotation["value"],
248+
"points": shape_points,
249+
"group_id": None,
250+
"shape_type": shape_type,
251+
"flags": {}
252+
}
253+
shapes.append(shape)
254+
labelmes.append({
255+
"version": "4.5.9",
256+
"flags": {},
257+
"shapes": shapes,
258+
"imagePath": task["name"],
259+
"imageData": None,
260+
"imageHeight": task["height"],
261+
"imageWidth": task["width"],
262+
})
263+
return labelmes
264+
265+
def __to_labelme_shape_type(annotation_type: str) -> str:
266+
if annotation_type == "polygon" or annotation_type == "segmentation":
267+
return "polygon"
268+
if annotation_type == "bbox":
269+
return "rectangle"
270+
if annotation_type == "keypoint":
271+
return "point"
272+
if annotation_type == "line":
273+
return "line"
274+
return None
275+
276+
223277
def __coco2pascalvoc(coco: dict) -> list:
224278
pascalvoc = []
225279

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setuptools.setup(
1010
name="fastlabel",
11-
version="0.9.3",
11+
version="0.9.4",
1212
author="eisuke-ueta",
1313
author_email="[email protected]",
1414
description="The official Python SDK for FastLabel API, the Data Platform for AI",

0 commit comments

Comments
 (0)