U
    9%e6                     @   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|| d S )NZvision)r   super__init__)selfargskwargs	__class__ d/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/tools/image_segmentation.pyr   *   s    zImageSegmentationTool.__init__r   )r
   labelc                 C   s   | j |g|gdddS )NTpt)r   ZimagespaddingZreturn_tensors)Zpre_processor)r   r
   r   r   r   r   encode.   s    zImageSegmentationTool.encodec              	   C   s&   t   | jf |j}W 5 Q R X |S )N)torchZ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   Z	fromarrayZastypenpZuint8)r   outputsarrayr   r   r   decode6   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   