Spaces:
Running
Running
import gradio as gr | |
from PIL import Image | |
import numpy as np | |
import cv2 | |
import os | |
def image_to_video(image): | |
# PIL μ΄λ―Έμ§λ₯Ό NumPy λ°°μ΄λ‘ λ³ννκ³ , RGB νμμΌλ‘ λ³ν | |
image_array = np.array(image.convert('RGB')) | |
# OpenCVλ BGR νμμ μ¬μ©νλ―λ‘ RGBμμ BGRλ‘ μμ μ±λμ μ¬μ λ ¬ | |
image_array = cv2.cvtColor(image_array, cv2.COLOR_RGB2BGR) | |
output_path = 'output_video.mp4' | |
if not os.path.exists('output'): | |
os.makedirs('output') | |
output_path = os.path.join('output', output_path) | |
height, width, layers = image_array.shape | |
size = (width, height) | |
fourcc = cv2.VideoWriter_fourcc(*'mp4v') | |
video = cv2.VideoWriter(output_path, fourcc, 30, size) | |
if not video.isOpened(): | |
print("λΉλμ€ μμ±μκ° νμΌμ μ΄μ§ λͺ»νμ΅λλ€.") | |
return None | |
for _ in range(150): # μ΄ 150 νλ μ μμ± | |
video.write(image_array) | |
video.release() | |
return output_path | |
def setup_interface(): | |
with gr.Blocks() as demo: | |
gr.Markdown("### μ΄λ―Έμ§λ₯Ό μ λ‘λνλ©΄ 5μ΄μ§λ¦¬ λΉλμ€λ₯Ό μμ±ν©λλ€.") | |
with gr.Row(): | |
image_input = gr.Image(type="pil") | |
video_output = gr.Video(label="μμ±λ λΉλμ€") | |
image_input.change(image_to_video, inputs=image_input, outputs=video_output) | |
return demo | |
demo = setup_interface() | |
demo.launch() | |