File size: 4,018 Bytes
9235940 |
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 |
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):
# Utility to decode encoded language instructions
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 # 2Mbps
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)
# Flush stream
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)
|