§
    ‚Štj¬3  ã                   ó,  — d dl mZ d dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ ddlmZ dd	lmZmZmZ dd
lmZmZmZ  ej        e¦  «        Z	 ddeeef         dededz  dedeeef         f
d„Z G d„ de
¦  «        Z G d„ de¦  «        ZddgZdS )é    )ÚOptionalN)Únn)Ú
functional)ÚDetrImageProcessor)ÚDetrImageProcessorPilé   )Úcenter_to_corners_format)ÚPILImageResamplingÚSizeDictÚ#get_image_size_for_max_height_width)Ú
TensorTypeÚloggingÚrequires_backendsé   Ú
image_sizeÚsizeÚmax_sizeÚmod_sizeÚreturnc                 ó$  — | \  }}d}|�lt          t          ||f¦  «        ¦  «        }t          t          ||f¦  «        ¦  «        }||z  |z  |k    r$||z  |z  }t          t	          |¦  «        ¦  «        }||k     r2|}	|�|�t          ||z  |z  ¦  «        }
ndt          ||z  |z  ¦  «        }
nN||k    r||k    s||k    r||k    r||}	}
n1|}
|�|�t          ||z  |z  ¦  «        }	nt          ||z  |z  ¦  «        }	|�|	|	|z  z
  }	|
|
|z  z
  }
|
|	fS )as  
    Computes the output image size given the input image size and the desired output size, while ensuring that both
    height and width are multiples of `mod_size`.

    This mirrors the YOLOS-specific behavior used in the torch/fast backends and is required so that all YOLOS
    image processing backends (PIL, torchvision, fast) produce identical output shapes.
    N)ÚfloatÚminÚmaxÚintÚround)r   r   r   r   ÚheightÚwidthÚraw_sizeÚmin_original_sizeÚmax_original_sizeÚowÚohs              úe/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/yolos/modular_yolos.pyÚ get_size_with_aspect_ratio_yolosr$      sf  € ð �M€FˆEØ€HØÐÝ!¥# v¨u oÑ"6Ô"6Ñ7Ô7ÐÝ!¥# v¨u oÑ"6Ô"6Ñ7Ô7ÐØÐ0Ñ0°4Ñ7¸(ÒBÐBØÐ"3Ñ3Ð6GÑGˆHÝ•u˜X‘”Ñ'Ô'ˆDàˆv‚~€~ØˆØÐ HÐ$8Ý�X Ñ&¨Ñ.Ñ/Ô/ˆBˆBå�T˜F‘] UÑ*Ñ+Ô+ˆBˆBØ
�EŠ/ˆ/˜f¨šn˜n°%¸6²/°/ÀeÈtÂmÀmØ˜ˆBˆˆàˆØÐ HÐ$8Ý�X Ñ%¨Ñ.Ñ/Ô/ˆBˆBå�T˜E‘\ FÑ*Ñ+Ô+ˆBàÐØ�2˜‘=Ñ!ˆØ�2˜‘=Ñ!ˆà�ˆ8€Oó    c            	       óŒ   ‡ — e Zd Z	 ddej        deded         dej        fˆ fd„Z	 dd	ed
e	e
e         z  fd„Zd„ Zd„ Zd„ Zˆ xZS )ÚYolosImageProcessorNÚimager   Úresamplez0PILImageResampling | tvF.InterpolationMode | intr   c                 ó´  •— |j         r0|j        r)t          |j        dd…         |j         |j        ¦  «        }ng|j        r0|j        r)t          |j        dd…         |j        |j        ¦  «        }n0|j        r|j        r|j        |j        f}nt          d|› d�¦  «        ‚ t          ¦   «         j        |ft          |d         |d         ¬¦  «        |dœ|¤Ž}|S )	a[  
        Resize the image to the given size. Size can be `min_size` (scalar) or `(height, width)` tuple. If size is an
        int, smaller edge of the image will be matched to this number.

        Args:
            image (`torch.Tensor`):
                Image to resize.
            size (`SizeDict`):
                Size of the image's `(height, width)` dimensions after resizing. Available options are:
                    - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`.
                        Do NOT keep the aspect ratio.
                    - `{"shortest_edge": int, "longest_edge": int}`: The image will be resized to a maximum size respecting
                        the aspect ratio and keeping the shortest edge less or equal to `shortest_edge` and the longest edge
                        less or equal to `longest_edge`.
                    - `{"max_height": int, "max_width": int}`: The image will be resized to the maximum size respecting the
                        aspect ratio and keeping the height less or equal to `max_height` and the width less or equal to
                        `max_width`.
            resample (`PILImageResampling | tvF.InterpolationMode | int`, *optional*, defaults to `PILImageResampling.BILINEAR`):
                Resampling filter to use if resizing the image.
        éþÿÿÿNú\Size must contain 'height' and 'width' keys or 'shortest_edge' and 'longest_edge' keys. Got ú.r   é   ©r   r   ©r   r)   )Úshortest_edgeÚlongest_edger$   ÚshapeÚ
max_heightÚ	max_widthr   r   r   Ú
ValueErrorÚsuperÚresizer   ©Úselfr(   r   r)   ÚkwargsÚnew_sizeÚ	__class__s         €r#   r8   zYolosImageProcessor.resize=   s  ø€ ð6 Ôð 	 $Ô"3ð 	õ 8¸¼ÀBÀCÀCÔ8HÈ$ÔJ\Ð^bÔ^oÑpÔpˆHˆHØŒ_ð 	 ¤ð 	Ý:¸5¼;ÀrÀsÀsÔ;KÈTÌ_Ð^bÔ^lÑmÔmˆHˆHØŒ[ð 	˜TœZð 	Øœ T¤ZÐ0ˆHˆHåØvÐosÐvÐvÐvñô ð ð •‘””Øð
Ý ¨°¬¸8ÀA¼;ÐGÑGÔGÐRZð
ð 
Ø^dð
ð 
ˆð ˆr%   ç      à?Ú	thresholdÚtarget_sizesc                 ó4  — |j         |j        }}|�/t          |¦  «        t          |¦  «        k    rt          d¦  «        ‚t          j                             |d¦  «        }|ddd…f                              d¦  «        \  }}t          |¦  «        }	|�®t          |t          ¦  «        r=t          j        d„ |D ¦   «         ¦  «        }
t          j        d„ |D ¦   «         ¦  «        }n|                     d¦  «        \  }
}t          j        ||
||
gd¬¦  «                             |	j        ¦  «        }|	|dd…ddd…f         z  }	g }t#          |||	¦  «        D ]C\  }}}|||k             }|||k             }|||k             }|                     |||d	œ¦  «         ŒD|S )
á‘  
        Converts the raw output of [`YolosForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y,
        bottom_right_x, bottom_right_y) format. Only supports PyTorch.

        Args:
            outputs ([`YolosObjectDetectionOutput`]):
                Raw outputs of the model.
            threshold (`float`, *optional*):
                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.
        Returns:
            `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image
            in the batch as predicted by the model.
        NúTMake sure that you pass in as many target sizes as the batch dimension of the logitséÿÿÿÿ.c                 ó   — g | ]
}|d          ‘ŒS ©r   © ©Ú.0Úis     r#   ú
<listcomp>zEYolosImageProcessor.post_process_object_detection.<locals>.<listcomp>Ž   ó   € Ð%AÐ%AÐ%A¨q a¨¤dÐ%AÐ%AÐ%Ar%   c                 ó   — g | ]
}|d          ‘ŒS ©r.   rG   rH   s     r#   rK   zEYolosImageProcessor.post_process_object_detection.<locals>.<listcomp>�   rL   r%   r.   ©Údim©ÚscoresÚlabelsÚboxes)ÚlogitsÚ
pred_boxesÚlenr6   r   r   Úsoftmaxr   r	   Ú
isinstanceÚlistÚtorchÚTensorÚunbindÚstackÚtoÚdeviceÚzipÚappend©r:   Úoutputsr?   r@   Ú
out_logitsÚout_bboxÚprobrR   rS   rT   Úimg_hÚimg_wÚ	scale_fctÚresultsÚsÚlÚbÚscoreÚlabelÚboxs                       r#   Úpost_process_object_detectionz1YolosImageProcessor.post_process_object_detectionj   sÀ  € ð&  'œ~¨wÔ/A�Hˆ
àÐ#Ý�:‰Œ¥# lÑ"3Ô"3Ò3Ð3Ý Øjñô ð õ Œ}×$Ò$ Z°Ñ4Ô4ˆØ˜c 3 B 3˜hœ×+Ò+¨BÑ/Ô/‰ˆ�õ )¨Ñ2Ô2ˆð Ð#Ý˜,­Ñ-Ô-ð 6ÝœÐ%AÐ%A°LÐ%AÑ%AÔ%AÑBÔB�ÝœÐ%AÐ%A°LÐ%AÑ%AÔ%AÑBÔB��à+×2Ò2°1Ñ5Ô5‘��uåœ U¨E°5¸%Ð$@ÀaÐHÑHÔH×KÒKÈEÌLÑYÔYˆIØ˜I a a a¨¨q¨q¨q jÔ1Ñ1ˆEàˆÝ˜6 6¨5Ñ1Ô1ð 	Mð 	M‰GˆAˆq�!Ø�a˜)’mÔ$ˆEØ�a˜)’mÔ$ˆEØ�A˜	’MÔ"ˆCØ�NŠN e°uÀsÐKÐKÑLÔLÐLÐLàˆr%   c                 ó    — t          d¦  «        ‚©NzHSegmentation post-processing is not implemented for Deformable DETR yet.©ÚNotImplementedError©r:   s    r#   Ú"post_process_instance_segmentationz6YolosImageProcessor.post_process_instance_segmentationŸ   ó   € Ý!Ð"lÑmÔmÐmr%   c                 ó    — t          d¦  «        ‚©NzQSemantic segmentation post-processing is not implemented for Deformable DETR yet.ru   rw   s    r#   Ú"post_process_semantic_segmentationz6YolosImageProcessor.post_process_semantic_segmentation¢   ó   € Ý!Ð"uÑvÔvÐvr%   c                 ó    — t          d¦  «        ‚©NzQPanoptic segmentation post-processing is not implemented for Deformable DETR yet.ru   rw   s    r#   Ú"post_process_panoptic_segmentationz6YolosImageProcessor.post_process_panoptic_segmentation¥   r}   r%   ©N©r>   N)Ú__name__Ú
__module__Ú__qualname__r[   r\   r   r   r8   r   r   rZ   Útuplerr   rx   r|   r€   Ú__classcell__©r=   s   @r#   r'   r'   <   sî   ø€ € € € € ð
 RVð	+ð +àŒ|ð+ð ð+ð ÐMÔNð	+ð 
Œð+ð +ð +ð +ð +ð +ð\ Y]ð3ð 3Ø"'ð3Ø=GÈ$ÈuÌ+Ñ=Uð3ð 3ð 3ð 3ðjnð nð nðwð wð wðwð wð wð wð wð wð wr%   r'   c            	       óŒ   ‡ — e Zd Z	 ddej        deded         dej        fˆ fd„Z	 dd	ed
e	e
e         z  fd„Zd„ Zd„ Zd„ Zˆ xZS )ÚYolosImageProcessorPilNr(   r   r)   r
   r   c                 óØ  •— |�|n| j         }|j        r7|j        r0t          |j        dd…         |j        |j        p|j        ¦  «        }ng|j        r0|j        r)t          |j        dd…         |j        |j        ¦  «        }n0|j        r|j	        r|j        |j	        f}nt          d|› d�¦  «        ‚ t          ¦   «         j        |ft          |d         |d         ¬¦  «        |dœ|¤Ž}|S )	a;  
        Resize the image to the given size. Size can be `min_size` (scalar) or `(height, width)` tuple. If size is an
        int, smaller edge of the image will be matched to this number.

        Args:
            image (`np.ndarray`):
                Image to resize.
            size (`SizeDict`):
                Size of the image's `(height, width)` dimensions after resizing. Available options are:
                    - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`.
                        Do NOT keep the aspect ratio.
                    - `{"shortest_edge": int, "longest_edge": int}`: The image will be resized to a maximum size respecting
                        the aspect ratio and keeping the shortest edge less or equal to `shortest_edge` and the longest edge
                        less or equal to `longest_edge`.
                    - `{"max_height": int, "max_width": int}`: The image will be resized to the maximum size respecting the
                        aspect ratio and keeping the height less or equal to `max_height` and the width less or equal to
                        `max_width`.
            resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`):
                Resampling filter to use if resizing the image.
        Nr+   r,   r-   r   r.   r/   r0   )r)   r1   r2   r$   r3   r4   r5   r   r   r   r6   r7   r8   r   r9   s         €r#   r8   zYolosImageProcessorPil.resizeª   s.  ø€ ð6  (Ð3�8�8¸¼ˆàÔð 	 $Ô"3ð 	õ 8Ø”˜B˜C˜CÔ ØÔ"ØÔ!Ð7 TÔ%7ñô ˆHˆHð
 Œ_ð 	 ¤ð 	Ý:¸5¼;ÀrÀsÀsÔ;KÈTÌ_Ð^bÔ^lÑmÔmˆHˆHØŒ[ð 	˜TœZð 	Øœ T¤ZÐ0ˆHˆHåØvÐosÐvÐvÐvñô ð ð •‘””Øð
å ¨!¤°H¸Q´KÐ@Ñ@Ô@Øð
ð 
ð ð	
ð 
ˆð ˆr%   r>   r?   r@   c                 óV  — t          | dg¦  «         |j        |j        }}|�/t          |¦  «        t          |¦  «        k    rt	          d¦  «        ‚t
          j                             |d¦  «        }|ddd…f                              d¦  «        \  }}t          |¦  «        }	|�®t          |t          ¦  «        r=t          j        d„ |D ¦   «         ¦  «        }
t          j        d„ |D ¦   «         ¦  «        }n|                     d¦  «        \  }
}t          j        ||
||
gd¬	¦  «                             |	j        ¦  «        }|	|dd…ddd…f         z  }	g }t%          |||	¦  «        D ]C\  }}}|||k             }|||k             }|||k             }|                     |||d
œ¦  «         ŒD|S )rB   r[   NrC   rD   .c                 ó   — g | ]
}|d          ‘ŒS rF   rG   rH   s     r#   rK   zHYolosImageProcessorPil.post_process_object_detection.<locals>.<listcomp>  rL   r%   c                 ó   — g | ]
}|d          ‘ŒS rN   rG   rH   s     r#   rK   zHYolosImageProcessorPil.post_process_object_detection.<locals>.<listcomp>  rL   r%   r.   rO   rQ   )r   rU   rV   rW   r6   r   r   rX   r   r	   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   s                       r#   rr   z4YolosImageProcessorPil.post_process_object_detectionà   sÒ  € õ& 	˜$  	Ñ*Ô*Ð*Ø&œ~¨wÔ/A�Hˆ
àÐ#Ý�:‰Œ¥# lÑ"3Ô"3Ò3Ð3Ý Øjñô ð õ Œ}×$Ò$ Z°Ñ4Ô4ˆØ˜c 3 B 3˜hœ×+Ò+¨BÑ/Ô/‰ˆ�õ )¨Ñ2Ô2ˆð Ð#Ý˜,­Ñ-Ô-ð 6ÝœÐ%AÐ%A°LÐ%AÑ%AÔ%AÑBÔB�ÝœÐ%AÐ%A°LÐ%AÑ%AÔ%AÑBÔB��à+×2Ò2°1Ñ5Ô5‘��uåœ U¨E°5¸%Ð$@ÀaÐHÑHÔH×KÒKÈEÌLÑYÔYˆIØ˜I a a a¨¨q¨q¨q jÔ1Ñ1ˆEàˆÝ˜6 6¨5Ñ1Ô1ð 	Mð 	M‰GˆAˆq�!Ø�a˜)’mÔ$ˆEØ�a˜)’mÔ$ˆEØ�A˜	’MÔ"ˆCØ�NŠN e°uÀsÐKÐKÑLÔLÐLÐLàˆr%   c                 ó    — t          d¦  «        ‚rt   ru   rw   s    r#   rx   z9YolosImageProcessorPil.post_process_instance_segmentation  ry   r%   c                 ó    — t          d¦  «        ‚r{   ru   rw   s    r#   r|   z9YolosImageProcessorPil.post_process_semantic_segmentation  r}   r%   c                 ó    — t          d¦  «        ‚r   ru   rw   s    r#   r€   z9YolosImageProcessorPil.post_process_panoptic_segmentation  r}   r%   r�   r‚   )rƒ   r„   r…   ÚnpÚndarrayr   r   r8   r   r   rZ   r†   rr   rx   r|   r€   r‡   rˆ   s   @r#   rŠ   rŠ   ©   sì   ø€ € € € € ð
 48ð	4ð 4àŒzð4ð ð4ð Ð/Ô0ð	4ð 
Œð4ð 4ð 4ð 4ð 4ð 4ðn Y]ð4ð 4Ø"'ð4Ø=GÈ$ÈuÌ+Ñ=Uð4ð 4ð 4ð 4ðlnð nð nðwð wð wðwð wð wð wð wð wð wr%   rŠ   )Nr   ) Útypingr   Únumpyr’   r[   r   Útorchvision.transforms.v2r   ÚtvFÚ.transformers.models.detr.image_processing_detrr   Ú2transformers.models.detr.image_processing_pil_detrr   Úimage_transformsr	   Úimage_utilsr
   r   r   Úutilsr   r   r   Ú
get_loggerrƒ   Úloggerr†   r   r$   r'   rŠ   Ú__all__rG   r%   r#   ú<module>r       s¼  ðØ Ð Ð Ð Ð Ð à Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7à MÐ MÐ MÐ MÐ MÐ MØ TÐ TÐ TÐ TÐ TÐ Tà 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;ð 
ˆÔ	˜HÑ	%Ô	%€ð Z\ð&ð &Ø�c˜3�h”ð&Ø'*ð&Ø69¸D±jð&ØSVð&à
ˆ3�ˆ8„_ð&ð &ð &ð &ðRjwð jwð jwð jwð jwÐ,ñ jwô jwð jwðZtwð twð twð twð twÐ2ñ twô twð twðn !Ð":Ð
;€€€r%   