Spaces:
Runtime error
Runtime error
from PIL import Image, ImageFilter | |
import random | |
import os | |
from pathlib import Path | |
import cv2 | |
import numpy as np | |
from modules.utils import image_frame_generator | |
class FrameVideoCreator: | |
def __init__(self, fps: int = 30): | |
""" | |
FrameVideoCreator ใฏใฉในใฎๅๆๅ้ขๆฐ | |
ๅผๆฐ: | |
- fps: ๅ็ปใฎใใฌใผใ ใฌใผใ (ใใใฉใซใใฏ30FPS) | |
""" | |
self.fps = fps | |
# ๆ้ ๏ผไธใใไธ๏ผใฎใจใใงใฏใใๅใใซ้ฉ็จ | |
self.ascending_order = True | |
def create_video_from_frames(self, frames: list, output_path: str): | |
""" | |
ไธใใใใ็ปๅใใฌใผใ ใฎใชในใใใๅ็ปใไฝๆใใพใใๅ็ปใใจใซๆ้ ใจ้้ ใฎใจใใงใฏใใไบคไบใซ้ฉ็จใใพใใ | |
ๅผๆฐ: | |
- frames: ็ปๅใใฌใผใ ใฎใชในใ | |
- output_path: ไฟๅญใใๅ็ปใฎใใน | |
""" | |
frames_bgr = [cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR) for frame in frames] | |
# ๆ้ ใ้้ ใฎใจใใงใฏใใ้ฉ็จใใใใซๅบใฅใใฆใใใฌใผใ ใฎ้ ็ชใๅคๆด | |
if not self.ascending_order: | |
frames_bgr = list(reversed(frames_bgr)) | |
# ๆฌกๅใฎๅ็ป็ๆใฎใใใซใจใใงใฏใใฎ้ ็ชใๅใๆฟใ | |
self.ascending_order = not self.ascending_order | |
height, width, layers = frames_bgr[0].shape | |
size = (width, height) | |
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'MP4V'), self.fps, size) | |
for frame in frames_bgr: | |
out.write(frame) | |
out.release() | |
def process_folder(self, input_folder: str): | |
""" | |
ๆๅฎใใใใฉใซใๅ ใฎ็ปๅใๅฆ็ใใๅ็ปใ็ๆใใพใใ | |
ๅผๆฐ: | |
- input_folder: ็ปๅใไฟๅญใใใฆใใใใฉใซใใฎใใน | |
""" | |
output_folder = f"{input_folder}_mov" | |
Path(output_folder).mkdir(parents=True, exist_ok=True) | |
for image_file in Path(input_folder).glob("*.png"): | |
frames = image_frame_generator.generate_centered_moving_frames(str(image_file)) | |
output_video_path = Path(output_folder) / f"{image_file.stem}.mp4" | |
self.create_video_from_frames(frames, str(output_video_path)) | |
if __name__ == '__main__': | |
# ใฏใฉในใฎไฝฟ็จไพ | |
video_creator = FrameVideoCreator() | |
video_creator.process_folder(r"image/Echoes-of-Creation_Blurred") |