o
    *j                     @   s   d dl mZ d dlmZmZmZ d dlZd dlZ	d dl
Z
d dl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 d d
lmZmZ ejejejdG dd deZdS )    N)AnyDictUnion)Image)	Pipelines)SDAFNet_Tryon)
OutputKeys)Pipeline)	PIPELINES)
load_image)	ModelFileTasks)module_namec                       s   e Zd Zdef fddZdeeeef 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
deeef deeef fddZ  ZS )VirtualTryonPipelinemodelc                    s   t  j|d ttj rdnd| _dd   fdd}| j| _tjt	
| jtjddd	}td
d| j| _|| j| | j  d| _ddlm} ||j| jdd| |ddg| _dS )z
        use `model` to create a virtual tryon pipeline for prediction
        Args:
            model: model id on modelscope hub.
        )r   cudacpuc                 S   s   g }|   D ]:\}}d|dd kr|dd  }dt| d vr(|dd}||v r@|| j|jkr@|| | || qd S )Nzmodule.r      z.module..)itemslistkeysreplaceshapeZcopy_append)
src_params	own_stateZcopied_keysnameparam r   p/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/virtual_try_on_pipeline.pyfilter_param#   s   
z3VirtualTryonPipeline.__init__.<locals>.filter_paramc                    s0   d|v r|d }|   } || | | d S )N
state_dict)r"   Zload_state_dict)r   r   r   r!   r   r    load_pretrained/   s
   
z6VirtualTryonPipeline.__init__.<locals>.load_pretrainedT)Zweights_only   )Zref_in_channel   r   )
transforms   )interpolation)      ?r*   r*   N)super__init__torchdevicer   Zis_availabler   Z
local_pathloadospjoinr   ZTORCH_MODEL_FILEr   toevalsizeZtorchvisionr'   ZComposeZResizeZToTensorZ	Normalizetest_transforms)selfr   kwargsr$   r   r'   	__class__r#   r    r,      s,   

zVirtualTryonPipeline.__init__inputreturnc           	      C   s  t |trd}d}d}nd}d}d}t || tr,t|| }t|| }t|| }nt || tjjrK|| d}|| d}|| d}nyt || tjrt	|j
dkrut|| tj}t|| tj}t|| tj}t|d d d d d d df d	d}t|d d d d d d df d	d}t|d d d d d d df d	d}n	td
t| | |}| |}| |}|d|d|dd}|S )Nr      r(   masked_modelposeclothRGBZuint8z9input should be either str, PIL.Image, np.array, but got )r=   r>   r?   )
isinstancetuplestrr   PILr   convertnpZndarraylenr   cv2ZcvtColorZCOLOR_GRAY2BGRZ	fromarrayZastype	TypeErrortyper5   Z	unsqueeze)	r6   r:   Zindex_modelZ
index_poseZindex_clothimg_agnosticr>   	cloth_imginputsr   r   r    
preprocessF   sd   


   


zVirtualTryonPipeline.preprocessrN   c                 C   sZ   |d  | j}|d  | j}|d  | j}tj||fdd}| |||}tj|iS )Nr=   r>   r?   r<   )dim)r2   r.   r-   catr   r   
OUTPUT_IMG)r6   rN   rL   r>   rM   Z	ref_inputtryon_resultr   r   r    forwards   s   
zVirtualTryonPipeline.forwardoutputsc                 C   sJ   |t j ddddd}|dddj 	 }||t j< |S )Nr   r(      r<   g      ?g       @   )
r   rR   ZpermuteZsqueezeadddivmuldatar   numpy)r6   rU   rS   r   r   r    postprocess|   s    
z VirtualTryonPipeline.postprocess)__name__
__module____qualname__rD   r,   r   r   r   rC   rO   rT   r]   __classcell__r   r   r8   r    r      s    -

"-*	r   )Zos.pathpathr0   typingr   r   r   rI   r\   rG   rE   r-   r   Zmodelscope.metainfor   Z!modelscope.models.cv.virual_tryonr   Zmodelscope.outputsr   Zmodelscope.pipelines.baser	   Zmodelscope.pipelines.builderr
   Zmodelscope.preprocessorsr   Zmodelscope.utils.constantr   r   Zregister_moduleZvirtual_try_onr   r   r   r   r    <module>   s$   