Omnibus commited on
Commit
72640c1
·
1 Parent(s): 0d06a67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -27
app.py CHANGED
@@ -60,10 +60,25 @@ def capture_function(vid=None):
60
  return fbox,fbox
61
 
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  def rem_bg(vid):
64
  new_video_in = str(vid)
65
  capture = cv2.VideoCapture(new_video_in)
66
  frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
 
67
 
68
  fbox2=[]
69
  cnt=0
@@ -77,32 +92,10 @@ def rem_bg(vid):
77
  cv2.imwrite(f'{uid}-rembg/{i}.png',out)
78
  fbox2.append(f'{uid}-rembg/{i}.png')
79
  frame_num=f'Working on {i+1} of {frame_count}'
80
- yield fbox2,frame_num
81
-
82
-
83
- def im_2_vid(images,fps):
84
- #width, height = Image.open(images[0]).size
85
- width, height = cv2.imread(images[0]).shape()
86
- print (width,height)
87
- size = (width, height)
88
- codec = cv2.VideoWriter_fourcc(*'mp4v') #DIVX, DIVD
89
- if (len(sys.argv) < 2 and n > 0):
90
- video = cv2.VideoWriter(vidName, codec, nfs, (W, H))
91
- else:
92
- video = cv2.VideoWriter(vidName, codec, nfs, size)
93
-
94
- #Do not open images in 'Image' as well as 'CV2', else following error occurs
95
- #<built-in function imread> returned NULL without setting an error
96
- for img in images:
97
- #In case images are of different size, resize them to user specified values
98
- if (len(sys.argv) < 2 and n > 0):
99
- newImg = cv2.imread(img)
100
- newImg = cv2.resize(newImg, (W, H))
101
- video.write(newImg)
102
- else:
103
- video.write(img)
104
-
105
 
 
 
106
 
107
 
108
  def rem_bg_og(inp):
@@ -139,8 +132,28 @@ def dl(inp):
139
  except Exception as e:
140
  print (e)
141
  out = None
142
- return out,out,out
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
 
 
 
144
  with gr.Blocks() as app:
145
  with gr.Tab("Load"):
146
 
@@ -154,6 +167,14 @@ with gr.Blocks() as app:
154
  frame_count=gr.Textbox(label="Frame Count",interactive = False)
155
  fps=gr.Textbox(label="FPS",interactive = False)
156
  outp_file=gr.Files()
 
 
 
 
 
 
 
 
157
  with gr.Tab("Frames"):
158
  frame_btn = gr.Button("Get Frames")
159
  with gr.Row():
@@ -175,9 +196,11 @@ with gr.Blocks() as app:
175
  text_input=gr.Textbox()
176
  text_output=gr.Textbox()
177
  url_params=gr.JSON()
 
 
178
  outp_vid.change(load_video,outp_vid,[frame_count,fps])
179
  frame_btn.click(capture_function,[outp_vid],[frame_gal,frame_file])
180
  rem_btn.click(rem_bg,[outp_vid],[rem_vid,frame_num])
181
- go_btn.click(dl,inp_url,[outp_vid,in_vid,outp_file])
182
  app.load(fn=predict, inputs=[text_input,url_params], outputs=[text_output,text_input],_js=load_js)
183
  app.launch()
 
60
  return fbox,fbox
61
 
62
 
63
+
64
+ def im_2_vid(images,fps):
65
+ #width, height = Image.open(images[0]).size
66
+ width, height = cv2.imread(images[0]).shape()
67
+ print (width,height)
68
+ size = (width, height)
69
+ codec = cv2.VideoWriter_fourcc(*'mp4v') #DIVX, DIVD
70
+ video = cv2.VideoWriter(vidName, codec, fps, size)
71
+
72
+ #Do not open images in 'Image' as well as 'CV2', else following error occurs
73
+ #<built-in function imread> returned NULL without setting an error
74
+ for img in images:
75
+ video.write(img)
76
+ return (video)
77
  def rem_bg(vid):
78
  new_video_in = str(vid)
79
  capture = cv2.VideoCapture(new_video_in)
80
  frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
81
+ fps = capture.get(cv2.CAP_PROP_FPS)
82
 
83
  fbox2=[]
84
  cnt=0
 
92
  cv2.imwrite(f'{uid}-rembg/{i}.png',out)
93
  fbox2.append(f'{uid}-rembg/{i}.png')
94
  frame_num=f'Working on {i+1} of {frame_count}'
95
+ yield fbox2,frame_num,None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
+ out_vid = im_2_vid(fbox2, fps)
98
+ return (fbox2,frame_num,out_vid)
99
 
100
 
101
  def rem_bg_og(inp):
 
132
  except Exception as e:
133
  print (e)
134
  out = None
135
+ return out,out,out,out
136
+ def trim_clip(inp):
137
+ new_video_in = str(vid)
138
+ capture = cv2.VideoCapture(new_video_in)
139
+ frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
140
+ fps = capture.get(cv2.CAP_PROP_FPS)
141
+ fbox3=[]
142
+ frame_count1= int(frame_count)
143
+ for i in range(int(frame_count1)):
144
+ capture.set(cv2.CAP_PROP_POS_FRAMES, i)
145
+ ret, frame_f = capture.read(i)
146
+ #frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
147
+ out = rm(frame_f)
148
+ print (i)
149
+ cv2.imwrite(f'{uid}-rembg/{i}.png',out)
150
+ fbox3.append(f'{uid}-rembg/{i}.png')
151
+ frame_num=f'Working on {i+1} of {frame_count}'
152
+ yield fbox2,frame_num,None
153
 
154
+
155
+
156
+
157
  with gr.Blocks() as app:
158
  with gr.Tab("Load"):
159
 
 
167
  frame_count=gr.Textbox(label="Frame Count",interactive = False)
168
  fps=gr.Textbox(label="FPS",interactive = False)
169
  outp_file=gr.Files()
170
+ with gr.Tab("Trim"):
171
+ with gr.Row():
172
+ start_f=gr.Number(value=0)
173
+ end_f=gr.Number(value=1)
174
+ trim_btn=gr.Button("Trim")
175
+ with gr.Row():
176
+ in_trim = gr.Video()
177
+ out_trim=gr.Video()
178
  with gr.Tab("Frames"):
179
  frame_btn = gr.Button("Get Frames")
180
  with gr.Row():
 
196
  text_input=gr.Textbox()
197
  text_output=gr.Textbox()
198
  url_params=gr.JSON()
199
+ in_trim.change(None,in_trim,out_trim)
200
+ trim_btn.click(trim_clip,in_trim,out_trim)
201
  outp_vid.change(load_video,outp_vid,[frame_count,fps])
202
  frame_btn.click(capture_function,[outp_vid],[frame_gal,frame_file])
203
  rem_btn.click(rem_bg,[outp_vid],[rem_vid,frame_num])
204
+ go_btn.click(dl,inp_url,[outp_vid,in_vid,outp_file,in_trim])
205
  app.load(fn=predict, inputs=[text_input,url_params], outputs=[text_output,text_input],_js=load_js)
206
  app.launch()