Skip to content

Commit 45831a4

Browse files
authored
Merge pull request #137 from fastlabel/feature/3475-improve-export-video-task
Fix export of video task for coco, yolo, PascalVOC
2 parents 56f4219 + 0b44e6f commit 45831a4

File tree

4 files changed

+373
-123
lines changed

4 files changed

+373
-123
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,14 +2084,15 @@ Support the following annotation types.
20842084
Get tasks and export as a [COCO format](https://cocodataset.org/#format-data) file.
20852085

20862086
```python
2087-
tasks = client.get_image_tasks(project="YOUR_PROJECT_SLUG")
2088-
client.export_coco(tasks)
2087+
project_slug = "YOUR_PROJECT_SLUG"
2088+
tasks = client.get_image_tasks(project=project_slug)
2089+
client.export_coco(project=project_slug, tasks=tasks)
20892090
```
20902091

20912092
Export with specifying output directory and file name.
20922093

20932094
```python
2094-
client.export_coco(tasks=tasks, output_dir="YOUR_DIRECTROY", output_file_name="YOUR_FILE_NAME")
2095+
client.export_coco(project="YOUR_PROJECT_SLUG", tasks=tasks, output_dir="YOUR_DIRECTROY", output_file_name="YOUR_FILE_NAME")
20952096
```
20962097

20972098
If you would like to export pose estimation type annotations, please pass annotations.
@@ -2100,7 +2101,7 @@ If you would like to export pose estimation type annotations, please pass annota
21002101
project_slug = "YOUR_PROJECT_SLUG"
21012102
tasks = client.get_image_tasks(project=project_slug)
21022103
annotations = client.get_annotations(project=project_slug)
2103-
client.export_coco(tasks=tasks, annotations=annotations, output_dir="YOUR_DIRECTROY", output_file_name="YOUR_FILE_NAME")
2104+
client.export_coco(project=project_slug, tasks=tasks, annotations=annotations, output_dir="YOUR_DIRECTROY", output_file_name="YOUR_FILE_NAME")
21042105
```
21052106

21062107
### FastLabel To YOLO
@@ -2113,8 +2114,9 @@ Support the following annotation types.
21132114
Get tasks and export as YOLO format files.
21142115

21152116
```python
2116-
tasks = client.get_image_tasks(project="YOUR_PROJECT_SLUG")
2117-
client.export_yolo(tasks, output_dir="YOUR_DIRECTROY")
2117+
project_slug = "YOUR_PROJECT_SLUG"
2118+
tasks = client.get_image_tasks(project=project_slug)
2119+
client.export_yolo(project=project_slug, tasks=tasks, output_dir="YOUR_DIRECTROY")
21182120
```
21192121

21202122
Get tasks and export as YOLO format files with classes.txt
@@ -2125,7 +2127,7 @@ project_slug = "YOUR_PROJECT_SLUG"
21252127
tasks = client.get_image_tasks(project=project_slug)
21262128
annotations = client.get_annotations(project=project_slug)
21272129
classes = list(map(lambda annotation: annotation["value"], annotations))
2128-
client.export_yolo(tasks=tasks, classes=classes, output_dir="YOUR_DIRECTROY")
2130+
client.export_yolo(project=project_slug, tasks=tasks, classes=classes, output_dir="YOUR_DIRECTROY")
21292131
```
21302132

21312133
### FastLabel To Pascal VOC
@@ -2138,8 +2140,9 @@ Support the following annotation types.
21382140
Get tasks and export as Pascal VOC format files.
21392141

21402142
```python
2141-
tasks = client.get_image_tasks(project="YOUR_PROJECT_SLUG")
2142-
client.export_pascalvoc(tasks)
2143+
project_slug = "YOUR_PROJECT_SLUG"
2144+
tasks = client.get_image_tasks(project=project_slug)
2145+
client.export_pascalvoc(project=project_slug, tasks=tasks)
21432146
```
21442147

21452148
### FastLabel To labelme
@@ -2403,7 +2406,6 @@ for image_file_path in glob.iglob(os.path.join(input_dataset_path, "**/**.jpg"),
24032406

24042407
> Please check const.COLOR_PALLETE for index colors.
24052408
2406-
24072409
## Execute endpoint
24082410

24092411
Create the endpoint from the screen at first.

fastlabel/__init__.py

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,7 @@ def __get_yolo_format_annotations(self, dataset_folder_path: str) -> dict:
21672167

21682168
def export_coco(
21692169
self,
2170+
project: str,
21702171
tasks: list,
21712172
annotations: list = [],
21722173
output_dir: str = os.path.join("output", "coco"),
@@ -2176,6 +2177,7 @@ def export_coco(
21762177
Convert tasks to COCO format and export as a file.
21772178
If you pass annotations, you can export Pose Estimation type annotations.
21782179
2180+
project is slug of your project (Required).
21792181
tasks is a list of tasks (Required).
21802182
annotations is a list of annotations (Optional).
21812183
output_dir is output directory(default: output/coco) (Optional).
@@ -2185,14 +2187,27 @@ def export_coco(
21852187
raise FastLabelInvalidException(
21862188
"Output file name must have a json extension", 422
21872189
)
2188-
coco = converters.to_coco(tasks, annotations)
2190+
2191+
project = self.find_project_by_slug(project)
2192+
if project is None:
2193+
raise FastLabelInvalidException(
2194+
"Project not found. Check the project slag.", 422
2195+
)
2196+
21892197
os.makedirs(output_dir, exist_ok=True)
2198+
coco = converters.to_coco(
2199+
project_type=project["type"],
2200+
tasks=tasks,
2201+
annotations=annotations,
2202+
output_dir=output_dir,
2203+
)
21902204
file_path = os.path.join(output_dir, output_file_name)
21912205
with open(file_path, "w") as f:
21922206
json.dump(coco, f, indent=4, ensure_ascii=False)
21932207

21942208
def export_yolo(
21952209
self,
2210+
project: str,
21962211
tasks: list,
21972212
classes: list = [],
21982213
output_dir: str = os.path.join("output", "yolo"),
@@ -2203,43 +2218,75 @@ def export_yolo(
22032218
If not , classes.txt will be generated based on passed tasks .
22042219
(Annotations never used in your project will not be exported.)
22052220
2221+
project is slug of your project (Required).
22062222
tasks is a list of tasks (Required).
22072223
classes is a list of annotation values. e.g. ['dog','bird'] (Optional).
22082224
output_dir is output directory(default: output/yolo) (Optional).
22092225
"""
2210-
annos, categories = converters.to_yolo(tasks, classes)
2226+
2227+
project = self.find_project_by_slug(project)
2228+
if project is None:
2229+
raise FastLabelInvalidException(
2230+
"Project not found. Check the project slag.", 422
2231+
)
2232+
2233+
os.makedirs(output_dir, exist_ok=True)
2234+
annos, categories = converters.to_yolo(
2235+
project_type=project["type"],
2236+
tasks=tasks,
2237+
classes=classes,
2238+
output_dir=output_dir,
2239+
)
22112240
for anno in annos:
22122241
file_name = anno["filename"]
22132242
basename = utils.get_basename(file_name)
22142243
file_path = os.path.join(output_dir, "annotations", basename + ".txt")
22152244
os.makedirs(os.path.dirname(file_path), exist_ok=True)
22162245
with open(file_path, "w", encoding="utf8") as f:
2217-
for obj in anno["object"]:
2246+
objects = anno.get("object")
2247+
if objects is None:
2248+
continue
2249+
for obj in objects:
22182250
f.write(obj)
22192251
f.write("\n")
22202252
classes_file_path = os.path.join(output_dir, "classes.txt")
2221-
os.makedirs(os.path.dirname(classes_file_path), exist_ok=True)
22222253
with open(classes_file_path, "w", encoding="utf8") as f:
22232254
for category in categories:
22242255
f.write(category["name"])
22252256
f.write("\n")
22262257

22272258
def export_pascalvoc(
2228-
self, tasks: list, output_dir: str = os.path.join("output", "pascalvoc")
2259+
self,
2260+
project: str,
2261+
tasks: list,
2262+
output_dir: str = os.path.join("output", "pascalvoc"),
22292263
) -> None:
22302264
"""
22312265
Convert tasks to Pascal VOC format as files.
22322266
2267+
project is slug of your project (Required).
22332268
tasks is a list of tasks (Required).
22342269
output_dir is output directory(default: output/pascalvoc) (Optional).
22352270
"""
2236-
pascalvoc = converters.to_pascalvoc(tasks)
2271+
2272+
project = self.find_project_by_slug(project)
2273+
if project is None:
2274+
raise FastLabelInvalidException(
2275+
"Project not found. Check the project slag.", 422
2276+
)
2277+
2278+
os.makedirs(output_dir, exist_ok=True)
2279+
pascalvoc = converters.to_pascalvoc(
2280+
project_type=project["type"], tasks=tasks, output_dir=output_dir
2281+
)
22372282
for voc in pascalvoc:
22382283
file_name = voc["annotation"]["filename"]
22392284
basename = utils.get_basename(file_name)
2240-
file_path = os.path.join(output_dir, basename + ".xml")
2285+
file_path = os.path.join(output_dir, "annotations", basename + ".xml")
22412286
os.makedirs(os.path.dirname(file_path), exist_ok=True)
2242-
xml = xmltodict.unparse(voc, pretty=True, full_document=False)
2287+
xml = xmltodict.unparse(
2288+
voc, pretty=True, indent=" ", full_document=False
2289+
)
22432290
with open(file_path, "w", encoding="utf8") as f:
22442291
f.write(xml)
22452292

0 commit comments

Comments
 (0)