o
    *j                     @   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   v/var/www/html/Deteccion_Ine/venv/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   Zcrop_imgZget_maskZgeneration_normalr	   )	r   r"   Z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)processZmaintain_orderi N  r%   g      ?zhuman_reconstruction.objzhuman_color.obj)verticesfacescolors)"torchcatfloatZ	unsqueezetor   ZdeviceZcalibZ	meshmodelZextract_featuresr   ZcoordsZmattrimeshZTrimeshr   r2   r3   Z
from_numpyTZzerosshaperangelenZ	query_rgbdetachcpunpminr   r   numpyr
   ZOUTPUT)r   r"   r,   r'   ZnormFZnormBr&   r+   r*   Zimage_tensorZcalib_tensornetZvertsr3   Zpre_meshZ
final_meshZverts_tensorcolorintervalileftrightZ
pred_colorrgbZvert_minresultsr   r   r!   forwardA   s`   
 
z#HumanReconstructionPipeline.forwardinputsc                 C   s   |S )Nr   )r   rL   r   r   r!   postprocessl   s   z'HumanReconstructionPipeline.postprocess)__name__
__module____qualname__strr   r   r   r   r.   rK   rM   __classcell__r   r   r   r!   r      s
    "*+r   )$osshutiltypingr   r   rB   r@   r5   r9   Zmodelscope.metainfor   Z/modelscope.models.cv.human_reconstruction.utilsr   r   r   r   r	   Zmodelscope.outputsr
   Zmodelscope.pipelinesr   Zmodelscope.pipelines.baser   r   r   Zmodelscope.pipelines.builderr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   r   Zregister_moduleZhuman_reconstructionr   r   r   r   r!   <module>   s&   