-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabelme_label_class_changer
executable file
·52 lines (34 loc) · 1.33 KB
/
labelme_label_class_changer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
import sys
import os
import json
import argparse
"""
USAGE:
labelme_label_class_changer --source=./labels/ --classes=<class to be replaced>:<replacement class> --classes=lycra_cut:beta
"""
parser = argparse.ArgumentParser(description='Label class changer')
parser.add_argument("--source", type=str, help='Input dir for labels')
parser.add_argument("--classes",action="append", type=str, help='Class to change')
args = parser.parse_args()
INDIR = args.source
print("Input dir: ", INDIR)
print("Class to change: ", args.classes)
OUTDIR = INDIR + "_changed"
if not os.path.exists(OUTDIR):
os.makedirs(OUTDIR)
classesToBeChanged = args.classes
print("Classes to be changed: ", classesToBeChanged)
classesToBeChanged = dict(item.split(":") for item in classesToBeChanged)
print("Classes to be changed: ", classesToBeChanged)
#read all files in the directory
for filename in os.listdir(INDIR):
if filename.endswith(".json"):
print("Processing file: ", filename)
with open(INDIR + "/" + filename) as f:
data = json.load(f)
for shape in data['shapes']:
if shape['label'] in classesToBeChanged:
shape['label'] = classesToBeChanged[shape['label']]
with open(OUTDIR + "/" + filename, 'w') as f:
json.dump(data, f, indent=2)