-
Notifications
You must be signed in to change notification settings - Fork 0
/
mot_converter.py
133 lines (113 loc) · 4.48 KB
/
mot_converter.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
import csv
from glob import glob
import os
import shutil
import ast
import numpy as np
from tqdm import tqdm
def get_id(object_name):
if "PALLET" in object_name:
return 0
elif "FORKLIFT" in object_name:
return 1
elif "KLT" in object_name:
return 2
elif "Barrel" in object_name:
return 3
elif "BOX_TRACK_2" == object_name:
return 4
elif "BOX_TRACK_1" == object_name:
return 5
elif "Gitter" in object_name:
return 6
def safe_create_folder(folderName):
if not os.path.exists(folderName):
os.mkdir(folderName)
return folderName
def create_folder_name(folderName):
if "9_43" in folderName:
return "LOG22-00-9_43"
elif "9_58" in folderName:
return "LOG22-00-9_58"
elif "10_00" in folderName:
return "LOG22-00-10_00"
elif "11_52" in folderName:
return "LOG22-00-11_52"
elif "15_39" in folderName:
return "LOG22-00-15_39"
elif "15_52" in folderName:
return "LOG22-00-15_52"
else:
return folderName
def main():
datasets = glob('dataset/*')
root = safe_create_folder(os.path.join("..", "mot"))
for dataset in datasets:
print(f"Processing dataset: {dataset}...")
folderName = create_folder_name(os.path.split(dataset)[-1])
data_path = safe_create_folder(os.path.join(root, folderName))
image_path = safe_create_folder(os.path.join(data_path, "img1"))
det_path = safe_create_folder(os.path.join(data_path, "det"))
gt_path = safe_create_folder(os.path.join(data_path, "gt"))
# annotation_path = safe_create_folder(os.path.join(data_path, "annotations"))
images = glob(os.path.join(dataset, "camera_6", "images", '*.jpg'))
images.sort(key=lambda img: int(os.path.split(img)[-1].split('.')[0]))
first_img_id = int(os.path.split(images[0])[-1].split('.')[0])
object_ids = {}
with open(os.path.join(dataset, "camera_6", "new_data.csv")) as f:
reader = csv.reader(f, delimiter=',')
next(reader)
data = []
# fileName ObjectName Position Rotation Occlusion Delta_Time BoundingBox Visible
for row in reader:
vis = ast.literal_eval(row[-1])
bb = ast.literal_eval(row[-2])
if row[1] not in object_ids:
object_ids[row[1]] = len(object_ids)
# [image_id, object_class, bb_y, bb_x, bb_h, bb_w, object_name, vis]
data.append([
int(os.path.split(row[0])[-1].split('.')[0]),
get_id(row[1]),
bb[1],
bb[0],
bb[3],
bb[2],
row[1],
vis
])
data = np.array(data)
first_id = int(os.path.split(images[0])[-1].split(".")[0])
print(first_id)
with open(os.path.join(gt_path, "gt.txt"), "w", newline="") as f:
writer = csv.writer(f, delimiter=",")
for obj_idf in object_ids:
for row in data[data[:,-2] == obj_idf]:
id_ = int(row[0])
if id_ >= first_id and ast.literal_eval(row[-1]) > 0:
object_id = object_ids[row[-2]]
# 1569,1,0,175,234,113,1,1,1
writer.writerow([
id_ - first_img_id,
object_id,
*row[2:6],
1,
row[1],
row[-1]
])
with open(os.path.join(det_path, "det.txt"), 'w', newline="") as f:
writer = csv.writer(f, delimiter=',')
for image_name in tqdm(images):
name = os.path.split(image_name)[-1]
id_ = int(name.split(".")[0])
# [318 0 352 165 117 153]
data_ = data[data[:,0].astype(int) == id_]
# 481,-1,1005,397,290,104,1,-1,-1,-1
for date in data_:
if ast.literal_eval(date[-1]) > 0:
writer.writerow([id_ - first_img_id, -1, *date[2:6], 1, -1, date[1], 1])
target = os.path.join(image_path, f"{id_ - first_img_id}.jpg")
if not os.path.exists(target):
shutil.copy(image_name, target)
print(object_ids)
if __name__ == "__main__":
main()