Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
from moviepy.editor import VideoFileClip, ImageSequenceClip
|
| 4 |
+
import numpy as np
|
| 5 |
+
from diffusers import AutoPipelineForImage2Image
|
| 6 |
+
from diffusers.utils import load_image
|
| 7 |
+
|
| 8 |
+
# Load the anime-style model
|
| 9 |
+
pipe = AutoPipelineForImage2Image.from_pretrained(
|
| 10 |
+
"nitrosocke/Arcane-Diffusion",
|
| 11 |
+
safety_checker=None,
|
| 12 |
+
)
|
| 13 |
+
pipe.to("cuda")
|
| 14 |
+
|
| 15 |
+
# Function to process a single frame
|
| 16 |
+
def process_frame(frame, prompt):
|
| 17 |
+
# Convert frame from BGR (OpenCV) to RGB
|
| 18 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 19 |
+
# Load the frame as an image for the model
|
| 20 |
+
image = load_image(frame)
|
| 21 |
+
# Apply the anime-style transformation
|
| 22 |
+
result = pipe(prompt=prompt, image=image, strength=0.75).images[0]
|
| 23 |
+
return np.array(result)
|
| 24 |
+
|
| 25 |
+
# Function to convert the entire video
|
| 26 |
+
def video_to_anime(video_path, prompt="Arcane style"):
|
| 27 |
+
# Load the video and extract frames
|
| 28 |
+
clip = VideoFileClip(video_path)
|
| 29 |
+
frames = [frame for frame in clip.iter_frames()]
|
| 30 |
+
|
| 31 |
+
# Process each frame with the anime-style model
|
| 32 |
+
processed_frames = [process_frame(frame, prompt) for frame in frames]
|
| 33 |
+
|
| 34 |
+
# Reassemble the processed frames into a video
|
| 35 |
+
new_clip = ImageSequenceClip(processed_frames, fps=clip.fps)
|
| 36 |
+
output_path = "output.mp4"
|
| 37 |
+
new_clip.write_videofile(output_path, codec="libx264")
|
| 38 |
+
|
| 39 |
+
return output_path
|
| 40 |
+
|
| 41 |
+
# Create the Gradio interface
|
| 42 |
+
iface = gr.Interface(
|
| 43 |
+
fn=video_to_anime,
|
| 44 |
+
inputs=[
|
| 45 |
+
gr.Video(label="Input Video"),
|
| 46 |
+
gr.Textbox(label="Style Prompt", default="Arcane style")
|
| 47 |
+
],
|
| 48 |
+
outputs=gr.Video(label="Output Video"),
|
| 49 |
+
title="Video to Anime Converter",
|
| 50 |
+
description="Upload a video and convert it to anime style!"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Launch the interface
|
| 54 |
+
iface.launch()
|