a
    d                     @   s^   d dl Zd dlZddlmZ ddlmZmZ ddlm	Z	 e rJd dl
mZ G dd	 d	e	ZdS )
    N   )CLIPSegForImageSegmentation)is_vision_availablerequires_backends   )PipelineTool)Imagec                       sZ   e Zd ZdZdZdZeZddgZdgZ	 fddZ
ded	d
dZdd Zdd Z  ZS )ImageSegmentationToola>  This is a tool that creates a segmentation mask of an image according to a label. It cannot create an image.It takes two arguments named `image` which should be the original image, and `label` which should be a text describing the elements what should be identified in the segmentation mask. The tool returns the mask.zCIDAS/clipseg-rd64-refinedZimage_segmenterimagetextc                    s"   t | dg t j|i | d S )NZvision)r   super__init__)selfargskwargs	__class__ n/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/transformers/tools/image_segmentation.pyr   *   s    zImageSegmentationTool.__init__r   )r
   labelc                 C   s2   |j d |j d d| jj_ | j|g|gdddS )Nr   r   )widthheightTpt)r   ZimagespaddingZreturn_tensors)sizeZpre_processorZimage_processor)r   r
   r   r   r   r   encode.   s    zImageSegmentationTool.encodec                 C   s>   t  " | jf i |j}W d    n1 s00    Y  |S )N)torchZno_gradmodellogits)r   inputsr   r   r   r   forward2   s    
0zImageSegmentationTool.forwardc                 C   s>   |    }d||dk< d||dk< t|d tjS )Nr   r      )cpudetachnumpyr   Z	fromarrayZastypenpZuint8)r   outputsarrayr   r   r   decode7   s    zImageSegmentationTool.decode)__name__
__module____qualname__descriptionZdefault_checkpointnamer   Zmodel_classr   r&   r   strr   r    r(   __classcell__r   r   r   r   r	      s   r	   )r$   r%   r   Zmodels.clipsegr   utilsr   r   baser   ZPILr   r	   r   r   r   r   <module>   s   