Spaces:
Running
Running
import gradio as gr | |
import torch | |
from transformers import pipeline | |
from huggingface_hub import hf_hub_download | |
import os | |
from PIL import Image | |
import numpy as np | |
from moviepy.editor import ImageSequenceClip | |
import time | |
# Kiểm tra xem có GPU không | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
print(f"Using device: {device}") | |
# Sử dụng mô hình text-to-video thay thế | |
def generate_video(image, prompt, num_frames=16, guidance_scale=7.5): | |
if image is None: | |
return None, "Vui lòng tải lên một hình ảnh" | |
# Đảm bảo hình ảnh là định dạng RGB | |
if image.mode != "RGB": | |
image = image.convert("RGB") | |
# Thay đổi kích thước hình ảnh về 512x512 | |
image = image.resize((512, 512)) | |
try: | |
# Lưu hình ảnh tạm thời | |
image_path = "input_image.jpg" | |
image.save(image_path) | |
# Tạo chuỗi khung hình đơn giản (hiệu ứng zoom-in) | |
frames = [] | |
for i in range(num_frames): | |
# Tạo hiệu ứng zoom | |
zoom_factor = 1.0 + (i / num_frames) * 0.2 | |
img_copy = image.copy() | |
# Tính toán kích thước và vị trí crop | |
size = int(img_copy.width / zoom_factor) | |
left = (img_copy.width - size) // 2 | |
top = (img_copy.height - size) // 2 | |
right = left + size | |
bottom = top + size | |
# Cắt và resize lại hình ảnh | |
cropped = img_copy.crop((left, top, right, bottom)) | |
frame = cropped.resize((512, 512)) | |
# Thêm hiệu ứng màu dựa theo prompt | |
# (đơn giản hóa - trong thực tế sẽ sử dụng mô hình ML) | |
frames.append(np.array(frame)) | |
# Tạo video từ các khung hình | |
clip = ImageSequenceClip(frames, fps=8) | |
output_path = f"output_video_{int(time.time())}.mp4" | |
clip.write_videofile(output_path, codec="libx264") | |
return output_path, "Video đã được tạo thành công!" | |
except Exception as e: | |
return None, f"Lỗi: {str(e)}" | |
# Tạo giao diện Gradio | |
with gr.Blocks(title="Ứng dụng tạo video từ hình ảnh") as demo: | |
gr.Markdown("# Tạo video từ hình ảnh") | |
gr.Markdown("Tải lên một hình ảnh và nhập mô tả để tạo hiệu ứng chuyển động đơn giản.") | |
with gr.Row(): | |
with gr.Column(): | |
image_input = gr.Image(type="pil", label="Tải lên hình ảnh") | |
prompt_input = gr.Textbox(label="Mô tả hiệu ứng", | |
placeholder="Hiệu ứng bạn muốn...", | |
value="Zoom in effect with cinematic lighting") | |
num_frames = gr.Slider(minimum=10, maximum=30, value=16, step=2, label="Số khung hình") | |
guidance = gr.Slider(minimum=1.0, maximum=10.0, value=7.5, step=0.5, label="Độ mạnh của hiệu ứng") | |
submit_btn = gr.Button("Tạo video") | |
with gr.Column(): | |
output_video = gr.Video(label="Video kết quả") | |
output_message = gr.Textbox(label="Thông báo") | |
submit_btn.click( | |
fn=generate_video, | |
inputs=[image_input, prompt_input, num_frames, guidance], | |
outputs=[output_video, output_message] | |
) | |
gr.Markdown("### Lưu ý") | |
gr.Markdown("- Đây là phiên bản đơn giản tạo hiệu ứng zoom-in cơ bản") | |
gr.Markdown("- Kích thước hình ảnh sẽ được thay đổi về 512x512 pixel") | |
demo.launch() |