Spaces:
Running
Running
File size: 16,241 Bytes
3bc3a35 7568270 3bc3a35 6120714 615c99b 3bc3a35 b99d9e0 3bc3a35 fc3ac0d 3bc3a35 b8a424e 3bc3a35 eac0bb2 3bc3a35 b8b7e89 3bc3a35 b8b7e89 3bc3a35 7de5432 615c99b 7de5432 615c99b 7de5432 2495f15 3bc3a35 8004dd8 1175bab 8004dd8 094f0c3 3bc3a35 615c99b 3bc3a35 615c99b 3bc3a35 4cb2e31 8004dd8 3b9a2a8 4cb2e31 143cda3 4cb2e31 ede0a5a 3a89838 88a5236 143cda3 3bc3a35 143cda3 3bc3a35 143cda3 4ff1ee9 143cda3 4ff1ee9 143cda3 4ff1ee9 3bc3a35 e06f7d0 4ff1ee9 e06f7d0 143cda3 4cb2e31 7de5432 143cda3 ae82bf3 143cda3 7de5432 4cb2e31 3bc3a35 4ff1ee9 e06f7d0 4ff1ee9 143cda3 3bc3a35 143cda3 3bc3a35 b8a424e 4cb2e31 3bc3a35 88a5236 5d59ca0 88a5236 4ff1ee9 143cda3 3bc3a35 4cb2e31 2495f15 4cb2e31 3bc3a35 b8a424e 3bc3a35 1d6d097 |
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
import gradio as gr
import yt_dlp
import os
#import json
import numpy as np
import cv2
import uuid
import moviepy
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
import moviepy.video.io.ImageSequenceClip
from moviepy.editor import *
from PIL import Image
#os.system("pip install --upgrade yt-dlp")
os.system("yt-dlp -U")
uid=uuid.uuid4()
if not os.path.exists(f'{uid}'): os.makedirs(f'{uid}')
if not os.path.exists(f'{uid}-frames'): os.makedirs(f'{uid}-frames')
if not os.path.exists(f'{uid}-rembg'): os.makedirs(f'{uid}-rembg')
esr = gr.Interface.load("spaces/Omnibus/real_esrgan_mod")
main_url= "https://omnibus-Deblur-Media.hf.space"
load_js = """
function(text_input, url_params) {
console.log(text_input, url_params);
const params = new URLSearchParams(window.location.search);
url_params = Object.fromEntries(params);
return [text_input, url_params]
}
"""
def load_video(vid):
#os.system("yt-dlp -U")
new_video_in = str(vid)
capture = cv2.VideoCapture(new_video_in)
fps = capture.get(cv2.CAP_PROP_FPS)
msc = capture.get(cv2.CAP_PROP_POS_MSEC)
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
msc = float(frame_count/fps)
capture.release()
vid_t = float(msc)
hours = int(vid_t/360)
minutes = int(vid_t/60)-(hours*360)
seconds = vid_t-(minutes*60)-(hours*360)
vid_len = f'{hours}:{minutes}:{seconds}'
return frame_count, fps, vid_len, vid
def im_2_vid(images,fps):
this_im=cv2.imread(images[0])
height=this_im.shape[0]
width= this_im.shape[1]
size = (width, height)
movie_clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(images, fps)
movie_clip.write_videofile(f'{uid}-rembg/bg_removed-{uid}.mp4')
return (f'{uid}-rembg/bg_removed-{uid}.mp4', f'{uid}-rembg/bg_removed-{uid}.mp4')
def predict(text, url_params):
mod_url=""
mod=gr.HTML("")
out = None
valid=gr.update(visible=False)
mod_url = url_params.get('url')
return ["" + text + "", mod_url]
def dl(inp,img):
fps="Error"
out = None
out_file=[]
if img == None and inp !="":
try:
inp_out=inp.replace("https://","")
inp_out=inp_out.replace("/","_").replace(".","_").replace("=","_").replace("?","_")
if "twitter" in inp:
os.system(f'yt-dlp "{inp}" --extractor-arg "twitter:api=syndication" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')
else:
os.system(f'yt-dlp "{inp}" --trim-filenames 160 -o "{uid}/{inp_out}.mp4" -S res,mp4 --recode mp4')
out = f"{uid}/{inp_out}.mp4"
capture = cv2.VideoCapture(out)
fps = capture.get(cv2.CAP_PROP_FPS)
capture.release()
except Exception as e:
print(e)
out = None
elif img !=None and inp == "":
capture = cv2.VideoCapture(img)
fps = capture.get(cv2.CAP_PROP_FPS)
capture.release()
out = f"{img}"
return out,out,out,out,fps
def trim_vid(vid,start_time,end_time):
print (vid)
start_hr=float(start_time.split(":",2)[0])*360
start_min=int(start_time.split(":",2)[1])*60
start_sec=int(start_time.split(":",2)[2])
end_hr=int(end_time.split(":",2)[0])*360
end_min=int(end_time.split(":",2)[1])*60
end_sec=float(end_time.split(":",2)[2])
start=start_hr+start_min+start_sec
end=end_hr+end_min+end_sec
clip = VideoFileClip(vid)
mod_fps=clip.fps
clip = clip.subclip(start, end)
clip.write_videofile(f"{uid}-clip.mp4", fps=mod_fps)
#ffmpeg_extract_subclip(vid, start, end, targetname=f"{uid}-clip.mp4")
out= f"{uid}-clip.mp4"
capture = cv2.VideoCapture(out)
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
capture.release()
return out,frame_count
def video_clip(program,fps,qual,model_name,denoise_strength,face_enhance,outscale):
_=None
fps=float(fps)
new_video_in = str(f"{uid}-clip.mp4")
capture = cv2.VideoCapture(new_video_in)
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
fbox=[]
out_box=[]
cnt=0
frame_count1= int(frame_count)
for i in range(int(frame_count1)-1):
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
ret, frame_f = capture.read(i)
#frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
cv2.imwrite(f'{uid}-frames/{i+1}.png',frame_f)
fbox.append(f'{uid}-frames/{i+1}.png')
mes=f'Working on {i+1} of {frame_count1}'
yield _,_,mes
yield _,_,"Frames Complete"
if qual:
mes = "Improving Quality"
yield _,_,mes
if qual:
for i,img in enumerate(fbox):
#print (img)
out = os.path.abspath(img)
out_url = f'{main_url}/file={out}'
mes=f'Improving frame {i+1} of {frame_count}'
yield _,_,mes
out = esr(out_url, model_name, float(denoise_strength), face_enhance, int(outscale))
#yield _,_,mes
#cv2.imwrite(f'{img}',out)
out_box.append(f'{out}')
print (f'{main_url}/file={out}')
#print (f'out::{out}')
print (f'out_box::{out_box}')
clip = ImageSequenceClip(out_box, fps = fps)
else:
mes="Writing Video"
clip = ImageSequenceClip(fbox, fps = fps)
yield _,_,"Writing Video"
videoclip = VideoFileClip(new_video_in)
audioclip=videoclip.audio
clip.audio=audioclip
clip.write_videofile(f"{uid}/enhanced_vid.mp4")
yield _,_,"Saving Video"
out = f"{uid}/enhanced_vid.mp4"
yield out,out,"Video Complete"
def gif_clip(program,fps,qual,model_name,denoise_strength,face_enhance,outscale):
_=None
fps=float(fps)
if program == 'imageio':
print (f"NOT adjusting fps from: {fps} to: {fps*2}")
fps=fps*2
new_video_in = str(f"{uid}-clip.mp4")
capture = cv2.VideoCapture(new_video_in)
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
fbox=[]
out_box=[]
cnt=0
frame_count1= int(frame_count)
for i in range(int(frame_count1)-1):
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
ret, frame_f = capture.read(i)
cv2.imwrite(f'{uid}-frames/{i+1}.png',frame_f)
fbox.append(f'{uid}-frames/{i+1}.png')
mes=f'Working on {i+1} of {frame_count1}'
yield _,_,mes
yield _,_,"Frames Complete"
if qual:
mes = "Improving Quality"
yield _,_,mes
if qual:
for i,img in enumerate(fbox):
out = os.path.abspath(img)
out_url = f'{main_url}/file={out}'
mes=f'Improving frame {i+1} of {frame_count}'
yield _,_,mes
out = esr(out_url, model_name, float(denoise_strength), face_enhance, int(outscale))
out_box.append(f'{out}')
print (f'{main_url}/file={out}')
clip = ImageSequenceClip(out_box, fps = fps)
else:
clip = ImageSequenceClip(fbox, fps = fps)
yield _,_,"Writing GIF"
clip.to_gif(f"{uid}/clip_gif.gif",fps=fps, program=program)
#clip.speedx(2).to_gif(f"{uid}/clip_gif.gif",program=program)
out = f"{uid}/clip_gif.gif"
yield out,out,"GIF Complete"
def improve_img(inp,model_name,denoise_strength,face_enhance,outscale):
_=None
yield _,"Reading image"
#img=cv2.imread(inp)
cv2.imwrite(f'{uid}-frames/tmp_im.png',cv2.cvtColor(inp, cv2.COLOR_BGR2RGB))
out = os.path.abspath(f'{uid}-frames/tmp_im.png')
out_url = f'{main_url}/file={out}'
yield _,"Improving image"
out = esr(out_url, model_name, float(denoise_strength), face_enhance, int(outscale))
yield out,"Complete"
def update_speed(inp,clip_speed,fps):
if "-split.mp4" in inp:
out_inp = f'{inp.split("-split.mp4",1)[0]}-split.mp4'
else:
out_inp = f'{inp.split(".mp4",1)[0]}-split.mp4'
mod_fps=float(fps)*float(clip_speed)
clip = VideoFileClip(inp)
final = moviepy.video.fx.all.speedx(clip, factor=clip_speed)
final.write_videofile(f'{out_inp}', fps=mod_fps)
out = f'{out_inp}'
return out,out,out
def echo_fn(inp):
return inp
def check_load(inp_url,outp_vid,hid_box,start_f,end_f):
if outp_vid == None and inp_url !="":
out_trim,in_vid,trim_count=trim_vid(hid_box,start_f,end_f)
elif outp_vid !=None and inp_url == "":
out_trim = None
in_vid=outp_vid
trim_count = ""
return out_trim,in_vid,trim_count
def run_samp(program,fps,qual,model_name,denoise_strength,face_enhance,outscale,frame_targ):
_=None
#fps=float(fps)
new_video_in = str(f"{uid}-clip.mp4")
capture = cv2.VideoCapture(new_video_in)
#frame_count1= int(frame_count)
i=int(frame_targ)
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
ret, frame_f = capture.read(i)
#frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
cv2.imwrite(f'{uid}-frames/{i}.png',frame_f)
out = os.path.abspath(f'{uid}-frames/{i}.png')
out_url = f'{main_url}/file={out}'
yield _,"Creating Sample Frame","Creating Sample Frame"
out = esr(out_url, model_name, float(denoise_strength), face_enhance, int(outscale))
yield out,"Sample Frame Complete","Sample Frame Complete"
def new_media(inp):
if inp == "Image":
im_box = gr.update(visible=True)
vid_box = gr.update(visible=False)
gif_row = gr.update(visible=False)
vid_row = gr.update(visible=False)
#qual = gr.update(value=True,interactive=False)
return (im_box,vid_box,gif_row,vid_row)
if inp == "Video":
im_box = gr.update(visible=False)
vid_box = gr.update(visible=True)
gif_row = gr.update(visible=False)
vid_row = gr.update(visible=True)
#qual = gr.update(value=False,interactive=True)
return (im_box,vid_box,gif_row,vid_row)
if inp == "GIF":
im_box = gr.update(visible=False)
vid_box = gr.update(visible=True)
gif_row = gr.update(visible=True)
vid_row = gr.update(visible=False)
#qual = gr.update(value=False,interactive=True)
return (im_box,vid_box,gif_row,vid_row)
css='''
#component-0{
background:repeating-radial-gradient(white, cornflowerblue);
}
.padded.svelte-90oupt{
background:cornflowerblue;
}
.dark .gr-box{
background:#344d74;!important;
opacity:1;!important;
}
.p-2 {
background:#344d74;!important;
opacity:1;!important;
}
.gap-4 {
background:#6681ab;!important;
opacity:1;!important;
}
.dark .gr-padded{
background:#21314a;!important;
opacity:1;!important;
}
'''
with gr.Blocks(css=css) as app:
with gr.Row():
gr.Column()
with gr.Column():
with gr.Group():
choose_media = gr.Radio(label="Media Type",choices=["Image","Video","GIF"],value="Image")
with gr.Accordion("Quality Options", open=False):
with gr.Row():
model_name = gr.Dropdown(label="Real-ESRGAN Model",
choices=["RealESRGAN_x4plus", "RealESRNet_x4plus", "RealESRGAN_x4plus_anime_6B",
"RealESRGAN_x2plus", "realesr-general-x4v3"],
value="realesr-general-x4v3", show_label=True)
face_enhance = gr.Checkbox(label="Face Enhancement using GFPGAN",
value=False, show_label=True)
denoise_strength = gr.Slider(label="Denoise Strength",
minimum=0, maximum=1, step=0.1, value=0.5)
outscale = gr.Slider(label="Image Upscaling Factor",
minimum=1, maximum=10, step=1, value=1, show_label=True)
with gr.Box(visible=True) as im_box:
with gr.Group():
inp_img_url=gr.Textbox(label="Image URL")
load_im_btn=gr.Button("Load URL")
inp_img=gr.Image(label = "Input Image")
with gr.Row():
proc_im_btn=gr.Button("Improve")
im_stat=gr.Textbox(label="Status")
outp_img=gr.Image(label="Enhanced Image")
with gr.Box(visible=False) as vid_box:
with gr.Group():
inp_url = gr.Textbox(label="Video URL")
go_btn = gr.Button("Run")
outp_vid=gr.Video(format="mp4")
with gr.Row():
frame_count=gr.Textbox(label="Frame Count",interactive = False)
fps=gr.Textbox(label="FPS",interactive = False)
outp_file=gr.Files()
clip_speed = gr.Slider(label="Speed", minimum=0.01, maximum=2, value=1, step=0.01)
speed_btn = gr.Button("Update Speed")
with gr.Row():
start_f = gr.Textbox(label = "Start", value = "0:00:00", placeholder = "0:00:23",interactive = True)
end_f = gr.Textbox(label = "End", value = "0:00:05", placeholder = "0:00:54",interactive = True)
trim_count = gr.Textbox(label="Trimmed Frames")
trim_btn=gr.Button("Trim")
out_trim=gr.Video(format="mp4")
hid_box = gr.Textbox(visible=False)
hid_fps = gr.Textbox(visible=False)
with gr.Row():
qual=gr.Checkbox(label="Improve Quality",value=False)
with gr.Accordion("Sample Frame",open=False):
with gr.Row():
frame_targ=gr.Number(label="Frame",value=1)
samp_btn=gr.Button("Get Sample")
samp_im = gr.Image(label="Sample Frame")
with gr.Column(visible=False) as gif_row:
with gr.Row():
choose_prog=gr.Radio(label="GIF Engine",choices=['imageio','ImageMagick','ffmpeg'], value = "imageio")
with gr.Row():
gif_btn = gr.Button("Make GIF")
gif_stat=gr.Textbox(label="Status")
with gr.Row(visible=False) as vid_row:
vid_btn = gr.Button("Make Video")
vid_stat=gr.Textbox(label="Status")
with gr.Row():
with gr.Column():
gif_show = gr.Video()
with gr.Column():
gif_file = gr.Files()
gr.Column()
with gr.Row(visible=False):
text_input=gr.Textbox()
text_output=gr.Textbox()
url_params=gr.JSON()
def load_im_fn(inp):
return inp
load_im_btn.click(load_im_fn,inp_img_url,inp_img)
proc_im_btn.click(improve_img,[inp_img,model_name,denoise_strength,face_enhance,outscale],[outp_img,im_stat])
choose_media.change(new_media,choose_media,[im_box,vid_box,gif_row,vid_row])
samp_btn.click(run_samp,[choose_prog,fps,qual,model_name,denoise_strength,face_enhance,outscale,frame_targ],[samp_im,gif_stat,vid_stat])
speed_btn.click(update_speed,[hid_box,clip_speed,hid_fps],[outp_vid,hid_box,outp_file])
gif_btn.click(gif_clip,[choose_prog,fps,qual,model_name,denoise_strength,face_enhance,outscale],[gif_show,gif_file,gif_stat])
vid_btn.click(video_clip,[choose_prog,fps,qual,model_name,denoise_strength,face_enhance,outscale],[gif_show,gif_file,vid_stat])
trim_btn.click(trim_vid,[hid_box,start_f,end_f],[out_trim,trim_count])
outp_vid.change(load_video,outp_vid,[frame_count,fps,end_f,hid_box]).then(trim_vid,[hid_box,start_f,end_f],[out_trim,trim_count])
go_btn.click(dl,[inp_url,outp_vid],[outp_vid,outp_file,out_trim,hid_box,hid_fps])
app.load(fn=predict, inputs=[text_input,url_params], outputs=[text_output,text_input],_js=load_js)
app.queue().launch(show_api=False) |