
    sg>'                     ^    d 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y)
z&
Image/Text processor class for OWLv2
    )ListN   )ProcessorMixin)BatchEncoding)is_flax_availableis_tf_availableis_torch_availablec                   R     e Zd ZdZddgZdZdZ fdZddZd Z	d	 Z
d
 Zd Z xZS )Owlv2Processora  
    Constructs an Owlv2 processor which wraps [`Owlv2ImageProcessor`] 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 ([`Owlv2ImageProcessor`]):
            The image processor is a required input.
        tokenizer ([`CLIPTokenizer`, `CLIPTokenizerFast`]):
            The tokenizer is a required input.
    image_processor	tokenizerOwlv2ImageProcessor)CLIPTokenizerCLIPTokenizerFastc                 &    t         |   ||       y )N)super__init__)selfr   r   kwargs	__class__s       ]/var/www/html/venv/lib/python3.12/site-packages/transformers/models/owlv2/processing_owlv2.pyr   zOwlv2Processor.__init__-   s    )4    c                    |||t        d      |{t        |t              s#t        |t              r+t        |d   t              s | j                  |f||d|g}nt        |t              rt        |d   t              rvg }t        |D cg c]  }t        |       c}      }	|D ]L  }t        |      |	k7  r|dg|	t        |      z
  z  z   } | j                  |f||d|}
|j                  |
       N nt        d      |dk(  rRt        j                  |D 
cg c]  }
|
d   	 c}
d	      }t        j                  |D 
cg c]  }
|
d
   	 c}
d	      }n!|dk(  rYt               rOddlm} |j                  |D 
cg c]  }
|
d   	 c}
d	      }|j                  |D 
cg c]  }
|
d
   	 c}
d	      }n|dk(  rWt               rMddl}|j!                  |D 
cg c]  }
|
d   	 c}
d      }|j!                  |D 
cg c]  }
|
d
   	 c}
d      }ng|dk(  rWt#               rMddl}|j'                  |D 
cg c]  }
|
d   	 c}
d	      }|j'                  |D 
cg c]  }
|
d
   	 c}
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 c c}w c c}
w c c}
w c c}
w c c}
w c c}
w c c}
w c c}
w c c}
w )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_tensors zLInput text should be a string, a list of strings or a nested list of stringsnp	input_ids)axisattention_maskjaxpt)dimtfz/Target return tensor type could not be returnedr   query_pixel_valuespixel_values)datatensor_type )
ValueError
isinstancestrr   r   maxlenappend	TypeErrorr   concatenater   	jax.numpynumpyr	   torchcatr   
tensorflowstackr   r   r&   dict)r   textimagesquery_imagesr   r   r   	encodingstmax_num_queriesencodingr   r    jnpr4   r$   r%   image_featuress                     r   __call__zOwlv2Processor.__call__1   s   H <L0V^j  $$D$)?
SWXYSZ\`Ha+T^^Dk'R`kdjkl	D$'JtAw,E	 #&t&<!s1v&<"=  /A1v03q6)A BB-t~~ajQ_jcijH$$X./   noo%NNR[+\hH[,A+\cde	!#\e0fPX:J1K0fmn!o5(->-@'OOS\,]xXk-B,]deOf	!$]f1gQY(;K2L1gno!p4',>,@!IIY&Wx'<&W]^I_	!&W`+a8H5E,F+agh!i4'O,='HHI%Vh{&;%V]^H_	!#V_*`(84D+E*`gh!i !!RSS$H$-H[!)7H%&#$H!5!5!5"-;"?E"l  .@H)*1T11&bb[abN 2'5'B'BH^$O%&*<'5'B'BH^$O!9O d&<^&<.YYy '= ,]0f
 -^1g
 'X+a
 &W*`s6   LL"L'?L,#L1L6?L;7M M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_detectionr   argsr   s      r   rD   z,Owlv2Processor.post_process_object_detection   s#    
 Bt##AA4R6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_detectionrE   s      r   rH   z2Owlv2Processor.post_process_image_guided_detection   s$    
 Ht##GGXQW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_decoderE   s      r   rJ   zOwlv2Processor.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   decoderE   s      r   rL   zOwlv2Processor.decode   s     
 %t~~$$d5f55r   )NNN
max_lengthr   )__name__
__module____qualname____doc__
attributesimage_processor_classtokenizer_classr   rB   rD   rH   rJ   rL   __classcell__)r   s   @r   r   r      sB    
 $[1J1<O5mZ`SY<6r   r   )rQ   typingr   r3   r   processing_utilsr   tokenization_utils_baser   utilsr   r   r	   r   r)   r   r   <module>rZ      s,      . 4 K Kb6^ b6r   