-
Notifications
You must be signed in to change notification settings - Fork 1
/
cvat2ultralytics.py
180 lines (145 loc) · 6.74 KB
/
cvat2ultralytics.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import os
import sys
import cv2
import ruamel.yaml as yaml
from lxml import etree
from collections import OrderedDict
from tqdm import tqdm
import shutil
from natsort import natsorted
if __name__ == "__main__":
if len(sys.argv) != 4 and len(sys.argv) != 5:
print("python cvat2ultralytics.py path_to_videos path_to_annotations dataset_name [skip_frames]")
exit(0)
elif len(sys.argv) == 4:
video_path = sys.argv[1]
annotation_path = sys.argv[2]
dataset = sys.argv[3]
skip = 10
elif len(sys.argv) == 5:
video_path = sys.argv[1]
annotation_path = sys.argv[2]
dataset = sys.argv[3]
skip = int(sys.argv[4])
# Create a YOLO dataset structure.
dataset_file = f"""
path: {dataset}
train: images/train
val: images/val
test: images/test
nc: 1
names: ['Animal']
"""
if os.path.exists(f"{dataset}"):
shutil.rmtree(f"{dataset}")
with open(f"{dataset}.yaml", "w") as file:
yaml.dump(yaml.load(dataset_file, Loader=yaml.RoundTripLoader, preserve_quotes=True),
file, Dumper=yaml.RoundTripDumper)
if not os.path.exists(f"{dataset}/images/train"):
os.makedirs(f"{dataset}/images/train")
if not os.path.exists(f"{dataset}/images/val"):
os.makedirs(f"{dataset}/images/val")
if not os.path.exists(f"{dataset}/images/test"):
os.makedirs(f"{dataset}/images/test")
if not os.path.exists(f"{dataset}/labels/train"):
os.makedirs(f"{dataset}/labels/train")
if not os.path.exists(f"{dataset}/labels/val"):
os.makedirs(f"{dataset}/labels/val")
if not os.path.exists(f"{dataset}/labels/test"):
os.makedirs(f"{dataset}/labels/test")
label2index = {
"Zebra": 0,
"Baboon": 1,
"Giraffe": 2
}
print("Process CVAT annotations...")
videos = []
annotations = []
for root, dirs, files in os.walk(annotation_path):
for file in files:
video_name = os.path.join(video_path + root[len(annotation_path):], os.path.splitext(file)[0])
if os.path.exists(video_name + ".MP4"):
videos.append(video_name + ".MP4")
else:
videos.append(video_name + ".mp4")
annotations.append(os.path.join(root, file))
for i, (video, annotation) in enumerate(zip(videos, annotations)):
print(f"{i + 1}/{len(annotations)}:")
if not os.path.exists(video):
print(f"Path {video} does not exist.")
continue
# Parse CVAT for video 1.1 annotation file.
root = etree.parse(annotation).getroot()
name = os.path.splitext(video.split("/")[-1])[0]
if root.find("meta").find("task") is not None:
annotated_size = int("".join(root.find("meta").find("task").find("size").itertext()))
width = int("".join(root.find("meta").find("task").find("original_size").find("width").itertext()))
height = int("".join(root.find("meta").find("task").find("original_size").find("height").itertext()))
else:
annotated_size = int("".join(root.find("meta").find("job").find("size").itertext()))
width = int("".join(root.find("meta").find("original_size").find("width").itertext()))
height = int("".join(root.find("meta").find("original_size").find("height").itertext()))
annotated = dict()
track2end = {}
for track in root.iterfind("track"):
track_id = int(track.attrib["id"])
label = label2index[track.attrib["label"].lower().capitalize()]
for box in track.iter("box"):
frame_id = int(box.attrib["frame"])
keyframe = int(box.attrib["keyframe"])
if keyframe == 1:
track2end[track_id] = frame_id
for track in root.iterfind("track"):
track_id = int(track.attrib["id"])
label = label2index[track.attrib["label"].lower().capitalize()]
for box in track.iter("box"):
frame_id = int(box.attrib["frame"])
if annotated.get(frame_id) is None:
annotated[frame_id] = OrderedDict()
if frame_id <= track2end[track_id]:
x_start = float(box.attrib["xtl"])
y_start = float(box.attrib["ytl"])
x_end = float(box.attrib["xbr"])
y_end = float(box.attrib["ybr"])
x_center = (x_start + (x_end - x_start) / 2) / width
y_center = (y_start + (y_end - y_start) / 2) / height
w = (x_end - x_start) / width
h = (y_end - y_start) / height
annotated[frame_id][track_id] = [label, x_center, y_center, w, h]
index = 0
vc = cv2.VideoCapture(video)
pbar = tqdm(total=annotated_size)
while vc.isOpened():
returned, frame = vc.read()
saved = False
if returned:
if index > max(track2end.values()):
pbar.update(annotated_size - index)
break
if annotated.get(index) is not None:
if index % skip == 0:
for box in annotated[index].values():
if not saved:
cv2.imwrite(f"{dataset}/images/train/{name}_{index}.jpg", frame)
saved = True
with open(f"{dataset}/labels/train/{name}_{index}.txt", "a") as file:
file.write(f"{box[0]} {box[1]:.6f} {box[2]:.6f} {box[3]:.6f} {box[4]:.6f}\n")
index += 1
pbar.update(1)
else:
break
pbar.close()
vc.release()
print("Distribute train, val, and test...")
images = natsorted([file for file in os.listdir(f"{dataset}/images/train") if
os.path.isfile(os.path.join(f"{dataset}/images/train", file))])
labels = natsorted([file for file in os.listdir(f"{dataset}/labels/train") if
os.path.isfile(os.path.join(f"{dataset}/labels/train", file))])
for file in tqdm(images[int(len(images) * 0.8):int(len(images) * 0.87)]):
shutil.move(f"{dataset}/images/train/{file}", f"{dataset}/images/val/{file}")
for file in tqdm(labels[int(len(labels) * 0.8):int(len(labels) * 0.87)]):
shutil.move(f"{dataset}/labels/train/{file}", f"{dataset}/labels/val/{file}")
for file in tqdm(images[int(len(images) * 0.87):]):
shutil.move(f"{dataset}/images/train/{file}", f"{dataset}/images/test/{file}")
for file in tqdm(labels[int(len(labels) * 0.87):]):
shutil.move(f"{dataset}/labels/train/{file}", f"{dataset}/labels/test/{file}")