o
    ॵi0                     @   s   d dl Z d dlZd dlmZmZ d dlZd dlZd dlm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlT d dlT d dlT d dlT d dlT d d	lmZ d d
lmZ d dlmZ ej ej!ej!dG dd deZ"dS )    N)AnyDict)ControlNetModelDiffusionPipelineEulerAncestralDiscreteSchedulerUniPCMultistepScheduler)Image)
TexturesUV)
transforms)	Pipelines)*)
OutputKeys)	PIPELINES)Tasks)module_namec                       s   e Zd ZdZdef fddZdeeef fddZdeeef deeef fd	d
Z	deeef deeef fddZ
  ZS )Tex2TexturePipelinea   Stable Diffusion for text_texture_generation Pipeline.
    Example:
    >>> import cv2
    >>> from modelscope.outputs import OutputKeys
    >>> from modelscope.pipelines import pipeline
    >>> from modelscope.utils.constant import Tasks
    >>> input = {'mesh_path':'data/test/mesh/mesh1.obj', 'prompt':'old backpage'}
    >>> model_id = 'damo/cv_diffuser_text-texture-generation'
    >>> txt2texture = pipeline(Tasks.text_texture_generation, model=model_id)
    >>> output = txt2texture(input)
    >>> print(output)
    modelc              
      s   t  jdd|i| tj rtd| _ntd t  |dd}z
|r-| j	j
  W n tyD } z
t| W Y d }~nd }~ww | j	j
  z
|rT| j	j  W n tyk } z
t| W Y d }~nd }~ww | j	j  d S )Nr   cudazno gpu avaiable*enable_xformers_memory_efficient_attentionT )super__init__torchr   is_availabledeviceprintexitgetr   piper   	Exceptionenable_model_cpu_offloadinpaintmodel)selfr   kwargsr   e	__class__r   l/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/text_texture_generation_pipeline.pyr   +   s4   
zTex2TexturePipeline.__init__returnc                 C   s  | dd }| j|\}}}}}}| dd }	| dd}
| dd}| dd}| d	d }|	d urBt|	d
||f}ntdd }tj	|d
d||f}|j
}tt |d dddd| j|jd |d d|_|
|||||||||d
}td |S )N	mesh_pathtexture_pathpromptcolorfuluvsizei   
image_size   
output_dirRGB)   r2         )r   N.r      r3      maps	faces_uvs	verts_uvs)
r+   meshfacesr-   mesh_centerscaler;   r.   init_texturer0   zmesh load done)r   r   mesh_normalizedr   openconvertresizenpones	fromarrayr;   r	   r
   ToTensorpermutetor   textures_idxtexturesr   )r"   inputsr)   r<   vertsr=   auxr>   r?   r*   r+   r-   r.   r0   r@   zero_mapnew_verts_uvsresultr   r   r'   
preprocessC   sR   
zTex2TexturePipeline.preprocessinputc           E      C   s  |d }|d }|d }|d }|d }|d }|d }|d }	|d	 }
|d
 }|d u r.d}t t||gtj| j}tj	
|d}tj|dd tj	
|d}tj|dd tj	
|d}tj|dd tj	
|d}tj|dd tj	
|d}tj|dd tj	
|d}tj|dd tj	
|d}tj|dd tj	
|d}tj|dd tj	
|d}tj|dd tj	
|d}tj|dd d}d}d}d}t|||dd\}}}}}t|||||||	|	d ||| j} tt|D ]<}!td|! ||! ||! ||! ||! f\}"}#}$}%d|%|}&t|"|#|$|!|!|| |||||	||||||| jddd d!\}'}(})}*}+},}-}.}/}0}1}2}3}4}5}6}7}8}9| jj|&|+|4|0d"d#}:t||||)|:|4|4|
||	d |d| j\}
};}tt |
d$ d%d&d'd| j|jd$ |d$ d(|_t|"|#|$|!|!|| |||||	d)|||||| jddd d!^}'}(})}*}+}<|!d&kr| jj|&|+|3|0d"d#}=t||||)|=|3|3|
||	d |d| j\}
};}tt |
d$ d%d&d'd| j|jd$ |d$ d(|_t||(^}>}<|>d%  }?|?d&d%d}?t |?d*}?|? tj	
|d+|! |d, }@t!"|@ # tj$d-}@|@ tj	
|d.|! qd|d d d d d d f  d%  }A|Ad&d%d}At |Ad-}A| jj%||+&d/|A&d/d0d0d1j'd% &||f}Bt t(|B| j}Ct t(|+| j}.d|d d d d d d f  }D|C|Dd%  |.d|Dd%    }.t!"|. # tj$d*}Bt)|d2||*  | |j+||j|B| j t,j-d3iS )4Nr+   r-   r<   r>   r?   r=   r;   r.   r@   r0   Gen_texturegenerateT)exist_okupdate	renderingnormalmaskdepth
similarity	inpaintedintermediateg      ?
   g        r7   F)use_principle   z=> processing view {}...z the {} view of {}g?)save_intermediatesmooth_maskview_thresholdg      ?)strengthr5   r   r6   r3   r8   g       @r1   z{}.pngg     o@Lz{}_texture_mask.png)r/   r/   r/   )r+   image
mask_imageheightwidthzmesh_post.objDone).r   
from_numpyrE   zerosastypefloat32rJ   r   ospathjoinmakedirsinit_viewpoints,build_similarity_texture_cache_for_all_viewsrangelenr   formatrender_one_view_and_build_masksr   r   backproject_from_imager	   r
   rH   rI   rK   rL   rendercpu
ToPILImagerC   saver   rG   numpyuint8r!   rD   imagesarraysave_full_objverts_packed	verts_idxr   OUTPUT)Er"   rT   r+   r-   r<   r>   r?   r=   r;   r.   r@   r0   exist_texturegenerate_dir
update_dirinit_image_dirnormal_map_dirmask_image_dirdepth_map_dirsimilarity_map_dirinpainted_image_dirmesh_dir
interm_dir	init_dist	init_elev	init_azim
fragment_k	dist_list	elev_list	azim_listsector_listview_punishmentspre_similarity_texture_cacheidxdistelevazimsectorprompt_view
view_scorerenderercameras	fragments
init_image
normal_map	depth_mapinit_images_tensornormal_maps_tensordepth_maps_tensorsimilarity_tensorkeep_mask_imageupdate_mask_imagegenerate_mask_imagekeep_mask_tensorupdate_mask_tensorgenerate_mask_tensorall_mask_tensorquad_mask_tensorgenerate_imageproject_mask_image_diffused_imageinter_images_tensorinter_imageexist_texture_imageri   post_texturediffused_image_tensormask_image_tensorr   r   r'   forwardi   s  






$


zTex2TexturePipeline.forwardrM   c                 C   s   |S )Nr   )r"   rM   r   r   r'   postprocess6  s   zTex2TexturePipeline.postprocess)__name__
__module____qualname____doc__strr   r   r   rS   r   r   __classcell__r   r   r%   r'   r      s    "& *Nr   )#rq   randomtypingr   r   r   rE   r   	diffusersr   r   r   r   PILr   pytorch3d.rendererr	   torchvisionr
   modelscope.metainfor   8modelscope.models.cv.text_texture_generation.lib2.camera;modelscope.models.cv.text_texture_generation.lib2.init_view<modelscope.models.cv.text_texture_generation.lib2.projection8modelscope.models.cv.text_texture_generation.lib2.viusel2modelscope.models.cv.text_texture_generation.utilsmodelscope.outputsr   modelscope.pipelines.builderr   modelscope.utils.constantr   register_moduletext_texture_generationr   r   r   r   r'   <module>   s.   