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|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from moviepy.editor import *
|
| 6 |
+
from share_btn import community_icon_html, loading_icon_html, share_js
|
| 7 |
+
|
| 8 |
+
xdecoder = gr.Interface.load(name="spaces/xdecoder/Instruct-X-Decoder")
|
| 9 |
+
|
| 10 |
+
def get_frames(video_in):
|
| 11 |
+
frames = []
|
| 12 |
+
#resize the video
|
| 13 |
+
clip = VideoFileClip(video_in)
|
| 14 |
+
|
| 15 |
+
#check fps
|
| 16 |
+
if clip.fps > 30:
|
| 17 |
+
print("vide rate is over 30, resetting to 30")
|
| 18 |
+
clip_resized = clip.resize(height=512)
|
| 19 |
+
clip_resized.write_videofile("video_resized.mp4", fps=30)
|
| 20 |
+
else:
|
| 21 |
+
print("video rate is OK")
|
| 22 |
+
clip_resized = clip.resize(height=512)
|
| 23 |
+
clip_resized.write_videofile("video_resized.mp4", fps=clip.fps)
|
| 24 |
+
|
| 25 |
+
print("video resized to 512 height")
|
| 26 |
+
|
| 27 |
+
# Opens the Video file with CV2
|
| 28 |
+
cap= cv2.VideoCapture("video_resized.mp4")
|
| 29 |
+
|
| 30 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 31 |
+
print("video fps: " + str(fps))
|
| 32 |
+
i=0
|
| 33 |
+
while(cap.isOpened()):
|
| 34 |
+
ret, frame = cap.read()
|
| 35 |
+
if ret == False:
|
| 36 |
+
break
|
| 37 |
+
cv2.imwrite('kang'+str(i)+'.jpg',frame)
|
| 38 |
+
frames.append('kang'+str(i)+'.jpg')
|
| 39 |
+
i+=1
|
| 40 |
+
|
| 41 |
+
cap.release()
|
| 42 |
+
cv2.destroyAllWindows()
|
| 43 |
+
print("broke the video into frames")
|
| 44 |
+
|
| 45 |
+
return frames, fps
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def create_video(frames, fps):
|
| 49 |
+
print("building video result")
|
| 50 |
+
clip = ImageSequenceClip(frames, fps=fps)
|
| 51 |
+
clip.write_videofile("movie.mp4", fps=fps)
|
| 52 |
+
|
| 53 |
+
return 'movie.mp4'
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def infer(prompt,video_in, trim_value):
|
| 57 |
+
print(prompt)
|
| 58 |
+
break_vid = get_frames(video_in)
|
| 59 |
+
|
| 60 |
+
frames_list= break_vid[0]
|
| 61 |
+
fps = break_vid[1]
|
| 62 |
+
n_frame = int(trim_value*fps)
|
| 63 |
+
|
| 64 |
+
if n_frame >= len(frames_list):
|
| 65 |
+
print("video is shorter than the cut value")
|
| 66 |
+
n_frame = len(frames_list)
|
| 67 |
+
|
| 68 |
+
result_frames = []
|
| 69 |
+
print("set stop frames to: " + str(n_frame))
|
| 70 |
+
|
| 71 |
+
for i in frames_list[0:int(n_frame)]:
|
| 72 |
+
xdecoder_img = xdecoder(i, prompt, fn_index=0)
|
| 73 |
+
res_image = xdecoder_img[0]
|
| 74 |
+
#rgb_im = images[0].convert("RGB")
|
| 75 |
+
|
| 76 |
+
# exporting the image
|
| 77 |
+
res_image.save(f"result_img-{i}.jpg")
|
| 78 |
+
result_frames.append(f"result_img-{i}.jpg")
|
| 79 |
+
print("frame " + i + "/" + str(n_frame) + ": done;")
|
| 80 |
+
|
| 81 |
+
final_vid = create_video(result_frames, fps)
|
| 82 |
+
print("finished !")
|
| 83 |
+
|
| 84 |
+
#return final_vid, gr.Group.update(visible=True)
|
| 85 |
+
return final_vid
|
| 86 |
+
|
| 87 |
+
title = """
|
| 88 |
+
<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
| 89 |
+
<div
|
| 90 |
+
style="
|
| 91 |
+
display: inline-flex;
|
| 92 |
+
align-items: center;
|
| 93 |
+
gap: 0.8rem;
|
| 94 |
+
font-size: 1.75rem;
|
| 95 |
+
"
|
| 96 |
+
>
|
| 97 |
+
<h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
|
| 98 |
+
Pix2Pix Video
|
| 99 |
+
</h1>
|
| 100 |
+
</div>
|
| 101 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
| 102 |
+
Apply Instruct X-Decoder Diffusion to a video
|
| 103 |
+
</p>
|
| 104 |
+
</div>
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
article = """
|
| 108 |
+
|
| 109 |
+
<div class="footer">
|
| 110 |
+
<p>
|
| 111 |
+
Examples by <a href="https://twitter.com/CitizenPlain" target="_blank">Nathan Shipley</a> •
|
| 112 |
+
Follow <a href="https://twitter.com/fffiloni" target="_blank">Sylvain Filoni</a> for future updates 🤗
|
| 113 |
+
</p>
|
| 114 |
+
</div>
|
| 115 |
+
<div id="may-like-container" style="display: flex;justify-content: center;flex-direction: column;align-items: center;margin-bottom: 30px;">
|
| 116 |
+
<p>You may also like: </p>
|
| 117 |
+
<div id="may-like-content" style="display:flex;flex-wrap: wrap;align-items:center;height:20px;">
|
| 118 |
+
|
| 119 |
+
<svg height="20" width="162" style="margin-left:4px;margin-bottom: 6px;">
|
| 120 |
+
<a href="https://huggingface.co/spaces/timbrooks/instruct-pix2pix" target="_blank">
|
| 121 |
+
<image href="https://img.shields.io/badge/🤗 Spaces-Instruct_Pix2Pix-blue" src="https://img.shields.io/badge/🤗 Spaces-Instruct_Pix2Pix-blue.png" height="20"/>
|
| 122 |
+
</a>
|
| 123 |
+
</svg>
|
| 124 |
+
|
| 125 |
+
</div>
|
| 126 |
+
|
| 127 |
+
</div>
|
| 128 |
+
|
| 129 |
+
"""
|
| 130 |
+
|
| 131 |
+
with gr.Blocks(css='style.css') as demo:
|
| 132 |
+
with gr.Column(elem_id="col-container"):
|
| 133 |
+
gr.HTML(title)
|
| 134 |
+
with gr.Row():
|
| 135 |
+
with gr.Column():
|
| 136 |
+
video_inp = gr.Video(label="Video source", source="upload", type="filepath", elem_id="input-vid")
|
| 137 |
+
prompt = gr.Textbox(label="Prompt", placeholder="enter prompt", show_label=False, elem_id="prompt-in")
|
| 138 |
+
with gr.Row():
|
| 139 |
+
trim_in = gr.Slider(label="Cut video at (s)", minimun=1, maximum=3, step=1, value=1)
|
| 140 |
+
with gr.Column():
|
| 141 |
+
video_out = gr.Video(label="Pix2pix video result", elem_id="video-output")
|
| 142 |
+
gr.HTML("""
|
| 143 |
+
<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>
|
| 144 |
+
work with longer videos / skip the queue:
|
| 145 |
+
""", elem_id="duplicate-container")
|
| 146 |
+
submit_btn = gr.Button("Generate X-Decoder video")
|
| 147 |
+
|
| 148 |
+
#with gr.Group(elem_id="share-btn-container", visible=False) as share_group:
|
| 149 |
+
# community_icon = gr.HTML(community_icon_html)
|
| 150 |
+
# loading_icon = gr.HTML(loading_icon_html)
|
| 151 |
+
# share_button = gr.Button("Share to community", elem_id="share-btn")
|
| 152 |
+
|
| 153 |
+
inputs = [prompt, video_inp, trim_in]
|
| 154 |
+
#outputs = [video_out, share_group]
|
| 155 |
+
outputs = [video_out]
|
| 156 |
+
|
| 157 |
+
gr.HTML(article)
|
| 158 |
+
|
| 159 |
+
submit_btn.click(infer, inputs, outputs)
|
| 160 |
+
#share_button.click(None, [], [], _js=share_js)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
demo.launch().queue(max_size=12)
|