Spaces:
Runtime error
Runtime error
File size: 4,938 Bytes
7f88dd7 b4d7fed 56afb5e e08a64e 7022cc6 9365f85 c96caf0 5bdf543 9e24192 5bdf543 9e24192 7022cc6 277878d 7022cc6 6580737 7022cc6 5e47d3b 9365f85 b639502 8d5c6cf 9365f85 8d5c6cf c96caf0 8d5c6cf 9365f85 5bdf543 8d5c6cf 5bdf543 9365f85 51ede94 7022cc6 3dbe3d9 9365f85 8d5c6cf 5bdf543 8d5c6cf 5bdf543 1ee735f bcc8c8c 5bdf543 6fa5ca9 8d5c6cf 7022cc6 2417919 fbaf836 2417919 9e24192 e793092 7f88dd7 46fd8e8 34d9197 c173614 34d9197 6ef6ed7 34d9197 e90bb7a 7f88dd7 9e24192 e90bb7a 8d5c6cf e90bb7a 8d5c6cf 9365f85 51ede94 5bdf543 51ede94 9365f85 e90bb7a 38722bf 5bdf543 9e24192 8c0fcd2 8d5c6cf e4cd716 e90bb7a 9e24192 |
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 |
import gradio as gr
import yt_dlp
import os
import json
from rembg import remove as rm
import cv2
import uuid
uid=uuid.uuid4()
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')
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 rem_cv(inp):
cap = cv2.VideoCapture(f'{inp}')
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
fgbg = cv2.bgsegm.createBackgroundSubtractorGMG()
while True:
ret, frame = cap.read()
fgmask = fgbg.apply(frame)
fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
yield (fgmask)
def load_video(vid):
new_video_in = str(vid)
capture = cv2.VideoCapture(new_video_in)
fps = capture.get(cv2.CAP_PROP_FPS)
#print (f'FPS:: {fps}')
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
capture.release()
return frame_count, fps
def capture_function(vid=None):
new_video_in = str(vid)
capture = cv2.VideoCapture(new_video_in)
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
fbox=[]
cnt=0
frame_count1= int(frame_count)
for i in range(int(frame_count1)):
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}/{i}.png',frame_ff)
fbox.append(f'{uid}/{i}.png')
return fbox,fbox
def rem_bg(vid):
new_video_in = str(vid)
capture = cv2.VideoCapture(new_video_in)
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
fbox2=[]
cnt=0
frame_count1= int(frame_count)
for i in range(int(frame_count1)):
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
ret, frame_f = capture.read(i)
#frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
out = rm(frame_f)
print (i)
cv2.imwrite(f'{uid}-rembg/{i}.png',out)
fbox2.append(f'{uid}-rembg/{i}.png')
yield fbox2
def rem_bg_og(inp):
#transparent background .mov
#os.system(f'backgroundremover -i "{inp}" -tv -o "{inp}.mov"')
#video video to be overlayed output
#os.system(f' backgroundremover -i "/path/to/video.mp4" -tov "/path/to/videtobeoverlayed.mp4" -o "output.mov"')
#video over image file
#os.system(f'backgroundremover -i "/path/to/video.mp4" -toi "/path/to/videtobeoverlayed.mp4" -o "output.mov"')
#output to transparent GIF
#os.system(f'backgroundremover -i "/path/to/video.mp4" -tg -o "output.gif"')
#output to matte background
os.system(f'backgroundremover -i "{inp}" -mk -o "{inp}.matte.mp4"')
return f'{inp}.matte.mp4'
def predict(text, url_params):
mod_url=""
mod=gr.HTML("")
out = None
valid=gr.update(visible=False)
mod_url = url_params.get('url')
print (mod_url)
return ["" + text + "", mod_url]
def dl(inp):
out = None
out_file=[]
try:
inp_out=inp.replace("https://","")
inp_out=inp_out.replace("/","_").replace(".","_")
os.system(f'yt-dlp "{inp}" --trim-filenames 100 -o "{inp_out}.mp4"')
out = f"{inp_out}.mp4"
except Exception as e:
print (e)
out = None
return out,out,out
with gr.Blocks() as app:
with gr.Tab("Load"):
inp_url = gr.Textbox()
go_btn = gr.Button("Run")
with gr.Row():
with gr.Column():
outp_vid=gr.Video()
with gr.Column():
with gr.Row():
frame_count=gr.Textbox(label="Frame Count",interactive = False)
fps=gr.Textbox(label="FPS",interactive = False)
outp_file=gr.Files()
with gr.Tab("Frames"):
frame_btn = gr.Button("Get Frames")
with gr.Row():
with gr.Column():
frame_gal = gr.Gallery(columns=6)
with gr.Column():
frame_file = gr.Files()
with gr.Tab("Rem BG"):
with gr.Row():
with gr.Column():
rem_btn=gr.Button()
in_vid=gr.Video()
with gr.Column():
#rem_vid=gr.Video()
rem_vid=gr.Gallery(columns=6)
with gr.Row(visible=False):
text_input=gr.Textbox()
text_output=gr.Textbox()
url_params=gr.JSON()
outp_vid.change(load_video,outp_vid,[frame_count,fps])
frame_btn.click(capture_function,[outp_vid],[frame_gal,frame_file])
rem_btn.click(rem_bg,[outp_vid],rem_vid)
go_btn.click(dl,inp_url,[outp_vid,in_vid,outp_file])
app.load(fn=predict, inputs=[text_input,url_params], outputs=[text_output,text_input],_js=load_js)
app.launch()
|