-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_small_boxes.py
More file actions
35 lines (28 loc) · 1.03 KB
/
remove_small_boxes.py
File metadata and controls
35 lines (28 loc) · 1.03 KB
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
import argparse
import json
def build_parser():
parser = argparse.ArgumentParser()
parser.add_argument('-json', '--json-file', required=True, type=str)
parser.add_argument('-thr', '--threshold', required=True, type=float)
parser.add_argument('-out', '--out-file', required=True, type=str)
return parser
def remove_small_boxes(json_dict, threshold):
sub = 0
ann_id = 1
for i in range(len(json_dict['annotations'])):
if json_dict['annotations'][i - sub]['area'] < threshold:
del json_dict['annotations'][i - sub]
sub += 1
else:
json_dict['annotations'][i - sub]['id'] = ann_id
ann_id += 1
return sub
if __name__ == '__main__':
parser = build_parser()
args = parser.parse_args()
with open(args.json_file, 'r') as f:
json_dict = json.load(f)
removed = remove_small_boxes(json_dict, args.threshold)
print('Removed {} boxes'.format(removed))
with open(args.out_file, 'w') as f:
json.dump(json_dict, f, indent=2)