
    Z jx                     ~   S SK r S SKJrJr  S SKrS SKJr  S SKJr  S SKJ	r
  SSKJr  SSKJrJr  SS	KJrJrJr  SS
KJrJrJrJrJrJrJrJrJrJrJrJ r   SSK!J"r"J#r#  SSK$J%r%J&r&   " S S\"SS9r'\RP                  \RR                  4r*S\+S\+S\RX                  S\RZ                  4S jr.  S(S\/S\\0-  S-  4S jjr1S\RZ                  S\RZ                  4S jr2S r3  S)S\RZ                  S\4S\0\ Rj                  -  S\/S\\0-  S\44S  jjr6 S*S!\7\+\+4   S"\+S#\+S-  S$\+S\7\+\+4   4
S% jjr8\& " S& S'\5      5       r9S'/r:g)+    N)AnyOptional)nn)
read_image)
functional   )TorchvisionBackend)BatchFeatureget_size_dict)center_to_corners_formatcorners_to_center_formatsafe_squeeze)IMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STDAnnotationFormatAnnotationTypeChannelDimension
ImageInputPILImageResamplingSizeDictget_image_size#get_image_size_for_max_height_widthget_max_height_widthvalidate_annotations)ImagesKwargsUnpack)
TensorTypeauto_docstringc                   4    \ rS rSr% Sr\\-  \S'   \\S'   Sr	g)YolosImageProcessorKwargs$   a  
format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`):
    Data format of the annotations. One of "coco_detection" or "coco_panoptic".
do_convert_annotations (`bool`, *optional*, defaults to `True`):
    Controls whether to convert the annotations to the format expected by the YOLOS model. Converts the
    bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`.
    Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method.
formatdo_convert_annotations N)
__name__
__module____qualname____firstlineno____doc__strr   __annotations__bool__static_attributes__r$       ځ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/yolos/image_processing_yolos.pyr    r    $   s     """  r.   r    F)totalheightwidthdevicereturnc                     SSK Jn  / nU  H  nUR                  XaU5      nUR	                  U5      n[        UR                  5      S:  a  US   n[        R                  " U[        R                  US9n[        R                  " USS9nUR                  U5        M     U(       a  [        R                  " USS9nU$ [        R                  " SX4[        R                  US9nU$ ! [         a    [        S5      ef = f)	a  
Convert a COCO polygon annotation to a mask.

Args:
    segmentations (`list[list[float]]`):
        List of polygons, each polygon represented by a list of x-y coordinates.
    height (`int`):
        Height of the mask.
    width (`int`):
        Width of the mask.
r   )maskz1Pycocotools is not installed in your environment.r   ).Ndtyper3      )axis)pycocotoolsr6   ImportErrorfrPyObjectsdecodelenshapetorch	as_tensoruint8anyappendstackzeros)	segmentationsr1   r2   r3   	coco_maskmaskspolygonsrlesr6   s	            r/   convert_coco_poly_to_maskrM   6   s    O1 E!$$Xu=%tzz?Q	?Dt5;;vFyyA&T " E* L Q.ekk&QL#  OMNNOs   C$ $C:return_segmentation_masksinput_data_formatc                 $   U R                  5       SS u  pEUS   n[        R                  " U/[        R                  U R                  S9nUS   n/ n/ n	/ n
/ nU Hl  nSU;  d  US   S:X  d  M  UR                  US   5        U	R                  US	   5        U
R                  US
   5        SU;   d  MX  UR                  US   5        Mn     [        R                  " U[        R                  U R                  S9n[        R                  " U	[        R                  U R                  S9n	[        R                  " U[        R                  U R                  S9n[        R                  " U
[        R                  U R                  S9R                  SS5      n
U
SS2SS24==   U
SS2SS24   -  ss'   U
SS2SSS24   R                  SUS9U
SS2SSS24'   U
SS2SSS24   R                  SUS9U
SS2SSS24'   U
SS2S4   U
SS2S4   :  U
SS2S4   U
SS2S4   :  -  nUX   X   X   X   [        R                  " [        U5      [        U5      /[        R                  U R                  S9S.nU(       a_  [        R                  " U[        R                  U R                  S9nX   nUR                  S   nU(       a  UR                  S5      OUnXS'   U(       a1  U Vs/ s H  oS   PM	     nn[        UXEU R                  S9nUU   US'   U$ s  snf )zF
Convert the target in COCO format into the format expected by YOLOS.
Nimage_idr7   annotationsiscrowdr   category_idareabbox	keypoints   r9   )minmax   r   )rR   class_labelsboxesrV   rT   	orig_size)rY   r   segmentationr3   rJ   )sizerA   rB   int64r3   rE   float32
zeros_likereshapeclipintr@   rM   )imagetargetrN   rO   image_heightimage_widthrR   rS   classesrV   r_   rX   objrT   keep
new_targetnum_keypointssegmentation_masksrJ   s                      r/   !prepare_coco_detection_annotationrt   Y   s    !&

RS 1Lj!HzU\\RH 'KGDEIC3y>Q#6NN3}-.KKF$LLV%c!  [!12  oogU[[NG??4u}}U\\JDwekk%,,OGOOEu||LTTUWYZ[E	!QR%LE!RaR%L L1add7^((QK(@E!QTT'N1add7^((QL(AE!QTT'N!Q$K%1+%%1+ad*CDD 
=__c,&7[9I%JRWR]R]fkfrfrsJ OOIU]]5<<X	O	!*2?I%%g.Y	"+; =HI[c.1[I)*<l`e`l`lm#Dk
7	 Js   LrJ   c           	         U R                  5       S:X  a  [        R                  " SU R                  S9$ U R                  SS u  p[        R
                  " SU[        R                  U R                  S9n[        R
                  " SU[        R                  U R                  S9n[        R                  " X4SS9u  p4U [        R                  " US5      -  nUR                  UR                  S   S	5      R                  S	5      S   n[        R                  " XR                  S5      [        R                  " S
U R                  S95      R                  U R                  S   S	5      R                  S	5      S   nU [        R                  " US5      -  nUR                  UR                  S   S	5      R                  S	5      S   n	[        R                  " XR                  S5      [        R                  " S
U R                  S95      R                  U R                  S   S	5      R                  S	5      S   n
[        R                  " XzXi/S5      $ )z
Compute the bounding boxes around the provided panoptic segmentation masks.

Args:
    masks: masks in format `[number_masks, height, width]` where N is the number of masks

Returns:
    boxes: bounding boxes in format `[number_masks, 4]` in xyxy format
r   )r   rZ   rb   rQ   Nr7   ij)indexingrY   g    חAr]   )numelrA   rG   r3   r@   arangere   meshgrid	unsqueezeviewr\   wheretensorr[   rF   )rJ   hwyxx_maskx_maxx_miny_masky_maxy_mins              r/   masks_to_boxesr      s    {{}{{6%,,77;;rsDAQu||DAQu||DA>>!.DAU__Q**FKKQ,004Q7EE;;q>5<<ELL+QRWWX]XcXcdeXfhjkooprstuv 
 U__Q**FKKQ,004Q7EE;;q>5<<ELL+QRWWX]XcXcdeXfhjkooprstuv 
 ;;e3Q77r.   c                    [        U [        R                  5      (       a  [        U R                  5      S:X  ai  U R
                  [        R                  :X  a  U R                  [        R                  5      n U SS2SS2S4   SU SS2SS2S4   -  -   SU SS2SS2S4   -  -   $ [        U S   SU S   -  -   SU S   -  -   5      $ )z"
Converts RGB color to unique ID.
r   Nr      r]   i   r9   )

isinstancerA   Tensorr?   r@   r8   rC   toint32ri   )colors    r/   	rgb_to_idr      s     %&&3u{{+;q+@;;%++%HHU[[)EQ1W~eAq!Gn 44y5Aq>7QQQuQx#a.(9uQx+??@@r.   rj   rk   
masks_pathreturn_masksc                    [        XS9u  pV[        R                  " U5      US   -  n0 n[        R                  " SU;   a  US   OUS   /[        R
                  U R                  S9US'   [        R                  " XV/[        R
                  U R                  S9US'   [        R                  " XV/[        R
                  U R                  S9US'   SU;   Ga  [        U5      R                  S	S
S5      R                  [        R                  U R                  S9n	[        U	5      n	[        R                  " US    V
s/ s H  oS   PM	     sn
U R                  S9nXSS2SS4   :H  n	U	R                  [        R                  5      n	U(       a  XS'   [        U	5      US'   [        R                  " US    V
s/ s H  oS   PM	     sn
[        R
                  U R                  S9US'   [        R                  " US    V
s/ s H  oS   PM	     sn
[        R
                  U R                  S9US'   [        R                  " US    V
s/ s H  oS   PM	     sn
[        R                  U R                  S9US'   U$ s  sn
f s  sn
f s  sn
f s  sn
f )z/
Prepare a coco panoptic annotation for YOLOS.
)channel_dim	file_namerR   idr7   rc   r`   segments_infor]   r9   r   rb   NrJ   r_   rU   r^   rT   rV   )r   pathlibPathrA   rB   rd   r3   r   permuter   r   r   r,   r   re   )rj   rk   r   r   rO   rl   rm   annotation_pathrq   rJ   segment_infoidss               r/    prepare_coco_panoptic_annotationr      sG    !/u TLll:.1DDOJ"__)V3
	FekkbgbnbnJz ,)DEKK`e`l`lmJv#oo|.IQVQ\Q\ejeqeqrJ{& ?+33Aq!<??ekkZ_ZfZf?g% oof_F]^F]lD1F]^glgsgstQd]++$"'w,U3
7%*__=CO=TU=T\-(=TU++<<&

>"
 !&9?9PQ9P)$9PQ++<<!

9
 #__6<_6MN6Ml&!6MN--<<

6 - _ V
 R
 Os   9I22I79I< J
image_sizerc   max_sizemod_sizec                    U u  pESnUbP  [        [        XE45      5      n[        [        XE45      5      nX-  U-  U:  a  X'-  U-  n[        [	        U5      5      nXT:  a*  Un	Ub  Ub  [        Xd-  U-  5      n
OQ[        X-  U-  5      n
O@XE::  a  XA:X  d
  XT::  a  XQ:X  a  XEpO)Un
Ub  Ub  [        Xe-  U-  5      n	O[        X-  U-  5      n	Ub  XU-  -
  n	XU-  -
  n
X4$ )a_  
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)floatr[   r\   ri   round)r   rc   r   r   r1   r2   raw_sizemin_original_sizemax_original_sizeowohs              r/    get_size_with_aspect_ratio_yolosr      s    MFH!#vo"67!#vo"67047(B36GGHuX'D~H$8X&./BT]U*+B
/fn%/emBH$8X%./BT\F*+B=!=!8Or.   c            $         ^  \ rS rSr\r\R                  r\	r
\r\R                  rSrSrSrSrSSS.rSrSS/rS	\\   S
S4U 4S jjr    S:S\R4                  S\S\S-  S\S-  S\\R>                  -  S-  S\\ -  S-  S
\4S jjr! S;S\R4                  S\"S\#S   S
\R4                  4U 4S jjjr$S\RJ                  4S\\\&4   S\'\(\(4   S\'\(\(4   S\)S\#S   4
U 4S jjjr*S\S\'\(\(4   S
\4S jr+S\S \'\(\(4   S!\'\(\(4   S
\4S" jr,   S<S\R4                  S#\'\(\(4   S\\\&4   S-  S$\S%\(4
S& jjr-\.   S=S'\/S(\0\1\0   -  S-  S\S-  S\\R>                  -  S-  S	\\   S
\24U 4S) jjj5       r3S'\1S*   S(\0\1\0   -  S-  S\S\\R>                  -  S-  S+\S\"SS,S-\S.\)S/\S0\S1\)\1\)   -  S-  S2\)\1\)   -  S-  S3\S4\"S-  S\\-  S-  S5\\4-  S-  S
\24$S6 jr5 S>S\)S7\4\1\'   -  4S8 jjr6S9r7U =r8$ )?YolosImageProcessori#  T   5  shortest_edgelongest_edgeFpixel_values
pixel_maskkwargsr4   Nc                   > UR                  SUR                  SU R                  5      5        UR                  SS 5      nUc  S OUR                  SS5      nUb  UOSSS.n[        X#SS	9US'   UR	                  S
5      nUR	                  S5      nUc$  [        U S
S 5      c  Ub  UOU R                  U l        [        TU ]$  " S0 UD6  g )Ndo_padpad_and_return_pixel_maskrc   r   r   r   r   F)r   default_to_squarer#   do_normalizer$   )

setdefaultpopr   r   getgetattrr   r#   super__init__)selfr   rc   r   r#   r   	__class__s         r/   r   YolosImageProcessor.__init__2  s    (FJJ/JDKK$XYzz&$'<4VZZ
D-I'tsTX-Y&tRWXv "(,D!Ezz.1!)gd<TVZ.[.c:F:R,X\XiXiD'"6"r.   rj   rk   r"   rN   r   rO   c                     Ub  UOU R                   nU[        R                  :X  a  Uc  SOUn[        XXFS9nU$ U[        R                  :X  a  Uc  SOUn[        UUUUUS9nU$ [        SU S35      e)z5
Prepare an annotation for feeding into YOLOS model.
F)rO   T)r   r   rO   zFormat z is not supported.)r"   r   COCO_DETECTIONrt   COCO_PANOPTICr   
ValueError)r   rj   rk   r"   rN   r   rO   s          r/   prepare_annotation&YolosImageProcessor.prepare_annotationC  s     "-4;;%4441J1RXq%68F  '5550I0QWp%5%6"3F  wvh.@ABBr.   rc   resamplez0PILImageResampling | tvF.InterpolationMode | intc                 $  > UR                   (       a@  UR                  (       a/  [        UR                  SS UR                   UR                  5      nOUR                  (       a@  UR
                  (       a/  [        UR                  SS UR                  UR
                  5      nOJUR                  (       a*  UR                  (       a  UR                  UR                  4nO[        SU S35      e[        TU ],  " U4[        US   US   S9US.UD6nU$ )	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.
rQ   Nz\Size must contain 'height' and 'width' keys or 'shortest_edge' and 'longest_edge' keys. Got .r   r]   r1   r2   rc   r   )r   r   r   r@   
max_height	max_widthr   r1   r2   r   r   resizer   )r   rj   rc   r   r   new_sizer   s         r/   r   YolosImageProcessor.resizec  s    6 $"3"3 8BC8H$J\J\^b^o^opH__:5;;rs;KT__^b^l^lmH[[TZZTZZ0Hnosnttuv  
 8A;GRZ
^d
 r.         ?
annotationr`   target_size	thresholdc                 ~  > [        X25       VVs/ s H	  u  pgXg-  PM     snnu  p0 n
X:S'   UR                  5        H  u  pUS:X  a;  UnU[        R                  " XX/[        R                  UR
                  S9-  nXS'   MF  US:X  a  UnXU-  -  nUU
S'   M\  US:X  a  USS2S4   nU Vs/ s H$  n[        [        U ]#  U[        US   US   S	9US
9PM&     nn[        R                  " U5      R                  [        R                  5      nUSS2S4   U:  nUU
S'   M  US:X  a  X:S'   M  XU'   M     U
$ s  snnf s  snf )ak  
Resizes an annotation to a target size.

Args:
    annotation (`dict[str, Any]`):
        The annotation dictionary.
    orig_size (`tuple[int, int]`):
        The original size of the input image.
    target_size (`tuple[int, int]`):
        The target size of the image, as returned by the preprocessing `resize` step.
    threshold (`float`, *optional*, defaults to 0.5):
        The threshold used to binarize the segmentation masks.
    resample (`PILImageResampling | tvF.InterpolationMode | int`, defaults to `tvF.InterpolationMode.NEAREST_EXACT`):
        The resampling filter to use when resizing the masks.
rc   r_   r7   rV   rJ   Nr   r]   r   r   )zipitemsrA   rB   re   r3   r   r   r   r   rF   r   )r   r   r`   r   r   r   rk   origratio_heightratio_widthnew_annotationkeyvaluer_   scaled_boxesrV   scaled_arearJ   r6   r   s                      r/   resize_annotation%YolosImageProcessor.resize_annotation  s}   . HK;Gb$cGb|vV]Gb$c!!,v$**,JCg~$u JRWR_R_hmhtht(   +7w'"L&@A)4v&ag
 !&	 !& -t;8;q>UV#Xck <  !&	   E*--emm<adi/*/w')4v&&+s#3 -6 A %d$s   D4++D:r   c                     Uu  p40 nUR                  5        HU  u  pgUS:X  aF  Un[        U5      nU[        R                  " XCXC/[        R                  UR
                  S9-  nXU'   MQ  XuU'   MW     U$ )Nr_   r7   )r   r   rA   rB   re   r3   )	r   r   r   rl   rm   norm_annotationr   r   r_   s	            r/   normalize_annotation(YolosImageProcessor.normalize_annotation  s    $.!$**,JCg~07 JRWR_R_hmhtht  (-$',$ - r.   input_image_sizeoutput_image_sizec                 ^   0 nX6S'   S [        X25       5       u  pxUR                  5        H~  u  pU	S:X  a*  U
n[        R                  " UUSS9n[	        US5      nXS'   M5  U	S:X  a3  U(       a,  U
nU[
        R                  " XX/UR                  S9-  nXS'   Mn  U	S:X  a  X6S'   Mz  XU	'   M     U$ )	z+
Update the annotation for a padded image.
rc   c              3   .   #    U  H  u  pX!-  v   M     g 7fNr$   ).0outputinputs      r/   	<genexpr>JYolosImageProcessor._update_annotation_for_padded_image.<locals>.<genexpr>  s     $rIqU^Iqs   rJ   r   fillr]   r_   rb   )r   r   tvFpadr   rA   rB   r3   )r   r   r   r   paddingupdate_bboxesr   r   r   r   r   rJ   r_   s                r/   #_update_annotation_for_padded_image7YolosImageProcessor._update_annotation_for_padded_image  s     !2v$rM^Iq$r!$**,JCg~
 %UA.*/w'M+[)_hmhthtuu*/w'):v&&+s## -$ r.   padded_sizer   r   c                    UR                  5       SS  nUS   US   -
  nUS   US   -
  nUS:  d  US:  a  [        SU SU S35      eXb:w  a0  SSX/n	[        R                  " XUS9nUb  U R	                  X6X)U5      n[
        R                  " U[
        R                  UR                  S9n
SU
S US   2S US   24'   XU4$ )	NrQ   r   r]   zzPadding dimensions are negative. Please make sure that the padded size is larger than the original size. Got padded size: z, original size: r   r   r7   )	rc   r   r   r   r   rA   rG   rd   r3   )r   rj   r   r   r   r   original_sizepadding_bottompadding_rightr   r   s              r/   r   YolosImageProcessor.pad  s    

RS)$Q-*::#Aq)99A!233>-?PQ^P__`b  '!];GGGE6E%!EE{]

 [[EKKU
=>
%]1%%'9q)9'99:*,,r.   imagesrS   c                 (   > [         TU ]  " XX440 UD6$ )a  
annotations (`AnnotationType` or `list[AnnotationType]`, *optional*):
    Annotations to transform according to the padding that is applied to the images.
return_segmentation_masks (`bool`, *optional*, defaults to `self.return_segmentation_masks`):
    Whether to return segmentation masks.
masks_path (`str` or `pathlib.Path`, *optional*):
    Path to the directory containing the segmentation masks.
)r   
preprocess)r   r  rS   rN   r   r   r   s         r/   r  YolosImageProcessor.preprocess  s    " w!&7Pg`fggr.   ztorch.Tensor	do_resizez7PILImageResampling | tvF.InterpolationMode | int | None
do_rescalerescale_factorr   r#   
image_mean	image_stdr   pad_sizereturn_tensorsc           
         Ub  [        U[        5      (       a  U/nUb<  [        U5      [        U5      :w  a$  [        S[        U5       S[        U5       S35      e[	        U5      nUb  [        U[        U5        UbQ  U[        R                  :X  a=  [        U[        R                  [        45      (       d  [        S[        U5       S35      e0 n/ n/ n/ n[        Xb  UOS/[        U5      -  5       H  u  nnUb"  U R                  UUUUU[        R                  S9nU(       aH  U R!                  UXgS9nUb3  U R#                  UUR%                  5       S	S UR%                  5       S	S S
9nUnU R'                  UXXU5      nU(       a-  Ub*  U R)                  U[+        U[        R                  5      5      nUR-                  U5        UR-                  U5        M     UnUb  UOSnU(       GaB  Ub  UR.                  UR0                  4nO[3        U5      n/ n/ n[        Xb  UOS/[        U5      -  5       H  u  nnUUR%                  5       S	S :X  aa  UR-                  U5        UR-                  [4        R6                  " U[4        R8                  UR:                  S95        UR-                  U5        M~  U R=                  UUUUS9u  nnnUR-                  U5        UR-                  U5        UR-                  U5        M     UnUb  UOSnUR?                  S[4        R@                  " USS905        UR?                  S[4        R@                  " USS905        [C        UUS9nUb  U Vs/ s H  n[C        UUS9PM     snUS'   U$ s  snf )zO
Preprocess an image or a batch of images so that it can be used by the model.
NzThe number of images (z) and annotations (z) do not match.zxThe path to the directory containing the mask PNG files should be provided as a `pathlib.Path` or string object, but is z	 instead.)rN   r   rO   r   rQ   )r`   r   r7   )r   r   r   r   dimr   )tensor_typelabels)"r   dictr?   r   r   r   SUPPORTED_ANNOTATION_FORMATSr   r   r   r*   typer   r   r   FIRSTr   r   rc   rescale_and_normalizer   r   rE   r1   r2   r   rA   onesrd   r3   r   updaterF   r
   )r   r  rS   rN   r   r  rc   r   r  r  r   r#   r  r	  r   r
  r"   r  r   dataprocessed_imagesprocessed_annotationspixel_masksrj   r   resized_imager   padded_imagespadded_annotationsr   encoded_inputss                                  r/   _preprocessYolosImageProcessor._preprocess,  s   0 "z+t'D'D&-K"s6{c+6F'F(V5H[IYHZZij  "&)" )E{S "*888zGLL#+>??<<@<L;MYX 
  "!$V<S[Z^Y_beflbmYm!nE:&!44.G)&6&<&< 5 
  $E P*!%!7!7""'**,rs"3$1$6$6$8$= "8 "J
 &..ujR^luvE%+*A!66z>RWYiYoYoCpq
##E*!((47 "o8 "/:/F+D#'?26:M!#%(@W^b]cfijpfq]q%r!z%**,rs"33!((/&&uzz+U[[Y^YeYe'fg&--j904;:Mc 19 1-z: $$U+"))*5"":. &s #F0;0G,TKKKu{{;A'FGH^U[[Q%?@A%dG"Wb(WbZ^DWb(N8$ (s   %Ntarget_sizesc                    UR                   UR                  pTUb#  [        U5      [        U5      :w  a  [        S5      e[        R
                  R                  US5      nUSSS24   R                  S5      u  px[        U5      n	Ub  [        U[        5      (       aS  [        R                  " U V
s/ s H  oS   PM	     sn
5      n[        R                  " U V
s/ s H  oS   PM	     sn
5      nOUR                  S5      u  p[        R                  " XX/SS9R                  U	R                   5      nXSS2SSS24   -  n	/ n[#        XxU	5       H1  u  nnnXU:     nUX:     nUX:     nUR%                  UUUS.5        M3     U$ s  sn
f s  sn
f )	a!  
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.
NzTMake sure that you pass in as many target sizes as the batch dimension of the logitsrY   .r   r]   r  )scoresr  r_   )logits
pred_boxesr?   r   r   r   softmaxr\   r   r   listrA   r   unbindrF   r   r3   r   rE   )r   outputsr   r"  
out_logitsout_bboxprobr$  r  r_   iimg_himg_w	scale_fctresultsslbscorelabelboxs                        r/   post_process_object_detection1YolosImageProcessor.post_process_object_detection  s}   &  '~~w/A/AH#:#l"33 j  }}$$Z4c3B3h++B/ )2 #,--L%ALqdL%ABL%ALqdL%AB+2215U5$@aHKKELLYIaqj11E651GAq!)m$Eam$EAM"CNNeusKL	 2  &B%As   /FF	)r#   )NNNNr   )NTr   )NNN)r   N)9r%   r&   r'   r(   r    valid_kwargsr   BILINEARr   r   r  r   r	  r   r   r"   r  r  r   r   rc   r   model_input_namesr   r   rA   r   r  r,   r*   r   r   r   r   r   r   r   NEARESTr   tupleri   r   r   r   r   r   r   r   r   r(  r
   r  r   r   r9  r-   __classcell__)r   s   @r/   r   r   #  s*   ,L!**H&J$I,,FIJLF $7D'6#(A!B #t #* +/1504;?||  !4'	
 $($; ',,&- !11D8 
H RV	+||+ + MN	+ 
+ +d QcQkQk7cN7 c?7 38_	7
 7 MN7 7rt sCx UY !!  S/! !c?	! 
!N -1"-||- 38_- cNT)	-
 - -<  EI1504hh $d>&::TAh $($;	h
 ',,&-h 23h 
h h$r^$r $d>&::TAr $(	r
 ',,&-r r r Lr r r r !%r DK'$.r 4;&-r r  T/!r" &&-#r$ j(4/%r( 
)rj Y]3"'3=G$u+=U3 3r.   r   )FN)TN)N   );r   typingr   r   rA   r   torchvision.ior   torchvision.transforms.v2r   r   image_processing_backendsr	   image_processing_utilsr
   r   image_transformsr   r   r   image_utilsr   r   r   r   r   r   r   r   r   r   r   r   processing_utilsr   r   utilsr   r   r    r   r   r  ri   r3   r   rM   r,   r*   rt   r   r   r  r   r   r?  r   r   __all__r$   r.   r/   <module>rL     s        % 7 ; A ` `    5 /!E ! !1 ? ?AQA_A_` S  ell _d_k_k L ',7;	=  $= (#-4	=@8%,, 85<< 8JA 04.<<.. gll". 	.
 (#-. 
.d Z\&c3h&'*&69Dj&SV&
38_&R o, o od !
!r.   