|
@@ -1,10 +1,12 @@
|
|
-#!/usr/bin/python
|
|
|
|
|
|
+#!/usr/bin/env python
|
|
|
|
|
|
import argparse
|
|
import argparse
|
|
import json
|
|
import json
|
|
import os
|
|
import os
|
|
import os.path as osp
|
|
import os.path as osp
|
|
|
|
+import warnings
|
|
|
|
|
|
|
|
+import numpy as np
|
|
import PIL.Image
|
|
import PIL.Image
|
|
import yaml
|
|
import yaml
|
|
|
|
|
|
@@ -14,31 +16,41 @@ from labelme import utils
|
|
def main():
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('json_file')
|
|
parser.add_argument('json_file')
|
|
|
|
+ parser.add_argument('-o', '--out', default=None)
|
|
args = parser.parse_args()
|
|
args = parser.parse_args()
|
|
|
|
|
|
json_file = args.json_file
|
|
json_file = args.json_file
|
|
|
|
|
|
- out_dir = osp.basename(json_file).replace('.', '_')
|
|
|
|
- out_dir = osp.join(osp.dirname(json_file), out_dir)
|
|
|
|
- os.mkdir(out_dir)
|
|
|
|
|
|
+ if args.out is None:
|
|
|
|
+ out_dir = osp.basename(json_file).replace('.', '_')
|
|
|
|
+ out_dir = osp.join(osp.dirname(json_file), out_dir)
|
|
|
|
+ else:
|
|
|
|
+ out_dir = args.out
|
|
|
|
+ if not osp.exists(out_dir):
|
|
|
|
+ os.mkdir(out_dir)
|
|
|
|
|
|
data = json.load(open(json_file))
|
|
data = json.load(open(json_file))
|
|
|
|
|
|
img = utils.img_b64_to_array(data['imageData'])
|
|
img = utils.img_b64_to_array(data['imageData'])
|
|
lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes'])
|
|
lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes'])
|
|
|
|
|
|
- lbl_viz = utils.draw_label(lbl, img, lbl_names)
|
|
|
|
|
|
+ captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)]
|
|
|
|
+ lbl_viz = utils.draw_label(lbl, img, captions)
|
|
|
|
|
|
PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
|
|
PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
|
|
PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
|
|
PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
|
|
PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))
|
|
PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))
|
|
|
|
|
|
- info = dict(label_names=lbl_names)
|
|
|
|
|
|
+ with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
|
|
|
|
+ for lbl_name in lbl_names:
|
|
|
|
+ f.write(lbl_name + '\n')
|
|
|
|
|
|
|
|
+ warnings.warn('info.yaml is being replaced by label_names.txt')
|
|
|
|
+ info = dict(label_names=lbl_names)
|
|
with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
|
|
with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
|
|
yaml.safe_dump(info, f, default_flow_style=False)
|
|
yaml.safe_dump(info, f, default_flow_style=False)
|
|
|
|
|
|
- print('wrote data to %s' % out_dir)
|
|
|
|
|
|
+ print('Saved to: %s' % out_dir)
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if __name__ == '__main__':
|