JoPmt commited on
Commit
76ce7ec
·
1 Parent(s): 5b0f0e1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -0
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
2
+ import gradio as gr
3
+ from PIL import Image
4
+ import cv2
5
+ import os, random, gc
6
+ import numpy as np
7
+ from transformers import pipeline
8
+ import PIL.Image
9
+ from diffusers.utils import load_image, export_to_video
10
+ from accelerate import Accelerator
11
+ from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel, EulerDiscreteScheduler
12
+ import torch
13
+ from moviepy.video.fx.all import crop
14
+ from diffusers.utils import export_to_gif
15
+ import mediapy
16
+ from image_tools.sizes import resize_and_crop
17
+ from moviepy.editor import *
18
+ from pathlib import Path
19
+ from typing import Optional, List
20
+ from tqdm import tqdm
21
+ import supervision as sv
22
+
23
+
24
+ accelerator = Accelerator(cpu=True)
25
+ pipe = accelerator.prepare(StableDiffusionControlNetImg2ImgPipeline.from_pretrained("stabilityai/sdxl-turbo", controlnet=controlnet, torch_dtype=torch.bfloat16, use_safetensors=False, variant="fp16", safety_checker=None))
26
+ pipe.unet.to(memory_format=torch.channels_last)
27
+ pipe.scheduler = accelerator.prepare(EulerDiscreteScheduler.from_config(pipe.scheduler.config))
28
+ pipe = pipe.to("cpu")
29
+
30
+ controlnet = accelerator.prepare(ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.bfloat16))
31
+ def plex(fpath, text, neg_prompt, one, two, three, four, five):
32
+ gc.collect()
33
+ prompt = text
34
+ video = './video.mp4'
35
+ orvid = './orvid.mp4'
36
+ canvid = './canvid.mp4'
37
+ frames = []
38
+ canframes = []
39
+ orframes = []
40
+ fin_frames = []
41
+ max_frames=0
42
+ cap = cv2.VideoCapture(fpath)
43
+ clip = VideoFileClip(fpath)
44
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
45
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
46
+ fps = cap.get(cv2.CAP_PROP_FPS)
47
+ aspect = width / height
48
+ if aspect == 1 and height >= 512:
49
+ nwidth = 512
50
+ nheight = 512
51
+ prep = clip.resize(height=nheight)
52
+ left = 0
53
+ top = 0
54
+ right = 512
55
+ bottom = 512
56
+ if aspect > 1 and height >= 512:
57
+ nheight = 512
58
+ nwidth = int(nheight * aspect)
59
+ prep = clip.resize(height=nheight)
60
+ left = (nwidth - width) / 2
61
+ top = 0
62
+ right = (nwidth + width) / 2
63
+ bottom = nheight
64
+ if aspect < 1 and width >= 512:
65
+ nwidth = 512
66
+ nheight = int(nwidth / aspect)
67
+ prep = clip.resize(height=nheight)
68
+ left = 0
69
+ top = (height - nheight) / 2
70
+ right = nwidth
71
+ bottom = (height + nheight) / 2
72
+ if aspect < 1 and width < 512:
73
+ return None
74
+ if aspect > 1 and height < 512:
75
+ return None
76
+ closer = crop(clip, x1=left, y1=top, x2=right, y2=bottom)
77
+ if fps > 33:
78
+ closer.write_videofile('./video.mp4', fps=33)
79
+ fps = 33
80
+ else:
81
+ closer.write_videofile('./video.mp4', fps=fps)
82
+ fps = fps
83
+ max_frames = int(fps * 4)
84
+ for frame in tqdm(sv.get_video_frames_generator(source_path=video,)):
85
+ frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
86
+ cap.release()
87
+ cv2.destroyAllWindows()
88
+ ncap = cv2.VideoCapture(video)
89
+ total_frames = int(ncap.get(cv2.CAP_PROP_FRAME_COUNT))
90
+ if total_frames <= 0:
91
+ return None
92
+ b = 0
93
+ if total_frames > max_frames:
94
+ max_frames = int(max_frames)
95
+ if total_frames < max_frames:
96
+ max_frames = int(total_frames)
97
+ for b in range(int(max_frames)):
98
+ frame = frames[b]
99
+ original = load_image(Image.fromarray(frame))
100
+ original.save('./image.png', 'PNG')
101
+ original = original.resize((512, 512))
102
+ original = original.convert("RGB")
103
+ original.save('./image.png', 'PNG')
104
+ orframes.append(original)
105
+ cannyimage = np.array(original)
106
+ cannyimage = cv2.Canny(cannyimage, 100, 200)
107
+ cannyimage = cannyimage[:, :, None]
108
+ cannyimage = np.concatenate([cannyimage, cannyimage, cannyimage], axis=2)
109
+ cannyimage = Image.fromarray(cannyimage)
110
+ canframes.append(cannyimage)
111
+ generator = torch.Generator(device="cpu").manual_seed(five)
112
+ imoge = pipe(prompt=prompt,image=[original],control_image=[cannyimage],guidance_scale=four,num_inference_steps=one,generator=generator,strength=two,negative_prompt=neg_prompt,controlnet_conditioning_scale=three,width=512,height=512)
113
+ fin_frames.append(imoge.images[0])
114
+ b += 1
115
+ ncap.release()
116
+ cv2.destroyAllWindows()
117
+ export_to_video(fin_frames, video, fps=fps)
118
+ export_to_video(orframes, orvid, fps=fps)
119
+ export_to_video(canframes, canvid, fps=fps)
120
+ return video, canvid, orvid
121
+
122
+ iface = gr.Interface(fn=plex, inputs=[gr.File(label="Your video",interactive=True),gr.Textbox(label="prompt"),gr.Textbox(label="neg prompt"), gr.Slider(label="num inference steps", minimum=1, step=1, maximum=5, value=2), gr.Slider(label="prompt strength", minimum=0.01, step=0.01, maximum=19.99, value=5.00), gr.Slider(label="controlnet scale", minimum=0.01, step=0.01, maximum=0.99, value=0.80), gr.Slider(label="Guidance scale", minimum=0.01, step=0.01, maximum=9.99, value=2.00), gr.Slider(label="Manual seed", minimum=0, step=32, maximum=4836928, value=0)], outputs=[gr.Video(label="final"), gr.Video(label="canny vid"), gr.Video(label="orig")],description="Running on cpu, very slow! by JoPmt.")
123
+ iface.queue(max_size=1,api_open=False)
124
+ iface.launch(max_threads=1)