Skip to content

Commit

Permalink
refactor: check labelme_json2dataset is ok
Browse files Browse the repository at this point in the history
  • Loading branch information
veraposeidon committed May 12, 2024
1 parent 7fbbc5c commit 8432c7a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
24 changes: 14 additions & 10 deletions labelme2datasets/labelme_json2dataset.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# coding=utf-8
"""
brief: covert single json file to single image dataset.
Expand All @@ -6,8 +7,6 @@
reference: https://github.com/wkentaro/labelme/blob/main/labelme/cli/json_to_dataset.py
"""

# coding=utf-8

import argparse
import base64
import json
Expand Down Expand Up @@ -50,7 +49,7 @@ def get_label_names(data, image):
for shape in sorted(data['shapes'], key=lambda x: x['label']):
label_name = shape['label']
if label_name in label_name_to_value:
pass
continue
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value
Expand Down Expand Up @@ -85,15 +84,15 @@ def save_image_and_label(image, lbl, output_dir, label_names):


def main():
""" main """
""" Main function. """
logger.warning(
'This script is aimed to demonstrate how to convert the'
'JSON file to a single image dataset, and not to handle'
'multiple JSON files to generate a real-use dataset.'
'This script demonstrates how to convert a JSON file '
'into a single image dataset. However, it is not intended '
'to handle multiple JSON files for generating a real-world dataset.'
)
logger.warning(
"It won't handle multiple JSON files to generate a "
"real-use dataset."
"This script does not support processing multiple JSON files "
"to create a real-world dataset."
)
parser = argparse.ArgumentParser()
parser.add_argument('--json_file')
Expand All @@ -102,11 +101,16 @@ def main():

json_file = args.json_file

if json_file is None or not os.path.exists(json_file):
logger.error("JSON file is not provided or does not exist. -h for help.")
return

if args.output_dir is None:
out_dir = osp.basename(json_file).replace('.', '_')
out_dir = osp.join(osp.dirname(json_file), out_dir)
out_dir = osp.join(osp.dirname(json_file), str(out_dir))
else:
out_dir = args.output_dir

if not osp.exists(out_dir):
os.mkdir(out_dir)

Expand Down
Binary file modified output/test_single_output/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified output/test_single_output/label_viz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ imgviz~=1.7.5
labelme~=5.4.1
progressbar~=2.5
scikit-learn~=1.4.2
xmltodict~=0.13.0
xmltodict~=0.13.0
setuptools~=69.5.1
pillow~=10.3.0
lxml~=5.2.1

0 comments on commit 8432c7a

Please sign in to comment.