o
    *j*                     @   s   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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mZmZmZmZ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$ e$ Z%ej&e"j'ej(dG dd deZ)dS )    N)AnyDict)Image)	Pipelines)DDIMSampler)	box2squre
box_in_boxexpand_bboxexpand_image_maskget_bbox_from_maskpad_to_squaresobel)
OutputKeys)InputPipeline)	PIPELINES)
load_image)Tasks)
get_logger)module_namec                       s   e Zd ZdZdef fddZedddZded	e	ee
f fd
dZ				d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 )AnydoorPipelinea   AnyDoor Pipeline.

    Examples:

    >>> from modelscope.pipelines import pipeline
    >>> from modelscope.utils.constant import Tasks
    >>> from PIL import Image

    >>> ref_image = 'data/test/images/image_anydoor_fg.png'
    >>> ref_mask = 'data/test/images/image_anydoor_fg_mask.png'
    >>> bg_image = 'data/test/images/image_anydoor_bg.png'
    >>> bg_mask = 'data/test/images/image_anydoor_bg_mask.png'

    >>> anydoor_pipeline = pipeline(Tasks.image_to_image_generation, model='damo/AnyDoor')
    >>> out = anydoor_pipeline((ref_image, ref_mask, bg_image, bg_mask))
    >>> assert isinstance(out['output_img'], Image.Image)
    modelc                    sT   t  jdd|i| tj| jj| jjj}| j	| j
|dd t| j| _dS )z
        use `model` to create a action detection pipeline for prediction
        Args:
            model: model id on modelscope hub.
        r   cuda)locationN )super__init__ospathjoinr   Z	model_dircfgZ
model_pathZload_state_dict_get_state_dictr   ddim_sampler)selfr   kwargsZ
model_ckpt	__class__r   i/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/anydoor_pipeline.pyr   1   s   zAnydoorPipeline.__init__cpuc                 C   st   dd }t j| \}}| dkrdd l}|jj| |d}n|tj| t|dd}||}t	d|  d	 |S )
Nc                 S   s   |  d| S )N
state_dict)get)dr   r   r'   get_state_dictA   s   z7AnydoorPipeline._get_state_dict.<locals>.get_state_dictz.safetensorsr   )deviceT)Zmap_locationZweights_onlyzLoaded state_dict from [])
r   r   splitextlowerZsafetensors.torchtorchZ	load_fileloadr-   print)Z	ckpt_pathr   r,   _	extensionZsafetensorsr)   r   r   r'   r!   >   s"   zAnydoorPipeline._get_state_dictinputsreturnc                 C   sD  |\}}}}t t|d}t t t|ddkddt j}t t|d}t t t|ddkddt j}t|}t |||gd}|| t 	|d d|   }|\}	}
}}||	|
||d d f }||	|
||f }t j
dd	d
 }t|||d\}}t |||gd}t|ddd}t|t jdt j}t|d ddd}t|t jdt j}|d d d d df }||}}t |||gd}t||d }t|}t||ddgd}t||ddgd}t||}|\}	}
}}||	|
||d d f }||	|
||f }t||}|\}	}
}}t|t j|| |
|	 f}t|t j|| |
|	 f}|dkt j}| }|||	|
||d d f< | d }d||	|
||d d f< t |||gd}|jd |jd }}t|dddt j}t|dddt j}t|dddt j}|jd |jd }}t|t jdt j}t|t jdt j}t|t jdt jdkt j}|d }|d d }|d d }t ||d d d d d df gd}t|| | | t ||||gt |d}|S )NRGBL      r            
   )ratioF)Z	pad_valuerandom)   rC   g?g333333?g?      @g              ?   rG   g      ?     _@)	tar_imagerefZjpghintextra_sizestar_box_yyxx_crop)npZasarrayr   convertwhereZastypeZuint8r   stackZ	ones_likerB   randintr
   r   cv2resizer   r	   r   r   copyshapeZfloat32Zconcatenatedictarray)r#   r6   Z	ref_imageZref_maskrI   Ztar_maskZref_box_yyxxZ
ref_mask_3Zmasked_ref_imagey1y2x1x2rA   Zmasked_ref_image_composeZref_mask_composeZref_image_collageZtar_box_yyxxrM   Zcropped_target_imageZcropped_tar_maskZcollageZcollage_maskH1W1H2W2itemr   r   r'   
preprocessS   s  










&zAnydoorPipeline.preprocessr;   rE      rD   ra   c              
      s  |d    }|d }|d }d}|  tjfddt|D ddtd		 |   tj fd
dt|D dd t d		  d\}	}
g| j
 gd}g| j
tdg| gd}d|	d |
d f}|gd | j
_| jj||||dd||d\}}| j
|}t|dd d    }|d d d d d d d df }t|dd}|d }t|dddd d d d d f }|d    }|d    }t||||dS )NrI   rJ   rK   r;   c                       g | ]} qS r   r   .0r4   )controlr   r'   
<listcomp>       z+AnydoorPipeline.forward.<locals>.<listcomp>r   )dimzb h w c -> b c h wc                    rd   r   r   re   )
clip_inputr   r'   rh      ri   rF   )Zc_concatZc_crossattn)r;      rC   rC            F)verboseetaZunconditional_guidance_scaleZunconditional_conditioningzb c h w -> b h w crH   r<   r=   rL   rM   )predrI   sizesrM   )r(   numpyfloatr   r1   rQ   rangeeinopsZ	rearrangecloner   Zget_learned_conditioningZzerosZcontrol_scalesr"   sampleZdecode_first_stagerN   ZcliprW   )r#   ra   Znum_samplesZstrengthZ
ddim_stepsscalerI   rJ   rK   HWZcondZun_condrV   Zsamplesr4   Z	x_samplesresultrr   rs   rM   r   )rk   rg   r'   forward   sh    

 $zAnydoorPipeline.forwardc                 C   s  |d }|d }|d }|d }|\}}}}	|\}
}}}t ||	|f}d}||krj||| || f ||
| || || || d d f< t| ddd}|ddd }tj|d	d
}t	j
|iS ||	k rt|	| d }|	| | }|d d || d d f }nt|| d }|| | }||| d d d d f }| }||| || f ||
| || || || d d f< t|ddd}|ddd }tj|d	d
}t	j
|iS )Nrr   rI   rs   rM   rl      r   r;   r8   )mode)rS   rT   r1   Z
from_numpyrU   Zpermutert   r   Z	fromarrayr   Z
OUTPUT_IMGint)r#   r6   rr   rI   rL   rM   r]   r^   r_   r`   rY   rZ   r[   r\   mZ	gen_imageZpad1Zpad2r   r   r'   postprocess  s6   >
>
zAnydoorPipeline.postprocess)r(   )r;   rE   rc   rD   )__name__
__module____qualname____doc__strr   staticmethodr!   r   r   r   rb   r~   r   __classcell__r   r   r%   r'   r      s     t


*?r   )*r   typingr   r   rS   rw   rt   rN   requestsr1   ZPILr   Zmodelscope.metainfor   Z-modelscope.models.cv.anydoor.cldm.ddim_hackedr   Z0modelscope.models.cv.anydoor.datasets.data_utilsr   r   r	   r
   r   r   r   Zmodelscope.outputsr   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessors.imager   Zmodelscope.utils.constantr   Zmodelscope.utils.loggerr   loggerZregister_moduleZimage_to_image_generationZanydoorr   r   r   r   r'   <module>   s,   $