Omnibus commited on
Commit
79bbd0e
·
1 Parent(s): 5e856ec

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +379 -0
app.py ADDED
@@ -0,0 +1,379 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ from PIL import Image
4
+ import numpy as np
5
+ import sys
6
+ import os
7
+ from pathlib import Path
8
+ from os import listdir
9
+ from os.path import isfile, join
10
+ import rembg
11
+
12
+ #frame_num=0
13
+ #frame_count=0
14
+ vid_file="vidcut0001-0250.mp4"
15
+ #capture = cv2.VideoCapture(vid_file)
16
+
17
+ rm_bg0=rembg()
18
+ rm_bg1=rembg()
19
+ rm_bg2=rembg()
20
+ rm_bg3=rembg()
21
+ rm_bg4=rembg()
22
+ rm_bg5=rembg()
23
+ rm_bg6=rembg()
24
+ rm_bg7=rembg()
25
+ rm_bg8=rembg()
26
+ rm_bg9=rembg()
27
+
28
+
29
+ css='''
30
+ .dark .gr-box{
31
+ background:#344d74;!important;
32
+ opacity:1;!important;
33
+ }
34
+ .p-2 {
35
+ background:#344d74;!important;
36
+ opacity:1;!important;
37
+ }
38
+ .gap-4 {
39
+ background:#6681ab;!important;
40
+ opacity:1;!important;
41
+ }
42
+ .dark .gr-padded{
43
+ background:#21314a;!important;
44
+ opacity:1;!important;
45
+ }
46
+ '''
47
+ def load_video(vid):
48
+ new_video_in = str(vid)
49
+ capture = cv2.VideoCapture(new_video_in)
50
+ frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
51
+ tmp_frame='1'
52
+ capture.release()
53
+ return vid,frame_count,frame_count
54
+ global f_proc
55
+ f_proc=0
56
+ def preview_function(vid,frame_count, denom):
57
+ #if vid==None:
58
+ #vid=img_file
59
+ new_video_in = str(vid)
60
+ capture = cv2.VideoCapture(new_video_in)
61
+ global fbox
62
+
63
+ fbox=[]
64
+ cnt=0
65
+ frame_count1= int(frame_count)-int(denom)
66
+ for i in range(int(frame_count1)):
67
+ if cnt == int(denom):
68
+ cnt=0
69
+ capture.set(cv2.CAP_PROP_POS_FRAMES, i-1)
70
+ ret, frame_f = capture.read(i-1)
71
+ frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
72
+ #frame_fff=Image.fromarray(frame_ff)
73
+ fbox.append(frame_ff)
74
+
75
+ else:
76
+ pass
77
+ cnt+=1
78
+ capture.set(cv2.CAP_PROP_POS_FRAMES, int(frame_count)-1)
79
+ ret, frame_f = capture.read()
80
+ frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
81
+ #frame_fff=Image.fromarray(frame_ff)
82
+ fbox.append(frame_ff)
83
+ capture.release()
84
+ f_box_count=len(fbox)
85
+ return fbox, f_box_count
86
+
87
+ def capture_function(vid=None,img_file=None,frame_count=None,frame_total=None, frame_col=None):
88
+ if vid==None:
89
+ vid=img_file
90
+ new_video_in = str(vid)
91
+ capture = cv2.VideoCapture(new_video_in)
92
+ fbox=[]
93
+ cnt=0
94
+ frame_count1= int(frame_count)-int(denom)
95
+ for i in range(int(frame_count1)):
96
+ if cnt == int(denom):
97
+ cnt=0
98
+ capture.set(cv2.CAP_PROP_POS_FRAMES, i)
99
+ ret, frame_f = capture.read(i)
100
+ frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
101
+
102
+ fbox.append(frame_ff)
103
+
104
+ else:
105
+ pass
106
+ cnt+=1
107
+ capture.set(cv2.CAP_PROP_POS_FRAMES, int(frame_count)-1)
108
+ ret, frame_f = capture.read()
109
+ frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
110
+ fbox.append(frame_ff)
111
+ capture.release()
112
+ return fbox
113
+ '''[0],fbox[1],fbox[2],fbox[3],fbox[4],fbox[5],fbox[6],fbox[7],fbox[8],fbox[9],
114
+ fbox[10],fbox[11],fbox[12],fbox[13],fbox[14],fbox[15],fbox[16],fbox[17],fbox[18],fbox[19],
115
+ fbox[20],fbox[21],fbox[22],fbox[23],fbox[24],fbox[25],fbox[26],fbox[27],fbox[28],fbox[29],
116
+ fbox[30],fbox[31],fbox[32],fbox[33],fbox[34],fbox[35],fbox[36],fbox[37],fbox[38],fbox[39],
117
+ fbox[40],fbox[41],fbox[42],fbox[43],fbox[44],fbox[45],fbox[46],fbox[47],fbox[48],fbox[49],
118
+ fbox[50],fbox[51],fbox[52],fbox[53],fbox[54],fbox[55],fbox[56],fbox[57],fbox[58],fbox[59],
119
+ fbox[60],fbox[61],fbox[62],fbox[63],fbox[64],fbox[65],fbox[66],fbox[67],fbox[68],fbox[69],
120
+ fbox[70],fbox[71],fbox[72],fbox[73],fbox[74],fbox[75],fbox[76],fbox[77],fbox[78],fbox[79],
121
+ fbox[80],fbox[81],fbox[82],fbox[83],fbox[84],fbox[85],fbox[86],fbox[87],fbox[88],fbox[89],
122
+ fbox[90],fbox[91],fbox[92],fbox[93],fbox[94],fbox[95],fbox[96],fbox[97],fbox[98],fbox[99],
123
+ fbox[100]'''
124
+
125
+
126
+ def remove_bg_func0(input):
127
+ img=fbox[0+int(input)]
128
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
129
+ cv2.imwrite(f"b-{input}0.png", img)
130
+ output=rm_bg0(f"b-{input}0.png")
131
+ return output
132
+ def remove_bg_func1(input):
133
+ img=fbox[1+int(input)]
134
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
135
+ cv2.imwrite(f"b-{input}1.png", img)
136
+ output=rm_bg1(f"b-{input}1.png")
137
+ return output
138
+ def remove_bg_func2(input):
139
+ img=fbox[2+int(input)]
140
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
141
+ cv2.imwrite(f"b-{input}2.png", img)
142
+ output=rm_bg2(f"b-{input}2.png")
143
+ return output
144
+ def remove_bg_func3(input):
145
+ img=fbox[3+int(input)]
146
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
147
+ cv2.imwrite(f"b-{input}3.png", img)
148
+ output=rm_bg3(f"b-{input}3.png")
149
+ return output
150
+ def remove_bg_func4(input):
151
+ img=fbox[4+int(input)]
152
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
153
+ cv2.imwrite(f"b-{input}4.png", img)
154
+ output=rm_bg4(f"b-{input}4.png")
155
+ return output
156
+ def remove_bg_func5(input):
157
+ img=fbox[5+int(input)]
158
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
159
+ cv2.imwrite(f"b-{input}5.png", img)
160
+ output=rm_bg5(f"b-{input}5.png")
161
+ return output
162
+ def remove_bg_func6(input):
163
+ img=fbox[6+int(input)]
164
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
165
+ cv2.imwrite(f"b-{input}6.png", img)
166
+ output=rm_bg6(f"b-{input}6.png")
167
+ return output
168
+ def remove_bg_func7(input):
169
+ img=fbox[7+int(input)]
170
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
171
+ cv2.imwrite(f"b-{input}7.png", img)
172
+ output=rm_bg7(f"b-{input}7.png")
173
+ return output
174
+ def remove_bg_func8(input):
175
+ img=fbox[8+int(input)]
176
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
177
+ cv2.imwrite(f"b-{input}8.png", img)
178
+ output=rm_bg8(f"b-{input}8.png")
179
+ return output
180
+ def remove_bg_func9(input):
181
+ img=fbox[9+int(input)]
182
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
183
+ cv2.imwrite(f"b-{input}9.png", img)
184
+ output=rm_bg9(f"b-{input}9.png")
185
+ return output
186
+
187
+
188
+ def main():
189
+ with gr.Blocks(css=css) as demo:
190
+ with gr.Box():
191
+ with gr.Group():
192
+
193
+ with gr.Row():
194
+
195
+ with gr.Column():
196
+
197
+ #vid=gr.Video(gr_input_type="file")
198
+ #vid=gr.PlayableVideo(value=vid_file, gr_input_type="filepath", file_extensions="mp4", visible=True)
199
+ vid=gr.Video(type="filepath", format="mp4", visible=True)
200
+ img_file=gr.Textbox(value=f"{vid_file}")
201
+ with gr.Row():
202
+ pv_btn=gr.Button("Preview Frames")
203
+ go_btn=gr.Button("Capture Frames")
204
+ ex_btn=gr.Button("Load Example")
205
+ with gr.Column():
206
+ with gr.Box():
207
+ #frame_slider=gr.Slider(1, frame_count=frame_count, value=frame_num, step=1)
208
+ with gr.Row():
209
+ every_n=gr.Textbox(label="every_n",value='10')
210
+ frame_tot=gr.Textbox(label="Total Frames",value='0')
211
+ with gr.Row():
212
+ idk=gr.Textbox(label="",value='10')
213
+ frames_to_proc=gr.Textbox(label="Selected Frames",value='0')
214
+ with gr.Row():
215
+ start_box=gr.Textbox(label="Start",value='0', interactive=True)
216
+ end_box=gr.Textbox(label="End",value='0', interactive=True)
217
+
218
+ with gr.Row():
219
+ with gr.Column():
220
+ with gr.Row():
221
+
222
+ f_1_btn=gr.Button("8")
223
+ f_2_btn=gr.Button("12")
224
+ f_3_btn=gr.Button("16")
225
+ f_4_btn=gr.Button("36")
226
+
227
+
228
+
229
+
230
+
231
+ with gr.Row():
232
+ bg_rgb_btn=gr.Button("Remove BG RGB")
233
+ depth_btn=gr.Button("Depth Frames")
234
+
235
+ bg_dpt_btn=gr.Button("Remove BG Depth")
236
+ make_3d=gr.Button("Make 3D")
237
+
238
+ with gr.Tab("RGB"):
239
+ with gr.Column():
240
+ with gr.Row():
241
+
242
+ fw1=gr.Gallery(label="Main", type="filepath").style(grid=10)
243
+ with gr.Row():
244
+ file_out=gr.File(file_count="list")
245
+
246
+ with gr.Tab("Remove BG RGB"):
247
+ with gr.Column():
248
+ with gr.Row():
249
+ f_proc=gr.Textbox(label="Frames Processed", show_progress=False)
250
+ #with gr.Group():
251
+ with gr.Group():
252
+ with gr.Row():
253
+ #with gr.Row():
254
+
255
+ bdfw1=gr.Pil(label="Frame 1", type="filepath")
256
+ bdfw2=gr.Pil(label="Frame 2", type="filepath")
257
+ bdfw3=gr.Pil(label="Frame 3", type="filepath")
258
+ bdfw4=gr.Pil(label="Frame 4", type="filepath")
259
+ bdfw5=gr.Pil(label="Frame 5", type="filepath")
260
+ bdfw6=gr.Pil(label="Frame 6", type="filepath")
261
+ bdfw7=gr.Pil(label="Frame 7", type="filepath")
262
+ bdfw8=gr.Pil(label="Frame 8", type="filepath")
263
+ bdfw9=gr.Pil(label="Frame 9", type="filepath")
264
+ bdfw10=gr.Pil(label="Frame 10", type="filepath")
265
+ bdfw11=gr.Pil(label="Frame 11", type="filepath")
266
+ bdfw12=gr.Pil(label="Frame 12", type="filepath")
267
+ bdfw13=gr.Pil(label="Frame 13", type="filepath")
268
+ bdfw14=gr.Pil(label="Frame 14", type="filepath")
269
+ bdfw15=gr.Pil(label="Frame 15", type="filepath")
270
+ bdfw16=gr.Pil(label="Frame 16", type="filepath")
271
+ bdfw17=gr.Pil(label="Frame 17", type="filepath")
272
+ bdfw18=gr.Pil(label="Frame 18", type="filepath")
273
+ bdfw19=gr.Pil(label="Frame 19", type="filepath")
274
+ bdfw20=gr.Pil(label="Frame 20", type="filepath")
275
+ bdfw21=gr.Pil(label="Frame 21", type="filepath")
276
+ bdfw22=gr.Pil(label="Frame 22", type="filepath")
277
+ bdfw23=gr.Pil(label="Frame 23", type="filepath")
278
+ bdfw24=gr.Pil(label="Frame 24", type="filepath")
279
+ bdfw25=gr.Pil(label="Frame 25", type="filepath")
280
+ bdfw26=gr.Pil(label="Frame 26", type="filepath")
281
+ bdfw27=gr.Pil(label="Frame 27", type="filepath")
282
+ bdfw28=gr.Pil(label="Frame 28", type="filepath")
283
+ bdfw29=gr.Pil(label="Frame 29", type="filepath")
284
+ bdfw30=gr.Pil(label="Frame 30", type="filepath")
285
+ bdfw31=gr.Pil(label="Frame 31", type="filepath")
286
+ bdfw32=gr.Pil(label="Frame 32", type="filepath")
287
+ bdfw33=gr.Pil(label="Frame 33", type="filepath")
288
+ bdfw34=gr.Pil(label="Frame 34", type="filepath")
289
+ bdfw35=gr.Pil(label="Frame 35", type="filepath")
290
+ bdfw36=gr.Pil(label="Frame 36", type="filepath")
291
+ bdfw37=gr.Pil(label="Frame 37", type="filepath")
292
+ bdfw38=gr.Pil(label="Frame 38", type="filepath")
293
+ bdfw39=gr.Pil(label="Frame 39", type="filepath")
294
+ bdfw40=gr.Pil(label="Frame 40", type="filepath")
295
+ bdfw41=gr.Pil(label="Frame 41", type="filepath")
296
+ bdfw42=gr.Pil(label="Frame 42", type="filepath")
297
+ bdfw43=gr.Pil(label="Frame 43", type="filepath")
298
+ bdfw44=gr.Pil(label="Frame 44", type="filepath")
299
+ bdfw45=gr.Pil(label="Frame 45", type="filepath")
300
+ bdfw46=gr.Pil(label="Frame 46", type="filepath")
301
+ bdfw47=gr.Pil(label="Frame 47", type="filepath")
302
+ bdfw48=gr.Pil(label="Frame 48", type="filepath")
303
+ bdfw49=gr.Pil(label="Frame 49", type="filepath")
304
+ bdfw50=gr.Pil(label="Frame 50", type="filepath")
305
+
306
+
307
+
308
+ with gr.Tab("Process", visible=False):
309
+ inpu1=gr.Textbox(value=0, visible=False)
310
+ inpu2=gr.Textbox(value=10, visible=False)
311
+ inpu3=gr.Textbox(value=20, visible=False)
312
+ inpu4=gr.Textbox(value=30, visible=False)
313
+ inpu5=gr.Textbox(value=40, visible=False)
314
+
315
+
316
+
317
+ bg_rgb_btn.click(remove_bg_func0, inputs=[inpu1], outputs=[bdfw1])
318
+ bg_rgb_btn.click(remove_bg_func1, inputs=[inpu1], outputs=[bdfw2])
319
+ bg_rgb_btn.click(remove_bg_func2, inputs=[inpu1], outputs=[bdfw3])
320
+ bg_rgb_btn.click(remove_bg_func3, inputs=[inpu1], outputs=[bdfw4])
321
+ bg_rgb_btn.click(remove_bg_func4, inputs=[inpu1], outputs=[bdfw5])
322
+ bg_rgb_btn.click(remove_bg_func5, inputs=[inpu1], outputs=[bdfw6])
323
+ bg_rgb_btn.click(remove_bg_func6, inputs=[inpu1], outputs=[bdfw7])
324
+ bg_rgb_btn.click(remove_bg_func7, inputs=[inpu1], outputs=[bdfw8])
325
+ bg_rgb_btn.click(remove_bg_func8, inputs=[inpu1], outputs=[bdfw9])
326
+ bg_rgb_btn.click(remove_bg_func9, inputs=[inpu1], outputs=[bdfw10])
327
+
328
+ bdfw1.change(remove_bg_func0, inputs=[inpu2], outputs=[bdfw11])
329
+ bdfw2.change(remove_bg_func1, inputs=[inpu2], outputs=[bdfw12])
330
+ bdfw3.change(remove_bg_func2, inputs=[inpu2], outputs=[bdfw13])
331
+ bdfw4.change(remove_bg_func3, inputs=[inpu2], outputs=[bdfw14])
332
+ bdfw5.change(remove_bg_func4, inputs=[inpu2], outputs=[bdfw15])
333
+ bdfw6.change(remove_bg_func5, inputs=[inpu2], outputs=[bdfw16])
334
+ bdfw7.change(remove_bg_func6, inputs=[inpu2], outputs=[bdfw17])
335
+ bdfw8.change(remove_bg_func7, inputs=[inpu2], outputs=[bdfw18])
336
+ bdfw9.change(remove_bg_func8, inputs=[inpu2], outputs=[bdfw19])
337
+ bdfw10.change(remove_bg_func9, inputs=[inpu2], outputs=[bdfw20])
338
+
339
+ bdfw11.change(remove_bg_func0, inputs=[inpu3], outputs=[bdfw21])
340
+ bdfw12.change(remove_bg_func1, inputs=[inpu3], outputs=[bdfw22])
341
+ bdfw13.change(remove_bg_func2, inputs=[inpu3], outputs=[bdfw23])
342
+ bdfw14.change(remove_bg_func3, inputs=[inpu3], outputs=[bdfw24])
343
+ bdfw15.change(remove_bg_func4, inputs=[inpu3], outputs=[bdfw25])
344
+ bdfw16.change(remove_bg_func5, inputs=[inpu3], outputs=[bdfw26])
345
+ bdfw17.change(remove_bg_func6, inputs=[inpu3], outputs=[bdfw27])
346
+ bdfw18.change(remove_bg_func7, inputs=[inpu3], outputs=[bdfw28])
347
+ bdfw19.change(remove_bg_func8, inputs=[inpu3], outputs=[bdfw29])
348
+ bdfw20.change(remove_bg_func9, inputs=[inpu3], outputs=[bdfw30])
349
+
350
+ bdfw21.change(remove_bg_func0, inputs=[inpu4], outputs=[bdfw31])
351
+ bdfw22.change(remove_bg_func1, inputs=[inpu4], outputs=[bdfw32])
352
+ bdfw23.change(remove_bg_func2, inputs=[inpu4], outputs=[bdfw33])
353
+ bdfw24.change(remove_bg_func3, inputs=[inpu4], outputs=[bdfw34])
354
+ bdfw25.change(remove_bg_func4, inputs=[inpu4], outputs=[bdfw35])
355
+ bdfw26.change(remove_bg_func5, inputs=[inpu4], outputs=[bdfw36])
356
+ bdfw27.change(remove_bg_func6, inputs=[inpu4], outputs=[bdfw37])
357
+ bdfw28.change(remove_bg_func7, inputs=[inpu4], outputs=[bdfw38])
358
+ bdfw29.change(remove_bg_func8, inputs=[inpu4], outputs=[bdfw39])
359
+ bdfw30.change(remove_bg_func9, inputs=[inpu4], outputs=[bdfw40])
360
+
361
+ bdfw31.change(remove_bg_func0, inputs=[inpu5], outputs=[bdfw41])
362
+ bdfw32.change(remove_bg_func1, inputs=[inpu5], outputs=[bdfw42])
363
+ bdfw33.change(remove_bg_func2, inputs=[inpu5], outputs=[bdfw43])
364
+ bdfw34.change(remove_bg_func3, inputs=[inpu5], outputs=[bdfw44])
365
+ bdfw35.change(remove_bg_func4, inputs=[inpu5], outputs=[bdfw45])
366
+ bdfw36.change(remove_bg_func5, inputs=[inpu5], outputs=[bdfw46])
367
+ bdfw37.change(remove_bg_func6, inputs=[inpu5], outputs=[bdfw47])
368
+ bdfw38.change(remove_bg_func7, inputs=[inpu5], outputs=[bdfw48])
369
+ bdfw39.change(remove_bg_func8, inputs=[inpu5], outputs=[bdfw49])
370
+ bdfw40.change(remove_bg_func9, inputs=[inpu5], outputs=[bdfw50])
371
+
372
+ vid.change(load_video, inputs=[vid], outputs=[vid,frame_tot,end_box])
373
+ ex_btn.click(load_video, inputs=[img_file], outputs=[vid,frame_tot,end_box])
374
+ pv_btn.click(preview_function,inputs=[vid, frame_tot, every_n],outputs=[fw1,frames_to_proc])
375
+ demo.queue(concurrency_count=120)
376
+ demo.launch(enable_queue=True, max_threads=120)
377
+
378
+ if __name__ == "__main__":
379
+ main()