fix pingpong, new node combine image background + overlay

This commit is contained in:
justumen
2024-08-02 10:41:14 +02:00
parent 1b6bfd3d18
commit c8bb3fa835
5 changed files with 137 additions and 11 deletions

View File

@@ -1,4 +1,8 @@
import torch
import os
import shutil
from PIL import Image
import numpy as np
class VideoPingPong:
@classmethod
@@ -14,10 +18,55 @@ class VideoPingPong:
CATEGORY = "Bjornulf"
def pingpong_images(self, images):
if isinstance(images, torch.Tensor):
reversed_images = torch.flip(images, [0])
combined_images = torch.cat((images, reversed_images[1:]), dim=0)
else:
reversed_images = images[::-1]
combined_images = images + reversed_images[1:]
return (combined_images,)
# Create a clean folder to store the images
temp_dir = "temp_pingpong"
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
os.makedirs(temp_dir)
try:
# Save each image in the temporary directory
num_images = images.shape[0]
for i in range(num_images):
img_tensor = images[i]
img_pil = Image.fromarray((img_tensor.cpu().numpy() * 255).astype('uint8'))
img_path = os.path.join(temp_dir, f"image_{i:04d}.png")
img_pil.save(img_path)
# Create the pingpong sequence
pingpong_list = list(range(num_images)) + list(range(num_images - 2, 0, -1))
# Process images in batches
batch_size = 10
pingpong_tensors = []
for i in range(0, len(pingpong_list), batch_size):
batch = pingpong_list[i:i+batch_size]
batch_tensors = []
for j in batch:
img_path = os.path.join(temp_dir, f"image_{j:04d}.png")
img_pil = Image.open(img_path)
img_np = np.array(img_pil).astype(np.float32) / 255.0
img_tensor = torch.from_numpy(img_np)
batch_tensors.append(img_tensor)
# Close the image to free up memory
img_pil.close()
# Stack the batch tensors
batch_tensor = torch.stack(batch_tensors)
pingpong_tensors.append(batch_tensor)
# Clear unnecessary variables
del batch_tensors
torch.cuda.empty_cache()
# Concatenate all batches
pingpong_tensor = torch.cat(pingpong_tensors, dim=0)
finally:
# Clean up the temporary directory
shutil.rmtree(temp_dir)
return (pingpong_tensor,)