-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess_xarm_data.py
415 lines (365 loc) · 14.9 KB
/
process_xarm_data.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import argparse
import numpy as np
import pickle
import cv2
import pandas as pd
import os
import subprocess
import os
import re
import shutil
import h5py
from pathlib import Path
from utils import DATA_DIR
parser = argparse.ArgumentParser()
parser.add_argument("--task-name", "-t", type=str, required=True)
args = parser.parse_args()
TASK_NAME = args.task_name
DATA_PATH = Path(DATA_DIR)
SAVE_PATH = Path(DATA_DIR) / "processed_data"
num_demos = None
cam_indices = {
1: "rgb",
2: "rgb",
51: "fish_eye",
52: "fish_eye",
}
states_file_name = "states"
sensor_file_name = "sensor"
# Create the save path
SAVE_PATH.mkdir(parents=True, exist_ok=True)
done_flag = True
skip_cam_processing = False
process_type = "cont"
print(f"#################### Processing task {TASK_NAME} ####################")
# Check if previous demos from this task exist
if Path(f"{SAVE_PATH}/{TASK_NAME}").exists():
num_prev_demos = len([f for f in (SAVE_PATH / TASK_NAME).iterdir() if f.is_dir()])
if num_prev_demos > 0:
cont_check = input(
f"Previous demonstrations from task {TASK_NAME} exist. Continue from existing demos? y/n."
)
if cont_check == "n":
ow_check = input(
f"Overwrite existing demonstrations from task {TASK_NAME}? y/n."
)
if ow_check == "y":
num_prev_demos = 0
else:
print("Appending new demonstrations to the existing ones.")
process_type = "append"
elif cont_check == "y":
num_prev_demos -= 1 # overwrite the last demo
else:
num_prev_demos = 0
(SAVE_PATH / TASK_NAME).mkdir(parents=True, exist_ok=True)
# demo directories
DEMO_DIRS = [
f
for f in (DATA_PATH / TASK_NAME).iterdir()
if f.is_dir() and "fail" not in f.name and "ignore" not in f.name
]
if num_demos is not None:
DEMO_DIRS = DEMO_DIRS[:num_demos]
for num, demo_dir in enumerate(sorted(DEMO_DIRS)):
process_sensor = True
# try:
if process_type == "cont" and num < num_prev_demos:
print(f"Skipping demonstration {demo_dir.name}")
continue
if process_type == "append":
demo_id = num + num_prev_demos
elif process_type == "cont":
demo_id = int(demo_dir.name.split("_")[-1])
print("Processing demonstration", demo_dir.name)
output_path = f"{SAVE_PATH}/{TASK_NAME}/demonstration_{demo_id}/"
print("Output path:", output_path)
Path(output_path).mkdir(parents=True, exist_ok=True)
csv_list = [f for f in os.listdir(output_path) if f.endswith(".csv")]
for f in csv_list:
os.remove(os.path.join(output_path, f))
cam_avis = [f"{demo_dir}/cam_{i}_{cam_indices[i]}_video.avi" for i in cam_indices]
try:
cartesian = h5py.File(f"{demo_dir}/xarm_cartesian_states.h5", "r")
state_timestamps = cartesian["timestamps"]
state_positions = cartesian["cartesian_positions"]
gripper = h5py.File(f"{demo_dir}/xarm_gripper_states.h5", "r")
gripper_positions = gripper["gripper_positions"]
except:
print("No cartesian or gripper states found. Skipping this demo.")
continue
try:
with h5py.File(f"{demo_dir}/reskin_sensor_values.h5") as hf:
sensor_timestamps = np.array(hf["timestamps"])
sensor_values = np.array(hf["sensor_values"])
except FileNotFoundError:
print("No sensor values found. Skipping sensor processing.")
process_sensor = False
state_positions = np.array(state_positions)
gripper_positions = np.array(gripper_positions)
gripper_positions = gripper_positions.reshape(-1, 1)
state_timestamps = np.array(state_timestamps)
# Find indices of timestamps where the robot moves
static_timestamps = []
static = False
start, end = None, None
for i in range(1, len(state_positions) - 1):
if (
np.array_equal(state_positions[i], state_positions[i + 1])
and static == False
):
static = True
start = i
elif (
not np.array_equal(state_positions[i], state_positions[i + 1])
and static == True
):
static = False
end = i
static_timestamps.append((start, end))
if static:
static_timestamps.append((start, len(state_positions) - 1))
# read metadata file
CAM_TIMESTAMPS = []
CAM_VALID_LENS = []
skip = False
for idx in cam_indices:
cam_meta_file_path = f"{demo_dir}/cam_{idx}_{cam_indices[idx]}_video.metadata"
with open(cam_meta_file_path, "rb") as f:
image_metadata = pickle.load(f)
image_timestamps = np.asarray(image_metadata["timestamps"]) / 1000.0
cam_timestamps = dict(timestamps=image_timestamps)
# convert to numpy array
cam_timestamps = np.array(cam_timestamps["timestamps"])
# Fish eye cam timestamps are divided by 1000
if max(cam_timestamps) < state_timestamps[static_timestamps[0][1]]:
cam_timestamps *= 1000
elif min(cam_timestamps) > state_timestamps[static_timestamps[-1][0]]:
cam_timestamps /= 1000
valid_indices = []
for k in range(len(static_timestamps) - 1):
start_idx = sum(cam_timestamps < state_timestamps[static_timestamps[k][1]])
end_idx = sum(
cam_timestamps < state_timestamps[static_timestamps[k + 1][0]]
)
valid_indices.extend([i for i in range(start_idx, end_idx)])
cam_timestamps = cam_timestamps[valid_indices]
# if no valid timestamps, skip
if len(cam_timestamps) == 0:
skip = True
break
CAM_VALID_LENS.append(valid_indices)
CAM_TIMESTAMPS.append(cam_timestamps)
if skip:
continue
# cam frames
if not skip_cam_processing:
CAM_FRAMES = []
for idx in range(len(cam_avis)): # cam_indices:
cam_avi = cam_avis[idx]
cam_frames = []
cap_cap = cv2.VideoCapture(cam_avi)
while cap_cap.isOpened():
ret, frame = cap_cap.read()
if ret == False:
break
cam_frames.append(frame)
cap_cap.release()
# save frames
cam_frames = np.array(cam_frames)
cam_frames = cam_frames[CAM_VALID_LENS[idx]]
CAM_FRAMES.append(cam_frames)
rgb_frames = CAM_FRAMES
timestamps = CAM_TIMESTAMPS
timestamps.append(state_timestamps)
min_time_index = np.argmin([len(timestamp) for timestamp in timestamps])
reference_timestamps = timestamps[min_time_index]
align = []
index = []
for i in range(len(timestamps)):
# aligning frames
if i == min_time_index:
align.append(timestamps[i])
index.append(np.arange(len(timestamps[i])))
continue
curindex = []
currrlist = []
for j in range(len(reference_timestamps)):
curlist = []
for k in range(len(timestamps[i])):
curlist.append(abs(timestamps[i][k] - reference_timestamps[j]))
min_index = curlist.index(min(curlist))
currrlist.append(timestamps[i][min_index])
curindex.append(min_index)
align.append(currrlist)
index.append(curindex)
index = np.array(index)
if process_sensor:
if max(sensor_timestamps) < state_timestamps[static_timestamps[0][1]]:
print("Sensor data issue")
elif min(sensor_timestamps) > state_timestamps[static_timestamps[-1][0]]:
print("Sensor data issue")
else:
print("All good with sensor data!")
sensor_valid_indices = []
for k in range(len(static_timestamps) - 1):
start_idx = sum(
sensor_timestamps < state_timestamps[static_timestamps[k][1]]
)
end_idx = sum(
sensor_timestamps < state_timestamps[static_timestamps[k + 1][0]]
)
sensor_valid_indices.extend([i for i in range(start_idx, end_idx)])
sensor_timestamps = sensor_timestamps[sensor_valid_indices]
sensor_values = sensor_values[sensor_valid_indices]
sensor_timestamps_test = pd.DataFrame(sensor_timestamps)
sensor_values_test = []
for i in range(len(sensor_values)):
sensor_values_test.append(np.array(sensor_values[i]))
sensor_values_test = pd.DataFrame(
{"column": [list(row) for row in sensor_values_test]}
)
sensor_test = pd.concat(
[sensor_timestamps_test, sensor_values_test],
axis=1,
)
with open(output_path + f"big_{sensor_file_name}.csv", "a") as f:
sensor_test.to_csv(
f,
header=["created timestamp", "sensor_values"],
index=False,
)
# convert left_state_timestamps and left_state_positions to a csv file with header "created timestamp", "pose_aa", "gripper_state"
state_timestamps_test = pd.DataFrame(state_timestamps)
# convert each pose_aa to a list
state_positions_test = state_positions
for i in range(len(state_positions_test)):
state_positions_test[i] = np.array(state_positions_test[i])
state_positions_test = pd.DataFrame(
{"column": [list(row) for row in state_positions_test]}
)
# convert left_gripper to True and False
gripper_positions_test = pd.DataFrame(gripper_positions)
state_test = pd.concat(
[state_timestamps_test, state_positions_test, gripper_positions_test],
axis=1,
)
with open(output_path + f"big_{states_file_name}.csv", "a") as f:
state_test.to_csv(
f,
header=["created timestamp", "pose_aa", "gripper_state"],
index=False,
)
df = pd.read_csv(output_path + f"big_{states_file_name}.csv")
for i in range(len(reference_timestamps)):
curlist = []
for j in range(len(state_timestamps)):
curlist.append(abs(state_timestamps[j] - reference_timestamps[i]))
min_index = curlist.index(min(curlist))
min_df = df.iloc[min_index]
min_df = min_df.to_frame().transpose()
with open(output_path + f"{states_file_name}.csv", "a") as f:
min_df.to_csv(f, header=f.tell() == 0, index=False)
if process_sensor:
df = pd.read_csv(output_path + f"big_{sensor_file_name}.csv")
for i in range(len(reference_timestamps)):
curlist = []
for j in range(len(sensor_timestamps)):
curlist.append(abs(sensor_timestamps[j] - reference_timestamps[i]))
min_index = curlist.index(min(curlist))
min_df = df.iloc[min_index]
min_df = min_df.to_frame().transpose()
with open(output_path + f"{sensor_file_name}.csv", "a") as f:
min_df.to_csv(f, header=f.tell() == 0, index=False)
# Create folders for each camera if they don't exist
output_folder = output_path + "videos"
os.makedirs(output_folder, exist_ok=True)
camera_folders = [f"camera{i}" for i in cam_indices]
for folder in camera_folders:
os.makedirs(os.path.join(output_folder, folder), exist_ok=True)
# Iterate over each camera and extract the frames based on the indexes
if not skip_cam_processing:
for camera_index, frames in enumerate(rgb_frames):
camera_folder = camera_folders[camera_index]
print(f"Extracting frames for {camera_folder}...")
indexes = index[camera_index]
# Iterate over the indexes and save the corresponding frames
for i, indexx in enumerate(indexes):
if i % 100 == 0:
print(f"Extracting frame {i}...")
frame = frames[indexx]
# name frame with its timestamp
image_output_path = os.path.join(
output_folder,
camera_folder,
f"frame_{i}_{timestamps[camera_index][indexx]}.jpg",
)
cv2.imwrite(image_output_path, frame)
csv_file = os.path.join(output_path, f"{states_file_name}.csv")
print(output_path, demo_dir.name)
def get_timestamp_from_filename(filename):
# Extract the timestamp from the filename using regular expression
timestamp_match = re.search(r"\d+\.\d+", filename)
if timestamp_match:
return float(timestamp_match.group())
else:
return None
# add desired gripper states
for file in [csv_file]:
df = pd.read_csv(file)
df["desired_gripper_state"] = df["gripper_state"].shift(-1)
df.loc[df.index[-1], "desired_gripper_state"] = df.loc[
df.index[-2], "gripper_state"
]
df.to_csv(file, index=False)
def save_only_videos(base_folder_path):
base_folder_path = os.path.join(base_folder_path, "videos")
# Iterate over each camera folder
for cam in cam_indices:
cam_folder = f"camera{cam}"
full_folder_path = os.path.join(base_folder_path, cam_folder)
# Check if the folder exists
if os.path.exists(full_folder_path):
# List all jpg files
all_files = [
f for f in os.listdir(full_folder_path) if f.endswith(".jpg")
]
# Sort files based on the floating-point number in their name
sorted_files = sorted(all_files, key=get_timestamp_from_filename)
# Write filenames to a temp file
temp_list_filename = os.path.join(base_folder_path, "temp_list.txt")
with open(temp_list_filename, "w") as f:
for filename in sorted_files:
f.write(f"file '{os.path.join(full_folder_path, filename)}'\n")
# Use ffmpeg to convert sorted images to video
output_video_path = os.path.join(base_folder_path, f"camera{cam}.mp4")
cmd = [
"ffmpeg",
"-f",
"concat",
"-safe",
"0",
"-i",
temp_list_filename,
"-framerate",
"30", # assuming 24 fps, change if needed
"-vcodec",
"libx264",
"-crf",
"18", # quality, lower means better quality
"-pix_fmt",
"yuv420p",
output_video_path,
]
try:
subprocess.run(cmd, check=True)
except Exception as e:
print(f"EXCEPTION: {e}")
input("Continue?")
# Delete the temporary list file and the image folder
os.remove(temp_list_filename)
shutil.rmtree(full_folder_path)
else:
print(f"Folder {cam_folder} does not exist!")
if not skip_cam_processing:
save_only_videos(output_path)