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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from moviepy.editor import *
|
| 8 |
+
from share_btn import community_icon_html, loading_icon_html, share_js
|
| 9 |
+
|
| 10 |
+
import pathlib
|
| 11 |
+
import shlex
|
| 12 |
+
import subprocess
|
| 13 |
+
|
| 14 |
+
if os.getenv('SYSTEM') == 'spaces':
|
| 15 |
+
with open('patch') as f:
|
| 16 |
+
subprocess.run(shlex.split('patch -p1'), stdin=f, cwd='ControlNet')
|
| 17 |
+
|
| 18 |
+
base_url = 'https://huggingface.co/lllyasviel/ControlNet/resolve/main/annotator/ckpts/'
|
| 19 |
+
|
| 20 |
+
names = [
|
| 21 |
+
'body_pose_model.pth',
|
| 22 |
+
'dpt_hybrid-midas-501f0c75.pt',
|
| 23 |
+
'hand_pose_model.pth',
|
| 24 |
+
'mlsd_large_512_fp32.pth',
|
| 25 |
+
'mlsd_tiny_512_fp32.pth',
|
| 26 |
+
'network-bsds500.pth',
|
| 27 |
+
'upernet_global_small.pth',
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
for name in names:
|
| 31 |
+
command = f'wget https://huggingface.co/lllyasviel/ControlNet/resolve/main/annotator/ckpts/{name} -O {name}'
|
| 32 |
+
out_path = pathlib.Path(f'ControlNet/annotator/ckpts/{name}')
|
| 33 |
+
if out_path.exists():
|
| 34 |
+
continue
|
| 35 |
+
subprocess.run(shlex.split(command), cwd='ControlNet/annotator/ckpts/')
|
| 36 |
+
|
| 37 |
+
from model import Model
|
| 38 |
+
model = Model()
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def controlnet(i, prompt, seed_in):
|
| 42 |
+
a_prompt = "best quality, extremely detailed"
|
| 43 |
+
n_prompt = "longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality"
|
| 44 |
+
num_samples = 1
|
| 45 |
+
image_resolution = 512
|
| 46 |
+
detect_resolution = 512
|
| 47 |
+
ddim_steps = 20
|
| 48 |
+
scale = 9.0
|
| 49 |
+
eta = 0.0
|
| 50 |
+
|
| 51 |
+
result = model.process_pose(i, prompt, a_prompt, n_prompt, num_samples,
|
| 52 |
+
image_resolution, detect_resolution, ddim_steps, scale, seed_in, eta)
|
| 53 |
+
print(result)
|
| 54 |
+
return 'done'
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def get_frames(video_in):
|
| 58 |
+
frames = []
|
| 59 |
+
#resize the video
|
| 60 |
+
clip = VideoFileClip(video_in)
|
| 61 |
+
|
| 62 |
+
#check fps
|
| 63 |
+
if clip.fps > 30:
|
| 64 |
+
print("vide rate is over 30, resetting to 30")
|
| 65 |
+
clip_resized = clip.resize(height=512)
|
| 66 |
+
clip_resized.write_videofile("video_resized.mp4", fps=30)
|
| 67 |
+
else:
|
| 68 |
+
print("video rate is OK")
|
| 69 |
+
clip_resized = clip.resize(height=512)
|
| 70 |
+
clip_resized.write_videofile("video_resized.mp4", fps=clip.fps)
|
| 71 |
+
|
| 72 |
+
print("video resized to 512 height")
|
| 73 |
+
|
| 74 |
+
# Opens the Video file with CV2
|
| 75 |
+
cap= cv2.VideoCapture("video_resized.mp4")
|
| 76 |
+
|
| 77 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 78 |
+
print("video fps: " + str(fps))
|
| 79 |
+
i=0
|
| 80 |
+
while(cap.isOpened()):
|
| 81 |
+
ret, frame = cap.read()
|
| 82 |
+
if ret == False:
|
| 83 |
+
break
|
| 84 |
+
cv2.imwrite('kang'+str(i)+'.jpg',frame)
|
| 85 |
+
frames.append('kang'+str(i)+'.jpg')
|
| 86 |
+
i+=1
|
| 87 |
+
|
| 88 |
+
cap.release()
|
| 89 |
+
cv2.destroyAllWindows()
|
| 90 |
+
print("broke the video into frames")
|
| 91 |
+
|
| 92 |
+
return frames, fps
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def create_video(frames, fps):
|
| 96 |
+
print("building video result")
|
| 97 |
+
clip = ImageSequenceClip(frames, fps=fps)
|
| 98 |
+
clip.write_videofile("movie.mp4", fps=fps)
|
| 99 |
+
|
| 100 |
+
return 'movie.mp4'
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def infer(prompt,video_in, seed_in, trim_value):
|
| 104 |
+
print(f"""
|
| 105 |
+
βββββββββββββββ
|
| 106 |
+
{prompt}
|
| 107 |
+
βββββββββββββββ""")
|
| 108 |
+
|
| 109 |
+
# 1. break video into frames and get FPS
|
| 110 |
+
break_vid = get_frames(video_in)
|
| 111 |
+
frames_list= break_vid[0]
|
| 112 |
+
fps = break_vid[1]
|
| 113 |
+
n_frame = int(trim_value*fps)
|
| 114 |
+
|
| 115 |
+
if n_frame >= len(frames_list):
|
| 116 |
+
print("video is shorter than the cut value")
|
| 117 |
+
n_frame = len(frames_list)
|
| 118 |
+
|
| 119 |
+
# 2. prepare frames result array
|
| 120 |
+
result_frames = []
|
| 121 |
+
print("set stop frames to: " + str(n_frame))
|
| 122 |
+
|
| 123 |
+
for i in frames_list[0:int(n_frame)]:
|
| 124 |
+
controlnet_img = controlnet(i, prompt, seed_in)
|
| 125 |
+
#images = controlnet_img[0]
|
| 126 |
+
#rgb_im = images[0].convert("RGB")
|
| 127 |
+
|
| 128 |
+
# exporting the image
|
| 129 |
+
#rgb_im.save(f"result_img-{i}.jpg")
|
| 130 |
+
#result_frames.append(f"result_img-{i}.jpg")
|
| 131 |
+
#print("frame " + i + "/" + str(n_frame) + ": done;")
|
| 132 |
+
|
| 133 |
+
#final_vid = create_video(result_frames, fps)
|
| 134 |
+
print("finished !")
|
| 135 |
+
|
| 136 |
+
#return final_vid, gr.Group.update(visible=True)
|
| 137 |
+
return controlnet_img
|
| 138 |
+
|
| 139 |
+
title = """
|
| 140 |
+
<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
| 141 |
+
<div
|
| 142 |
+
style="
|
| 143 |
+
display: inline-flex;
|
| 144 |
+
align-items: center;
|
| 145 |
+
gap: 0.8rem;
|
| 146 |
+
font-size: 1.75rem;
|
| 147 |
+
"
|
| 148 |
+
>
|
| 149 |
+
<h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
|
| 150 |
+
Pix2Pix Video
|
| 151 |
+
</h1>
|
| 152 |
+
</div>
|
| 153 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
| 154 |
+
Apply Instruct Pix2Pix Diffusion to a video
|
| 155 |
+
</p>
|
| 156 |
+
</div>
|
| 157 |
+
"""
|
| 158 |
+
|
| 159 |
+
article = """
|
| 160 |
+
|
| 161 |
+
<div class="footer">
|
| 162 |
+
<p>
|
| 163 |
+
Examples by <a href="https://twitter.com/CitizenPlain" target="_blank">Nathan Shipley</a> β’
|
| 164 |
+
Follow <a href="https://twitter.com/fffiloni" target="_blank">Sylvain Filoni</a> for future updates π€
|
| 165 |
+
</p>
|
| 166 |
+
</div>
|
| 167 |
+
<div id="may-like-container" style="display: flex;justify-content: center;flex-direction: column;align-items: center;margin-bottom: 30px;">
|
| 168 |
+
<p>You may also like: </p>
|
| 169 |
+
<div id="may-like-content" style="display:flex;flex-wrap: wrap;align-items:center;height:20px;">
|
| 170 |
+
|
| 171 |
+
<svg height="20" width="162" style="margin-left:4px;margin-bottom: 6px;">
|
| 172 |
+
<a href="https://huggingface.co/spaces/timbrooks/instruct-pix2pix" target="_blank">
|
| 173 |
+
<image href="https://img.shields.io/badge/π€ Spaces-Instruct_Pix2Pix-blue" src="https://img.shields.io/badge/π€ Spaces-Instruct_Pix2Pix-blue.png" height="20"/>
|
| 174 |
+
</a>
|
| 175 |
+
</svg>
|
| 176 |
+
|
| 177 |
+
</div>
|
| 178 |
+
|
| 179 |
+
</div>
|
| 180 |
+
|
| 181 |
+
"""
|
| 182 |
+
|
| 183 |
+
with gr.Blocks(css='style.css') as demo:
|
| 184 |
+
with gr.Column(elem_id="col-container"):
|
| 185 |
+
gr.HTML(title)
|
| 186 |
+
with gr.Row():
|
| 187 |
+
with gr.Column():
|
| 188 |
+
video_inp = gr.Video(label="Video source", source="upload", type="filepath", elem_id="input-vid")
|
| 189 |
+
prompt = gr.Textbox(label="Prompt", placeholder="enter prompt", show_label=False, elem_id="prompt-in")
|
| 190 |
+
with gr.Row():
|
| 191 |
+
seed_inp = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, value=123456)
|
| 192 |
+
trim_in = gr.Slider(label="Cut video at (s)", minimun=1, maximum=5, step=1, value=1)
|
| 193 |
+
with gr.Column():
|
| 194 |
+
status = gr.Textbox()
|
| 195 |
+
video_out = gr.Video(label="Pix2pix video result", elem_id="video-output")
|
| 196 |
+
gr.HTML("""
|
| 197 |
+
<a style="display:inline-block" href="https://huggingface.co/spaces/fffiloni/Pix2Pix-Video?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a>
|
| 198 |
+
work with longer videos / skip the queue:
|
| 199 |
+
""", elem_id="duplicate-container")
|
| 200 |
+
submit_btn = gr.Button("Generate Pix2Pix video")
|
| 201 |
+
|
| 202 |
+
with gr.Group(elem_id="share-btn-container", visible=False) as share_group:
|
| 203 |
+
community_icon = gr.HTML(community_icon_html)
|
| 204 |
+
loading_icon = gr.HTML(loading_icon_html)
|
| 205 |
+
share_button = gr.Button("Share to community", elem_id="share-btn")
|
| 206 |
+
|
| 207 |
+
inputs = [prompt,video_inp,seed_inp, trim_in]
|
| 208 |
+
#outputs = [video_out, share_group]
|
| 209 |
+
outputs = [status]
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
gr.HTML(article)
|
| 213 |
+
|
| 214 |
+
submit_btn.click(infer, inputs, outputs)
|
| 215 |
+
share_button.click(None, [], [], _js=share_js)
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
demo.launch().queue(max_size=12)
|