fast-frames / app.py
Omnibus's picture
Update app.py
8b4f527
import gradio as gr
import cv2
from PIL import Image
import numpy as np
import sys
import os
from pathlib import Path
from os import listdir
from os.path import isfile, join
import rembg
#frame_num=0
#frame_count=0
vid_file="vidcut0001-0250.mp4"
#capture = cv2.VideoCapture(vid_file)
rm_bg0=gr.Interface.load("spaces/Omnibus/fast-bg-remove")
rm_bg1=gr.Interface.load("spaces/Omnibus/fast-bg-remove")
rm_bg2=gr.Interface.load("spaces/Omnibus/fast-bg-remove")
rm_bg3=gr.Interface.load("spaces/Omnibus/fast-bg-remove")
rm_bg4=gr.Interface.load("spaces/Omnibus/fast-bg-remove")
rm_bg5=gr.Interface.load("spaces/Omnibus/fast-bg-remove")
rm_bg6=gr.Interface.load("spaces/Omnibus/fast-bg-remove")
rm_bg7=gr.Interface.load("spaces/Omnibus/fast-bg-remove")
rm_bg8=gr.Interface.load("spaces/Omnibus/fast-bg-remove")
rm_bg9=gr.Interface.load("spaces/Omnibus/fast-bg-remove")
css='''
.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;
}
'''
def load_video(vid):
new_video_in = str(vid)
capture = cv2.VideoCapture(new_video_in)
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
tmp_frame='1'
capture.release()
return vid,frame_count,frame_count
global f_proc
f_proc=0
def preview_function(vid,frame_count, denom):
#if vid==None:
#vid=img_file
new_video_in = str(vid)
capture = cv2.VideoCapture(new_video_in)
global fbox
fbox=[]
cnt=0
frame_count1= int(frame_count)-int(denom)
for i in range(int(frame_count1)):
if cnt == int(denom):
cnt=0
capture.set(cv2.CAP_PROP_POS_FRAMES, i-1)
ret, frame_f = capture.read(i-1)
frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
#frame_fff=Image.fromarray(frame_ff)
fbox.append(frame_ff)
else:
pass
cnt+=1
capture.set(cv2.CAP_PROP_POS_FRAMES, int(frame_count)-1)
ret, frame_f = capture.read()
frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
#frame_fff=Image.fromarray(frame_ff)
fbox.append(frame_ff)
capture.release()
f_box_count=len(fbox)
return fbox, f_box_count
def capture_function(vid=None,img_file=None,frame_count=None,frame_total=None, frame_col=None):
if vid==None:
vid=img_file
new_video_in = str(vid)
capture = cv2.VideoCapture(new_video_in)
fbox=[]
cnt=0
frame_count1= int(frame_count)-int(denom)
for i in range(int(frame_count1)):
if cnt == int(denom):
cnt=0
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
ret, frame_f = capture.read(i)
frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
fbox.append(frame_ff)
else:
pass
cnt+=1
capture.set(cv2.CAP_PROP_POS_FRAMES, int(frame_count)-1)
ret, frame_f = capture.read()
frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
fbox.append(frame_ff)
capture.release()
return fbox
'''[0],fbox[1],fbox[2],fbox[3],fbox[4],fbox[5],fbox[6],fbox[7],fbox[8],fbox[9],
fbox[10],fbox[11],fbox[12],fbox[13],fbox[14],fbox[15],fbox[16],fbox[17],fbox[18],fbox[19],
fbox[20],fbox[21],fbox[22],fbox[23],fbox[24],fbox[25],fbox[26],fbox[27],fbox[28],fbox[29],
fbox[30],fbox[31],fbox[32],fbox[33],fbox[34],fbox[35],fbox[36],fbox[37],fbox[38],fbox[39],
fbox[40],fbox[41],fbox[42],fbox[43],fbox[44],fbox[45],fbox[46],fbox[47],fbox[48],fbox[49],
fbox[50],fbox[51],fbox[52],fbox[53],fbox[54],fbox[55],fbox[56],fbox[57],fbox[58],fbox[59],
fbox[60],fbox[61],fbox[62],fbox[63],fbox[64],fbox[65],fbox[66],fbox[67],fbox[68],fbox[69],
fbox[70],fbox[71],fbox[72],fbox[73],fbox[74],fbox[75],fbox[76],fbox[77],fbox[78],fbox[79],
fbox[80],fbox[81],fbox[82],fbox[83],fbox[84],fbox[85],fbox[86],fbox[87],fbox[88],fbox[89],
fbox[90],fbox[91],fbox[92],fbox[93],fbox[94],fbox[95],fbox[96],fbox[97],fbox[98],fbox[99],
fbox[100]'''
def remove_bg_func0(input):
img=fbox[0+int(input)]
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(f"b-{input}0.png", img)
output=rm_bg0(f"b-{input}0.png")
return output
def remove_bg_func1(input):
img=fbox[1+int(input)]
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(f"b-{input}1.png", img)
output=rm_bg1(f"b-{input}1.png")
return output
def remove_bg_func2(input):
img=fbox[2+int(input)]
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(f"b-{input}2.png", img)
output=rm_bg2(f"b-{input}2.png")
return output
def remove_bg_func3(input):
img=fbox[3+int(input)]
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(f"b-{input}3.png", img)
output=rm_bg3(f"b-{input}3.png")
return output
def remove_bg_func4(input):
img=fbox[4+int(input)]
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(f"b-{input}4.png", img)
output=rm_bg4(f"b-{input}4.png")
return output
def remove_bg_func5(input):
img=fbox[5+int(input)]
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(f"b-{input}5.png", img)
output=rm_bg5(f"b-{input}5.png")
return output
def remove_bg_func6(input):
img=fbox[6+int(input)]
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(f"b-{input}6.png", img)
output=rm_bg6(f"b-{input}6.png")
return output
def remove_bg_func7(input):
img=fbox[7+int(input)]
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(f"b-{input}7.png", img)
output=rm_bg7(f"b-{input}7.png")
return output
def remove_bg_func8(input):
img=fbox[8+int(input)]
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(f"b-{input}8.png", img)
output=rm_bg8(f"b-{input}8.png")
return output
def remove_bg_func9(input):
img=fbox[9+int(input)]
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(f"b-{input}9.png", img)
output=rm_bg9(f"b-{input}9.png")
return output
def main():
with gr.Blocks(css=css) as demo:
with gr.Box():
with gr.Group():
with gr.Row():
with gr.Column():
#vid=gr.Video(gr_input_type="file")
#vid=gr.PlayableVideo(value=vid_file, gr_input_type="filepath", file_extensions="mp4", visible=True)
vid=gr.Video(type="filepath", format="mp4", visible=True)
img_file=gr.Textbox(value=f"{vid_file}")
with gr.Row():
pv_btn=gr.Button("Preview Frames")
go_btn=gr.Button("Capture Frames")
ex_btn=gr.Button("Load Example")
with gr.Column():
with gr.Box():
#frame_slider=gr.Slider(1, frame_count=frame_count, value=frame_num, step=1)
with gr.Row():
every_n=gr.Textbox(label="every_n",value='10')
frame_tot=gr.Textbox(label="Total Frames",value='0')
with gr.Row():
idk=gr.Textbox(label="",value='10')
frames_to_proc=gr.Textbox(label="Selected Frames",value='0')
with gr.Row():
start_box=gr.Textbox(label="Start",value='0', interactive=True)
end_box=gr.Textbox(label="End",value='0', interactive=True)
with gr.Row():
with gr.Column():
with gr.Row():
f_1_btn=gr.Button("8")
f_2_btn=gr.Button("12")
f_3_btn=gr.Button("16")
f_4_btn=gr.Button("36")
with gr.Row():
bg_rgb_btn=gr.Button("Remove BG RGB")
depth_btn=gr.Button("Depth Frames")
bg_dpt_btn=gr.Button("Remove BG Depth")
make_3d=gr.Button("Make 3D")
with gr.Tab("RGB"):
with gr.Column():
with gr.Row():
fw1=gr.Gallery(label="Main", type="filepath").style(grid=10)
with gr.Row():
file_out=gr.File(file_count="list")
with gr.Tab("Remove BG RGB"):
with gr.Column():
with gr.Row():
f_proc=gr.Textbox(label="Frames Processed", show_progress=False)
#with gr.Group():
with gr.Group():
with gr.Row():
#with gr.Row():
bdfw1=gr.Pil(label="Frame 1", type="filepath")
bdfw2=gr.Pil(label="Frame 2", type="filepath")
bdfw3=gr.Pil(label="Frame 3", type="filepath")
bdfw4=gr.Pil(label="Frame 4", type="filepath")
bdfw5=gr.Pil(label="Frame 5", type="filepath")
bdfw6=gr.Pil(label="Frame 6", type="filepath")
bdfw7=gr.Pil(label="Frame 7", type="filepath")
bdfw8=gr.Pil(label="Frame 8", type="filepath")
bdfw9=gr.Pil(label="Frame 9", type="filepath")
bdfw10=gr.Pil(label="Frame 10", type="filepath")
bdfw11=gr.Pil(label="Frame 11", type="filepath")
bdfw12=gr.Pil(label="Frame 12", type="filepath")
bdfw13=gr.Pil(label="Frame 13", type="filepath")
bdfw14=gr.Pil(label="Frame 14", type="filepath")
bdfw15=gr.Pil(label="Frame 15", type="filepath")
bdfw16=gr.Pil(label="Frame 16", type="filepath")
bdfw17=gr.Pil(label="Frame 17", type="filepath")
bdfw18=gr.Pil(label="Frame 18", type="filepath")
bdfw19=gr.Pil(label="Frame 19", type="filepath")
bdfw20=gr.Pil(label="Frame 20", type="filepath")
bdfw21=gr.Pil(label="Frame 21", type="filepath")
bdfw22=gr.Pil(label="Frame 22", type="filepath")
bdfw23=gr.Pil(label="Frame 23", type="filepath")
bdfw24=gr.Pil(label="Frame 24", type="filepath")
bdfw25=gr.Pil(label="Frame 25", type="filepath")
bdfw26=gr.Pil(label="Frame 26", type="filepath")
bdfw27=gr.Pil(label="Frame 27", type="filepath")
bdfw28=gr.Pil(label="Frame 28", type="filepath")
bdfw29=gr.Pil(label="Frame 29", type="filepath")
bdfw30=gr.Pil(label="Frame 30", type="filepath")
bdfw31=gr.Pil(label="Frame 31", type="filepath")
bdfw32=gr.Pil(label="Frame 32", type="filepath")
bdfw33=gr.Pil(label="Frame 33", type="filepath")
bdfw34=gr.Pil(label="Frame 34", type="filepath")
bdfw35=gr.Pil(label="Frame 35", type="filepath")
bdfw36=gr.Pil(label="Frame 36", type="filepath")
bdfw37=gr.Pil(label="Frame 37", type="filepath")
bdfw38=gr.Pil(label="Frame 38", type="filepath")
bdfw39=gr.Pil(label="Frame 39", type="filepath")
bdfw40=gr.Pil(label="Frame 40", type="filepath")
bdfw41=gr.Pil(label="Frame 41", type="filepath")
bdfw42=gr.Pil(label="Frame 42", type="filepath")
bdfw43=gr.Pil(label="Frame 43", type="filepath")
bdfw44=gr.Pil(label="Frame 44", type="filepath")
bdfw45=gr.Pil(label="Frame 45", type="filepath")
bdfw46=gr.Pil(label="Frame 46", type="filepath")
bdfw47=gr.Pil(label="Frame 47", type="filepath")
bdfw48=gr.Pil(label="Frame 48", type="filepath")
bdfw49=gr.Pil(label="Frame 49", type="filepath")
bdfw50=gr.Pil(label="Frame 50", type="filepath")
with gr.Tab("Process", visible=False):
inpu1=gr.Textbox(value=0, visible=False)
inpu2=gr.Textbox(value=10, visible=False)
inpu3=gr.Textbox(value=20, visible=False)
inpu4=gr.Textbox(value=30, visible=False)
inpu5=gr.Textbox(value=40, visible=False)
bg_rgb_btn.click(remove_bg_func0, inputs=[inpu1], outputs=[bdfw1])
bg_rgb_btn.click(remove_bg_func1, inputs=[inpu1], outputs=[bdfw2])
bg_rgb_btn.click(remove_bg_func2, inputs=[inpu1], outputs=[bdfw3])
bg_rgb_btn.click(remove_bg_func3, inputs=[inpu1], outputs=[bdfw4])
bg_rgb_btn.click(remove_bg_func4, inputs=[inpu1], outputs=[bdfw5])
bg_rgb_btn.click(remove_bg_func5, inputs=[inpu1], outputs=[bdfw6])
bg_rgb_btn.click(remove_bg_func6, inputs=[inpu1], outputs=[bdfw7])
bg_rgb_btn.click(remove_bg_func7, inputs=[inpu1], outputs=[bdfw8])
bg_rgb_btn.click(remove_bg_func8, inputs=[inpu1], outputs=[bdfw9])
bg_rgb_btn.click(remove_bg_func9, inputs=[inpu1], outputs=[bdfw10])
bdfw1.change(remove_bg_func0, inputs=[inpu2], outputs=[bdfw11])
bdfw2.change(remove_bg_func1, inputs=[inpu2], outputs=[bdfw12])
bdfw3.change(remove_bg_func2, inputs=[inpu2], outputs=[bdfw13])
bdfw4.change(remove_bg_func3, inputs=[inpu2], outputs=[bdfw14])
bdfw5.change(remove_bg_func4, inputs=[inpu2], outputs=[bdfw15])
bdfw6.change(remove_bg_func5, inputs=[inpu2], outputs=[bdfw16])
bdfw7.change(remove_bg_func6, inputs=[inpu2], outputs=[bdfw17])
bdfw8.change(remove_bg_func7, inputs=[inpu2], outputs=[bdfw18])
bdfw9.change(remove_bg_func8, inputs=[inpu2], outputs=[bdfw19])
bdfw10.change(remove_bg_func9, inputs=[inpu2], outputs=[bdfw20])
bdfw11.change(remove_bg_func0, inputs=[inpu3], outputs=[bdfw21])
bdfw12.change(remove_bg_func1, inputs=[inpu3], outputs=[bdfw22])
bdfw13.change(remove_bg_func2, inputs=[inpu3], outputs=[bdfw23])
bdfw14.change(remove_bg_func3, inputs=[inpu3], outputs=[bdfw24])
bdfw15.change(remove_bg_func4, inputs=[inpu3], outputs=[bdfw25])
bdfw16.change(remove_bg_func5, inputs=[inpu3], outputs=[bdfw26])
bdfw17.change(remove_bg_func6, inputs=[inpu3], outputs=[bdfw27])
bdfw18.change(remove_bg_func7, inputs=[inpu3], outputs=[bdfw28])
bdfw19.change(remove_bg_func8, inputs=[inpu3], outputs=[bdfw29])
bdfw20.change(remove_bg_func9, inputs=[inpu3], outputs=[bdfw30])
bdfw21.change(remove_bg_func0, inputs=[inpu4], outputs=[bdfw31])
bdfw22.change(remove_bg_func1, inputs=[inpu4], outputs=[bdfw32])
bdfw23.change(remove_bg_func2, inputs=[inpu4], outputs=[bdfw33])
bdfw24.change(remove_bg_func3, inputs=[inpu4], outputs=[bdfw34])
bdfw25.change(remove_bg_func4, inputs=[inpu4], outputs=[bdfw35])
bdfw26.change(remove_bg_func5, inputs=[inpu4], outputs=[bdfw36])
bdfw27.change(remove_bg_func6, inputs=[inpu4], outputs=[bdfw37])
bdfw28.change(remove_bg_func7, inputs=[inpu4], outputs=[bdfw38])
bdfw29.change(remove_bg_func8, inputs=[inpu4], outputs=[bdfw39])
bdfw30.change(remove_bg_func9, inputs=[inpu4], outputs=[bdfw40])
bdfw31.change(remove_bg_func0, inputs=[inpu5], outputs=[bdfw41])
bdfw32.change(remove_bg_func1, inputs=[inpu5], outputs=[bdfw42])
bdfw33.change(remove_bg_func2, inputs=[inpu5], outputs=[bdfw43])
bdfw34.change(remove_bg_func3, inputs=[inpu5], outputs=[bdfw44])
bdfw35.change(remove_bg_func4, inputs=[inpu5], outputs=[bdfw45])
bdfw36.change(remove_bg_func5, inputs=[inpu5], outputs=[bdfw46])
bdfw37.change(remove_bg_func6, inputs=[inpu5], outputs=[bdfw47])
bdfw38.change(remove_bg_func7, inputs=[inpu5], outputs=[bdfw48])
bdfw39.change(remove_bg_func8, inputs=[inpu5], outputs=[bdfw49])
bdfw40.change(remove_bg_func9, inputs=[inpu5], outputs=[bdfw50])
vid.change(load_video, inputs=[vid], outputs=[vid,frame_tot,end_box])
ex_btn.click(load_video, inputs=[img_file], outputs=[vid,frame_tot,end_box])
pv_btn.click(preview_function,inputs=[vid, frame_tot, every_n],outputs=[fw1,frames_to_proc])
demo.queue(concurrency_count=120)
demo.launch(enable_queue=True, max_threads=120)
if __name__ == "__main__":
main()