|
import cv2 |
|
import numpy as np |
|
import IPython |
|
import os |
|
|
|
|
|
num_rows = 6 |
|
|
|
output_folder = "output/output_gifs/" |
|
total_tasks = os.listdir(output_folder) |
|
|
|
videos = [cv2.VideoCapture(os.path.join(output_folder, s)) |
|
for s in total_tasks if s.endswith("mp4") and not s.startswith("grid")] |
|
num_cols = len(videos) // num_rows + 1 |
|
|
|
print(f"num_rows: {num_rows} num_cols: {num_cols}") |
|
|
|
|
|
video_width = int(videos[0].get(cv2.CAP_PROP_FRAME_WIDTH)) |
|
video_height = int(videos[0].get(cv2.CAP_PROP_FRAME_HEIGHT)) |
|
|
|
|
|
output_width = video_width * num_cols |
|
output_height = video_height * num_rows |
|
|
|
output_filename = output_folder + 'grid_video.mp4' |
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
|
output_video = cv2.VideoWriter(output_filename, fourcc, 30.0, (output_width, output_height)) |
|
|
|
|
|
max_length = 200 |
|
|
|
|
|
video_frames = [[] for _ in range(len(videos))] |
|
for i, video in enumerate((videos)): |
|
while True: |
|
ret, frame = video.read() |
|
if not ret: |
|
break |
|
video_frames[i].append(frame) |
|
if len(video_frames) == 0 : |
|
continue |
|
|
|
repeat_ratio = max_length // len(video_frames[i]) |
|
left_ratio = max_length % len(video_frames[i]) |
|
|
|
video_frames[i] = video_frames[i] * repeat_ratio |
|
video_frames[i] += video_frames[i][:left_ratio] |
|
|
|
|
|
video_frames = [v for v in video_frames if len(v) == max_length] |
|
|
|
|
|
print(len(video_frames), len(video_frames[0])) |
|
|
|
for j, video_frame in enumerate(zip(*video_frames)): |
|
output_frame = 255 * np.ones((output_height, output_width, 3), np.uint8) |
|
for i, frame in enumerate(video_frame): |
|
|
|
|
|
|
|
row = i // num_cols |
|
col = i % num_cols |
|
|
|
|
|
x = col * (video_width ) |
|
y = row * (video_height ) |
|
|
|
|
|
output_frame[y:y+frame.shape[0], x:x+frame.shape[1]] = frame |
|
output_video.write(output_frame) |
|
|
|
output_video.release() |
|
zoomed_output_filename = output_folder + 'grid_video_zoomed.mp4' |
|
output_video = cv2.VideoCapture(output_filename) |
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
|
grid_video = cv2.VideoWriter(zoomed_output_filename, fourcc, 30.0, (video_width, video_height)) |
|
|
|
|
|
stop = 50 |
|
|
|
|
|
for idx in range(max_length): |
|
if idx < stop: |
|
ratio = 0.2 |
|
else: |
|
ratio = 0.2 + 0.8 * float(idx - stop) / (max_length - stop) |
|
|
|
ret, frame = output_video.read() |
|
if not ret: |
|
break |
|
|
|
|
|
center = frame.shape[0] // 2, frame.shape[1] // 2 |
|
size = int(ratio * center[0]), int(ratio * center[1]) |
|
zoomed_frame = frame[center[0]-size[0]:center[0]+size[0],center[1]-size[1]:center[1]+size[1]] |
|
|
|
|
|
|
|
resized_image = cv2.resize(zoomed_frame, (video_width, video_height)) |
|
|
|
|
|
cv2.imshow('Zoom Out Grid', resized_image) |
|
grid_video.write(resized_image) |
|
|
|
|
|
if cv2.waitKey(1) & 0xFF == ord('q'): |
|
break |
|
|
|
|
|
grid_video.release() |
|
output_video.release() |
|
cv2.destroyAllWindows() |