Omnibus commited on
Commit
5bdf543
·
1 Parent(s): f950f97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -18
app.py CHANGED
@@ -6,8 +6,11 @@ import rembg
6
  import cv2
7
  import uuid
8
  uid=uuid.uuid4()
9
- if not os.path.exists(f'{uid}'): os.makedirs(f'{uid}')
 
 
10
  load_js = """
 
11
  function(text_input, url_params) {
12
  console.log(text_input, url_params);
13
  const params = new URLSearchParams(window.location.search);
@@ -39,10 +42,10 @@ def load_video(vid):
39
 
40
 
41
  def capture_function(vid=None,frame_count=None):
42
- if vid==None:
43
- vid=img_file
44
  new_video_in = str(vid)
45
- capture = cv2.VideoCapture(new_video_in)
 
 
46
  fbox=[]
47
  cnt=0
48
  frame_count1= int(frame_count)
@@ -53,21 +56,40 @@ def capture_function(vid=None,frame_count=None):
53
  cv2.imwrite(f'{uid}/{i}.png',frame_ff)
54
  fbox.append(f'{uid}/{i}.png')
55
 
56
- capture.set(cv2.CAP_PROP_POS_FRAMES, int(frame_count)-1)
57
- ret, frame_f = capture.read()
58
- frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
59
- cv2.imwrite(f'{uid}/{int(frame_count)-1}.png',frame_ff)
60
- fbox.append(f'{uid}/{int(frame_count)-1}.png')
61
- capture.release()
62
  return fbox,fbox
63
 
64
 
65
- def rem_bg(inp):
66
- cap = cv2.VideoCapture(f'{inp}')
67
- ret, frame = cap.read()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- out = rem_bg()
70
- out = cv2.VideoWriter('video.avi',cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
71
 
72
 
73
 
@@ -121,7 +143,7 @@ with gr.Blocks() as app:
121
  frame_btn = gr.Button("Get Frames")
122
  with gr.Row():
123
  with gr.Column():
124
- frame_gal = gr.Gallery(columns=6,show_label=True)
125
  with gr.Column():
126
  frame_file = gr.Files()
127
 
@@ -132,14 +154,14 @@ with gr.Blocks() as app:
132
  in_vid=gr.Video()
133
  with gr.Column():
134
  #rem_vid=gr.Video()
135
- rem_vid=gr.Gallery()
136
  with gr.Row(visible=False):
137
  text_input=gr.Textbox()
138
  text_output=gr.Textbox()
139
  url_params=gr.JSON()
140
  outp_vid.change(load_video,outp_vid,frame_count)
141
  frame_btn.click(capture_function,[outp_vid,frame_count],[frame_gal,frame_file])
142
- rem_btn.click(rem_cv,in_vid,rem_vid)
143
  go_btn.click(dl,inp_url,[outp_vid,in_vid,outp_file])
144
  app.load(fn=predict, inputs=[text_input,url_params], outputs=[text_output,text_input],_js=load_js)
145
  app.launch()
 
6
  import cv2
7
  import uuid
8
  uid=uuid.uuid4()
9
+ if not os.path.exists(f'{uid}-frames'): os.makedirs(f'{uid}')
10
+ if not os.path.exists(f'{uid}-rembg'): os.makedirs(f'{uid}')
11
+
12
  load_js = """
13
+
14
  function(text_input, url_params) {
15
  console.log(text_input, url_params);
16
  const params = new URLSearchParams(window.location.search);
 
42
 
43
 
44
  def capture_function(vid=None,frame_count=None):
 
 
45
  new_video_in = str(vid)
46
+ capture = cv2.VideoCapture(new_video_in)
47
+ #frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
48
+
49
  fbox=[]
50
  cnt=0
51
  frame_count1= int(frame_count)
 
56
  cv2.imwrite(f'{uid}/{i}.png',frame_ff)
57
  fbox.append(f'{uid}/{i}.png')
58
 
59
+ #capture.set(cv2.CAP_PROP_POS_FRAMES, int(frame_count)-1)
60
+ #ret, frame_f = capture.read()
61
+ #frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
62
+ #cv2.imwrite(f'{uid}/{int(frame_count)-1}.png',frame_ff)
63
+ #fbox.append(f'{uid}/{int(frame_count)-1}.png')
64
+ #capture.release()
65
  return fbox,fbox
66
 
67
 
68
+ def rem_bg(vid,frame_count):
69
+ new_video_in = str(vid)
70
+ capture = cv2.VideoCapture(new_video_in)
71
+ fbox2=[]
72
+ cnt=0
73
+ frame_count1= int(frame_count)
74
+ for i in range(int(frame_count1)):
75
+ capture.set(cv2.CAP_PROP_POS_FRAMES, i)
76
+ ret, frame_f = capture.read(i)
77
+ frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
78
+ out = rem_bg(frame_ff)
79
+
80
+ cv2.imwrite(f'{uid}-rembg/{i}.png',out)
81
+ fbox2.append(f'{uid}-rembg/{i}.png')
82
+
83
+ #capture.set(cv2.CAP_PROP_POS_FRAMES, int(frame_count)-1)
84
+ #ret, frame_f = capture.read()
85
+ #frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
86
+ #cv2.imwrite(f'{uid}/{int(frame_count)-1}.png',frame_ff)
87
+ #fbox.append(f'{uid}/{int(frame_count)-1}.png')
88
+ #capture.release()
89
+ #out = cv2.VideoWriter('video.avi',cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
90
+ return fbox2
91
+
92
 
 
 
93
 
94
 
95
 
 
143
  frame_btn = gr.Button("Get Frames")
144
  with gr.Row():
145
  with gr.Column():
146
+ frame_gal = gr.Gallery(columns=6)
147
  with gr.Column():
148
  frame_file = gr.Files()
149
 
 
154
  in_vid=gr.Video()
155
  with gr.Column():
156
  #rem_vid=gr.Video()
157
+ rem_vid=gr.Gallery(columns=6)
158
  with gr.Row(visible=False):
159
  text_input=gr.Textbox()
160
  text_output=gr.Textbox()
161
  url_params=gr.JSON()
162
  outp_vid.change(load_video,outp_vid,frame_count)
163
  frame_btn.click(capture_function,[outp_vid,frame_count],[frame_gal,frame_file])
164
+ rem_btn.click(rem_cv,[outp_vid,frame_count],rem_vid)
165
  go_btn.click(dl,inp_url,[outp_vid,in_vid,outp_file])
166
  app.load(fn=predict, inputs=[text_input,url_params], outputs=[text_output,text_input],_js=load_js)
167
  app.launch()