-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbdd5k2youcook.py
More file actions
262 lines (215 loc) · 10.7 KB
/
Copy pathbdd5k2youcook.py
File metadata and controls
262 lines (215 loc) · 10.7 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import os
import cv2
import json
from copy import deepcopy
import argparse
event_sort_key = {
"prerecognition": 0,
"recognition": 1,
"judgement": 2,
"action": 3,
"avoidance": 4,
}
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--videos_dir",
type=str,
default="./datasets/videos",
required=True,
help="Path to the directory containing WTS videos",
)
parser.add_argument(
"--caption_dir",
type=str,
default="./datasets/annotations/caption",
required=True,
help="Path to the directory containing WTS captions",
)
parser.add_argument("--bbox_dir", type=str, default=None)
parser.add_argument(
"--output_dir",
type=str,
default="./datasets/new_annotations",
)
parser.add_argument("--merge_val", action="store_true")
return parser.parse_args()
def convert_bdd5k_to_youcook_format(video_filepath, caption_filepath):
# print(f"Loading video: {video_filepath}")
cap = cv2.VideoCapture(video_filepath)
video_fps = cap.get(cv2.CAP_PROP_FPS)
fps = 30
if video_filepath.split("/")[-3] == "20231006_21_CN5_T1":
print("Large videos: ", video_filepath)
fps = 10
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
duration = frame_count / video_fps
with open(caption_filepath, "r") as f:
caption_data = json.load(f)
event_phase = caption_data["event_phase"]
event_phase = sorted(event_phase, key=lambda x: event_sort_key[x["labels"][0]])
video_fps = float(
caption_data.get("fps", video_fps)
) # use the fps from the caption file if available
if video_fps > 40:
video_fps = 30
pedestrian_data = {
"duration": round(duration, 2),
"fps": 1,
"original_fps": round(fps, 2),
"timestamps": [],
"sentences": [],
"video_fps": round(video_fps, 2),
"timestamps_video_fps": [],
}
vehicle_data = deepcopy(pedestrian_data)
for event in event_phase:
start_time = float(event["start_time"])
end_time = float(event["end_time"])
caption_pedestrian = event["caption_pedestrian"]
caption_vehicle = event["caption_vehicle"]
# start = round(start_time * fps)
# end = round(end_time * fps)
start = start_time
end = end_time
start_video_fps = round(start_time * video_fps)
end_video_fps = round(end_time * video_fps)
pedestrian_data["timestamps"].append([start, end])
pedestrian_data["timestamps_video_fps"].append([start_video_fps, end_video_fps])
pedestrian_data["sentences"].append(caption_pedestrian)
vehicle_data["timestamps"].append([start, end])
vehicle_data["timestamps_video_fps"].append([start_video_fps, end_video_fps])
vehicle_data["sentences"].append(caption_vehicle)
return pedestrian_data, vehicle_data
if __name__ == "__main__":
args = parse_args()
VIDEOS_DIR = args.videos_dir
CAPTION_DIR = args.caption_dir
OUTPUT_DIR = args.output_dir
os.makedirs(OUTPUT_DIR, exist_ok=True)
annotation_data = {}
for split in sorted(os.listdir(CAPTION_DIR)):
if split not in ["train", "val"]:
continue
videos_split_dir = os.path.join(VIDEOS_DIR, split)
caption_split_dir = os.path.join(CAPTION_DIR, split)
split_annotation_data = {
"pedestrian": {},
"vehicle": {},
}
print(f"Processing {split} ...")
for view in sorted(os.listdir(caption_split_dir)):
video_view_dir = os.path.join(videos_split_dir, view)
caption_view_dir = os.path.join(caption_split_dir, view)
if not os.path.isdir(video_view_dir) or not os.path.isdir(caption_view_dir):
print(f"Skipping {view} ...")
continue
print(f"Processing {view} ...")
for json_file in os.listdir(caption_view_dir):
if not json_file.endswith(".json"):
continue
json_filepath = os.path.join(caption_view_dir, json_file)
# print("Processing", json_filepath)
with open(json_filepath, "r") as f:
caption_data = json.load(f)
video_name = caption_data["video_name"]
video_path = os.path.join(video_view_dir, video_name)
if not os.path.exists(video_path):
print("Video not found:", video_path)
exit(1)
pedestrian_data, vehicle_data = convert_bdd5k_to_youcook_format(
video_path, json_filepath
)
pedestrian_data["video_path"] = video_path
vehicle_data["video_path"] = video_path
if video_name.endswith(".mp4"):
video_name = ".".join(video_name.split(".")[:-1])
split_annotation_data["pedestrian"][video_name] = pedestrian_data
split_annotation_data["vehicle"][video_name] = vehicle_data
annotation_data[split] = split_annotation_data
if args.merge_val:
for video in annotation_data["val"]["pedestrian"]:
annotation_data["train"]["pedestrian"][video] = annotation_data["val"][
"pedestrian"
][video]
annotation_data["train"]["vehicle"][video] = annotation_data["val"][
"vehicle"
][video]
for split in annotation_data:
for obj in ["pedestrian", "vehicle"]:
output_filepath = os.path.join(OUTPUT_DIR, f"{obj}_{split}.json")
with open(output_filepath, "w") as f:
json.dump(annotation_data[split][obj], f, indent=4)
# for video_id in sorted(os.listdir(caption_split_dir)):
# # print(f"Processing {video_id} ...")
# video_dir = os.path.join(videos_split_dir, video_id)
# caption_dir = os.path.join(caption_split_dir, video_id)
# if video_id == "normal_trimmed":
# for normal_video_id in sorted(os.listdir(video_dir)):
# normal_video_dir = os.path.join(video_dir, normal_video_id)
# normal_caption_dir = os.path.join(caption_dir, normal_video_id)
# for normal_view in sorted(os.listdir(normal_caption_dir)):
# normal_video_view_dir = os.path.join(normal_video_dir, normal_view)
# normal_caption_view_dir = os.path.join(normal_caption_dir, normal_view)
# for json_file in os.listdir(normal_caption_view_dir):
# if not json_file.endswith(".json"):
# continue
# json_filepath = os.path.join(normal_caption_view_dir, json_file)
# # print("Processing", json_filepath)
# with open(json_filepath, "r") as f:
# caption_data = json.load(f)
# video_filenames = []
# if normal_view.lower() == "overhead_view":
# video_filenames = caption_data['overhead_videos']
# else:
# video_filenames.append(caption_data['vehicle_view'])
# for video_filename in video_filenames:
# video_filepath = os.path.join(normal_video_view_dir, video_filename)
# if not video_filename.lower().endswith(".mp4"):
# print("Skipping", video_filepath)
# continue
# # print("Processing", video_filepath)
# pedestrian_data, vehicle_data = convert_wts_to_youcook_format(
# video_filepath, json_filepath
# )
# pedestrian_data["video_path"] = video_filepath
# vehicle_data["video_path"] = video_filepath
# video_name = video_filename.split(".")[:-1]
# video_name = ".".join(video_name)
# split_annotation_data["pedestrian"][video_name] = pedestrian_data
# split_annotation_data["vehicle"][video_name] = vehicle_data
# else:
# for view in sorted(os.listdir(caption_dir)):
# video_view_dir = os.path.join(video_dir, view)
# caption_view_dir = os.path.join(caption_dir, view)
# for json_file in os.listdir(caption_view_dir):
# if not json_file.endswith(".json"):
# continue
# json_filepath = os.path.join(caption_view_dir, json_file)
# # print("Processing", json_filepath)
# with open(json_filepath, "r") as f:
# caption_data = json.load(f)
# video_filenames = []
# if view.lower() == "overhead_view":
# video_filenames = caption_data['overhead_videos']
# else:
# video_filenames.append(caption_data['vehicle_view'])
# for video_filename in video_filenames:
# video_filepath = os.path.join(video_view_dir, video_filename)
# if not video_filename.lower().endswith(".mp4"):
# print("Skipping", video_filepath)
# continue
# # print("Processing", video_filepath)
# pedestrian_data, vehicle_data = convert_wts_to_youcook_format(
# video_filepath, json_filepath
# )
# pedestrian_data["video_path"] = video_filepath
# vehicle_data["video_path"] = video_filepath
# video_name = video_filename.split(".")[:-1]
# video_name = ".".join(video_name)
# split_annotation_data["pedestrian"][video_name] = pedestrian_data
# split_annotation_data["vehicle"][video_name] = vehicle_data
# for obj in ["pedestrian", "vehicle"]:
# output_filepath = os.path.join(OUTPUT_DIR, f"{obj}_{split}.json")
# with open(output_filepath, "w") as f:
# json.dump(split_annotation_data[obj], f, indent=4)