
    g"+                     n    d Z ddlZddlmZ ddlZddlmZ ddlm	Z	 ddl
mZmZmZ  G d d	e          ZdS )
z(
Image/Text processor class for OWL-ViT
    N)List   )ProcessorMixin)BatchEncoding)is_flax_availableis_tf_availableis_torch_availablec                        e Zd ZdZddgZdZdZd fd	Zdd
Zd Z	d Z
d Zd Zd Zed             Zed             Z xZS )OwlViTProcessora3  
    Constructs an OWL-ViT processor which wraps [`OwlViTImageProcessor`] and [`CLIPTokenizer`]/[`CLIPTokenizerFast`]
    into a single processor that interits both the image processor and tokenizer functionalities. See the
    [`~OwlViTProcessor.__call__`] and [`~OwlViTProcessor.decode`] for more information.

    Args:
        image_processor ([`OwlViTImageProcessor`], *optional*):
            The image processor is a required input.
        tokenizer ([`CLIPTokenizer`, `CLIPTokenizerFast`], *optional*):
            The tokenizer is a required input.
    image_processor	tokenizerOwlViTImageProcessor)CLIPTokenizerCLIPTokenizerFastNc                    d }d|v r/t          j        dt                     |                    d          }||n|}|t	          d          |t	          d          t                                          ||           d S )Nfeature_extractorzhThe `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor` instead.z)You need to specify an `image_processor`.z"You need to specify a `tokenizer`.)warningswarnFutureWarningpop
ValueErrorsuper__init__)selfr   r   kwargsr   	__class__s        h/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/models/owlvit/processing_owlvit.pyr   zOwlViTProcessor.__init__.   s     &((M  
 !'

+> ? ?-<-H//N_"HIIIABBB)44444    
max_lengthnpc                 D   |||t          d          |t          |t                    s0t          |t                    r.t          |d         t                    s | j        |f||d|g}nt          |t                    rt          |d         t                    rsg }t          d |D                       }|D ]T}	t          |	          |k    r|	dg|t          |	          z
  z  z   }	 | j        |	f||d|}
|                    |
           Unt          d          |dk    rBt          j
        d	 |D             d
          }t          j
        d |D             d
          }n"|dk    rWt                      rIddlm} |
                    d |D             d
          }|
                    d |D             d
          }n|dk    rUt                      rGddl}|                    d |D             d          }|                    d |D             d          }nj|dk    rUt#                      rGddl}|                    d |D             d
          }|                    d |D             d
          }nt          d          t)                      }
||
d<   ||
d<   |(t)                      }
 | j        |fd|i|j        }||
d<   | | j        |fd|i|}|||j        |
d<   |
S |||j        |
d<   |
S |||
S t)          t/          di ||          S )a
  
        Main method to prepare for the model one or several text(s) and image(s). This method forwards the `text` and
        `kwargs` arguments to CLIPTokenizerFast's [`~CLIPTokenizerFast.__call__`] if `text` is not `None` to encode:
        the text. To prepare the image(s), this method forwards the `images` and `kwrags` arguments to
        CLIPImageProcessor's [`~CLIPImageProcessor.__call__`] if `images` is not `None`. Please refer to the doctsring
        of the above two methods for more information.

        Args:
            text (`str`, `List[str]`, `List[List[str]]`):
                The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
                (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
                `is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
            images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`,
            `List[torch.Tensor]`):
                The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch
                tensor. Both channels-first and channels-last formats are supported.
            query_images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`):
                The query image to be prepared, one query image is expected per target image to be queried. Each image
                can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image
                should be of shape (C, H, W), where C is a number of channels, H and W are image height and width.
            return_tensors (`str` or [`~utils.TensorType`], *optional*):
                If set, will return tensors of a particular framework. Acceptable values are:
                - `'tf'`: Return TensorFlow `tf.constant` objects.
                - `'pt'`: Return PyTorch `torch.Tensor` objects.
                - `'np'`: Return NumPy `np.ndarray` objects.
                - `'jax'`: Return JAX `jnp.ndarray` objects.
        Returns:
            [`BatchEncoding`]: A [`BatchEncoding`] with the following fields:
            - **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`.
            - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
              `return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not
              `None`).
            - **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`.
        NzXYou have to specify at least one text or query image or image. All three cannot be none.r   )paddingreturn_tensorsc                 ,    g | ]}t          |          S  )len).0ts     r   
<listcomp>z,OwlViTProcessor.__call__.<locals>.<listcomp>q   s    &<&<&<!s1vv&<&<&<r    zLInput text should be a string, a list of strings or a nested list of stringsr    c                     g | ]
}|d          S 	input_idsr%   r'   encodings     r   r)   z,OwlViTProcessor.__call__.<locals>.<listcomp>~   s    +\+\+\hH[,A+\+\+\r   )axisc                     g | ]
}|d          S attention_maskr%   r.   s     r   r)   z,OwlViTProcessor.__call__.<locals>.<listcomp>   s    0f0f0fPX:J1K0f0f0fr   jaxc                     g | ]
}|d          S r,   r%   r.   s     r   r)   z,OwlViTProcessor.__call__.<locals>.<listcomp>   s    ,],],]xXk-B,],],]r   c                     g | ]
}|d          S r2   r%   r.   s     r   r)   z,OwlViTProcessor.__call__.<locals>.<listcomp>   s    1g1g1gQY(;K2L1g1g1gr   ptc                     g | ]
}|d          S r,   r%   r.   s     r   r)   z,OwlViTProcessor.__call__.<locals>.<listcomp>   s    &W&W&Wx'<&W&W&Wr   )dimc                     g | ]
}|d          S r2   r%   r.   s     r   r)   z,OwlViTProcessor.__call__.<locals>.<listcomp>   s    +a+a+a8H5E,F+a+a+ar   tfc                     g | ]
}|d          S r,   r%   r.   s     r   r)   z,OwlViTProcessor.__call__.<locals>.<listcomp>   s    %V%V%Vh{&;%V%V%Vr   c                     g | ]
}|d          S r2   r%   r.   s     r   r)   z,OwlViTProcessor.__call__.<locals>.<listcomp>   s    *`*`*`(84D+E*`*`*`r   z/Target return tensor type could not be returnedr-   r3   r#   query_pixel_valuespixel_values)datatensor_typer%   )r   
isinstancestrr   r   maxr&   append	TypeErrorr    concatenater   	jax.numpynumpyr	   torchcatr   
tensorflowstackr   r   r?   dict)r   textimagesquery_imagesr"   r#   r   	encodingsmax_num_queriesr(   r/   r-   r3   jnprJ   r;   r>   image_featuress                     r   __call__zOwlViTProcessor.__call__@   s(   H <L0V^j   $$$ pD$)?)? p
SWXYSZ\`HaHa p+T^Dk'R`kkdjkkl		D$'' pJtAw,E,E p	 #&&<&<t&<&<&<"="=  / /A1vv003q66)A BB-t~ajQ_jjcijjH$$X..../   nooo%%N+\+\R[+\+\+\cdeee	!#0f0f\e0f0f0fmn!o!o!o5((->-@-@(''''''OO,],]S\,],],]deOff	!$1g1g]f1g1g1gno!p!p4'',>,@,@'!II&W&WY&W&W&W]^I__	!&+a+aW`+a+a+agh!i!i4''O,=,='''''HH%V%VI%V%V%V]^H__	!#*`*`V_*`*`*`gh!i!i !!RSSS$H$-H[!)7H%&#$H!5!5" "-;"?E" "  .@H)*1T1&bbb[abbN 2'5'BH^$O%&*<'5'BH^$O!9O d&<&<^&<&<.YYYYr   c                 &     | j         j        |i |S )z
        This method forwards all its arguments to [`OwlViTImageProcessor.post_process`]. Please refer to the docstring
        of this method for more information.
        )r   post_processr   argsr   s      r   rX   zOwlViTProcessor.post_process   s    
 1t#0$A&AAAr   c                 &     | j         j        |i |S )z
        This method forwards all its arguments to [`OwlViTImageProcessor.post_process_object_detection`]. Please refer
        to the docstring of this method for more information.
        )r   post_process_object_detectionrY   s      r   r\   z-OwlViTProcessor.post_process_object_detection   s     
 Bt#A4R6RRRr   c                 &     | j         j        |i |S )z
        This method forwards all its arguments to [`OwlViTImageProcessor.post_process_one_shot_object_detection`].
        Please refer to the docstring of this method for more information.
        )r   #post_process_image_guided_detectionrY   s      r   r^   z3OwlViTProcessor.post_process_image_guided_detection   s!    
 Ht#GXQWXXXr   c                 &     | j         j        |i |S )z
        This method forwards all its arguments to CLIPTokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please
        refer to the docstring of this method for more information.
        )r   batch_decoderY   s      r   r`   zOwlViTProcessor.batch_decode   s    
 +t~*D;F;;;r   c                 &     | j         j        |i |S )z
        This method forwards all its arguments to CLIPTokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to
        the docstring of this method for more information.
        )r   decoderY   s      r   rb   zOwlViTProcessor.decode   s    
 %t~$d5f555r   c                 D    t          j        dt                     | j        S )Nzg`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.)r   r   r   image_processor_classr   s    r   feature_extractor_classz'OwlViTProcessor.feature_extractor_class   s'    u	
 	
 	
 ))r   c                 D    t          j        dt                     | j        S )Nz[`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.)r   r   r   r   re   s    r   r   z!OwlViTProcessor.feature_extractor   s'    i	
 	
 	
 ##r   )NN)NNNr   r    )__name__
__module____qualname____doc__
attributesrd   tokenizer_classr   rV   rX   r\   r^   r`   rb   propertyrf   r   __classcell__)r   s   @r   r   r      s       
 
 $[1J2<O5 5 5 5 5 5$mZ mZ mZ mZ^B B BS S SY Y Y< < <6 6 6 * * X* $ $ X$ $ $ $ $r   r   )rk   r   typingr   rI   r    processing_utilsr   tokenization_utils_baser   utilsr   r   r	   r   r%   r   r   <module>rt      s                . . . . . . 4 4 4 4 4 4 K K K K K K K K K KC$ C$ C$ C$ C$n C$ C$ C$ C$ C$r   