|
import os |
|
import io |
|
import av |
|
import json |
|
from pickle import dumps, loads |
|
import numpy as np |
|
import torch |
|
from torchvision.transforms.functional import resize |
|
import tensorflow as tf |
|
import tensorflow_datasets as tfds |
|
from einops import rearrange |
|
|
|
def decode_inst(insts): |
|
|
|
decoded_insts = [] |
|
for inst in insts: |
|
decoded_insts.append(bytes(inst[np.where(inst != 0)].tolist()).decode("utf-8")) |
|
return decoded_insts |
|
|
|
def save_video(file, video): |
|
container = av.open(file, 'w', 'mp4') |
|
stream = container.add_stream('libx264', rate=30) |
|
stream.height = video[0].shape[0] |
|
stream.width = video[0].shape[1] |
|
stream.bit_rate = 2000000 |
|
stream.pix_fmt = 'yuv420p' |
|
for i in range(len(video)): |
|
frame = av.VideoFrame.from_ndarray(video[i], format='rgb24') |
|
frame = frame.reformat(format=stream.pix_fmt) |
|
for packet in stream.encode(frame): |
|
container.mux(packet) |
|
|
|
for packet in stream.encode(): |
|
container.mux(packet) |
|
container.close() |
|
|
|
if __name__ == '__main__': |
|
tf_builder = tfds.builder_from_directory('./droid/1.0.0/') |
|
tf_dataset = tf_builder.as_dataset(split="train") |
|
skip_episode = 78663 |
|
js_path = 'index.json' |
|
if os.path.exists(js_path): |
|
js_data = json.load(open(js_path, 'r')) |
|
else: |
|
js_data = [] |
|
for episode_id, episode in enumerate(tf_dataset): |
|
file_path = episode['episode_metadata']['file_path'].numpy().decode('utf-8') |
|
recording_folderpath = episode['episode_metadata']['recording_folderpath'].numpy().decode('utf-8') |
|
if episode_id <= skip_episode or 'success' not in file_path: |
|
print(f'skipping {episode_id}/{len(tf_dataset)}') |
|
continue |
|
left_camera = [] |
|
arm_camera = [] |
|
right_camera = [] |
|
inst = [] |
|
skip_episode = False |
|
for step_id, single_step in enumerate(episode['steps']): |
|
if single_step['language_instruction'].numpy().decode('utf-8') not in inst: |
|
inst.append(single_step['language_instruction'].numpy().decode('utf-8')) |
|
if single_step['language_instruction_2'].numpy().decode('utf-8') not in inst: |
|
inst.append(single_step['language_instruction_2'].numpy().decode('utf-8')) |
|
if single_step['language_instruction_3'].numpy().decode('utf-8') not in inst: |
|
inst.append(single_step['language_instruction_3'].numpy().decode('utf-8')) |
|
if len(inst) == 1 and inst[0] == '': |
|
skip_episode = True |
|
break |
|
left_camera.append(single_step['observation']['exterior_image_1_left'].numpy()) |
|
right_camera.append(single_step['observation']['exterior_image_2_left'].numpy()) |
|
arm_camera.append(single_step['observation']['wrist_image_left'].numpy()) |
|
if skip_episode: |
|
print(f'skipping {episode_id}/{len(tf_dataset)}') |
|
continue |
|
print(f'saving {episode_id}/{len(tf_dataset)}') |
|
save_video(f'droid_videos/episode_{episode_id}_left_camera.mp4', left_camera) |
|
save_video(f'droid_videos/episode_{episode_id}_right_camera.mp4', right_camera) |
|
save_video(f'droid_videos/episode_{episode_id}_arm_camera.mp4', arm_camera) |
|
for i in range(len(inst)): |
|
if inst[i] == '': |
|
continue |
|
js_data.append({"path": f'droid_videos/episode_{episode_id}_left_camera.mp4', "recording_folder": recording_folderpath, "cap": [inst[i]]}) |
|
js_data.append({"path": f'droid_videos/episode_{episode_id}_right_camera.mp4', "recording_folder": recording_folderpath, "cap": [inst[i]]}) |
|
js_data.append({"path": f'droid_videos/episode_{episode_id}_arm_camera.mp4', "recording_folder": recording_folderpath, "cap": [inst[i]]}) |
|
if episode_id % 1000 < 10: |
|
json.dump(js_data, open(js_path, 'w'), indent=4) |
|
json.dump(js_data, open(js_path, 'w'), indent=4) |
|
|