33import gradio as gr
44from PIL import Image
55
6- from src import backbone
6+ from src import backbone , video_mode
77from src .core import core_generation_funnel , unload_models , run_makevideo
88from src .depthmap_generation import ModelHolder
99from src .gradio_args_transport import GradioComponentBundle
@@ -217,6 +217,33 @@ def open_folder_action():
217217 else :
218218 sp .Popen (["xdg-open" , path ])
219219
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+
220247def on_ui_tabs ():
221248 inp = GradioComponentBundle ()
222249 with gr .Blocks (analytics_enabled = False , title = "DepthMap" ) as depthmap_interface :
@@ -248,6 +275,8 @@ def on_ui_tabs():
248275 label = "Skip generation and use (edited/custom) depthmaps "
249276 "in output directory when a file already exists." ,
250277 value = True )
278+ with gr .TabItem ('Single Video' ) as depthmap_mode_3 :
279+ inp = depthmap_mode_video (inp )
251280 submit = gr .Button ('Generate' , elem_id = "depthmap_generate" , variant = 'primary' )
252281 inp |= main_ui_panel (True ) # Main panel is inserted here
253282 unloadmodels = gr .Button ('Unload models' , elem_id = "depthmap_unloadmodels" )
@@ -310,6 +339,7 @@ def on_ui_tabs():
310339 depthmap_mode_0 .select (lambda : '0' , None , inp ['depthmap_mode' ])
311340 depthmap_mode_1 .select (lambda : '1' , None , inp ['depthmap_mode' ])
312341 depthmap_mode_2 .select (lambda : '2' , None , inp ['depthmap_mode' ])
342+ depthmap_mode_3 .select (lambda : '3' , None , inp ['depthmap_mode' ])
313343
314344 def custom_depthmap_change_fn (turned_on ):
315345 return inp ['custom_depthmap_img' ].update (visible = turned_on ), \
@@ -369,6 +399,18 @@ def custom_depthmap_change_fn(turned_on):
369399 return depthmap_interface
370400
371401
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+
372414def run_generate (* inputs ):
373415 inputs = GradioComponentBundle .enkey_to_dict (inputs )
374416 depthmap_mode = inputs ['depthmap_mode' ]
@@ -381,10 +423,21 @@ def run_generate(*inputs):
381423 custom_depthmap_img = inputs ['custom_depthmap_img' ]
382424
383425 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
388441
389442 if depthmap_mode == '2' and depthmap_batch_output_dir != '' :
390443 outpath = depthmap_batch_output_dir
@@ -410,7 +463,9 @@ def run_generate(*inputs):
410463 image = Image .open (os .path .abspath (img .name ))
411464 inputimages .append (image )
412465 inputnames .append (os .path .splitext (img .orig_name )[0 ])
466+ print (f'{ len (inputimages )} images will be processed' )
413467 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.
414469 assert not backbone .get_cmd_opt ('hide_ui_dir_config' , False ), '--hide-ui-dir-config option must be disabled'
415470 if depthmap_batch_input_dir == '' :
416471 return [], None , None , "Please select an input directory."
@@ -444,40 +499,40 @@ def run_generate(*inputs):
444499
445500 gen_obj = core_generation_funnel (outpath , inputimages , inputdepthmaps , inputnames , inputs , backbone .gather_ops ())
446501
447- show_images = []
502+ # Saving images
503+ img_results = []
504+ results_total = 0
448505 inpainted_mesh_fi = mesh_simple_fi = None
449506 msg = "" # Empty string is never returned
450507 while True :
451508 try :
452509 input_i , type , result = next (gen_obj )
510+ results_total += 1
453511 except StopIteration :
454512 # 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>'
456515 break
457516 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 )
466518 break
467519 if type == 'simple_mesh' :
468520 mesh_simple_fi = result
469521 continue
470522 if type == 'inpainted_mesh' :
471523 inpainted_mesh_fi = result
472524 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 )]
473529
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 ]
479530 if inputs ["save_outputs" ]:
480531 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
481536 suffix = "" if type == "depth" else f"{ type } "
482537 backbone .save_image (result , path = outpath , basename = basename , seed = None ,
483538 prompt = None , extension = backbone .get_opt ('samples_format' , 'png' ), short_filename = True ,
@@ -496,4 +551,4 @@ def run_generate(*inputs):
496551 if backbone .get_opt ('depthmap_script_show_3d_inpaint' , True ):
497552 if inpainted_mesh_fi is not None and len (inpainted_mesh_fi ) > 0 :
498553 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