CoolFace
Apppublic

hololens/stable-diffusion-webui-depthmap-script

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
common_ui.cpython-310.pyc193 linesDownload Raw Back to __pycache__
1o

2��f��@s�ddlZddlmZddlZddlmZddlmZm	Z	ddl3mZmZm
Z
ddlmZddlmZddlTdd	lmZd4d�Ze�dd
�Zdd�Zdd�ZdZdd�Zdefdd�Zdd�ZdS)�N)�Path)�Image)�backbone�5video_mode)�core_generation_funnel�
unload_models�
run_makevideo)�ModelHolder)�GradioComponentBundle)�*)�GenerationOptionsc6Cslzddl}tj�|��d�}tj�|�st�|�WdSWdSty5}zt�	�WYd}~dSd}~ww)Nr�gradio)7�tempfile�os�path�joinZ8gettempdir�exists�mkdir�	Exception�	traceback�	print_exc)rr�e�r�EF:\3dpicmaker\stable-diffusion-webui-depthmap-script\src\common_ui.py�ensure_gradio_temp_directorys���rc9
s�
t��t�����t���-}�d|f8��tjtjdddgdd�f7��tjtjdgd�dd	d10�f7�Wd�n1s=wYt�	���}�d|f8�t���!�tj11tjdd
dd�f7��tjtjddd�f7�Wd�n1suwYt���$��tj
tjddddd�f7��tjtjddddd�f7�Wd�n1s�wYt����tjtjddd�f7�Wd�n1s�wYWd�n1s�wYt�	���}�d|f8�t���St����dtjddd�f7�Wd�n	1�swYt����tjtjdd �f7��tjtjd!d �f7�Wd�n	1�s/wYWd�n	1�s?wYt���#}�tjtjd"d �f7��tjtjd#d$d%gd&dd'�f7�Wd�n	1�sowYWd�n	1�swYt�	��q}�d(|f8�t���#�tjtjd)d �f7��tjtjd*d+d,gd&dd'�f7�Wd�n	1�s�wYtjdd-��$}�tjtjd.d/d0d1d�f7��tjtjd.d/d0d2d�f7�Wd�n	1�s�wYWd�n	1�s�wYt�	���t����tjtjd3d �f7�Wd�n	1�s"wYtjdd-���}t���#�tjtjgd4�d.t�d5d��rEd6nd7�d8d �f7�Wd�n	1�sYwYt���$�tj tjd9d:d;d<d�f7��tj!tjd=d>d;d?d�f7�Wd�n	1�s�wYt���3�tj"tjd@gdA�d&dB�f7��tj#tjdCd/dDd/dE�f7��tj$tjdFdGd9dHd�f7�Wd�n	1�s�wYWd�n	1�s�wYWd�n	1�s�wYt�	���t����tj%tjdId �f7�Wd�n	1�swYtjdd-���}t���+�tj&tjdJd �f7��tj'tjd/dKdDdLddM�f7���(tj'dNtj&�Wd�n	1�sOwYt���*�tj)tjdOd �f7��tj*tjd/dKdDdPd�f7���(tj*dNtj)�Wd�n	1�s�wYt���+�tj+tjdQd �f7��tj,tjd/dKdDdRddM�f7���(tj,dNtj+�Wd�n	1�s�wYt����tj-tjdSd �f7�Wd�n	1�s�wYWd�n	1�s�wYWd�n	1�s�wYt�dTd��r?t�	��(t����tj.tjdUd �f7�Wd�n	1�s*wYWd�n	1�s:wYt�	���t����tj/tjdVd �f7�Wd�n	1�s_wYtjdd-��N}t���tj0dWdX�Wd�n	1�s�wYt����tj1tjdYd �f7��tj2tjdZd �f7�Wd�n	1�s�wYWd�n	1�s�wYWd�n	1�s�wY|�r5t�	��Tt����tj3tjd[d �f7�Wd�n	1�s�wYtjdd-��}t�0d\��tj4tjd]d �f7�t�0d^�Wd�n	1�s wYWd�n	1�s0wYt�	���t����tj5tjd_d �f7�Wd�n	1�sUwYtjdd-��W}t����tj6tjd`d �f7��tj7tjdad �f7�Wd�n	1�s�wYt����tj8tjdbd&gdc�dd�f7�Wd�n	1�s�wYWd�n	1�s�wYWd�n	1�s�wYt�	��t�0t9�de��t�0df�Wd�n	1�s�wY�fdgdh�}	�tjj:|	�tj�tj
�tjgdi��tj12j:��fdjdk��tj13�tjg�tj�gdi���(�dltj��tjj:�fdmdk��tjg�tj14�tjgdi���(|dNtj���(tjdNtj���(tjdNtj���(tjdNtj���(|dNtj��tjj:dndk��tj�tjg�tjgddo��tjj:dpdk��tj�tjg�tjgddo���(|dNtj���(|dNtj%���(|dNtj/�|�rЈ�(|dNtj3���(|dNtj5�Wd��S1�s�wY�S)qN�depthmap_gen_row_0z15Compute onZGPUZCPU)�label�choices�valueZModel)Zres101zdpt_beit_large_512 (midas 3.1)zdpt_beit_large_384 (midas 3.1)zdpt_large_384 (midas 3.0)zdpt_hybrid_384 (midas 3.0)Z	midas_v21Zmidas_v21_smallzzoedepth_n (indoor)zzoedepth_k (outdoor)Zzoedepth_nkzMarigold v1zDepth AnythingzDepth Anything v2 Small�Depth Anything v2 BasezDepth Anything v2 Larger�index)rrr�type�depthmap_gen_row_1�BOOSTz8Generate depth map parts in a mosaic fashion - very slowF)r�inforzMatch net size to input sizez4Net size affects quality, performance and VRAM usage)rr$�@iz	Net width)�minimum�maximum�steprz16Net heightzTiling modez>Reduces seams that appear if the depthmap is tiled into a gridZdepthmap_gen_row_2�save_outputszSave OutputsT�rrzOutput DepthMap)rzInvert (black=near, white=far)z)Combine input and depthmap into one imagezCombine axisZVerticalZ17Horizontalr)rrr!�visible�depthmap_gen_row_3zClip and renormalize DepthMapZModeZRangeZOutliers�r+r�g����MbP?zFar clipz	Near clipz#Generate stereoscopic (3D) image(s))z18left-rightz19right-leftz20top-bottomz21bottom-topzred-cyan-anaglyphz	left-onlyz22only-rightzcyan-red-reverseanaglyphZ!depthmap_script_extra_stereomodes���Outputg�������?g��(\�.@g{�G�z�?zDivergence (3D effect)g�g@zSeparation (moves images apart)zGap fill technique)�noneZnaiveZnaive_interpolatingZpolylines_softZpolylines_sharp)rrr!zMagic exponent�)rr&r'r(g���?zBalance between eyeszGenerate NormalMapz!Smooth before calculating normals�zPre-smooth kernel size)r&r'r(rr+�23visible-ifzSobel gradientzSobel kernel sizez Smooth after calculating normalszPost-smooth kernel sizeZInvertZ#depthmap_script_gen_heatmap_from_uizGenerate HeatMapzGenerate simple 3D meshzPGenerates fast, accurate only with ZoeDepth models and no boost, no custom maps.�rzRemove occluded edgeszEquirectangular projectionzGenerate 3D inpainted meshz@Generation is sloooow. Required for generating videos from mesh.z.Generate 4 demo videos with 3D inpainted mesh.zIMore options for generating video can be found in the Generate video tab.zRemove backgroundzSave the foreground maskszPre-depth background removalzRembg Model)Zu2netZu2netpZu2net_human_segZsiluetazisnet-general-usezisnet-anime)rr!rz<br/>z�Information, comment and share @ <a href='https://github.com/thygate/stable-diffusion-webui-depthmap-script'>https://github.com/thygate/stable-diffusion-webui-depthmap-script</a>cs2t�|�\}}�tjj|d��tjj|d�fS)Nr7)r	Zget_default_net_size�go�	NET_WIDTH�update�24NET_HEIGHT)Z25model_type�w�h��inprr�update_default_net_size�s$z.main_ui_panel.<locals>.update_default_net_size��fn�inputs�outputscs(�tjj|d��j|o|d�fS)Nr-)r8�NET_SIZE_MATCHr:��a�b�r?Zoptions_depend_on_match_sizerr�<lambda>�s�zmain_ui_panel.<locals>.<lambda>�visible-if-notcsD|r�tjjdd��tjjdd�fS�tj���tj��fS)NFr7T)r8r#r:rE)rGr>rrrJ�s26� �cSs||kr|S|S�NrrFrrrrJ��)rBrCrDZ
show_progresscSs||kr|S|SrLrrFrrrrJ�rM);r27�gr�Blocks�Rowr8ZCOMPUTE_DEVICEZRadioZ28MODEL_TYPE�DropdownZBoxr#�CheckboxrEr9�Sliderr;ZTILING_MODE�Group�DO_OUTPUT_DEPTHZOUTPUT_DEPTH_INVERTZOUTPUT_DEPTH_COMBINEZOUTPUT_DEPTH_COMBINE_AXISZ	CLIPDEPTHZCLIPDEPTH_MODEZ
CLIPDEPTH_FARZCLIPDEPTH_NEARZ29GEN_STEREO�ColumnZSTEREO_MODESZ
CheckboxGroupr�get_optZSTEREO_DIVERGENCEZSTEREO_SEPARATIONZSTEREO_FILL_ALGOZSTEREO_OFFSET_EXPONENTZSTEREO_BALANCEZ
GEN_NORMALMAPZNORMALMAP_PRE_BLURZNORMALMAP_PRE_BLUR_KERNEL�add_ruleZNORMALMAP_SOBELZNORMALMAP_SOBEL_KERNELZNORMALMAP_POST_BLURZNORMALMAP_POST_BLUR_KERNELZNORMALMAP_INVERTZGEN_HEATMAPZGEN_SIMPLE_MESH�HTMLZSIMPLE_MESH_OCCLUDEZSIMPLE_MESH_SPHERICALZGEN_INPAINTED_MESHZGEN_INPAINTED_MESH_DEMOSZ	GEN_REMBGZSAVE_BACKGROUND_REMOVAL_MASKSZPRE_DEPTH_BACKGROUND_REMOVALZREMBG_MODEL�SCRIPT_FULL_NAME�change)30Zis_depth_tabZcur_option_rootZ options_depend_on_output_depth_1Zclip_options_row_1Zstereo_optionsZnormalmap_optionsZmesh_optionsZinpaint_options_row_0Z
bgrem_optionsr@rrIr�
main_ui_panels�31��32
3334���35�3637����383940�41���42434445����4647�����48 49 50�5152���5354���5556�������57�58�59�60�61�����6263���6465�66�67�����68 697071��727374����75 76 77�78�7980������81�82�83�848586�8788�8990��91�B��Br\cCs�t��}t�dd�rdStj�|�rtj�|�std��ddl}ddl	}tj�92|�}|��dkr7t�|�dS|��dkrF|�
d|g�dSd|��jvrV|�
d	|g�dS|�
d93|g�dS)N�hide_ui_dir_configFzCouldn't open output folderrZWindowsZDarwin�openzmicrosoft-standard-WSL2zwsl-openzxdg-open)r�get_outpath�get_cmd_optrrr�isdirr�platform�94subprocess�normpath�system�	startfile�Popen�uname�release)�frb�sprrrr�open_folder_action�srlcCs�tjdd�|tjdddddd�7}|tjd	d95dd�7}|tjd
ddddgdd�7}|tjdddddd�7}t��� |tjdddd�7}|tjdddddddd�7}Wd�n1s]wY|�ddd	�|�d
dd	�|�ddd�|S) NaDSingle video mode allows generating videos from videos. Please keep in mind that all the frames of the video need to be processed - therefore it is important to pick settings so that the generation is not too slow. For the best results, use a zoedepth model, since they provide the highest level of coherency between frames.r7�depthmap_vm_inputzVideo or animated file�singleT�file��elem_idr�96file_count�interactiver!�depthmap_vm_custom_checkboxz&Use custom/pregenerated DepthMap videoF�rqrr�depthmap_vm_smoothening_modeZSmootheningrr2Zexperimental)rqrr!rr�depthmap_vm_custom)rqrrrsr!r+�depthmap_vm_compress_checkboxzCompress colorvideos?�depthmap_vm_compress_bitratezBitrate (kbit)i�i�:iP���)rqrr+r&rr'r(r6rK)rNrY�FilerRrQrPrSrXr>rrr�depthmap_mode_video�s.97��9899�100�101102��r|zF103#depthmap_vm_input {height: 75px}104#depthmap_vm_custom {height: 75px}105cst��tjddtd����}tjdd���8tjdd���"�dtjddd	�f7�t����t�d106��S}t�	��5t���!�tj107ddd
ddd�7��tjddd
dddd�7�Wd�n1s`wYWd�n1sowY�tjdddd�7�Wd�n1s�wYt�d��}�tjdddd
dd�7�Wd�n1s�wYt�d��=}�tj
dsddd �t���d!d"i��7��tj
dsd#d$d �t���d!d%i��7�t�d&��tjd'd(d
d�7�Wd�n1s�wYt�d)��}t���Wd�n	1�s
wYWd�n	1�swYtjd*d+d,d-�}�td
�O�tjd.d/d0�}Wd�n	1�sBwYtjdd����tjd1d0����t�d2��Ut�	��tjd3dd4d5d6�}Wd�n	1�svwYt���t��}Wd�n	1�s�wYd7}	tj|	t�d8d�d9�jd:d;�ggd<�Wd�n	1�s�wYt�d=��@t�	��+tjd>gd?�d@�}108t���
t�dA�}Wd�n	1�s�wYWd�n	1�s�wYWd�n	1�swYt�dB���t�	���t���
t�dC�Wd�n	1�s)wYt���tjddD�}Wd�n	1�sDwYt��� t��}
t��}tj
dsdEdFit���d!dGi��}Wd�n	1�sqwYt���.tj
dHdIdJ�}tj
dKdLdJ�}tjdMdNdOgdNdPdQdR�}tjdSgdT�dUdPdVdR�}Wd�n	1�s�wYt���*tjdWgdX�dYdZd[dR�}tj
d\d]dJ�}tj
d^d_dJ�}tjd`ddadb�}Wd�n	1�s�wYt���tjdcddd,d-�}Wd�n	1�swYWd�n	1�swYWd�n	1�s wYWd�n	1�s0wYWd�n	1�s@wYWd�n	1�sPwY����7�|�ded;�d�d�|�dfd;�d�d�|�dgd;�d�d�|�dhd;�d�d��fdidj�}gdk�}|D]!}�|j|�fdldm�|D��fdndm�ddodpdqtjfD�d<��q�|jtggd<�|jdrd;�g|109gd<�|jt� t!���"�|||110|gd<�|jt� t#�|||||||||g	||
|gd<�Wd�|S1�s�wY|S)tNF�DepthMap)Zanalytics_enabled�titleZcss)Zequal_heightZpanel)�variant�
depthmap_mode�0)r+rzSingle ImageZSourceZuploadTZpil�depthmap_input_image)r�sourcersr!rqzCustom DepthMaprnro�custom_depthmap_img)rrrrsr!rqr+�custom_depthmapzUse custom DepthMapruz
Batch Process�image_batchZmultiplerpzBatch from Directory�depthmap_batch_input_dirzInput directory)rqrZplaceholderz<A directory on the same machine where the server is running.�depthmap_batch_output_dirzOutput directoryz/Leave blank to save images to the default path.z1Files in the output directory may be overwritten.�depthmap_batch_reusezaSkip generation and use (edited/custom) depthmaps in output directory when a file already exists.zSingle VideoZGenerateZdepthmap_generateZprimary)rqrz
Unload modelsZdepthmap_unloadmodels)rqZmode_depthmap_outputzDepth Outputr1Zdepthmap_gallery�)rZ111show_labelrq�columnsu📂r]r-cSst�SrL)rlrrrrrJCszon_ui_tabs.<locals>.<lambda>rAz3D Meshz3d Mesh)r4r4r4r4)rZclear_colorZClearzGenerate videoz&Generate video from inpainted(!) mesh.)rsrzInput Mesh (.ply | .obj)z7A file on the same machine where the server is running.zNumber of framesZ300r*Z	FramerateZ40ZFormatZmp4ZwebmrZvideo_format)rrrr!rqZSSAA)�1�2�3�4r�Z112video_ssaaZ113Trajectory)z
straight-line�double-straight-lineZcircler�r Zvideo_trajectoryzTranslate: x, y, zz-0.015, 0.0, -0.05zCrop: top, left, bottom, rightz0.03, 0.03, 0.05, 0.03ZDollyZsmalltxt)rrZelem_classeszGenerate VideoZdepthmap_generatevideocS�dS)Nr�rrrrrrJp�cSr�)Nr�rrrrrrJqr�cSr�)Nr�rrrrrrJrr�cSr�)Nr�rrrrrrJsr�cs^|dkr|p|dko|}�dj|d��dj|d��dj|d��dj|d�|fS)Nr�r�r�r-rr"r,)r:)�modeZzero_onZthree_onZhider>rr�custom_depthmap_change_fnus�z-on_ui_tabs.<locals>.custom_depthmap_change_fn)r�r�rtc�g|]}�|�qSrr)�.0�elr>rr�114<listcomp>�zon_ui_tabs.<locals>.<listcomp>cr�rr)r��str>rrr��r�rr"r,cSsdSrLrrrrrrJ�r�r)$r115rNrO�116custom_cssrPrVrYZTabsZTabItemrTrr{rRZTextboxrZ
get_hide_dirsr|ZButtonr\ZGalleryr`ZclickZModel3DZMarkdownZVideorQZ117enkey_tailZselectr[r8rUrZwrap_gradio_gpu_call�run_generateZ118enkey_bodyr)Zdepthmap_interfaceZdepthmap_mode_0Zdepthmap_mode_1Zdepthmap_mode_2Zdepthmap_mode_3ZsubmitZunloadmodelsZ
result_imagesZ	html_infoZ
folder_symbolZresult_depthmeshZ	clearmeshZ	depth_vidZvid_html_info_xZ
vid_html_infoZfn_meshZ
vid_numframesZvid_fpsZ119vid_formatZvid_ssaaZvid_trajZ	vid_shiftZ120vid_borderZ	vid_dollyZ121submit_vidr�Zcustom_depthmap_change_elsr�rr>r�122on_ui_tabssz123124125�126�����	127���128��129130131��132����!133��134135�136��137138�����139140�141�142143��144���145���146	������������W147�148���������149���r�rcCs�t��dt|�dd}d|vr	|Sdt��vr!|d7}|Sdt��vr-|d7}|Sd|vrC|d	t�d150�7}|dt��d7}|S)
Nz<h3>ERROR: z</h3>�151zout of GPU memoryz)torch.hub.load('facebookresearch/dinov2',z�<h4>To use Depth Anything integration in WebUI mode, please add "--disable-safe-unpickle" to the command line flags. Alternatively, use Standalone mode. This is a known issue.zError(s) in loading state_dict z�<h4>There was issue during loading the model.Please add "--disable-safe-unpickle" to the command line flags. This is a known issue.z=Please report this issue <a href="https://github.com/thygate/z>/issues">here</a>. Make sure to provide the full stacktrace: 152z <code style="white-space: pre;">z</code>)rr�strZ153format_exc�REPOSITORY_NAME)r�msgrrr�format_exception�s&
�����r�c$
Gs�t�|�}|d}|d}|d}|d}|d}|d}|d}|d}g}	g}154g}|d	krwz+|d155r8|dnd}|drB|d
nd}t�|dt��||||d�}
gdd|
fWStyp}z156t|�}
WYd}~nd}~wwgdd|
fS|dkr�|dkr�|}nt��}|dkr�|dur�gdddfS|	�|�|�d�|r�|dur�gdddfS|157�t	�158tj�
|j���n|159�d�|dkr�|dur�gddddfS|D]}t	�160tj�
|j��}|	�|�|�tj�|j�d�q�tt|	��d��n�|dk�r�t�dd��rJd��|dk�rgdddfS||k�rgdddfSt�|�}|D]~}z\|	�t	�161|��|�|�d}|�rzt|�j}|t�dd�k�rz|�dt�d d!���|�d"�t|�j�g}|D]}tj�||�}tj�|��rxt	�162tj�
|��}n�q]|163�|�W�q#t�y�}ztd#|�d$t|����WYd}~�q#d}~wwtd%d&�|164D��}tt|	��d'|�d(��t||	|165||t���}g}d}d}}d}	z
t|�\}} }!|d*7}Wn)t�y�|dk�r�d+nd,}Yn�t�y}z166t|�}WYd}~n�d}~ww| d-k�r
|!}�q�| d.k�r|!}�q�t|!t	j	��s*td/| �d0|�d1���q�||| |!fg7}|d2�r�z>d3}|dk�rV||du�rV|t�dd�k�rVt||�j}| d4k�r]dn| �}"tj |!||ddt�d d!�d)d)dd5|"d6�Wn+t�y�}zd7t|�v�s�d8t|�v�s�|�td9�t!�"�WYd}~nd}~ww�q�d}#t�d:d)��r�|}#t�d;d)��r�|du�r�t|�dk�r�|}#t#d<d=�|�||#|�$d>d?�fS)@Nr�r�r�r�r�r�r�r�r�rtrwrxryrmrvr��r�zPlease select an input imagezQCustom depthmap is not specified. Please either supply it or disable this option.r�zPlease select input imagesrz images will be processedr]Fz,--hide-ui-dir-config option must be disabledz!Please select an input directory.z7Please pick different directories for batch processing.Zoutdir_extras_samplesz-0000.Zsamples_formatZpngz.pngzFailed to load z, ignoring. Exception: cSsg|]}|durd�qS)Nr.r)r��xrrrr�sz run_generate.<locals>.<listcomp>z images will be processed, z" existing depthmaps will be reusedTr.z<h3>Successfully generated</h3>zQ<h3>Successfully generated nothing - please check the settings and try again</h3>Zsimple_meshZinpainted_meshz4This is not supposed to happen! Somehow output type z is not supported! Input_i: �.r)Zdepthmap�depth�extras)167r�basenameZseed�prompt�	extensionZshort_filenameZ	no_promptZgridZpnginfo_section_name�suffixzimage has wrong modezI;16z(Catched exception: image has wrong mode!Zdepthmap_script_show_3dZdepthmap_script_show_3d_inpaintcSs|dS)Nr3r)r�rrrrJSszrun_generate.<locals>.<lambda>r�z<br>)%r168Z
enkey_to_dictrZ	gen_videorr_rr��appendrr^rr�abspath�name�splitextZ	orig_name�print�lenr`Z	listfilesr�stemrWr�isfiler�rZ169gather_ops�next�
StopIteration�170isinstanceZ171save_imagerr�map�replace)$rCr�r�r�r�r�r�r�r�ZinputimagesZinputdepthmapsZ172inputnamesZcolorvids_bitrate�retrZoutpathZimgZimageZ173image_listrr�Zname_candidatesZfn_candZ	path_candZinputdepthmaps_nZgen_objZimg_resultsZ
results_totalZinpainted_mesh_fiZmesh_simple_fir�Zinput_ir!�resultr�Zdisplay_mesh_firrrr��s174175��176�����177178�179180181182183184185186187��&�����188189190191����)r�)r�pathlibrr
rNZPILr�srcrrZsrc.corerrrZsrc.depthmap_generationr	Zsrc.gradio_args_transportr192�src.miscZsrc.common_constantsrr8rr\rlr|r�r�rr�r�rrrr�<module>s*193G