o
    *Îj0  ã                   @   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)ZckptÚdevice)Zmap_locationé   é@   Úglobalé   é	   é   Úinstance)Ú
model_path)éÿÿÿÿr!   r!   )r   r   r   Ú
resolutionr!   )r   r   Nr   zmodel load over© )#ÚsuperÚ__init__Útorchr   Zis_availabler   ÚloggerÚinfoÚformatr   ZTORCH_MODEL_FILEZTF_GRAPH_FILEr   Z	meshmodelr
   ÚdetectorZload_state_dictÚloadr   ÚnetBÚnetFÚtoÚevalr   Úportrait_mattingÚnpÚarrayr   ZcoordsZmatÚidentityr   ÚfloatZcalibÚ	unsqueeze)Úselfr   ZmodelconfigÚargsÚkwargsr    Znormal_back_modelZnormal_front_modelZhuman_seg_modelZfastrcnn_ckptZb_minZb_maxZprojection_matrix©Ú	__class__r#   úy/var/www/html/Deteccion_Ine/venv/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   é   )Zaxis)r0   ÚrunÚrepeat)r6   ÚimgÚresultÚmaskr#   r#   r;   Ú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@é   r<   r   r   )Zdtype)N.g       @gš™™™™™ñ?Z
similarity)Zoutput_shaper!   éÿ   )r   Úshaper&   ZtensorÚ	transposeZfloat32r*   r=   Úmaxr1   r2   Úintr   r   ZinverseÚastypeÚuint8)r6   Zimg_urlÚimageÚhÚwÚ_Z
image_sizeZimage_tensorZbboxÚleftÚrightÚtopÚbottomZold_sizeÚcenterÚsizeZsrc_ptsZDST_PTSZtformZ	dst_imager#   r#   r;   Ú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)ç      à?rW   rW   )rD   rD   ÚRGBr   )r   r<   r   rW   r!   rC   )Ú
transformsZComposeZToTensorZ	NormalizeÚcv2ÚresizeÚImageZ	fromarrayÚconvertr5   r.   r   r-   Úforwardr,   r   ÚdetachÚnumpyr1   rG   rJ   rK   )r6   r?   rA   Z	to_tensorZim_512Z	image_512Znml_fZnml_br#   r#   r;   Úgeneration_normalq   sN   
þÿÿÿÿÿÿÿÿÿÿz%HumanReconstruction.generation_normal)
Ú__name__Ú
__module__Ú__qualname__r%   rB   r&   Zno_gradrV   ra   Ú__classcell__r#   r#   r9   r;   r      s    .
r   ),Zos.pathÚpathZospÚtypingr   rZ   r`   r1   Z	PIL.Imager\   r&   Ztorchvision.transformsrY   Z
skimage.ior   Zskimage.transformr   r   Zmodelscope.metainfor   Zmodelscope.models.baser   r   Zmodelscope.models.builderr	   Z:modelscope.models.cv.human_reconstruction.models.detectorsr
   Z@modelscope.models.cv.human_reconstruction.models.human_segmenterr   Z9modelscope.models.cv.human_reconstruction.models.networksr   Z:modelscope.models.cv.human_reconstruction.models.PixToMeshr   Z/modelscope.models.cv.human_reconstruction.utilsr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   r'   Zregister_moduleZhuman_reconstructionr   r#   r#   r#   r;   Ú<module>   s0   ÿ