Omnibus commited on
Commit
143cda3
1 Parent(s): 615c99b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -81
app.py CHANGED
@@ -133,18 +133,10 @@ def video_clip(program,fps,qual,model_name,denoise_strength,face_enhance,outscal
133
  mes="Writing Video"
134
  clip = ImageSequenceClip(fbox, fps = fps)
135
  yield _,_,"Writing Video"
136
-
137
- ###################
138
  videoclip = VideoFileClip(new_video_in)
139
  audioclip=videoclip.audio
140
- #clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(images, fps)
141
  clip.audio=audioclip
142
  clip.write_videofile(f"{uid}/enhanced_vid.mp4")
143
- ########################
144
-
145
-
146
-
147
- #clip.speedx(2).to_gif(f"{uid}/clip_gif.gif",program=program)
148
  yield _,_,"Saving Video"
149
  out = f"{uid}/enhanced_vid.mp4"
150
  yield out,out,"Video Complete"
@@ -157,13 +149,11 @@ def gif_clip(program,fps,qual,model_name,denoise_strength,face_enhance,outscale)
157
  frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
158
  fbox=[]
159
  out_box=[]
160
-
161
  cnt=0
162
  frame_count1= int(frame_count)
163
  for i in range(int(frame_count1)-1):
164
  capture.set(cv2.CAP_PROP_POS_FRAMES, i)
165
  ret, frame_f = capture.read(i)
166
- #frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
167
  cv2.imwrite(f'{uid}-frames/{i+1}.png',frame_f)
168
  fbox.append(f'{uid}-frames/{i+1}.png')
169
  mes=f'Working on {i+1} of {frame_count1}'
@@ -174,27 +164,34 @@ def gif_clip(program,fps,qual,model_name,denoise_strength,face_enhance,outscale)
174
  yield _,_,mes
175
  if qual:
176
  for i,img in enumerate(fbox):
177
- #print (img)
178
  out = os.path.abspath(img)
179
  out_url = f'{main_url}/file={out}'
180
  mes=f'Improving frame {i+1} of {frame_count}'
181
  yield _,_,mes
182
  out = esr(out_url, model_name, float(denoise_strength), face_enhance, int(outscale))
183
- #yield _,_,mes
184
- #cv2.imwrite(f'{img}',out)
185
  out_box.append(f'{out}')
186
  print (f'{main_url}/file={out}')
187
- #print (f'out::{out}')
188
- print (f'out_box::{out_box}')
189
  clip = ImageSequenceClip(out_box, fps = fps)
190
  else:
191
  clip = ImageSequenceClip(fbox, fps = fps)
192
  yield _,_,"Writing GIF"
193
  clip.speedx(2).to_gif(f"{uid}/clip_gif.gif",program=program)
194
  out = f"{uid}/clip_gif.gif"
195
- yield out,out,"GIF Complete"
196
-
 
 
 
197
 
 
 
 
 
 
 
 
 
 
198
 
199
  def update_speed(inp,clip_speed,fps):
200
  if "-split.mp4" in inp:
@@ -207,7 +204,52 @@ def update_speed(inp,clip_speed,fps):
207
  final.write_videofile(f'{out_inp}', fps=mod_fps)
208
  out = f'{out_inp}'
209
  return out,out,out
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
 
212
 
213
 
@@ -243,10 +285,27 @@ with gr.Blocks(css=css) as app:
243
  gr.Column()
244
  with gr.Column():
245
  choose_media = gr.Radio(label="Media Type",choices=["Image","Video","GIF"],value="Image")
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  with gr.Box(visible=True) as im_box:
247
  with gr.Group():
248
  inp_img_url=gr.Textbox(label="Image URL")
 
249
  inp_img=gr.Image(label = "Input Image")
 
 
 
250
  outp_img=gr.Image(label="Enhanced Image")
251
  with gr.Box(visible=False) as vid_box:
252
  with gr.Group():
@@ -270,23 +329,11 @@ with gr.Blocks(css=css) as app:
270
 
271
  with gr.Row():
272
  qual=gr.Checkbox(label="Improve Quality (slow)",value=False)
273
- with gr.Accordion("Quality Options", open=False):
274
  with gr.Row():
275
- model_name = gr.Dropdown(label="Real-ESRGAN Model",
276
- choices=["RealESRGAN_x4plus", "RealESRNet_x4plus", "RealESRGAN_x4plus_anime_6B",
277
- "RealESRGAN_x2plus", "realesr-general-x4v3"],
278
- value="realesr-general-x4v3", show_label=True)
279
- face_enhance = gr.Checkbox(label="Face Enhancement using GFPGAN",
280
- value=False, show_label=True)
281
- denoise_strength = gr.Slider(label="Denoise Strength",
282
- minimum=0, maximum=1, step=0.1, value=0.5)
283
- outscale = gr.Slider(label="Image Upscaling Factor",
284
- minimum=1, maximum=10, step=1, value=1, show_label=True)
285
- with gr.Accordion("Sample",open=False):
286
- with gr.Row():
287
- frame_targ=gr.Number(label="Frame",value=1)
288
- samp_btn=gr.Button("Get Sample")
289
- samp_im = gr.Image(label="Sample Frame")
290
 
291
 
292
  with gr.Column(visible=False) as gif_row:
@@ -309,56 +356,10 @@ with gr.Blocks(css=css) as app:
309
  text_output=gr.Textbox()
310
  url_params=gr.JSON()
311
 
312
- def echo_fn(inp):
313
- return inp
314
-
315
- def check_load(inp_url,outp_vid,hid_box,start_f,end_f):
316
- if outp_vid == None and inp_url !="":
317
- out_trim,in_vid,trim_count=trim_vid(hid_box,start_f,end_f)
318
- elif outp_vid !=None and inp_url == "":
319
- out_trim = None
320
- in_vid=outp_vid
321
- trim_count = ""
322
- return out_trim,in_vid,trim_count
323
- def run_samp(program,fps,qual,model_name,denoise_strength,face_enhance,outscale,frame_targ):
324
- _=None
325
- #fps=float(fps)
326
- new_video_in = str(f"{uid}-clip.mp4")
327
- capture = cv2.VideoCapture(new_video_in)
328
- #frame_count1= int(frame_count)
329
- i=int(frame_targ)
330
- capture.set(cv2.CAP_PROP_POS_FRAMES, i)
331
- ret, frame_f = capture.read(i)
332
- #frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
333
- cv2.imwrite(f'{uid}-frames/{i}.png',frame_f)
334
- out = os.path.abspath(f'{uid}-frames/{i}.png')
335
- out_url = f'{main_url}/file={out}'
336
- yield _,"Creating Sample Frame"
337
- out = esr(out_url, model_name, float(denoise_strength), face_enhance, int(outscale))
338
- yield out,"Sample Frame Complete"
339
 
340
- def new_media(inp):
341
- if inp == "Image":
342
- im_box = gr.update(visible=True)
343
- vid_box = gr.update(visible=False)
344
- gif_row = gr.update(visible=False)
345
- vid_row = gr.update(visible=False)
346
- return (im_box,vid_box,gif_row,vid_row)
347
- if inp == "Video":
348
- im_box = gr.update(visible=False)
349
- vid_box = gr.update(visible=True)
350
- gif_row = gr.update(visible=False)
351
- vid_row = gr.update(visible=True)
352
- return (im_box,vid_box,gif_row,vid_row)
353
- if inp == "GIF":
354
- im_box = gr.update(visible=False)
355
- vid_box = gr.update(visible=True)
356
- gif_row = gr.update(visible=True)
357
- vid_row = gr.update(visible=False)
358
- return (im_box,vid_box,gif_row,vid_row)
359
-
360
  choose_media.change(new_media,choose_media,[im_box,vid_box,gif_row,vid_row])
361
- samp_btn.click(run_samp,[choose_prog,fps,qual,model_name,denoise_strength,face_enhance,outscale,frame_targ],[samp_im,gif_stat])
362
  speed_btn.click(update_speed,[hid_box,clip_speed,hid_fps],[outp_vid,hid_box,outp_file])
363
  gif_btn.click(gif_clip,[choose_prog,fps,qual,model_name,denoise_strength,face_enhance,outscale],[gif_show,gif_file,gif_stat])
364
  vid_btn.click(video_clip,[choose_prog,fps,qual,model_name,denoise_strength,face_enhance,outscale],[gif_show,gif_file,vid_stat])
 
133
  mes="Writing Video"
134
  clip = ImageSequenceClip(fbox, fps = fps)
135
  yield _,_,"Writing Video"
 
 
136
  videoclip = VideoFileClip(new_video_in)
137
  audioclip=videoclip.audio
 
138
  clip.audio=audioclip
139
  clip.write_videofile(f"{uid}/enhanced_vid.mp4")
 
 
 
 
 
140
  yield _,_,"Saving Video"
141
  out = f"{uid}/enhanced_vid.mp4"
142
  yield out,out,"Video Complete"
 
149
  frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
150
  fbox=[]
151
  out_box=[]
 
152
  cnt=0
153
  frame_count1= int(frame_count)
154
  for i in range(int(frame_count1)-1):
155
  capture.set(cv2.CAP_PROP_POS_FRAMES, i)
156
  ret, frame_f = capture.read(i)
 
157
  cv2.imwrite(f'{uid}-frames/{i+1}.png',frame_f)
158
  fbox.append(f'{uid}-frames/{i+1}.png')
159
  mes=f'Working on {i+1} of {frame_count1}'
 
164
  yield _,_,mes
165
  if qual:
166
  for i,img in enumerate(fbox):
 
167
  out = os.path.abspath(img)
168
  out_url = f'{main_url}/file={out}'
169
  mes=f'Improving frame {i+1} of {frame_count}'
170
  yield _,_,mes
171
  out = esr(out_url, model_name, float(denoise_strength), face_enhance, int(outscale))
 
 
172
  out_box.append(f'{out}')
173
  print (f'{main_url}/file={out}')
 
 
174
  clip = ImageSequenceClip(out_box, fps = fps)
175
  else:
176
  clip = ImageSequenceClip(fbox, fps = fps)
177
  yield _,_,"Writing GIF"
178
  clip.speedx(2).to_gif(f"{uid}/clip_gif.gif",program=program)
179
  out = f"{uid}/clip_gif.gif"
180
+ yield out,out,"GIF Complete"
181
+
182
+ def improve_img(inp,model_name,denoise_strength,face_enhance,outscale):
183
+ _=None
184
+ yield _,"Reading image"
185
 
186
+ img=cv2.imread(inp)
187
+ cv2.imwrite(f'{uid}-frames/{i}.png',inp)
188
+ out = os.path.abspath(f'{uid}-frames/{i}.png')
189
+ out_url = f'{main_url}/file={out}'
190
+ yield _,"Improving image"
191
+ out = esr(out_url, model_name, float(denoise_strength), face_enhance, int(outscale))
192
+ yield out,"Complete"
193
+
194
+
195
 
196
  def update_speed(inp,clip_speed,fps):
197
  if "-split.mp4" in inp:
 
204
  final.write_videofile(f'{out_inp}', fps=mod_fps)
205
  out = f'{out_inp}'
206
  return out,out,out
207
+ def echo_fn(inp):
208
+ return inp
209
+ def check_load(inp_url,outp_vid,hid_box,start_f,end_f):
210
+ if outp_vid == None and inp_url !="":
211
+ out_trim,in_vid,trim_count=trim_vid(hid_box,start_f,end_f)
212
+ elif outp_vid !=None and inp_url == "":
213
+ out_trim = None
214
+ in_vid=outp_vid
215
+ trim_count = ""
216
+ return out_trim,in_vid,trim_count
217
+ def run_samp(program,fps,qual,model_name,denoise_strength,face_enhance,outscale,frame_targ):
218
+ _=None
219
+ #fps=float(fps)
220
+ new_video_in = str(f"{uid}-clip.mp4")
221
+ capture = cv2.VideoCapture(new_video_in)
222
+ #frame_count1= int(frame_count)
223
+ i=int(frame_targ)
224
+ capture.set(cv2.CAP_PROP_POS_FRAMES, i)
225
+ ret, frame_f = capture.read(i)
226
+ #frame_ff = cv2.cvtColor(frame_f, cv2.COLOR_BGR2RGB)
227
+ cv2.imwrite(f'{uid}-frames/{i}.png',frame_f)
228
+ out = os.path.abspath(f'{uid}-frames/{i}.png')
229
+ out_url = f'{main_url}/file={out}'
230
+ yield _,"Creating Sample Frame","Creating Sample Frame"
231
+ out = esr(out_url, model_name, float(denoise_strength), face_enhance, int(outscale))
232
+ yield out,"Sample Frame Complete","Sample Frame Complete"
233
 
234
+ def new_media(inp):
235
+ if inp == "Image":
236
+ im_box = gr.update(visible=True)
237
+ vid_box = gr.update(visible=False)
238
+ gif_row = gr.update(visible=False)
239
+ vid_row = gr.update(visible=False)
240
+ return (im_box,vid_box,gif_row,vid_row)
241
+ if inp == "Video":
242
+ im_box = gr.update(visible=False)
243
+ vid_box = gr.update(visible=True)
244
+ gif_row = gr.update(visible=False)
245
+ vid_row = gr.update(visible=True)
246
+ return (im_box,vid_box,gif_row,vid_row)
247
+ if inp == "GIF":
248
+ im_box = gr.update(visible=False)
249
+ vid_box = gr.update(visible=True)
250
+ gif_row = gr.update(visible=True)
251
+ vid_row = gr.update(visible=False)
252
+ return (im_box,vid_box,gif_row,vid_row)
253
 
254
 
255
 
 
285
  gr.Column()
286
  with gr.Column():
287
  choose_media = gr.Radio(label="Media Type",choices=["Image","Video","GIF"],value="Image")
288
+ with gr.Accordion("Quality Options", open=False):
289
+ with gr.Row():
290
+ model_name = gr.Dropdown(label="Real-ESRGAN Model",
291
+ choices=["RealESRGAN_x4plus", "RealESRNet_x4plus", "RealESRGAN_x4plus_anime_6B",
292
+ "RealESRGAN_x2plus", "realesr-general-x4v3"],
293
+ value="realesr-general-x4v3", show_label=True)
294
+ face_enhance = gr.Checkbox(label="Face Enhancement using GFPGAN",
295
+ value=False, show_label=True)
296
+ denoise_strength = gr.Slider(label="Denoise Strength",
297
+ minimum=0, maximum=1, step=0.1, value=0.5)
298
+ outscale = gr.Slider(label="Image Upscaling Factor",
299
+ minimum=1, maximum=10, step=1, value=1, show_label=True)
300
+
301
  with gr.Box(visible=True) as im_box:
302
  with gr.Group():
303
  inp_img_url=gr.Textbox(label="Image URL")
304
+ load_im_btn=gr.Button("Load URL")
305
  inp_img=gr.Image(label = "Input Image")
306
+ with gr.Row():
307
+ proc_im_btn=gr.Button("Improve")
308
+ im_stat=gr.Textbox(label="Status")
309
  outp_img=gr.Image(label="Enhanced Image")
310
  with gr.Box(visible=False) as vid_box:
311
  with gr.Group():
 
329
 
330
  with gr.Row():
331
  qual=gr.Checkbox(label="Improve Quality (slow)",value=False)
332
+ with gr.Accordion("Sample Frame",open=False):
333
  with gr.Row():
334
+ frame_targ=gr.Number(label="Frame",value=1)
335
+ samp_btn=gr.Button("Get Sample")
336
+ samp_im = gr.Image(label="Sample Frame")
 
 
 
 
 
 
 
 
 
 
 
 
337
 
338
 
339
  with gr.Column(visible=False) as gif_row:
 
356
  text_output=gr.Textbox()
357
  url_params=gr.JSON()
358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
 
360
+ proc_im_btn.click(improve_img,[inp,model_name,denoise_strength,face_enhance,outscale],[outp_img,im_stat])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  choose_media.change(new_media,choose_media,[im_box,vid_box,gif_row,vid_row])
362
+ samp_btn.click(run_samp,[choose_prog,fps,qual,model_name,denoise_strength,face_enhance,outscale,frame_targ],[samp_im,gif_stat,vid_stat])
363
  speed_btn.click(update_speed,[hid_box,clip_speed,hid_fps],[outp_vid,hid_box,outp_file])
364
  gif_btn.click(gif_clip,[choose_prog,fps,qual,model_name,denoise_strength,face_enhance,outscale],[gif_show,gif_file,gif_stat])
365
  vid_btn.click(video_clip,[choose_prog,fps,qual,model_name,denoise_strength,face_enhance,outscale],[gif_show,gif_file,vid_stat])