-
Notifications
You must be signed in to change notification settings - Fork 5
/
convert_ch.py
101 lines (83 loc) · 3.06 KB
/
convert_ch.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Author: Li Chuming
# contact [email protected]
import argparse
import json
import os
import cv2
from tqdm import tqdm
def parse_args():
parser = argparse.ArgumentParser(description="extract frame from video list")
parser.add_argument("-f", type=str)
parser.add_argument("--input_file", type=str, default="", help="")
parser.add_argument("--output_file", type=str, default="", help="output json")
args = parser.parse_args()
return args
args = parse_args()
args.image_root = 'path/val2017/'
args.input_file = 'path/annotation_val.odgt'
args.output_file = 'CrowdHuman_val.json'
PERSON_LABEL = 1
def Convert2POD(args):
f = open(args.input_file)
output_list = []
image_count = 0
box_count = 0
ignore_count = 0
for line in tqdm(f):
"""
{
"ID": "273271,1c72c000a2ee47d5",
"gtboxes": [
{"fbox": [23, 230, 228, 597], "tag": "person", "hbox": [125, 233, 82, 110], "extra": {"box_id": 0, "occ": 1}, "vbox": [62, 234, 186, 487], "head_attr": {"ignore": 0, "occ": 0, "unsure": 0}},
{"fbox": [0, 308, 164, 416], "tag": "mask", "hbox": [0, 308, 164, 416], "extra": {"ignore": 1}, "vbox": [0, 308, 164, 416], "head_attr": {}}
]
}
"""
line = json.loads(line.strip())
json_data = {}
json_data["filename"] = line['ID'] + '.jpg'
img = cv2.imread(os.path.join(args.image_root, json_data["filename"]))
json_data["image_height"] = img.shape[0]
json_data["image_width"] = img.shape[1]
json_data["instances"] = []
for gtbox in line['gtboxes']:
fbox = gtbox['fbox']
tag = gtbox['tag']
hbox = gtbox['hbox']
extra = gtbox['extra']
vbox = gtbox['vbox']
head_attr = gtbox['head_attr']
is_ignored = extra.get("ignore", 0)
is_ignored = {0: False, 1: True}[is_ignored]
if is_ignored:
ignore_count += 1
assert tag in {'mask', 'person'}, '{} {}'.format(line['ID'], gtbox)
else:
box_count += 1
assert tag == 'person', tag
label = PERSON_LABEL
bbox = fbox
vbbox = vbox
bbox[2] += bbox[0] - 1
bbox[3] += bbox[1] - 1
vbbox[2] += vbbox[0] - 1
vbbox[3] += vbbox[1] - 1
json_data["instances"].append(
{
"is_ignored": is_ignored,
"bbox": bbox,
"vbbox": vbbox,
"label": label,
}
)
image_count += 1
output_list.append(json_data)
print('total {} images, {} boxes, {} ignores'.format(image_count, box_count, ignore_count))
def write_json_file(output_list, filename):
f = open(filename, 'w')
print('start to write file {}'.format(filename))
for line in tqdm(output_list):
f.write(json.dumps(line, ensure_ascii=False) + '\n')
f.close()
write_json_file(output_list, args.output_file)
Convert2POD(args)