§
    ‚ŠtjÇ(  ã                   óø   — 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mZ ddlmZmZ dd	lmZmZmZ erd
dlmZmZ  G d„ de
d¬¦  «        Z G d„ ded¬¦  «        Ze G d„ de¦  «        ¦   «         ZdgZdS )z&
Image/Text processor class for OWLv2
é    )ÚTYPE_CHECKINGNé   )ÚBatchFeature)Ú
ImageInput)ÚImagesKwargsÚProcessingKwargsÚProcessorMixinÚUnpack)ÚPreTokenizedInputÚ	TextInput)Ú
TensorTypeÚauto_docstringÚis_torch_availableé   )Ú%Owlv2ImageGuidedObjectDetectionOutputÚOwlv2ObjectDetectionOutputc                   ó$   — e Zd ZU dZedz  ed<   dS )ÚOwlv2ImagesKwargsaa  
    query_images (`ImageInput`, *optional*):
        Query images to use for image-guided object detection. When provided, these images serve as visual queries
        to find similar objects in the main `images`. The query images override any text prompts, and the model
        performs image-to-image matching instead of text-to-image matching.
    NÚquery_images)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__© ó    úh/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/owlv2/processing_owlv2.pyr   r   &   s/   € € € € € € ðð ð ˜tÑ#Ð#Ð#Ñ#Ð#Ð#r   r   F)Útotalc                   ó,   — e Zd ZU eed<   ddiddidœZdS )ÚOwlv2ProcessorKwargsÚimages_kwargsÚpaddingÚ
max_lengthÚreturn_tensorsÚnp)Útext_kwargsÚcommon_kwargsN)r   r   r   r   r   Ú	_defaultsr   r   r   r    r    1   s@   € € € € € € Ø$Ð$Ð$Ñ$ð �|ð
ð ˜dð
ð	ð €I€I€Ir   r    c                   ó  ‡ — e Zd Zˆ fd„Ze	 	 ddedz  deez  ee         z  ee         z  de	e
         defd„¦   «         Z	 	 	 dd	d
dedeee         z  dz  deee                  dz  fd„Z	 	 	 dd	ddededeee         z  dz  fd„Zˆ xZS )ÚOwlv2Processorc                 óL   •— t          ¦   «                              ||¦  «         d S ©N)ÚsuperÚ__init__)ÚselfÚimage_processorÚ	tokenizerÚkwargsÚ	__class__s       €r   r.   zOwlv2Processor.__init__?   s#   ø€ Ý‰Œ×Ò˜¨)Ñ4Ô4Ð4Ð4Ð4r   NÚimagesÚtextr2   Úreturnc                 óþ  —  | j         t          fd| j        j        i|¤Ž}|d                              dd¦  «        }|d         d         }|€|€|€t          d¦  «        ‚i }|��Ët          |t          ¦  «        s0t          |t          ¦  «        r1t          |d         t          ¦  «        s | j        |fi |d         ¤Žg}nµt          |t          ¦  «        r‘t          |d         t          ¦  «        rvg }t          d	„ |D ¦   «         ¦  «        }	|D ]W}
t          |
¦  «        |	k    r|
d
g|	t          |
¦  «        z
  z  z   }
 | j        |
fi |d         ¤Ž}|                     |¦  «         ŒXnt          d¦  «        ‚|dk    rAt          j        d„ |D ¦   «         d¬¦  «        }t          j        d„ |D ¦   «         d¬¦  «        }nj|dk    rUt          ¦   «         rGddl}|                     d„ |D ¦   «         d¬¦  «        }|                     d„ |D ¦   «         d¬¦  «        }nt          d¦  «        ‚||d<   ||d<   |� | j        |fi |d         ¤Žj        }d|i}|� | j        |fi |d         ¤Ž}|j        |d<   t)          ||¬¦  «        S )aÏ  
        Returns:
            [`BatchFeature`]: A [`BatchFeature`] 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`.
            - **query_pixel_values** -- Pixel values of the query images to be fed to a model. Returned when `query_images` is not `None`.
        Útokenizer_init_kwargsr!   r   Nr&   r$   zXYou have to specify at least one text or query image or image. All three cannot be none.r   c              3   ó4   K  — | ]}t          |¦  «        V — Œd S r,   )Úlen)Ú.0Útext_singles     r   ú	<genexpr>z*Owlv2Processor.__call__.<locals>.<genexpr>j   s*   è è € Ð%OÐ%O¸;¥c¨+Ñ&6Ô&6Ð%OÐ%OÐ%OÐ%OÐ%OÐ%Or   ú 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   ú
<listcomp>z+Owlv2Processor.__call__.<locals>.<listcomp>w   s   € Ð+\Ð+\Ð+\Àh¨H°[Ô,AÐ+\Ð+\Ð+\r   )Úaxisc                 ó   — g | ]
}|d          ‘ŒS ©Úattention_maskr   rB   s     r   rD   z+Owlv2Processor.__call__.<locals>.<listcomp>x   s   € Ð0fÐ0fÐ0fÐPX°Ð:JÔ1KÐ0fÐ0fÐ0fr   Úptc                 ó   — g | ]
}|d          ‘ŒS r@   r   rB   s     r   rD   z+Owlv2Processor.__call__.<locals>.<listcomp>|   s   € Ð&WÐ&WÐ&WÀ x°Ô'<Ð&WÐ&WÐ&Wr   )Údimc                 ó   — g | ]
}|d          ‘ŒS rG   r   rB   s     r   rD   z+Owlv2Processor.__call__.<locals>.<listcomp>}   s   € Ð+aÐ+aÐ+aÈ8¨HÐ5EÔ,FÐ+aÐ+aÐ+ar   z/Target return tensor type could not be returnedrA   rH   Úquery_pixel_valuesÚpixel_values)ÚdataÚtensor_type)Ú_merge_kwargsr    r1   Úinit_kwargsÚpopÚ
ValueErrorÚ
isinstanceÚstrÚlistÚmaxr:   ÚappendÚ	TypeErrorr%   Úconcatenater   ÚtorchÚcatr0   rN   r   )r/   r4   r5   r2   Úoutput_kwargsr   r$   rO   Ú	encodingsÚmax_num_queriesr<   rC   rA   rH   r\   rM   Úimage_featuress                    r   Ú__call__zOwlv2Processor.__call__B   s1  € ð$ +˜Ô*Ý ð
ð 
à"&¤.Ô"<ð
ð ð
ð 
ˆð
 % _Ô5×9Ò9¸.È$ÑOÔOˆØ& }Ô5Ð6FÔGˆàˆ<˜LÐ0°V°^ÝØjñô ð ð ˆØÑÝ˜$¥Ñ$Ô$ð p­°D½$Ñ)?Ô)?ð pÍ
ÐSWÐXYÔSZÕ\`ÑHaÔHað pØ+˜Tœ^¨DÐQÐQ°MÀ-Ô4PÐQÐQÐR�	�	å˜D¥$Ñ'Ô'ð p­J°t¸A´wÅÑ,EÔ,Eð pØ�	õ #&Ð%OÐ%OÈ$Ð%OÑ%OÔ%OÑ"OÔ"O�ð $(ð /ð /�KÝ˜;Ñ'Ô'¨?Ò:Ð:Ø&1°S°E¸_ÍsÐS^ÑO_ÔO_Ñ=_Ñ4`Ñ&`˜à-˜tœ~¨kÐZÐZ¸]È=Ô=YÐZÐZ�HØ×$Ò$ XÑ.Ô.Ð.Ð.ð/õ  Ð nÑoÔoÐoà Ò%Ð%ÝœNÐ+\Ð+\ÐR[Ð+\Ñ+\Ô+\ÐcdÐeÑeÔe�	Ý!#¤Ð0fÐ0fÐ\eÐ0fÑ0fÔ0fÐmnÐ!oÑ!oÔ!o��Ø 4Ò'Ð'Õ,>Ñ,@Ô,@Ð'Ø���à!ŸIšIÐ&WÐ&WÈYÐ&WÑ&WÔ&WÐ]^˜IÑ_Ô_�	Ø!&§¢Ð+aÐ+aÐW`Ð+aÑ+aÔ+aÐgh Ñ!iÔ!i��å Ð!RÑSÔSÐSà )ˆD�ÑØ%3ˆDÐ!Ñ"àÐ#Ø!5 Ô!5°lÐ!eÐ!eÀmÐTcÔFdÐ!eÐ!eÔ!rÐà(Ð*<Ð=ˆDàÐØ1˜TÔ1°&Ð[Ð[¸MÈ/Ô<ZÐ[Ð[ˆNØ#1Ô#>ˆD�Ñ å °>ÐBÑBÔBÐBr   çš™™™™™¹?Úoutputsr   Ú	thresholdÚtarget_sizesÚtext_labelsc                 ó  ‡— | j                              |||¬¦  «        }|�/t          |¦  «        t          |¦  «        k    rt          d¦  «        ‚|�0t	          ||¦  «        D ]\  }Šˆfd„|d         D ¦   «         }||d<   Œn
|D ]}d|d<   Œ|S )a€  
        Converts the raw output of [`Owlv2ForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y,
        bottom_right_x, bottom_right_y) format.

        Args:
            outputs ([`Owlv2ObjectDetectionOutput`]):
                Raw outputs of the model.
            threshold (`float`, *optional*, defaults to 0.1):
                Score threshold to keep object detection predictions.
            target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*):
                Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size
                `(height, width)` of each image in the batch. If unset, predictions will not be resized.
            text_labels (`list[list[str]]`, *optional*):
                List of lists of text labels for each image in the batch. If unset, "text_labels" in output will be
                set to `None`.

        Returns:
            `list[Dict]`: A list of dictionaries, each dictionary containing the following keys:
            - "scores": The confidence scores for each predicted box on the image.
            - "labels": Indexes of the classes predicted by the model on the image.
            - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format.
            - "text_labels": The text labels for each predicted bounding box on the image.
        )rd   re   rf   NzAMake sure that you pass in as many lists of text labels as imagesc                 ó    •— g | ]
}‰|         ‘ŒS r   r   )r;   ÚiÚimage_text_labelss     €r   rD   zIOwlv2Processor.post_process_grounded_object_detection.<locals>.<listcomp>¸   s   ø€ Ð%[Ð%[Ð%[¸qÐ&7¸Ô&:Ð%[Ð%[Ð%[r   Úlabelsrg   )r0   Úpost_process_object_detectionr:   rT   Úzip)	r/   rd   re   rf   rg   ÚoutputÚimage_outputÚobject_text_labelsrk   s	           @r   Ú&post_process_grounded_object_detectionz5Owlv2Processor.post_process_grounded_object_detection�   sØ   ø€ ð< Ô%×CÒCØ y¸|ð Dñ 
ô 
ˆð Ð"¥s¨;Ñ'7Ô'7½3¸v¹;¼;Ò'FÐ'FÝÐ`ÑaÔaÐað Ð"Ý36°v¸{Ñ3KÔ3Kð Að AÑ/�Ð/Ø%[Ð%[Ð%[Ð%[ÀLÐQYÔDZÐ%[Ñ%[Ô%[Ð"Ø.@�˜]Ñ+Ð+ðAð !'ð 3ð 3�Ø.2�˜]Ñ+Ð+àˆr   ç        ç333333Ó?r   Únms_thresholdc                 ó>   — | j                              ||||¬¦  «        S )aä  
        Converts the output of [`Owlv2ForObjectDetection.image_guided_detection`] into the format expected by the COCO
        api.

        Args:
            outputs ([`Owlv2ImageGuidedObjectDetectionOutput`]):
                Raw outputs of the model.
            threshold (`float`, *optional*, defaults to 0.0):
                Minimum confidence threshold to use to filter out predicted boxes.
            nms_threshold (`float`, *optional*, defaults to 0.3):
                IoU threshold for non-maximum suppression of overlapping boxes.
            target_sizes (`torch.Tensor`, *optional*):
                Tensor of shape (batch_size, 2) where each entry is the (height, width) of the corresponding image in
                the batch. If set, predicted normalized bounding boxes are rescaled to the target sizes. If left to
                None, predictions will not be unnormalized.

        Returns:
            `list[Dict]`: A list of dictionaries, each dictionary containing the following keys:
            - "scores": The confidence scores for each predicted box on the image.
            - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format.
            - "labels": Set to `None`.
        )rd   re   ru   rf   )r0   Ú#post_process_image_guided_detection)r/   rd   re   ru   rf   s        r   rw   z2Owlv2Processor.post_process_image_guided_detectionÁ   s/   € ð: Ô#×GÒGØ yÀÐ\hð Hñ 
ô 
ð 	
r   )NN)rc   NN)rs   rt   N)r   r   r   r.   r   r   r   r   rW   r
   r    r   rb   Úfloatr   ÚtuplerV   rr   rw   Ú__classcell__)r3   s   @r   r*   r*   =   s�  ø€ € € € € ð5ð 5ð 5ð 5ð 5ð ð %)ØZ^ðICð ICà˜TÑ!ðICð Ð+Ñ+¨d°9¬oÑ=ÀÐEVÔ@WÑWðICð Ð-Ô.ð	ICð
 
ðICð ICð ICñ „^ðICð^ Ø8<Ø.2ð.ð .à-ð.ð ð.ð ! 4¨¤;Ñ.°Ñ5ð	.ð
 ˜$˜sœ)”_ tÑ+ð.ð .ð .ð .ðh Ø"Ø8<ð
ð 
à8ð
ð ð
ð ð	
ð
 ! 4¨¤;Ñ.°Ñ5ð
ð 
ð 
ð 
ð 
ð 
ð 
ð 
r   r*   )r   Útypingr   Únumpyr%   Úimage_processing_utilsr   Úimage_utilsr   Úprocessing_utilsr   r   r	   r
   Útokenization_utils_baser   r   Úutilsr   r   r   Úmodeling_owlv2r   r   r   r    r*   Ú__all__r   r   r   ú<module>r„      s›  ððð ð !Ð  Ð  Ð  Ð  Ð  à Ð Ð Ð à 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø %Ð %Ð %Ð %Ð %Ð %ðð ð ð ð ð ð ð ð ð ð ð ð DÐ CÐ CÐ CÐ CÐ CÐ CÐ CØ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ Cð ð bØaÐaÐaÐaÐaÐaÐaÐað$ð $ð $ð $ð $˜¨Eð $ñ $ô $ð $ð	ð 	ð 	ð 	ð 	Ð+°5ð 	ñ 	ô 	ð 	ð ðb
ð b
ð b
ð b
ð b
�^ñ b
ô b
ñ „ðb
ðJ Ð
€€€r   