Spaces:
Runtime error
Runtime error
File size: 2,503 Bytes
6ed89af |
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 |
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") |