
    g`%                     H   d dl Z d dlmZ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            rd dlmZ ddlmZ  e
            rdd	lmZmZmZmZ  ej        e          Zeeef         Zee         Z e	 ed
                     G d de                      Z dS )    N)AnyDictListUnion   )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)has_image_processorc                   n     e Zd ZdZ fdZd Zddeeee	         f         f fdZ
ddZd Z	 ddZ x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).
    c                     t                      j        |i | | j        dk    rt          d| j         d          t          | d           t          j                    }|                    t                     |                    t                     |                    t                     |                     |           d S )NtfzThe z is only available in PyTorch.vision)super__init__	framework
ValueError	__class__r   r   copyupdater   r   r   check_model_type)selfargskwargsmappingr    s       e/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/pipelines/image_segmentation.pyr   z"ImageSegmentationPipeline.__init__D   s    $)&)))>T!!RDNRRRSSS$)))<ACCDEEEDEEEEFFFg&&&&&    c                     i }i }d|v r|d         |d<   |d         |d<   d|v r|d         |d<   d|v r|d         |d<   d|v r|d         |d<   d|v r%t          j        dt                     |d         |d<   |i |fS )Nsubtask	thresholdmask_thresholdoverlap_mask_area_thresholdtimeoutzUThe `timeout` argument is deprecated and will be removed in version 5 of Transformers)warningswarnFutureWarning)r$   r&   preprocess_kwargspostprocess_kwargss       r(   _sanitize_parametersz.ImageSegmentationPipeline._sanitize_parametersQ   s    ,29,=y)+1)+<i(&  .4[.A{+v%%39:J3K/0(F22@FGd@e<=Mgiv   ,2)+<i( "&888r)   Nreturnc                     d|v r|                     d          }|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.

        Return:
            A dictionary or a list of dictionaries containing the result. 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   __call__)r$   inputsr&   r    s      r(   r:   z"ImageSegmentationPipeline.__call__e   sU    P vZZ))F>hiiiuww11&111r)   c                    t          ||          }|j        |j        fg}| j        j        j        j        dk    r}|i }nd|gi} | j        d
|gdd|}| j        dk    r|	                    | j
                  }|                     |d         d| j        j        j        | j                  d         |d<   n=|                     |gd          }| j        dk    r|	                    | j
                  }||d	<   |S )N)r/   OneFormerConfigtask_inputspt)r8   return_tensors
max_length)paddingrA   r@   	input_idstarget_size )r   heightwidthmodelconfigr    __name__image_processorr   totorch_dtype	tokenizertask_seq_len)r$   imager+   r/   rD   r&   r;   s          r(   
preprocessz$ImageSegmentationPipeline.preprocess   s(   5'222ek23:&/3DDD''3)T)X%XXQWXXF~%%4#344$(NN}%$:,9#~	 %3 % %
 %F=!! ))%)NNF~%%4#344 +}r)   c                 T    |                     d          } | j        di |}||d<   |S )NrD   rE   )r9   rH   )r$   model_inputsrD   model_outputss       r(   _forwardz"ImageSegmentationPipeline._forward   s<    "&&}55"
22\22'2m$r)   ?      ?c                 6   d }|dv r"t          | j        d          r| j        j        }n%|dv r!t          | j        d          r| j        j        }| ||||||d                   d         }g }|d         }	|d	         D ]}
|	|
d
         k    dz  }t	          j        |                                                    t          j	                  d          }| j
        j        j        |
d                  }|
d         }|                    |||d           n|dv rt          | j        d          r| j                            ||d                   d         }g }|                                }	t          j        |	          }|D ]n}|	|k    dz  }t	          j        |                    t          j	                  d          }| j
        j        j        |         }|                    d ||d           on't!          d| dt#          | j
                             |S )N>   Npanoptic"post_process_panoptic_segmentation>   Ninstance"post_process_instance_segmentationrD   )r,   r-   r.   target_sizesr   segmentationsegments_infoid   L)modelabel_idscore)re   labelmask>   Nsemantic"post_process_semantic_segmentation)r]   zSubtask z is not supported for model )hasattrrK   rZ   r\   r   	fromarraynumpyastypenpuint8rH   rI   id2labelappendri   uniquer   type)r$   rT   r+   r,   r-   r.   fnoutputs
annotationr^   segmentrg   rf   re   labelss                  r(   postprocessz%ImageSegmentationPipeline.postprocess   sh    (((WT5IKo-p-p(%HBB***wt7KMq/r/r*%HB>b#-,G*=9   G J">2L"?3 R R$5<tzz||':':28'D'D3OOO
)27:3FG(!!EE4"P"PQQQQR ***wt7KMq/r/r**MMM-,H N  G J"==??LY|,,F Q Q$-4t{{28'<'<3GGG
)259!!D5$"O"OPPPP	Q ___TRVR\M]M]__```r)   )N)NN)NrV   rW   rW   )rJ   
__module____qualname____doc__r   r5   r   Predictionsr   
Predictionr:   rQ   rU   ry   __classcell__)r    s   @r(   r   r      s        ! !F' ' ' ' '9 9 9(,2 ,2{DDT7T1U ,2 ,2 ,2 ,2 ,2 ,2\   0   kn, , , , , , , ,r)   r   )!r0   typingr   r   r   r   rl   rn   utilsr   r	   r
   r   r   baser   r   PILr   image_utilsr   models.auto.modeling_autor   r   r   r   
get_loggerrJ   loggerstrr~   r}   r   rE   r)   r(   <module>r      s    ) ) ) ) ) ) ) ) ) ) ) )     k k k k k k k k k k k k k k 4 4 4 4 4 4 4 4  )((((((             
	H	%	% #s(^
: ,,FFFGG} } } } } } } HG} } }r)   