CoolFace
Apppublic

hololens/stable-diffusion-webui-depthmap-script

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

2��f�3@s�ddlmZddlmZzddlmZWn4ddlmZYddlZddl	Z	ddl5Z6ddlZddl
ZddlZddlZddlZddlTddlmZddlTddlmZdd	lmZdd7lmZddlmZz"ddlmZm Z m!Z!dd
l"m#Z#m$Z$m%Z%ddl&m'Z'ddl(m)Z)Wne*y�Z+ze,d�ddl-Z-e-�.�WYdZ+[+ndZ+[+wwda/da0e�Z1dd�Z2dd�Z3Gdd�d�Z4d-dd�Z5d.dd�Z6dd�Z7dd�Z8	d/d d!�Z9d"d#�Z:d$d%�Z;d&d'�Z<d(d)�Z=d0d+d,�Z>dS)1�)�Path)�Image)�trange)�rangeN)�*)�GenerationOptions)�create_stereoimages)�create_normalmap)�ModelHolder)�backbone)�8write_mesh�	read_mesh�output_3d_photo)�Inpaint_Color_Net�Inpaint_Depth_Net�Inpaint_Edge_Net)�
path_planning)�sparse_bilateral_filteringz-Impaint import failed. Impaint will not work.cCs4d}dd|}t�||dd|d�}|�d�S)N��g-C��6?rg�������?�uint16)�np�clip�astype)�arr�numbytes�max_val�out�r�@F:\3dpicmaker\stable-diffusion-webui-depthmap-script\src\core.py�convert_to_i16,s9r cCs\t�|�}|d|dd�dd�df<|d|dd�dd�df<|d|dd�dd�df<|S)N�p@r�r)r�10zeros_like)�image�like�outputrrr�convert_i16_to_rgb4s1112r'c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�CoreGenerationFunnelInpz�This class takes a dictionary and creates a core_generation_funnel inp.13    Non-applicable parameters are silently discarded (no error)cCs\t|t�r|j}dd�|��D�}i|_tD]}|j��}||vr%||n|j|j|<qdS)NcSs*i|]\}}t|t�r|jn|��|�qSr)�14isinstancer�name�lower)�.0�k�vrrr�15<dictcomp>Cs*z4CoreGenerationFunnelInp.__init__.<locals>.<dictcomp>)r)r(�values�itemsrr*r+�df)�selfr0�settingr*rrr�__init__@s1617�z CoreGenerationFunnelInp.__init__cCs$t|t�r
|j|j��S|j|S�N)r)rr0r*r+�r3�itemrrr�__getitem__Js1819z#CoreGenerationFunnelInp.__getitem__cCs||Sr6rr7rrr�__getattr__Osz#CoreGenerationFunnelInp.__getattr__N)�__name__�20__module__�__qualname__�__doc__r5r9r:rrrrr(=s2122r(c1cs�23�t|�dks
|ddurdS|dust|�dkr$dd�tt|��D�}tdd�|D��}t|�}|dur9t��}tjdPi|��tt	�tdtj24j���t��g}|t
jrq|t
jrit||t
j�}|}nt||t
j�}|t
jdkr�tj��r�t�d�}ntd�t�d	�}nt�d	�}td25|�g}	g}26�zJ�z�|s�td�t�|t
j||t
j|t
j�td�tdt|��D�]�}||jd
kr�||jdd�dd�||�d�||<d}	d}
	d}|du�r�||du�r�||}t|t j ��rw|j!||j!k�s|j"||j"k�r5z|�#||j!||j"ft j$j%�}Wn|�#||j!||j"f�}Yt|�&��dk�rbt'j(|dd�}|�)�}|dk�rQd}n27|dk�rYd}nd}|d|}n�t'j(|dd�dd�dd�df}|d}n�t'j(|dd�}||j"|j*dk�s�Jd��||j!|j*dk�s�Jd��n�|t
j+�r�||j!ddd}||j"ddd}n28|t
j,}|t
j-}t�.||||�\}}
t/|�)�|�0��t'�1d�j2k�rWt'�3|�}|
�r�|d 9}|t
j4�r�|d!t'�3|�fV|t
j5�rH|t
j6d"k�r%||�0�|�)�|�0�}t'�7||t
j8|t
j9�}n#|t
j6d#k�rHt'�:||t
j8d$|t
j9d$g�\}}t'�7|||�}||�0�|�)�|�0�}nt'�;|j*�}t<|�}	|t
j=�rt|	�>||�|29�>|�|t
j�r�td%�||}t'�?|�}|dd�dd�dfdk|dd�dd�dfdk@|dd�dd�d&fdk@|dd�dd�d'fd(k@}d||<|d)|fV|t
j@�r�d|�Ad*�d+}t'jB||||fd&d,�}t �C|�At'jD��}|d-|fV|t
jE�r6|t
jF�r�tG�H|�n|}|t
jI�r-|t
jJd.k�rdnd}t �Ct'jK||tL|||�f|d,��}|d/|fVn	|d0t �C|�fV|t
jM�rstN||||t
jO|t
jP|t
jQ|t
jR|t
jS|t
jT�} tdt| ��D]}!||t
jQ|!| |!fV�qb|t
jU�r�tV||t
jW�r�|t
jXnd|t
jY�r�|t
jZnd|t
j[�r�|t
j\nd|t
j]�}"|d1|"fV|t
j^�r�dd2l_m`}#t �C|#|d3d4��}$|d5|$fV|t
ja�r_td6�d7}%tb||%d8d9�}&|du�r�|n|}'|'�0�|'�)�}(})|t
jd:v�s|t
j�s||du�rC|t
jdk�s||du�r|)|'|(}'|'�)�}*|'�0�}+|(dk�r.|'|(}'|'�)�}*|'�0�}+|'�)�d;k�r?d<|'|(|)|(}'|'d=}'tc|||'|t
jd|t
jed>�},|,�f|&�|d?|&fVq�td@�Wnhtg�y�}-z[ddlh}.dAti|-��j�v�r�tti|-��dB}/|t
j�r�|/dC7}/tj30tjkjlk�r�|/dD7}/|t�d	�k�r�|/dE7}/|t
jdFk�r�|/dG7}/|t
j�s�|/dH7}/tdI�tg|/��tdI�|-�d}-~-wwWt�mdJdK��r�t�n�nt�o�tp�q�t�r�nt�mdJdK��r�t�n�nt�o�tp�q�t�r�w|t
j=�r:zts||	|31|||t
jtddL�}0ddM|0fVWntg�y9}-ztti|-��dN��WYd}-~-nd}-~-wwt�u�tdO�dS)QNrcSsg|]}d�qSr6r)r,�_rrr�32<listcomp>Wsz*core_generation_funnel.<locals>.<listcomp>cSsg|]}|du�qSr6r)r,�xrrrr@X�z33Backbone: �GPU�cudaz4WARNING: Cuda device was not found, cpu will be used�cpuz34device: %szLoading model(s) ..zComputing output(s) ..�IcSs|dS)Ng�I��p?r)�prrr�<lambda>�sz(core_generation_funnel.<locals>.<lambda>�RGB)�modeFr"�float)�dtype�ri�� g@r!zCustom depthmap height mismatchzCustom depthmap width mismatch������Zdepth_prediction�Range�OutliersgY@zapplying background masksr�g�������?Zbackground_removed�int8����axisZforeground_mask�35HorizontalZconcat_depth�depth�	normalmap)�colorizeZinferno)Zcmap�heatmapz36Generating (occluded) mesh ..�depthmap�obj�simple)�r�	g$@�@��?)�37keep_edges�	spherical�simple_meshzComputing output(s) done.z
out of memoryzeout of GPU memory, could not generate depthmap! Here are some suggestions to work around this issue:38zU * Disable BOOST (generation will be faster, but the depthmap will be less detailed)39zH * Run DepthMap in the standalone mode - without launching the SD WebUI40z= * Select CPU as the processing device (this will be slower)41�z\ * Use a different model (generally, more memory-consuming models produce better depthmaps)42z/ * Reduce net size (this could reduce quality)43zFail.44Zdepthmap_script_keepmodelsT�mp4�inpainted_meshz+, some issue with generating inpainted meshz45All done.46r)v�lenr�allr(r�47gather_ops�model_holderZupdate_settings�print�SCRIPT_FULL_NAME�
USED_BACKBONEr*�unload_sd_model�go�	GEN_REMBG�PRE_DEPTH_BACKGROUND_REMOVAL�batched_background_removal�REMBG_MODEL�COMPUTE_DEVICE�torchrD�is_available�deviceZ
ensure_models�48MODEL_TYPE�BOOST�TILING_MODErrJ�point�convertr)r�width�height�resize�49Resampling�LANCZOS�getbandsr�asarray�max�shape�NET_SIZE_MATCH�	NET_WIDTH�50NET_HEIGHTZget_raw_prediction�abs�min�finfo�eps�copy�DO_OUTPUT_DEPTH_PREDICTION�	CLIPDEPTH�CLIPDEPTH_MODEr�
CLIPDEPTH_FAR�CLIPDEPTH_NEAR�51percentile�zerosr �GEN_INPAINTED_MESH�append�array�SAVE_BACKGROUND_REMOVAL_MASKSr�stack�	fromarray�uint8�DO_OUTPUT_DEPTH�OUTPUT_DEPTH_INVERT�cv2�bitwise_not�OUTPUT_DEPTH_COMBINE�OUTPUT_DEPTH_COMBINE_AXIS�concatenater'�52GEN_STEREOr�STEREO_DIVERGENCE�STEREO_SEPARATION�STEREO_MODES�STEREO_BALANCE�STEREO_OFFSET_EXPONENT�STEREO_FILL_ALGO�
GEN_NORMALMAPr	�NORMALMAP_PRE_BLUR�NORMALMAP_PRE_BLUR_KERNEL�NORMALMAP_SOBEL�NORMALMAP_SOBEL_KERNEL�NORMALMAP_POST_BLUR�NORMALMAP_POST_BLUR_KERNEL�NORMALMAP_INVERT�GEN_HEATMAPZdzoedepth.utils.miscr\�GEN_SIMPLE_MESH�get_uniquefn�create_mesh�SIMPLE_MESH_OCCLUDE�SIMPLE_MESH_SPHERICAL�export�	Exception�	traceback�strr+�BackboneType�53STANDALONE�get_optZoffload�
unload_models�gc�collect�torch_gc�run_3dphoto�GEN_INPAINTED_MESH_DEMOS�reload_sd_model)1�outpath�inputimages�inputdepthmaps�54inputnames�inp�opsZinputdepthmaps_completeZbackground_removed_imagesr{Zinpaint_imgsZinpaint_depths�countZraw_predictionZraw_prediction_invertrZdp�out_max�	bit_depthZ	net_widthZ55net_height�fb�nbZ56img_outputZbackground_removed_imageZbackground_removed_arrayZbg_maskZbg_array�57mask_array�58mask_image�	img_depthrXZ59img_concatZstereoimages�cr[r\r]�basenameZ
meshsimple_fiZdepthiZ60depthi_minZ61depthi_maxZ	depth_maxZ	depth_min�mesh�er��62suggestion�mesh_firrr�core_generation_funnelSs��63646566"$$6768 69 7071�"72(73742�����*75�76�����7778�7980���r��c81Cs�t�||�}|dkr|d8}|dkrd|��}td�D] }tj�||�d||d�|�d|���}tj�|�s;|Sq|�d|�d|��S)	Nrr"r��-���04�.z-99999)r�get_next_sequence_numberr�os�path�join�exists)r�r��ext�suffix�	basecount�i�fullfnrrrr�`s82(�r�cCs`d}�ztd�d}	d}83d}tjddd�t|	d	d84gd�t|85dd
gd�t|ddgd�td�tdd�}tj|	t�|�d�}
|�|
�|�	|�}|�86�td�t�}tj|87t�|�d�}|j|dd�|�	|�}|�88�|�	|�}td�t�}tj|t�|�d�}|�|�|�89�|�	|�}i}d|d<d|d<d|d<d|d<d|d <d!|d"<d#|d$<d%|d&<d#|d'<d#|d(<d|d)<d*|d+<d,|d-<d.|d/<d|d0<d1|d2<t
�d3d4�|d5<d|d6<|t�d7�kr�d8|d<tdt|��D�]}d9}|du�r
||du�r
t||�}|j}t||d:�}td;�||j}||j}t�t||�d|d<gdt||�|d<ggd=�g��tj�}|��d>k�rj|ddd�ft|�|ddd�f<|d>dd�ft|�|d>dd�f<||�tj�}||��}tj||��d?d@�|��}||��dA}dBt�|dC�}t� ||�}t|j!�d<k�r�|j!d<dDk�r�t�"|tj#�}d,|dE<gdF�|dG<dH|dI<dJ|dK<t$|�%�|�%�||dEd4dL�\}}|d8}t&|||||||||�	}|d4u�r
|�r
t'|||dMdNgdO�gdP�gdQ�gdR�gdS�gdT�d4||�t
�(�q�W~d}~d}~d}t
�(�|S~d}~d}~d}t
�(�w)UNr�zRunning 3D Photo Inpainting .. z./models/3dphoto/edge_model.pthz ./models/3dphoto/depth_model.pthz ./models/3dphoto/color_model.pthz./models/3dphoto/T��exist_okznhttps://huggingface.co/spaces/Epoching/3D_Photo_Inpainting/resolve/e389e564fd2a55cf/checkpoints/edge-model.pthzHhttps://filebox.ece.vt.edu/~jbhuang/project/3DPhoto/model/edge-model.pthZ@b1d768bd008ad5fe9f540004f870b8c3d355e4939b2009aa4db493fd313217c9zohttps://huggingface.co/spaces/Epoching/3D_Photo_Inpainting/resolve/e389e564fd2a55cf/checkpoints/depth-model.pthzIhttps://filebox.ece.vt.edu/~jbhuang/project/3DPhoto/model/depth-model.pthZ@2d0e63e89a22762ddfa8bc8c9f8c992e5532b140123274ffc6e4171baa1b76f8zohttps://huggingface.co/spaces/Epoching/3D_Photo_Inpainting/resolve/e389e564fd2a55cf/checkpoints/color-model.pthzIhttps://filebox.ece.vt.edu/~jbhuang/project/3DPhoto/model/color-model.pthZ@383c9b1db70097907a6f9c8abb0303e7056f50d5456a36f34ab784592b8b2c20zLoading edge model ..)Zinit_weights)Zmap_locationzLoading depth model ..)�strictzLoading rgb model ..rZgpu_ids�<Zextrapolation_thicknessZextrapolate_borderg{�G�z�?Zdepth_threshold�Zredundant_numberg����Mb`?Zext_edge_threshold�FZbackground_thickness�Zcontext_thicknessZbackground_thickness_2Zcontext_thickness_2Z	log_depth�90Zdepth_edge_dilate�Zdepth_edge_dilate_2iZlargest_sizeZrepeat_inpaint_edge�binZply_fmtZdepthmap_script_save_plyFZsave_plyZsave_objrErQr^r_z691Generating inpainted mesh .. (go make some coffee) ..r�rrr"r")rTrT)Zksizeg@rd皙�����?�Zsparse_iter)rarar�r�r�Zfilter_sizercZsigma_sg�?Zsigma_r)Znum_iterZspdbi,�()���Q��?r�r�r�)�double-straight-liner��circler�)�r����Q���r)r�r�rg�)皙������rrr)z
dolly-zoom-inzzoom-inr�Zswing))ror��makedirs�ensure_file_downloadedrry�loadr{Zload_state_dict�to�evalrrrr�rrkr�stemr�r�r�rr�r�r�float32rKr�r��blur�maximumr�r�ZcvtColorZCOLOR_BGRA2BGRrr�r�run_3dphoto_videosr�)r{Zimg_rgbr�r�r�Zgen_inpainted_mesh_demos�vid_ssaa�92vid_formatr�Zedgemodel_pathZdepthmodel_pathZcolormodel_pathZdepth_edge_modelZdepth_edge_weightZdepth_feat_modelZdepth_feat_weightZ	rgb_modelZrgb_feat_weight�configr�r�rG�W�H�int_mtx�disprZ�imgZ93vis_photosZ94vis_depthsZrt_inforrrr�ms������9596979899100101102103104<$$ 105��106�107�B�108r�c6Cs&ddl}z!t��dkr|jdd�nt��dkr|�d�n|jdd�Wn2ddl}t|���td�d	D]}z	|j|d�Wntd109|���t|���Yq9Ytdus`t|krrzbWntd�Y|at	|�at\}}}}}}}}|}}}|}}}t110�t||�d|dgdt||�|dggd
�g��
t111j�}|��dkr�|ddd�ft|�|ddd�f<|ddd�ft|�|ddd�f<i} || d<|| d<|| d<|| d<|| d<|| d<|| d<|	| d<|112| d<|
| d<t113�d�}!t| d�t| d�k�r.t| d�k�r.t| d�k�r.t| d�k�s3Jd��Jd��|!dgg}"g}#tt| d��D]L}$g}%t| d| d|$| d|$| d|$| d|$d�\}&}'}(t|&|'|(�D]\})}*}+|%�|!d�t114�|)|*|+g�|%ddd�df<�qk|#|%g7}#�qC|!d}"td�d \},}-t�|#�|}.}/|d|d!|}0|d|d"|}1|0||1|}2}3d#d$�|0|2|1|3fD�}4t|��|��|��t�|�t�|�t�|�t�|�t�|"�| dt�|!�t�| d�dt�|�| d|.|/||f|4d|,|-|||d%��\},}-}5|5S)&Nr�Windows�PyQt5)�app�Darwin�PyQt6�eglzTrying an alternative...)rrrzOn zdel video_mesh_data failedrr�r"Zvideo_folder�115num_frames�fps�crop_border�116traj_types�
x_shift_range�
y_shift_range�
z_shift_range�
video_postfixZssaar�z�The number of elements in 'traj_types', 'x_shift_range', 'y_shift_range', 'z_shift_range' and             'video_postfix' should be equal.)�	path_typerdrQrTzGenerating videos ..�NN)r"r)rrcSsg|]}t|��qSr)�int)r,�xxrrrr@WrBz&run_3dphoto_videos.<locals>.<listcomp>)�borderrZ�
normal_canvas�117all_canvas�mean_loc_depth�dollyZfnExt)�vispy�platform�system�user�ro�118format_exc�
video_mesh_fn�video_mesh_datar
rr�r�rrrK�eyerkrr�zipr�r��deepcopyr)6r�r�r�rrrrrrr r!�	vid_dollyr
rr+r��u�verts�colors�faces�Height�WidthZhFovZvFovr)Z119original_wZoutput_wrZ120original_hZoutput_hrrrZgeneric_poseZtgt_poseZ121tgts_posesZtraj_idxZ	tgt_poses�sx�sy�szr%�yy�zzr'r(Zvideos_posesZvideo_basename�top�leftZdown�rightr&�fn_savedrrrrs���122<$$123124125126127128���129�$��rcCs�t|�dkstj�|�std��t|�}|dkrdg}n|dkr$dg}n|dkr+dg}t|�}t|�}|�d�}
t|
�d	krBtd130��t|
d�g}t|
d�g}t|
d�g}|�d�}t|�dkrftd��t|d�t|d�t|d�t|d	�g}|	s�t�	�}	|131s�t132|�j}133t�|	|134�}|dkr�|d}d}t
d
�D]*}|135dkr�||d�n	|136�d||d��}tj�|	|�d�|�}tj�|�s�nq�t137|�j}138|139dd�}140td�t||141|	|||||||dg|||�}|d|ddfS)NrzCould not open mesh.z
straight-liner"r�rr��,rTzTranslate requires 3 elements.r�z Crop Border requires 4 elements.r�r�Z05r�r�z_.rQzLoading mesh ..)rkr�r�r�r�r$�splitrKr�get_outpathrrr�rr�ror)�fn_mesh�
vid_numframes�vid_fps�vid_traj�	vid_shift�142vid_borderr*r
rr�r�Znum_fpsr�shiftsrrr �bordersrr�r�r��fnrDrrr�
run_makevideofsR143144,145(�146�rQcCst��dSr6)rnr�rrrrr��sr�c	Cs�ddlm}m}td�g}t�t���d�}tj|dd�t	|�tj147d<||�}tdt|��D]}t
�||||d��}|�t�|��q0~|S)	Nr)�new_session�removezcreating background masksz
models/rem_bgTr�Z148U2NET_HOME)�session)ZrembgrRrSror�joinpath�resolver�rr��environrrkrr�r�rr�)	Zinimages�149model_namerRrSZ	outimagesZbg_model_dirZbackground_removal_sessionr�Z
bg_remove_imgrrrrv�srvcCs�|��}t�tjtj|jd�}t�tjdtjd|jd�}t�||�\}}|��}|��}|t�|�t�|�}|t�|�t�|�}|t�|�}tj|||gdd�}|S)z�150    360 depth to world points151    given 2D depth is an equirectangular projection of a spherical image152    Treat depth as radius153    longitude : -pi to pi154    latitude : -pi/2 to pi/2155    r"rrrW)	�flattenr�linspace�pir��meshgrid�cos�sinr�)rZ�radiusZlonZlatrA�y�z�pts3drrr�pano_depth_to_world_points�s156"rccCs0t�|�\}}t�|d|d�}|dk}|S)z�Returns a mask of edges in the depth map.157    Args:158    depth: 2D numpy array of shape (H, W) with dtype float32.159    Returns:160    mask: 2D numpy array of shape (H, W) with dtype bool.161    rr�)r�gradient�sqrt)rZZdepth_dxZdepth_dyZ162depth_grad�maskrrr�depth_edges_mask�srgFcCsddl}ddlm}m}t�dd�}|�||f�|s"||d�}nt|�}|�dd�}|�dd�}	t	�163|�}|rE||jd|jd�}164n||jd|jdt|�d�}165|�dd�}|j
|	|166|d	�}|rtjd167}
gd�}gd�}|j�|
||�}|�|�|S)
Nr)�depth_to_points�create_trianglesZdepthmap_script_mesh_maxsizeirQrTr")rf)Zverticesr9Z
vertex_colorsr)r"rr)rrr)�trimeshZdzoedepth.utils.geometryrhrirr��	thumbnailrc�reshaperr�r�rgZTrimesh�mathr[ZtransformationsZrotation_matrixZapply_transform)r$rZrerfrjrhri�maxsizerbr7Z	trianglesr8r��angle�	direction�centerZ168rot_matrixrrrr��s,169 170171r�r6)r�r#)FF)?�pathlibr�PILr�tqdmr�builtinsrryr�r��os.pathr��numpyrr�r,rm�src.misc�src.common_constantsrrsZsrc.stereoimage_generationrZsrc.normalmap_generationr	�src.depthmap_generationr172�srcrZinpaint.meshrr
rZinpaint.networksrrrZ
inpaint.utilsrZinpaint.bilateral_filteringrr�r�ror��	print_excr1r0rnr r'r(r�r�r�rrQr�rvrcrgr�rrrr�<module>sf��	173174
f175�7