Image2VideoProcessingPipelin / modules /FrameVideoCreator.py
HALU-HAL's picture
update files
6ed89af
raw
history blame
2.5 kB
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")