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Zd dl	m
Z
 d dl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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)keep_largestreconstructionsave_obj_meshsave_obj_mesh_with_color	to_tensor)
OutputKeys)pipeline)InputModelPipeline)	PIPELINES)	ModelFileTasks)
get_logger)module_namec                       s   e Zd Zdef fddZde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 )HumanReconstructionPipelinemodelc                    s8   t  jdd|i| t| jtstd tddS )a  The inference pipeline for human reconstruction task.
        Human Reconstruction Pipeline. Given one image generate a human mesh.

        Args:
            model (`str` or `Model` or module instance): A model instance or a model local dir
                or a model id in the model hub.

        Example:
            >>> from modelscope.pipelines import pipeline
            >>> test_input = 'human_reconstruction.jpg' # input image path
            >>> pipeline_humanRecon = pipeline('human-reconstruction',
                model='damo/cv_hrnet_image-human-reconstruction')
            >>> result = pipeline_humanRecon(test_input)
            >>> output =  result[OutputKeys.OUTPUT]
        r   z model object is not initialized.N )super__init__
isinstancer   r   loggererror	Exception)selfr   kwargs	__class__r   i/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/human_reconstruction_pipeline.pyr      s
   
z$HumanReconstructionPipeline.__init__inputreturnc           	      C   sx   | j |}| j |\}}| j ||\}}t|d d }t|d d }t|d d }t|}||||d}|S )N      )imgmasknormal_Fnormal_B)r   crop_imgget_maskgeneration_normalr	   )	r   r"   img_cropr&   r'   normal_fnormal_bimageresultr   r   r!   
preprocess1   s   z&HumanReconstructionPipeline.preprocessc                 C   s  |d }|d }|d }|d }|d  |d< |d  |d< || }|| }|| }t j|||gdd }|d| jj}	| jj}
| jj}|	|	 t
||
| jj| jj\}}tj||d	d
d}t|}|j}|j}t |jd| jj }t |j}d}tt|| D ]<}|| }|| | }|t|| d krd}||d d d d ||f |
}|d   d d }|j|||< qt|d d df }|d d df | |d d df< td|| td|||  ||| d}t j!|iS )Nr&   r'   r(   r)   )r%   .)r   .r   )dimFT)processmaintain_orderi N  r%   g      ?zhuman_reconstruction.objzhuman_color.obj)verticesfacescolors)"torchcatfloat	unsqueezetor   devicecalib	meshmodelextract_featuresr   coordsmattrimeshTrimeshr   r7   r8   
from_numpyTzerosshaperangelen	query_rgbdetachcpunpminr   r   numpyr
   OUTPUT)r   r"   r0   r'   normFnormBr&   r/   r.   image_tensorcalib_tensornetvertsr8   pre_mesh
final_meshverts_tensorcolorintervalileftright
pred_colorrgbvert_minresultsr   r   r!   forwardA   s`   
 
z#HumanReconstructionPipeline.forwardinputsc                 C   s   |S )Nr   )r   rg   r   r   r!   postprocessl   s   z'HumanReconstructionPipeline.postprocess)__name__
__module____qualname__strr   r   r   r   r2   rf   rh   __classcell__r   r   r   r!   r      s
    "*+r   )$osshutiltypingr   r   rR   rP   r:   rE   modelscope.metainfor   /modelscope.models.cv.human_reconstruction.utilsr   r   r   r   r	   modelscope.outputsr
   modelscope.pipelinesr   modelscope.pipelines.baser   r   r   modelscope.pipelines.builderr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r   register_modulehuman_reconstructionr   r   r   r   r!   <module>   s&   