-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathodgt2yolo.py
118 lines (79 loc) · 2.41 KB
/
odgt2yolo.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#
# odgt2yolo.py
# convert odgt format of https://www.crowdhuman.org to YOLO format.
#
import os
import sys
import json
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
from pathlib import Path
import argparse
FILE = Path(__file__).resolve()
ROOT = FILE.parents[0] # YOLOv5 root directory
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
#print(f'ROOT={ROOT}')
def convert_in_original_format(opt):
with open(opt.path, 'r') as rf:
for x in rf:
aitem = json.loads(x)
with open(aitem['ID'] + '.txt', 'w') as wf:
# wf.write(str(aitem))
jason_object = json.dumps(aitem, indent=4)
wf.write(jason_object)
def convert_to_yolo_format(opt):
with open(opt.path, 'r') as rf:
for x in rf:
aitem = json.loads(x)
with open(aitem['ID'] + '.txt', 'w') as wf:
gtboxes = aitem["gtboxes"]
which = 'val' if opt.val else 'train'
image_path = f'{ROOT}/images/{which}/{aitem["ID"]}.jpg'
img = Image.open(image_path)
for af in gtboxes:
if af["tag"] == 'mask':
continue
extra = af['extra']
try:
if extra["ignore"] == 1:
continue
except KeyError:
pass
vbox = af["vbox"]
center_x = vbox[0] + vbox[2]/2
center_y = vbox[1] + vbox[3]/2
width = vbox[2]
height = vbox[3]
wf.write(f'0 {(center_x/img.width):.6f} {(center_y/img.height):.6f} {(width/img.width):.6f} {(height/img.height):.06f}\n')
head_attr = af["head_attr"]
try:
if head_attr['ignore'] == 0:
hbox = af["hbox"]
center_x = hbox[0] + hbox[2]/2
center_y = hbox[1] + hbox[3]/2
width = hbox[2]
height = hbox[3]
wf.write(f'1 {(center_x/img.width):.6f} {(center_y/img.height):.6f} {(width/img.width):.6f} {(height/img.height):.06f}\n')
except KeyError:
pass
img.close()
def convert(opt):
if opt.original:
convert_in_original_format(opt)
else:
convert_to_yolo_format(opt)
def usage():
print(f'usage: python {__file__} filename')
def parse_opt(known=False):
parser = argparse.ArgumentParser()
parser.add_argument('--original', action='store_true', help='show in the original format.')
parser.add_argument('--val', action='store_true', help='val')
parser.add_argument('path')
return parser.parse_known_args()[0] if known else parser.parse_args()
if __name__ == "__main__":
if len(sys.argv) < 2:
usage()
else:
opt = parse_opt()
convert(opt)