o
    ॵi	                     @   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
 d dl	Z	d dlZd dlmZ d dlmZ d dlmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
l m!Z! d dl"m#Z#m$Z$ d dl%m&Z& e& Z'ej(e$j)ej)dG dd deZ*dS )    N)AnyDict)	Pipelines)mesh_to_string)
projectionread_objrenderrotate_xrotate_y	translate)	MsDataset)
OutputKeys)ModelPipeline)	PIPELINES)is_model)InvokeTasks)
get_logger)module_namec                       s   e Zd ZdZddef fddZdeeef deeef fdd	Zd
d Z	dd Z
dddZdeeef deeef fddZdeeef fddZ  ZS )Human3DRenderPipelinea   Human3D library render pipeline
    Example:

    ```python
    >>> from modelscope.pipelines import pipeline
    >>> human3d = pipeline(Tasks.human3d_render,
                'damo/cv_3d-human-synthesis-library')
    >>> human3d({
            'data_dir': '/data/human3d-syn-library', # data dir path (str)
            'case_id': '3f2a7538253e42a8', # case id (str)
        })
    >>> #
    ```
    gpumodelc                    s    t  jdd|i| || _dS )z
        use model to create a image sky change pipeline for image editing
        Args:
            model (str or Model): model_id on modelscope hub
            device (str): only support gpu
        r   N )super__init__	model_dir)selfr   devicekwargs	__class__r   c/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/human3d_render_pipeline.pyr   .   s   
zHuman3DRenderPipeline.__init__inputsreturnc                 C   s   |S )Nr   )r   r#   r   r   r"   
preprocess8   s   z Human3DRenderPipeline.preprocessc                 C   sP   t |}|dd}tj|stjdtjd}nt	|}|
 |d< ||fS )N.objz.png)   r'      )dtypetexture_map)r   replaceospathexistsnpzerosuint8cv2imreadcopy)r   	mesh_pathmeshtex_pathtexr   r   r"   load_3d_model;   s   
z#Human3DRenderPipeline.load_3d_modelc                 C   s  |d }|j dd|jdd d }||8 }|d }| dkr$|d n|}|d }|d }| dkr8|d n|}t|tj }t|tj }	t|tj }
t|tj }|d d d	d d d d d	f }t|tjd
  }||	|
||fS )Nverticesr   )axis   faces   uvsfaces_uv     o@)	maxmintorch
from_numpyastyper/   float32cudaint32)r   r6   r8   vertcenttrivert_uvtri_uvvtx_pospos_idxvtx_uvuv_idxr   r   r"   format_nvdiffrast_formatE   s   z.Human3DRenderPipeline.format_nvdiffrast_format   c                 C   s  t j|std|  t j| jddd}| |\}}| ||\}}}}}	t	
 }
d}d}dtj | }g }g }tt|D ]}tdd	d
d}ttdt|}ttddd|}t||tj}t|
||||||	|ddd
\}}}tt|d    d ddtjd d dd d d d f }tt|d    d ddtjd d dd d d d f }|| || || }qCtdt jt j|  |||fS )Nz!can not found %s, use default onez	3D-assets3f2a7538253e42a8body.objg        P   r<   g?g      ?g      i@)xnfr   gF	   )
resolution
enable_mipmax_mip_levelrB      rA   zrender case %s done) r,   r-   r.   loggerinfojoinr   r9   rT   drRasterizeCudaContextr/   pitqdmranger   matmulr	   r
   r   rG   rH   r   cliprintdetachcpunumpyr1   appendbasenamedirname)r   r5   r]   r6   texturerP   rQ   rR   rS   r8   glctxangframe_lengthstepframes_colorframes_normalsiproja_rota_mvr_mvppred_img	pred_masknormalcolornormalsr   r   r"   render_sceneV   sf   



z"Human3DRenderPipeline.render_sceneinputc                 C   s   |d }|d }d|v r|d }nd}| dr|}n)|dd }|dd }tj|||d	jd
 d }tj||}	tj|	d}| ||\}
}}|
||d}t	j
d t	j|iS )N
dataset_idcase_idr]   rU   r&   /rA   r   )	namespacesubset_namesplit_configtestrW   )r6   rw   frames_normal)endswithsplitr   loadconfig_kwargsr,   r-   rc   r   r   
OUTPUT_OBJOUTPUT)r   r   r   r   r]   r5   dataset_name	user_namedata_dircase_dirr6   colorsr   resultsr   r   r"   forward   s2   

zHuman3DRenderPipeline.forwardc           	      K   sp   | dd}|tj }|tj }|r't }t|d }|jdd}|| tj|tj|r3d i}|S |i}|S )Nr   Fr6   zutf-8)encoding)	getr   r   r   ioBytesIOr   encodewrite)	r   r#   r   r   
output_objr   mesh_str
mesh_bytesresultr   r   r"   postprocess   s   



z!Human3DRenderPipeline.postprocess)r   )rU   )__name__
__module____qualname____doc__strr   r   r   r%   r9   rT   r   r   r   __classcell__r   r   r    r"   r      s    "


"/r   )+r   r,   typingr   r   r2   rn   r/   nvdiffrast.torchrE   rd   rg   modelscope.metainfor   .modelscope.models.cv.face_reconstruction.utilsr   &modelscope.models.cv.human3d_animationr   r   r   r	   r
   r   modelscope.msdatasetsr   modelscope.outputsr   modelscope.pipelines.baser   r   modelscope.pipelines.builderr   modelscope.pipelines.utilr   modelscope.utils.constantr   r   modelscope.utils.loggerr   ra   register_modulehuman3d_renderr   r   r   r   r"   <module>   s.    