o
    *jA                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZ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 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 ejejejdG dd deZ dS )    N)AnyDictOptionalUnion)StableDiffusionInpaintPipeline)	Pipelines)
OutputKeys)	PIPELINES)DiffusersPipeline)
load_image)Config)Tasks)module_namec                       sx   e Zd ZdZddedef fddZ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 )ImageInpaintingSDV2Pipelinea&   Stable Diffusion for Image Inpainting Pipeline.

    Example:

    >>> import cv2
    >>> from modelscope.outputs import OutputKeys
    >>> from modelscope.pipelines import pipeline
    >>> from modelscope.utils.constant import Tasks

    >>> input_location = 'data/test/images/image_inpainting/image_inpainting.png'
    >>> input_mask_location = 'data/test/images/image_inpainting/image_inpainting_mask.png'
    >>> prompt = 'background'

    >>> input = {
    >>>     'image': input_location,
    >>>     'mask': input_mask_location,
    >>>     'prompt': prompt
    >>> }
    >>> image_inpainting = pipeline(Tasks.image_inpainting, model='damo/cv_stable-diffusion-v2_image-inpainting_base')
    >>> output = image_inpainting(input)[OutputKeys.OUTPUT_IMG]
    >>> cv2.imwrite('result.png', output)

    gpumodeldevicec                    sd   t  j||fi | |dtj}tj||d| _| j| j	 |dd}|r0| j
  dS dS )z
        Use `model` to create a stable diffusion pipeline for image inpainting.
        Args:
            model: model id on modelscope hub.
            device: str = 'gpu'
        torch_dtype)r   enable_attention_slicingTN)super__init__gettorchZfloat16r   Zfrom_pretrainedpipelinetor   r   )selfr   r   kwargsr   r   	__class__ w/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/image_inpainting_sdv2_pipeline.pyr   3   s   z$ImageInpaintingSDV2Pipeline.__init__c                 K   s
   i ||fS )a|  
        this method should sanitize the keyword args to preprocessor params,
        forward params and postprocess params on '__call__' or '_process_single' method

        Returns:
            Dict[str, str]:  preprocess_params = {}
            Dict[str, str]:  forward_params = pipeline_parameters
            Dict[str, str]:  postprocess_params = pipeline_parameters
        r   )r   Zpipeline_parametersr   r   r    _sanitize_parametersG   s   

z0ImageInpaintingSDV2Pipeline._sanitize_parametersinputsreturnc                 K   s  t |tstdtt |dd}|dd}|dd }|dd}|d	d
}d| v r7|d }n|dd}td|  t|d }	t|d }
|	j	\}}td| d| d t
dd ||f\}}|	||f}	|
||f}
| j||	|
|||||||d
jd }d|iS )Nz/Expected the input to be a dictionary, but got num_inference_steps2   guidance_scaleg      @negative_promptnum_images_per_prompt   etag        prompt
backgroundzTest with prompt: imagemaskzloaded input image of size (z, )c                 S   s   | | d  S )N@   r   )xr   r   r    <lambda>l   s    z5ImageInpaintingSDV2Pipeline.forward.<locals>.<lambda>)
r+   r-   Z
mask_imageheightwidthr$   r&   r'   r(   r*   r   result)
isinstancedict
ValueErrortypeinputr   keysprintr   sizemapresizer   Zimages)r   r"   Zforward_paramsr$   r&   r'   r(   r*   r+   r-   r.   whr4   r3   Z	out_imager   r   r    forwardS   sJ   



z#ImageInpaintingSDV2Pipeline.forwardc                 K   s0   t |d }tj|d d d d d d df iS )Nr5   )nparrayr   Z
OUTPUT_IMG)r   r"   r   r5   r   r   r    postprocess~   s   "z'ImageInpaintingSDV2Pipeline.postprocess)r   )__name__
__module____qualname____doc__strr   r!   r   r   rB   rF   __classcell__r   r   r   r    r      s    

*+r   )!mathossystempfiletypingr   r   r   r   Zcv2numpyrD   r   Z	diffusersr   Zmodelscope.metainfor   Zmodelscope.outputsr   Zmodelscope.pipelines.builderr	   ZEmodelscope.pipelines.multi_modal.diffusers_wrapped.diffusers_pipeliner
   Zmodelscope.preprocessors.imager   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   Zregister_moduleZimage_inpaintingZimage_inpainting_sdv2r   r   r   r   r    <module>   s(   