Spaces:
Running
Running
Update app.py
Browse files12345678910112 unmodified lines141516171819202122232425262728293031323334353637383940import gradio as grimport cv2from moviepy.editor import VideoFileClip, ImageSequenceClipimport numpy as npfrom diffusers import AutoPipelineForImage2Imagefrom diffusers.utils import load_image# Load the anime-style modelpipe = AutoPipelineForImage2Image.from_pretrained("nitrosocke/Arcane-Diffusion",safety_checker=None,2 unmodified lines# Function to process a single framedef process_frame(frame, prompt):# Convert frame from BGR (OpenCV) to RGBframe = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)# Load the frame as an image for the modelimage = load_image(frame)# Apply the anime-style transformationresult = pipe(prompt=prompt, image=image, strength=0.75).images[0]return np.array(result)# Function to convert the entire videodef video_to_anime(video_path, prompt="Arcane style"):# Load the video and extract framesclip = VideoFileClip(video_path)frames = [frame for frame in clip.iter_frames()]# Process each frame with the anime-style modelprocessed_frames = [process_frame(frame, prompt) for frame in frames]# Reassemble the processed frames into a videonew_clip = ImageSequenceClip(processed_frames, fps=clip.fps)output_path = "output.mp4"new_clip.write_videofile(output_path, codec="libx264")return output_path123456789102 unmodified lines1314151617181920212223242526272829303132333435363738394041424344454647484950515253import gradio as grimport cv2import numpy as npfrom diffusers import AutoPipelineForImage2Imagefrom diffusers.utils import load_image# Load the anime-style diffusion modelpipe = AutoPipelineForImage2Image.from_pretrained("nitrosocke/Arcane-Diffusion",safety_checker=None,2 unmodified lines# Function to process a single framedef process_frame(frame, prompt):# Convert frame from BGR (OpenCV format) to RGB (expected by the model)frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)# Load the frame as an image for the diffusion modelimage = load_image(frame_rgb)# Apply the anime-style transformationresult = pipe(prompt=prompt, image=image, strength=0.75).images[0]# Convert back to numpy arrayreturn np.array(result)# Function to convert the entire videodef video_to_anime(video_path, prompt="Arcane style"):# Read the input video using OpenCVcap = cv2.VideoCapture(video_path)fps = cap.get(cv2.CAP_PROP_FPS)frames = []while cap.isOpened():ret, frame = cap.read()if not ret:breakframes.append(frame)cap.release()# Process each frame to anime styleprocessed_frames = [process_frame(frame, prompt) for frame in frames]# Write the output video using OpenCVheight, width, _ = processed_frames[0].shapefourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for MP4output_path = "output.mp4"out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))for frame in processed_frames:# Convert back to BGR for OpenCVframe_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)out.write(frame_bgr)out.release()return output_path
app.py+26-13