o
    ߥi0                     @   s  d dl mZ d dlmZ d dlZd dlZd dlm	Z	 d dl
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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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)Optional)imread)estimate_transformwarp)Models)Tensor
TorchModel)MODELS)
FasterRCNN)human_segmenter)define_G)
Pixto3DNet)create_grid)	ModelFileTasks)
get_logger)module_namec                       sD   e Zd Z fddZdd Ze dd Ze dd Z  Z	S )	HumanReconstructionc              
      s  t  j|d|i| tj r td| _td| j ntd| _td| j d|t	j
}d|d}d|d}d|t	j}d|d	}	tdi |d
 | _t|	| jd| _| jtj|dd tddddddddd	| _tddddddddd	| _| jt| | jt| | j| j| _| j| j| _| j  | j  | j| j | _t|d| _tg d}
tg d}t|d |
|\| _| _td}d|d< t|  | j| _!| j!ddddf "d| _!td dS )aW  The HumanReconstruction is modified based on PiFuHD and pix2pixhd, publicly available at
                https://shunsukesaito.github.io/PIFuHD/ &
                https://github.com/NVIDIA/pix2pixHD

        Args:
            model_dir: the root directory of the model files
            modelconfig: the config param path of the model
        	model_dircudazUse GPU: {}cpuzUse CPU: {}z{}/{}zNorm_B_GAN.pthzNorm_F_GAN.pthzfasterrcnn_resnet50.pthmodel)ckptdevice)map_location   @   global   	      instance)
model_path)r#   r#   )r    r    r    
resolutionr#   )r    r    Nr   zmodel load over )#super__init__torchr   is_availabler   loggerinfoformatr   TORCH_MODEL_FILETF_GRAPH_FILEr   	meshmodelr
   detectorload_state_dictloadr   netBnetFtoevalr   portrait_mattingnparrayr   coordsmatidentityr   floatcalib	unsqueeze)selfr   modelconfigargskwargsr"   normal_back_modelnormal_front_modelhuman_seg_modelfastrcnn_ckptb_minb_maxprojection_matrix	__class__r%   l/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/cv/human_reconstruction/Reconstruction.pyr'   "   sH   	




zHumanReconstruction.__init__c                 C   s*   | j |}|d }|jddd}||fS )N).Nr      )axis)r7   runrepeat)r@   imgresultmaskr%   r%   rM   get_maskP   s   zHumanReconstruction.get_maskc                 C   s  t |d d d d d df d }|j\}}}d}tj|dddtjdd }| j|}|d }	|d }
|d }|d }t|
|	 || }t	
|
|
|	 d	  ||| d	  g}t|d
 }t	
|d |d  |d |d  g|d |d  |d |d  g|d |d  |d |d  gg}t	
ddgd|d g|d dgg}td||}t||j||fd}|d d d d d d df d t	j}|S )Nr        o@   rN   r   r    )dtype)N.g       @g?
similarity)output_shaper#      )r   shaper(   tensor	transposefloat32r0   rP   maxr8   r9   intr   r   inverseastypeuint8)r@   img_urlimagehw_
image_sizeimage_tensorbboxleftrighttopbottomold_sizecentersizesrc_ptsDST_PTStform	dst_imager%   r%   rM   crop_imgV   s:   ""$(zHumanReconstruction.crop_imgc                 C   s>  t t  t ddg}t|d}t|d}||	d}|
| j}| j|}| j|}t|d}t  |	d}| |    d }t|dd d d d d d d d df d }| |    d }t|dd d d d d d d d df d }|tj}|tj}||fS )	N)      ?ry   ry   )rW   rW   RGBr   )r    rN   r   ry   r#   rV   )
transformsComposeToTensor	Normalizecv2resizeImage	fromarrayconvertr?   r5   r   r4   forwardr3   r   detachnumpyr8   r^   rc   rd   )r@   rR   rT   	to_tensorim_512	image_512nml_fnml_br%   r%   rM   generation_normalq   sN   
z%HumanReconstruction.generation_normal)
__name__
__module____qualname__r'   rU   r(   no_gradrx   r   __classcell__r%   r%   rK   rM   r      s    .
r   ),os.pathpathosptypingr   r   r   r8   	PIL.Imager   r(   torchvision.transformsr{   
skimage.ior   skimage.transformr   r   modelscope.metainfor   modelscope.models.baser   r   modelscope.models.builderr	   :modelscope.models.cv.human_reconstruction.models.detectorsr
   @modelscope.models.cv.human_reconstruction.models.human_segmenterr   9modelscope.models.cv.human_reconstruction.models.networksr   :modelscope.models.cv.human_reconstruction.models.PixToMeshr   /modelscope.models.cv.human_reconstruction.utilsr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r*   register_modulehuman_reconstructionr   r%   r%   r%   rM   <module>   s0   