39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
from concurrent.futures import ThreadPoolExecutor
|
|
from pathlib import Path
|
|
|
|
import numpy
|
|
import PIL
|
|
import torch
|
|
|
|
|
|
def concatenate_episodes(ep_dicts):
|
|
data_dict = {}
|
|
|
|
keys = ep_dicts[0].keys()
|
|
for key in keys:
|
|
if torch.is_tensor(ep_dicts[0][key][0]):
|
|
data_dict[key] = torch.cat([ep_dict[key] for ep_dict in ep_dicts])
|
|
else:
|
|
if key not in data_dict:
|
|
data_dict[key] = []
|
|
for ep_dict in ep_dicts:
|
|
for x in ep_dict[key]:
|
|
data_dict[key].append(x)
|
|
|
|
total_frames = data_dict["frame_index"].shape[0]
|
|
data_dict["index"] = torch.arange(0, total_frames, 1)
|
|
return data_dict
|
|
|
|
|
|
def save_images_concurrently(imgs_array: numpy.array, out_dir: Path, max_workers: int = 4):
|
|
out_dir = Path(out_dir)
|
|
out_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
def save_image(img_array, i, out_dir):
|
|
img = PIL.Image.fromarray(img_array)
|
|
img.save(str(out_dir / f"frame_{i:06d}.png"), quality=100)
|
|
|
|
num_images = len(imgs_array)
|
|
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
|
[executor.submit(save_image, imgs_array[i], i, out_dir) for i in range(num_images)]
|