o
    h7                     @   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 r%d dl
mZ G dd	 d	e	ZdS )
    N   )CLIPSegForImageSegmentation)is_vision_availablerequires_backends   )PipelineTool)Imagec                       s\   e Zd ZdZdZdZeZddgZdgZ	 fddZ
ddd	ef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-refinedimage_segmenterimagetextc                    s"   t | dg t j|i | d S )Nvision)r   super__init__)selfargskwargs	__class__ [/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/tools/image_segmentation.pyr   *   s   zImageSegmentationTool.__init__r   labelc                 C   s   | j |g|gdddS )NTpt)r   imagespaddingreturn_tensors)pre_processor)r   r   r   r   r   r   encode.   s   zImageSegmentationTool.encodec                 C   s@   t   | jdi |j}W d    |S 1 sw   Y  |S )Nr   )torchno_gradmodellogits)r   inputsr!   r   r   r   forward1   s   

zImageSegmentationTool.forwardc                 C   s>   |    }d||dk< d||dk< t|d tjS )Nr   r      )cpudetachnumpyr   	fromarrayastypenpuint8)r   outputsarrayr   r   r   decode6   s   zImageSegmentationTool.decode)__name__
__module____qualname__descriptiondefault_checkpointnamer   model_classr"   r,   r   strr   r#   r.   __classcell__r   r   r   r   r	      s    r	   )r'   r*   r   models.clipsegr   utilsr   r   baser   PILr   r	   r   r   r   r   <module>   s   