Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,16 +7,11 @@ import cv2
|
|
7 |
import uuid
|
8 |
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
|
9 |
|
10 |
-
|
11 |
uid=uuid.uuid4()
|
12 |
if not os.path.exists(f'{uid}-frames'): os.makedirs(f'{uid}-frames')
|
13 |
if not os.path.exists(f'{uid}-rembg'): os.makedirs(f'{uid}-rembg')
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
load_js = """
|
19 |
-
|
20 |
function(text_input, url_params) {
|
21 |
console.log(text_input, url_params);
|
22 |
const params = new URLSearchParams(window.location.search);
|
@@ -26,10 +21,8 @@ function(text_input, url_params) {
|
|
26 |
"""
|
27 |
def rem_cv(inp):
|
28 |
cap = cv2.VideoCapture(f'{inp}')
|
29 |
-
|
30 |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
|
31 |
fgbg = cv2.bgsegm.createBackgroundSubtractorGMG()
|
32 |
-
|
33 |
while True:
|
34 |
ret, frame = cap.read()
|
35 |
fgmask = fgbg.apply(frame)
|
@@ -49,7 +42,6 @@ def capture_function(vid=None):
|
|
49 |
new_video_in = str(vid)
|
50 |
capture = cv2.VideoCapture(new_video_in)
|
51 |
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
52 |
-
|
53 |
fbox=[]
|
54 |
cnt=0
|
55 |
frame_count1= int(frame_count)
|
@@ -59,16 +51,13 @@ def capture_function(vid=None):
|
|
59 |
frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
|
60 |
cv2.imwrite(f'{uid}/{i}.png',frame_ff)
|
61 |
fbox.append(f'{uid}/{i}.png')
|
62 |
-
|
63 |
return fbox,fbox
|
64 |
|
65 |
-
|
66 |
-
|
67 |
def im_2_vid(images,fps):
|
68 |
#width, height = Image.open(images[0]).size
|
69 |
this_im=cv2.imread(images[0])
|
70 |
height=this_im.shape[0]
|
71 |
-
width=
|
72 |
print (width,height)
|
73 |
size = (width, height)
|
74 |
codec = cv2.VideoWriter_fourcc(*'mp4v') #DIVX, DIVD
|
@@ -81,7 +70,6 @@ def rem_bg(vid):
|
|
81 |
capture = cv2.VideoCapture(new_video_in)
|
82 |
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
83 |
fps = capture.get(cv2.CAP_PROP_FPS)
|
84 |
-
|
85 |
fbox2=[]
|
86 |
cnt=0
|
87 |
frame_count1= int(frame_count)
|
@@ -98,7 +86,6 @@ def rem_bg(vid):
|
|
98 |
|
99 |
out_vid = im_2_vid(fbox2, fps)
|
100 |
return (fbox2,frame_num,out_vid)
|
101 |
-
|
102 |
|
103 |
def rem_bg_og(inp):
|
104 |
#transparent background .mov
|
@@ -111,9 +98,8 @@ def rem_bg_og(inp):
|
|
111 |
#os.system(f'backgroundremover -i "/path/to/video.mp4" -tg -o "output.gif"')
|
112 |
#output to matte background
|
113 |
os.system(f'backgroundremover -i "{inp}" -mk -o "{inp}.matte.mp4"')
|
114 |
-
|
115 |
-
|
116 |
return f'{inp}.matte.mp4'
|
|
|
117 |
def predict(text, url_params):
|
118 |
mod_url=""
|
119 |
mod=gr.HTML("")
|
@@ -157,13 +143,8 @@ def other():
|
|
157 |
fps = capture.get(cv2.CAP_PROP_FPS)
|
158 |
fbox3=[]
|
159 |
frame_count1= int(frame_count)
|
160 |
-
|
161 |
start = (int(fps*int(start_f)))
|
162 |
-
|
163 |
capture.set(cv2.CAP_PROP_POS_FRAMES, i-1)
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
for i in range(int(frame_count1)):
|
168 |
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
|
169 |
ret, frame_f = capture.read(i)
|
@@ -175,9 +156,6 @@ def other():
|
|
175 |
frame_num=f'Working on {i+1} of {frame_count}'
|
176 |
yield fbox2,frame_num,None
|
177 |
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
with gr.Blocks() as app:
|
182 |
with gr.Tab("Load"):
|
183 |
with gr.Row():
|
|
|
7 |
import uuid
|
8 |
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
|
9 |
|
|
|
10 |
uid=uuid.uuid4()
|
11 |
if not os.path.exists(f'{uid}-frames'): os.makedirs(f'{uid}-frames')
|
12 |
if not os.path.exists(f'{uid}-rembg'): os.makedirs(f'{uid}-rembg')
|
13 |
|
|
|
|
|
|
|
14 |
load_js = """
|
|
|
15 |
function(text_input, url_params) {
|
16 |
console.log(text_input, url_params);
|
17 |
const params = new URLSearchParams(window.location.search);
|
|
|
21 |
"""
|
22 |
def rem_cv(inp):
|
23 |
cap = cv2.VideoCapture(f'{inp}')
|
|
|
24 |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
|
25 |
fgbg = cv2.bgsegm.createBackgroundSubtractorGMG()
|
|
|
26 |
while True:
|
27 |
ret, frame = cap.read()
|
28 |
fgmask = fgbg.apply(frame)
|
|
|
42 |
new_video_in = str(vid)
|
43 |
capture = cv2.VideoCapture(new_video_in)
|
44 |
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
|
45 |
fbox=[]
|
46 |
cnt=0
|
47 |
frame_count1= int(frame_count)
|
|
|
51 |
frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
|
52 |
cv2.imwrite(f'{uid}/{i}.png',frame_ff)
|
53 |
fbox.append(f'{uid}/{i}.png')
|
|
|
54 |
return fbox,fbox
|
55 |
|
|
|
|
|
56 |
def im_2_vid(images,fps):
|
57 |
#width, height = Image.open(images[0]).size
|
58 |
this_im=cv2.imread(images[0])
|
59 |
height=this_im.shape[0]
|
60 |
+
width= this_im.shape[1]
|
61 |
print (width,height)
|
62 |
size = (width, height)
|
63 |
codec = cv2.VideoWriter_fourcc(*'mp4v') #DIVX, DIVD
|
|
|
70 |
capture = cv2.VideoCapture(new_video_in)
|
71 |
frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
72 |
fps = capture.get(cv2.CAP_PROP_FPS)
|
|
|
73 |
fbox2=[]
|
74 |
cnt=0
|
75 |
frame_count1= int(frame_count)
|
|
|
86 |
|
87 |
out_vid = im_2_vid(fbox2, fps)
|
88 |
return (fbox2,frame_num,out_vid)
|
|
|
89 |
|
90 |
def rem_bg_og(inp):
|
91 |
#transparent background .mov
|
|
|
98 |
#os.system(f'backgroundremover -i "/path/to/video.mp4" -tg -o "output.gif"')
|
99 |
#output to matte background
|
100 |
os.system(f'backgroundremover -i "{inp}" -mk -o "{inp}.matte.mp4"')
|
|
|
|
|
101 |
return f'{inp}.matte.mp4'
|
102 |
+
|
103 |
def predict(text, url_params):
|
104 |
mod_url=""
|
105 |
mod=gr.HTML("")
|
|
|
143 |
fps = capture.get(cv2.CAP_PROP_FPS)
|
144 |
fbox3=[]
|
145 |
frame_count1= int(frame_count)
|
|
|
146 |
start = (int(fps*int(start_f)))
|
|
|
147 |
capture.set(cv2.CAP_PROP_POS_FRAMES, i-1)
|
|
|
|
|
|
|
148 |
for i in range(int(frame_count1)):
|
149 |
capture.set(cv2.CAP_PROP_POS_FRAMES, i)
|
150 |
ret, frame_f = capture.read(i)
|
|
|
156 |
frame_num=f'Working on {i+1} of {frame_count}'
|
157 |
yield fbox2,frame_num,None
|
158 |
|
|
|
|
|
|
|
159 |
with gr.Blocks() as app:
|
160 |
with gr.Tab("Load"):
|
161 |
with gr.Row():
|