3
3
import gradio as gr
4
4
from PIL import Image
5
5
6
- from src import backbone
6
+ from src import backbone , video_mode
7
7
from src .core import core_generation_funnel , unload_models , run_makevideo
8
8
from src .depthmap_generation import ModelHolder
9
9
from src .gradio_args_transport import GradioComponentBundle
@@ -217,6 +217,33 @@ def open_folder_action():
217
217
else :
218
218
sp .Popen (["xdg-open" , path ])
219
219
220
+
221
+ def depthmap_mode_video (inp ):
222
+ inp += gr .File (elem_id = 'depthmap_input_video' , label = "Video or animated file" ,
223
+ file_count = "single" , interactive = True , type = "file" )
224
+ inp += gr .Checkbox (elem_id = "depthmap_vm_custom_checkbox" ,
225
+ label = "Use custom/pregenerated DepthMap video" , value = False )
226
+ inp += gr .File (elem_id = 'depthmap_vm_custom' , file_count = "single" ,
227
+ interactive = True , type = "file" , visible = False )
228
+ with gr .Row ():
229
+ inp += gr .Checkbox (elem_id = 'depthmap_vm_compress_checkbox' , label = "Compress colorvideos?" , value = False )
230
+ inp += gr .Slider (elem_id = 'depthmap_vm_compress_bitrate' , label = "Bitrate (kbit)" , visible = False ,
231
+ minimum = 1000 , value = 15000 , maximum = 50000 , step = 250 )
232
+
233
+ inp ['depthmap_vm_custom_checkbox' ].change (
234
+ fn = lambda v : inp ['depthmap_vm_custom' ].update (visible = v ),
235
+ inputs = [inp ['depthmap_vm_custom_checkbox' ]],
236
+ outputs = [inp ['depthmap_vm_custom' ]]
237
+ )
238
+
239
+ inp ['depthmap_vm_compress_checkbox' ].change (
240
+ fn = lambda v : inp ['depthmap_vm_compress_bitrate' ].update (visible = v ),
241
+ inputs = [inp ['depthmap_vm_compress_checkbox' ]],
242
+ outputs = [inp ['depthmap_vm_compress_bitrate' ]]
243
+ )
244
+
245
+ return inp
246
+
220
247
def on_ui_tabs ():
221
248
inp = GradioComponentBundle ()
222
249
with gr .Blocks (analytics_enabled = False , title = "DepthMap" ) as depthmap_interface :
@@ -248,6 +275,8 @@ def on_ui_tabs():
248
275
label = "Skip generation and use (edited/custom) depthmaps "
249
276
"in output directory when a file already exists." ,
250
277
value = True )
278
+ with gr .TabItem ('Single Video' ) as depthmap_mode_3 :
279
+ inp = depthmap_mode_video (inp )
251
280
submit = gr .Button ('Generate' , elem_id = "depthmap_generate" , variant = 'primary' )
252
281
inp |= main_ui_panel (True ) # Main panel is inserted here
253
282
unloadmodels = gr .Button ('Unload models' , elem_id = "depthmap_unloadmodels" )
@@ -310,6 +339,7 @@ def on_ui_tabs():
310
339
depthmap_mode_0 .select (lambda : '0' , None , inp ['depthmap_mode' ])
311
340
depthmap_mode_1 .select (lambda : '1' , None , inp ['depthmap_mode' ])
312
341
depthmap_mode_2 .select (lambda : '2' , None , inp ['depthmap_mode' ])
342
+ depthmap_mode_3 .select (lambda : '3' , None , inp ['depthmap_mode' ])
313
343
314
344
def custom_depthmap_change_fn (turned_on ):
315
345
return inp ['custom_depthmap_img' ].update (visible = turned_on ), \
@@ -369,6 +399,18 @@ def custom_depthmap_change_fn(turned_on):
369
399
return depthmap_interface
370
400
371
401
402
+ def format_exception (e : Exception ):
403
+ traceback .print_exc ()
404
+ msg = '<h3>' + 'ERROR: ' + str (e ) + '</h3>' + '\n '
405
+ if 'out of GPU memory' not in msg :
406
+ msg += \
407
+ 'Please report this issue ' \
408
+ f'<a href="https://github.com/thygate/{ REPOSITORY_NAME } /issues">here</a>. ' \
409
+ 'Make sure to provide the full stacktrace: \n '
410
+ msg += '<code style="white-space: pre;">' + traceback .format_exc () + '</code>'
411
+ return msg
412
+
413
+
372
414
def run_generate (* inputs ):
373
415
inputs = GradioComponentBundle .enkey_to_dict (inputs )
374
416
depthmap_mode = inputs ['depthmap_mode' ]
@@ -381,10 +423,21 @@ def run_generate(*inputs):
381
423
custom_depthmap_img = inputs ['custom_depthmap_img' ]
382
424
383
425
inputimages = []
384
- # Allow supplying custom depthmaps
385
- inputdepthmaps = []
386
- # Also keep track of original file names
387
- inputnames = []
426
+ inputdepthmaps = [] # Allow supplying custom depthmaps
427
+ inputnames = [] # Also keep track of original file names
428
+
429
+ if depthmap_mode == '3' :
430
+ try :
431
+ custom_depthmap = inputs ['depthmap_vm_custom' ] \
432
+ if inputs ['depthmap_vm_custom_checkbox' ] else None
433
+ colorvids_bitrate = inputs ['depthmap_vm_compress_bitrate' ] \
434
+ if inputs ['depthmap_vm_compress_checkbox' ] else None
435
+ ret = video_mode .gen_video (
436
+ inputs ['depthmap_input_video' ], backbone .get_outpath (), inputs , custom_depthmap , colorvids_bitrate )
437
+ return [], None , None , ret
438
+ except Exception as e :
439
+ ret = format_exception (e )
440
+ return [], None , None , ret
388
441
389
442
if depthmap_mode == '2' and depthmap_batch_output_dir != '' :
390
443
outpath = depthmap_batch_output_dir
@@ -410,7 +463,9 @@ def run_generate(*inputs):
410
463
image = Image .open (os .path .abspath (img .name ))
411
464
inputimages .append (image )
412
465
inputnames .append (os .path .splitext (img .orig_name )[0 ])
466
+ print (f'{ len (inputimages )} images will be processed' )
413
467
elif depthmap_mode == '2' : # Batch from Directory
468
+ # TODO: There is a RAM leak when we process batches, I can smell it! Or maybe it is gone.
414
469
assert not backbone .get_cmd_opt ('hide_ui_dir_config' , False ), '--hide-ui-dir-config option must be disabled'
415
470
if depthmap_batch_input_dir == '' :
416
471
return [], None , None , "Please select an input directory."
@@ -444,40 +499,40 @@ def run_generate(*inputs):
444
499
445
500
gen_obj = core_generation_funnel (outpath , inputimages , inputdepthmaps , inputnames , inputs , backbone .gather_ops ())
446
501
447
- show_images = []
502
+ # Saving images
503
+ img_results = []
504
+ results_total = 0
448
505
inpainted_mesh_fi = mesh_simple_fi = None
449
506
msg = "" # Empty string is never returned
450
507
while True :
451
508
try :
452
509
input_i , type , result = next (gen_obj )
510
+ results_total += 1
453
511
except StopIteration :
454
512
# TODO: return more info
455
- msg = '<h3>Successfully generated.</h3>'
513
+ msg = '<h3>Successfully generated</h3>' if results_total > 0 else \
514
+ '<h3>Successfully generated nothing - please check the settings and try again</h3>'
456
515
break
457
516
except Exception as e :
458
- traceback .print_exc ()
459
- msg = '<h3>' + 'ERROR: ' + str (e ) + '</h3>' + '\n '
460
- if 'out of GPU memory' not in msg :
461
- msg += \
462
- 'Please report this issue ' \
463
- f'<a href="https://github.com/thygate/{ REPOSITORY_NAME } /issues">here</a>. ' \
464
- 'Make sure to provide the full stacktrace: \n '
465
- msg += '<code style="white-space: pre;">' + traceback .format_exc () + '</code>'
517
+ msg = format_exception (e )
466
518
break
467
519
if type == 'simple_mesh' :
468
520
mesh_simple_fi = result
469
521
continue
470
522
if type == 'inpainted_mesh' :
471
523
inpainted_mesh_fi = result
472
524
continue
525
+ if not isinstance (result , Image .Image ):
526
+ print (f'This is not supposed to happen! Somehow output type { type } is not supported! Input_i: { input_i } .' )
527
+ continue
528
+ img_results += [(input_i , type , result )]
473
529
474
- basename = 'depthmap'
475
- if depthmap_mode == '2' and inputnames [input_i ] is not None and outpath != backbone .get_opt ('outdir_extras_samples' , None ):
476
- basename = Path (inputnames [input_i ]).stem
477
-
478
- show_images += [result ]
479
530
if inputs ["save_outputs" ]:
480
531
try :
532
+ basename = 'depthmap'
533
+ if depthmap_mode == '2' and inputnames [input_i ] is not None :
534
+ if outpath != backbone .get_opt ('outdir_extras_samples' , None ):
535
+ basename = Path (inputnames [input_i ]).stem
481
536
suffix = "" if type == "depth" else f"{ type } "
482
537
backbone .save_image (result , path = outpath , basename = basename , seed = None ,
483
538
prompt = None , extension = backbone .get_opt ('samples_format' , 'png' ), short_filename = True ,
@@ -496,4 +551,4 @@ def run_generate(*inputs):
496
551
if backbone .get_opt ('depthmap_script_show_3d_inpaint' , True ):
497
552
if inpainted_mesh_fi is not None and len (inpainted_mesh_fi ) > 0 :
498
553
display_mesh_fi = inpainted_mesh_fi
499
- return show_images , inpainted_mesh_fi , display_mesh_fi , msg .replace ('\n ' , '<br>' )
554
+ return map ( lambda x : x [ 2 ], img_results ) , inpainted_mesh_fi , display_mesh_fi , msg .replace ('\n ' , '<br>' )
0 commit comments