a
    h&                     @   s   d dl mZmZmZ d dlZddlmZmZm	Z	m
Z
mZ ddlmZmZ e	 rfd dlmZ ddlmZ e rdd	lmZmZmZmZ e
eZeed
dG dd deZdS )    )AnyUnionoverloadN   )add_end_docstringsis_torch_availableis_vision_availableloggingrequires_backends   )Pipelinebuild_pipeline_init_args)Image)
load_image)*MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES-MODEL_FOR_INSTANCE_SEGMENTATION_MAPPING_NAMES-MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES.MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMEST)Zhas_image_processorc                	       s  e Zd ZdZdZdZdZdZ fddZdd Z	e
eed	f eeeeef  d
ddZe
eee ed	 f eeeeeef   d
ddZeed	ee ed	 f eeeeeef  eeeeef   f d
 fddZdddZdd ZdddZ  ZS )ImageSegmentationPipelinea  
    Image segmentation pipeline using any `AutoModelForXXXSegmentation`. This pipeline predicts masks of objects and
    their classes.

    Example:

    ```python
    >>> from transformers import pipeline

    >>> segmenter = pipeline(model="facebook/detr-resnet-50-panoptic")
    >>> segments = segmenter("https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png")
    >>> len(segments)
    2

    >>> segments[0]["label"]
    'bird'

    >>> segments[1]["label"]
    'bird'

    >>> type(segments[0]["mask"])  # This is a black and white mask showing where is the bird on the original image.
    <class 'PIL.Image.Image'>

    >>> segments[0]["mask"].size
    (768, 512)
    ```


    This image segmentation pipeline can currently be loaded from [`pipeline`] using the following task identifier:
    `"image-segmentation"`.

    See the list of available models on
    [huggingface.co/models](https://huggingface.co/models?filter=image-segmentation).
    FTNc                    sl   t  j|i | | jdkr.td| j dt| d t }|t	 |t
 |t | | d S )NtfzThe z is only available in PyTorch.Zvision)super__init__	framework
ValueError	__class__r
   r   copyupdater   r   r   Zcheck_model_type)selfargskwargsmappingr    e/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/pipelines/image_segmentation.pyr   D   s    




z"ImageSegmentationPipeline.__init__c                 K   s   i }i }d|v r(|d |d< |d |d< d|v r<|d |d< d|v rP|d |d< d|v rd|d |d< d|v rx|d |d< |i |fS )Nsubtask	thresholdmask_thresholdoverlap_mask_area_thresholdtimeoutr"   )r   r   Zpreprocess_kwargsZpostprocess_kwargsr"   r"   r#   _sanitize_parametersQ   s    z.ImageSegmentationPipeline._sanitize_parameterszImage.Image)inputsr   returnc                 K   s   d S Nr"   r   r*   r   r"   r"   r#   __call__b   s    z"ImageSegmentationPipeline.__call__c                 K   s   d S r,   r"   r-   r"   r"   r#   r.   e   s    c                    s6   d|v r| d}|du r"tdt j|fi |S )a	  
        Perform segmentation (detect masks & classes) in the image(s) passed as inputs.

        Args:
            inputs (`str`, `list[str]`, `PIL.Image` or `list[PIL.Image]`):
                The pipeline handles three types of images:

                - A string containing an HTTP(S) link pointing to an image
                - A string containing a local path to an image
                - An image loaded in PIL directly

                The pipeline accepts either a single image or a batch of images. Images in a batch must all be in the
                same format: all as HTTP(S) links, all as local paths, or all as PIL images.
            subtask (`str`, *optional*):
                Segmentation task to be performed, choose [`semantic`, `instance` and `panoptic`] depending on model
                capabilities. If not set, the pipeline will attempt tp resolve in the following order:
                  `panoptic`, `instance`, `semantic`.
            threshold (`float`, *optional*, defaults to 0.9):
                Probability threshold to filter out predicted masks.
            mask_threshold (`float`, *optional*, defaults to 0.5):
                Threshold to use when turning the predicted masks into binary values.
            overlap_mask_area_threshold (`float`, *optional*, defaults to 0.5):
                Mask overlap threshold to eliminate small, disconnected segments.
            timeout (`float`, *optional*, defaults to None):
                The maximum time in seconds to wait for fetching images from the web. If None, no timeout is set and
                the call may block forever.

        Return:
            If the input is a single image, will return a list of dictionaries, if the input is a list of several images,
            will return a list of list of dictionaries corresponding to each image.

            The dictionaries contain the mask, label and score (where applicable) of each detected object and contains
            the following keys:

            - **label** (`str`) -- The class label identified by the model.
            - **mask** (`PIL.Image`) -- A binary mask of the detected object as a Pil Image of shape (width, height) of
              the original image. Returns a mask filled with zeros if no object is found.
            - **score** (*optional* `float`) -- Optionally, when the model is capable of estimating a confidence of the
              "object" described by the label and the mask.
        imagesNzICannot call the image-classification pipeline without an inputs argument!)popr   r   r.   r-   r!   r"   r#   r.   h   s
    ,
c                 C   s   t ||d}|j|jfg}| jjjjdkr|d u r8i }n
d|gi}| jf |gdd|}| jdkrp|	| j
}| j|d d| jjj| jdd |d< n&| j|gdd}| jdkr|	| j
}||d	< |S )
N)r(   ZOneFormerConfigZtask_inputspt)r/   return_tensors
max_length)paddingr3   r2   Z	input_idstarget_size)r   heightwidthmodelconfigr   __name__image_processorr   toZdtype	tokenizerZtask_seq_len)r   imager$   r(   r5   r   r*   r"   r"   r#   
preprocess   s,    



z$ImageSegmentationPipeline.preprocessc                 C   s&   | d}| jf i |}||d< |S )Nr5   )r0   r8   )r   Zmodel_inputsr5   model_outputsr"   r"   r#   _forward   s    
z"ImageSegmentationPipeline._forward?      ?c                 C   s  d }|dv r"t | jdr"| jj}n|dv r>t | jdr>| jj}|d ur||||||d dd }g }|d }	|d	 D ]Z}
|	|
d
 kd }tj| tj	dd}| j
jj|
d  }|
d }||||d qtn|dv rht | jdrh| jj||d dd }g }| }	t|	}|D ]H}|	|kd }tj|tj	dd}| j
jj| }|d ||d qntd| dt| j
 |S )N>   ZpanopticN"post_process_panoptic_segmentation>   instanceN"post_process_instance_segmentationr5   )r%   r&   r'   target_sizesr   segmentationZsegments_infoid   L)modeZlabel_idscore)rM   labelmask>   ZsemanticN"post_process_semantic_segmentation)rG   zSubtask z is not supported for model )hasattrr;   rD   rF   r   Z	fromarraynumpyZastypenpZuint8r8   r9   Zid2labelappendrP   uniquer   type)r   r@   r$   r%   r&   r'   fnoutputs
annotationrH   segmentrO   rN   rM   labelsr"   r"   r#   postprocess   sN    

z%ImageSegmentationPipeline.postprocess)NN)NrB   rC   rC   )r:   
__module____qualname____doc__Z_load_processorZ_load_image_processorZ_load_feature_extractorZ_load_tokenizerr   r)   r   r   strr   listdictr.   r?   rA   r\   __classcell__r"   r"   r!   r#   r      s$   #(4&2
 r   )typingr   r   r   rR   rS   utilsr   r   r   r	   r
   baser   r   ZPILr   Zimage_utilsr   Zmodels.auto.modeling_autor   r   r   r   Z
get_loggerr:   loggerr   r"   r"   r"   r#   <module>   s   
