File size: 7,105 Bytes
e54e757 2943808 e54e757 2943808 e54e757 4250afc e54e757 6d03b8b 2943808 4250afc 2943808 3433893 6d03b8b 2943808 7913792 6c0195c 1c5960f 2943808 4250afc 2943808 00cdda2 2943808 00cdda2 2943808 e54e757 2943808 e54e757 2943808 e54e757 2943808 e54e757 6d03b8b e54e757 32d058f e54e757 3433893 e54e757 6d03b8b e54e757 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
from PIL import Image
import os
import cv2
import numpy as np
from PIL import Image
from moviepy.editor import *
import gradio as gr
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
import torch
torch.backends.cuda.matmul.allow_tf32 = True
import gc
controlnet = ControlNetModel.from_pretrained("ioclab/control_v1p_sd15_brightness", torch_dtype=torch.float16, use_safetensors=True)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
controlnet=controlnet,
torch_dtype=torch.float16,
safety_checker=None,
)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_xformers_memory_efficient_attention()
pipe.enable_model_cpu_offload()
pipe.enable_attention_slicing()
def get_frames(video_in):
frames = []
#resize the video
clip = VideoFileClip(video_in)
#check fps
if clip.fps > 30:
print("vide rate is over 30, resetting to 30")
clip_resized = clip.resize(height=512)
clip_resized.write_videofile("video_resized.mp4", fps=30)
else:
print("video rate is OK")
clip_resized = clip.resize(height=512)
clip_resized.write_videofile("video_resized.mp4", fps=clip.fps)
print("video resized to 512 height")
# Opens the Video file with CV2
cap= cv2.VideoCapture("video_resized.mp4")
fps = cap.get(cv2.CAP_PROP_FPS)
print("video fps: " + str(fps))
i=0
while(cap.isOpened()):
ret, frame = cap.read()
if ret == False:
break
cv2.imwrite('kang'+str(i)+'.jpg',frame)
frames.append('kang'+str(i)+'.jpg')
i+=1
cap.release()
cv2.destroyAllWindows()
print("broke the video into frames")
return frames, fps
def create_video(frames, fps):
print("building video result")
clip = ImageSequenceClip(frames, fps=fps)
clip.write_videofile("_result.mp4", fps=fps)
return "_result.mp4"
def process_brightness(
prompt,
negative_prompt,
conditioning_image,
num_inference_steps=30,
size=512,
guidance_scale=7.0,
seed=1234,
):
conditioning_image_raw = Image.fromarray(conditioning_image)
conditioning_image = conditioning_image_raw.convert('L')
g_cpu = torch.Generator()
if seed == -1:
generator = g_cpu.manual_seed(g_cpu.seed())
else:
generator = g_cpu.manual_seed(seed)
output_image = pipe(
prompt,
conditioning_image,
height=size,
width=size,
num_inference_steps=num_inference_steps,
generator=generator,
negative_prompt=negative_prompt,
guidance_scale=guidance_scale,
controlnet_conditioning_scale=1.0,
).images[0]
del conditioning_image, conditioning_image_raw
gc.collect()
return output_image
def infer(video_in, trim_value, prompt,
negative_prompt,
num_inference_steps=30,
size=512,
guidance_scale=7.0,
seed=1234
):
# 1. break video into frames and get FPS
break_vid = get_frames(video_in)
frames_list= break_vid[0]
fps = break_vid[1]
n_frame = int(trim_value * fps)
#n_frame = len(frames_list)
if n_frame >= len(frames_list):
print("video is shorter than the cut value")
n_frame = len(frames_list)
# 2. prepare frames result arrays
result_frames = []
print("set stop frames to: " + str(n_frame))
for i, image in enumerate(frames_list[0:int(n_frame)]):
conditioning_image = Image.open(image).convert("RGB")
conditioning_image = np.array(conditioning_image)
output_frame = process_brightness(
prompt,
negative_prompt,
conditioning_image,
num_inference_steps=30,
size=512,
guidance_scale=7.0,
seed=1234
)
print(output_frame)
#image = Image.open(output_frame)
#image = Image.fromarray(output_frame[0])
output_frame.save("_frame_" + str(i) + ".jpeg")
result_frames.append("_frame_" + str(i) + ".jpeg")
print("frame " + str(i) + "/" + str(n_frame) + ": done;")
final_vid = create_video(result_frames, fps)
return final_vid
with gr.Blocks() as demo:
gr.Markdown(
"""
# ControlNet on Brightness • Video
This is a demo on ControlNet based on brightness for video.
""")
with gr.Row():
with gr.Column():
prompt = gr.Textbox(
label="Prompt",
)
negative_prompt = gr.Textbox(
label="Negative Prompt",
)
video_in = gr.Video(
label="Conditioning Video",
source="upload",
type="filepath"
)
trim_in = gr.Slider(label="Cut video at (s)", minimun=1, maximum=5, step=1, value=1)
with gr.Accordion('Advanced options', open=False):
with gr.Row():
num_inference_steps = gr.Slider(
10, 40, 20,
step=1,
label="Steps",
)
size = gr.Slider(
256, 768, 512,
step=128,
label="Size",
)
with gr.Row():
guidance_scale = gr.Slider(
label='Guidance Scale',
minimum=0.1,
maximum=30.0,
value=7.0,
step=0.1
)
seed = gr.Slider(
label='Seed',
value=-1,
minimum=-1,
maximum=2147483647,
step=1,
# randomize=True
)
submit_btn = gr.Button(
value="Submit",
variant="primary"
)
with gr.Column(min_width=300):
output = gr.Video(
label="Result",
)
submit_btn.click(
fn=infer,
inputs=[
video_in, trim_in, prompt, negative_prompt, num_inference_steps, size, guidance_scale, seed
],
outputs=output
)
gr.Markdown(
"""
* [Dataset](https://huggingface.co/datasets/ioclab/grayscale_image_aesthetic_3M)
* [Diffusers model](https://huggingface.co/ioclab/control_v1p_sd15_brightness), [Web UI model](https://huggingface.co/ioclab/ioc-controlnet)
* [Training Report](https://api.wandb.ai/links/ciaochaos/oot5cui2), [Doc(Chinese)](https://aigc.ioclab.com/sd-showcase/brightness-controlnet.html)
""")
demo.launch() |