Ë
    ÜÓ^j(±  ã                   óÀ  — d dl mZ d dlZd dlmc 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 dd	lmZmZmZmZmZmZmZmZ dd
lmZmZ ddlmZm Z m!Z!  G d„ ded¬«      Z"d„ Z#d„ Z$	 d"dejJ                  de&e'e'f   de(de&ejJ                  ejJ                  f   fd„Z)	 d"dejJ                  dejJ                  de&e'e'f   de(dejJ                  f
d„Z*d#dejJ                  de'dejJ                  fd„Z+dejJ                  de&ejJ                  ejJ                  f   fd„Z,	 d#dejJ                  dejJ                  de'dejJ                  fd„Z-e  G d „ d!e
«      «       Z.d!gZ/y)$é    )ÚUnionN)Ú
functional)ÚTorchvisionBackendé   )Ú'SemanticSegmentationPostProcessorOutput)ÚBatchFeature)Úgroup_images_by_shapeÚreorder_images)ÚIMAGENET_DEFAULT_MEANÚIMAGENET_DEFAULT_STDÚChannelDimensionÚ
ImageInputÚPILImageResamplingÚSizeDictÚ#get_image_size_for_max_height_widthÚmake_list_of_images)ÚImagesKwargsÚUnpack)Ú
TensorTypeÚauto_docstringÚis_torch_availablec                   ó   — e Zd ZU dZeed<   y)ÚSapiens2ImageProcessorKwargsak  
    do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`):
        Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0
        is used for background, and background itself is not included in all classes of a dataset (e.g.
        ADE20k). The background label will be replaced by 255.
    Údo_reduce_labelsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚboolÚ__annotations__© ó    ú�/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/sapiens2/image_processing_sapiens2.pyr   r   ,   s   … ñð Ôr"   r   F)Útotalc                 ót   — | j                  d«      \  } }}}| || |z   ||z   g}t        j                  |d¬«      S )Néÿÿÿÿ©Údim©ÚunbindÚtorchÚstack©ÚxÚyÚwÚhÚbs        r#   Úbox_xywh_to_xyxyr3   7   s>   € Ø—‘˜"“�J€A€qˆ!ˆQØ
ˆq�A˜‘E˜Q ™UÐ$€AÜ�;‰;�q˜bÔ!Ð!r"   c                 ó€   — | j                  d«      \  } }}}| d|z  z   |d|z  z   ||g}t        j                  |d¬«      S )Nr&   ç      à?r'   r)   r-   s        r#   Úbox_xywh_to_cxcywhr6   =   sF   € Ø—‘˜"“�J€A€qˆ!ˆQØ
ˆc�A‰g‰+˜˜S 1™W™¨¨QÐ0€AÜ�;‰;�q˜bÔ!Ð!r"   ÚboxesÚoutput_sizeÚpaddingÚreturnc                 ó8  — | j                  d«      \  }}}}t        j                  ||gd¬«      }||z  }||z  }	|\  }
}||
z  }t        j                  ||	|z  kD  d   t        j                  |||z  gd¬«      t        j                  |	|z  |	gd¬«      «      }||fS )aS  Compute crop center and scale from bounding boxes, applying padding and aspect ratio correction.

    Accepts either a single box `(4,)` or multiple boxes `(num_boxes, 4)` and returns center/scale with a matching
    leading dimension.

    Args:
        boxes (`torch.Tensor` of shape `(4,)` or `(num_boxes, 4)`): Bounding box in
            (center-x, center-y, width, height) format, with values in absolute pixel coordinates.
        output_size (`tuple[int, int]`): Target output size as `(height, width)`, used to compute
            the aspect ratio for scale correction.
        padding (`float`, *optional*, defaults to `1.25`): Multiplicative factor applied to the
            bounding box dimensions, adding context around the region of interest.

    Returns:
        `tuple[torch.Tensor, torch.Tensor]`: A pair `(center, scale)` where `center` has shape
        `(..., 2)` with (x, y) in input-image pixel coordinates, and `scale` has shape `(..., 2)`
        with (width, height) in input-image pixels representing the dimensions of the padded,
        aspect-ratio-corrected crop window.
    r&   r'   ).N)r*   r+   r,   Úwhere)r7   r8   r9   Úcenter_xÚcenter_yÚwidthÚheightÚcenterÚscaled_widthÚscaled_heightÚoutput_heightÚoutput_widthÚaspect_ratioÚscales                 r#   Úboxes_to_crop_paramsrH   C   s³   € ð0 ).¯©°RÓ(8Ñ%€Hˆh˜˜vÜ�[‰[˜( HÐ-°2Ô6€FØ˜7‘?€LØ˜WÑ$€MØ"-Ñ€M�<Ø -Ñ/€LÜ�K‰KØ	˜¨Ñ4Ñ	4°iÑ@Ü�‰�\ <°,Ñ#>Ð?ÀRÔHÜ�‰�] \Ñ1°=ÐAÀrÔJó€Eð
 �5ˆ=Ðr"   Úimagec           	      ó>  — |\  }}| j                   \  }}}t        |||¬«      \  }	}
|	j                  d«      \  }}|
j                  d«      \  }}|dz
  |z  }|dz
  |z  }t        j                  ||«      dk  }t        j
                  t        j                  |t        j                  | j                  ¬«      t        j                  |t        j                  | j                  ¬«      d¬«      \  }}||dd…ddf   z  |dd…ddf   z   d	|dd…ddf   z  z
  }||dd…ddf   z  |dd…ddf   z   d	|dd…ddf   z  z
  }t        j                  d
|z  |dz
  z  dz
  d
|z  |dz
  z  dz
  gd¬«      }|j                   d   }t        j                  ||||| j                  | j                  ¬«      }| j                  d«      }|df| dffD ]V  \  }}|j                  «       sŒt        j                  |j!                  |j#                  «       ddd«      ||   |dd¬«      ||<   ŒX |S )aR  Crops and resizes bounding box regions from the input image to the target output size.

    Applies padding and aspect ratio correction to each crop before resizing.
    Uses bilinear interpolation for downscaling and bicubic for upscaling.

    This implementation is equivalent to the cv2 affine warp with rotation=0 used in the original
    Sapiens2 codebase. Rotation is always zero because we don't support rotated bounding boxes.

    Args:
        image (`torch.Tensor`): Input image tensor of shape `(C, H, W)` in float32.
        boxes (`torch.Tensor`): Bounding boxes in (center-x, center-y, width, height) format,
            shape `(num_boxes, 4)`, with values in absolute pixel coordinates.
        output_size (`tuple[int, int]`): Target output size as `(height, width)`.
        padding (`float`, *optional*, defaults to `1.25`): Multiplicative factor applied to the
            bounding box dimensions before cropping, adding context around the region of interest.

    Returns:
        `torch.Tensor`: Cropped and resized images of shape `(num_boxes, C, output_height, output_width)`.
    )r8   r9   r&   é   ç      ð?©ÚdtypeÚdeviceÚij)ÚindexingNr5   g       @r'   r   ©rO   rN   ÚbilinearÚbicubicÚzerosT)ÚmodeÚpadding_modeÚalign_corners)ÚshaperH   r*   r+   ÚminimumÚmeshgridÚarangeÚfloat32rO   r,   ÚemptyrN   Ú	unsqueezeÚanyÚFÚgrid_sampleÚexpandÚsum)rI   r7   r8   r9   rD   rE   Únum_channelsÚinput_heightÚinput_widthrA   rG   r=   r>   Úboxes_widthÚboxes_heightÚscale_xÚscale_yÚis_bilinearÚgrid_yÚgrid_xÚin_xÚin_yÚgridsÚ	num_boxesÚoutputÚimage_4dÚmaskrV   s                               r#   Úcrop_and_resizerv   i   s;  € ð2 #.Ñ€M�<Ø.3¯k©kÑ+€L�, Ü(¨¸KÐQXÔY�M€FˆEØŸ™ rÓ*Ñ€HˆhØ %§¡¨RÓ 0Ñ€K�à˜aÑ ;Ñ.€GØ˜qÑ  LÑ0€GÜ—-‘- ¨Ó1°CÑ7€Kä—^‘^Ü�‰�]¬%¯-©-ÀÇÁÔMÜ�‰�\¬¯©¸u¿|¹|ÔLØô�N€FˆFð
 �GšA˜t T˜MÑ*Ñ*¨X²a¸¸t°mÑ-DÑDÀsÈ[ÒYZÐ\`ÐbfÐYfÑMgÑGgÑg€DØ�GšA˜t T˜MÑ*Ñ*¨X²a¸¸t°mÑ-DÑDÀsÈ\ÒZ[Ð]aÐcgÐZgÑMhÑGhÑh€Dä�K‰K˜˜t™ {°Q¡Ñ7¸#Ñ=¸sÀT¹zÈ\Ð\]ÑM]Ñ?^ÐadÑ?dÐeÐkmÔn€Eà—‘˜A‘€IÜ�[‰[˜ L°-ÀÐV[×VbÑVbÐjo×juÑjuÔv€Fð �‰˜qÓ!€HØ# ZÐ0°K°<ÀÐ2KÐLò ‰
ˆˆdØ�8‰8�:ÜŸ=™=Ø—‘ §¡£
¨B°°BÓ7Ø�d‘ØØ$Ø"ôˆF�4ŠLðð €Mr"   ÚheatmapsÚkernelc                 ó  — |dz  dk(  s|dk  rt        d«      ‚d|dz
  dz  dz
  z  dz   }|dz
  dz  }| j                  d¬	«      }t        j                  | ||||fd
d¬«      }t	        j
                  |||g||g¬«      }|dd…|| …|| …f   }|j                  d¬	«      }t        j                  |dkD  |t        j                  |«      «      }	t        j                  |dkD  ||	z  t        j                  |«      «      }
||
dd…ddf   z  S )a¬  Gaussian blur per-keypoint heatmap, preserving the original max value.

    Matches cv2.GaussianBlur with sigma=0 which means that the sigma is automatically
    computed from the kernel size.

    Args:
        heatmaps: Shape `(K, height, width)`.
        kernel: Odd integer kernel size for the Gaussian blur. Must be greater than 1.

    Returns:
        `torch.Tensor`: Blurred heatmaps of the same shape as the input.
    é   r   rK   z2Kernel size must be an odd integer greater than 1.g333333Ó?r5   gš™™™™™é?)rK   rz   r'   Úconstantç        )rV   Úvalue)Úkernel_sizeÚsigmaN)	Ú
ValueErrorÚamaxra   ÚpadÚtvFÚgaussian_blurr+   r<   Ú	ones_like)rw   rx   r   ÚborderÚorigin_maxesÚpaddedÚblurredÚresultÚresult_maxesÚ
safe_maxesrG   s              r#   Úgaussian_blur_preserve_maxr�   ¨   s#  € ð ��z�Q‚˜& Aš+ÜÐMÓNÐNØ�F˜Q‘J #Ñ%¨Ñ)Ñ*¨SÑ0€EØ�q‰j˜QÑ€FØ—=‘= V�=Ó,€Lô �U‰U�8˜f f¨f°fÐ=ÀJÐVYÔZ€FÜ×Ñ °V¸VÐ4DÈUÐTYÈNÔ[€GØ’Q˜ ˜w˜¨°¨w¨Ð6Ñ7€Fà—;‘; 6�;Ó*€LÜ—‘˜\¨AÑ-¨|¼U¿_¹_È\Ó=ZÓ[€JÜ�K‰K˜ qÑ(¨,¸Ñ*CÄUÇ_Á_ÐUaÓEbÓc€EØ�Eš!˜T 4˜-Ñ(Ñ(Ð(r"   c           	      ó°  — | j                   \  }}}}| j                  }| j                  ||d«      }|j                  d¬«      }|j	                  d¬«      }||z  j                  «       }	||z  j                  «       }
t        j                  |j                  d«      dkD  t        j                  |	|
gd¬«      t        j                  ||dfd|¬«      «      }||fS )aC  Predict keypoint locations and confidence scores from heatmaps.

    Args:
        heatmaps: Shape `(num_persons, num_keypoints, height, width)`.

    Returns:
        locations: `(num_persons, num_keypoints, 2)` x/y in heatmap pixel coordinates.
        scores: `(num_persons, num_keypoints)` per-keypoint confidence.
    r&   r'   r|   rz   g      ð¿©rO   )rY   rO   Úreshaper�   ÚargmaxÚfloatr+   r<   r_   r,   Úfull)rw   Únum_personsÚnum_keypointsÚ_Úheatmap_widthrO   Úheatmap_flatÚscoresÚ
flat_indexÚlocations_xÚlocations_yÚ	locationss               r#   Úget_keypoint_predictionsrž   Æ   s×   € ð 4<·>±>Ñ0€K�  =Ø�_‰_€FØ×#Ñ# K°ÀÓC€LØ×Ñ 2ÐÓ&€FØ×$Ñ$¨Ð$Ó,€JØ Ñ-×4Ñ4Ó6€KØ Ñ.×5Ñ5Ó7€KÜ—‘Ø×Ñ˜Ó˜sÑ"Ü�‰�[ +Ð.°BÔ7Ü�
‰
�K °Ð2°DÀÔHó€Ið
 �fÐÐr"   Ú	keypointsÚblur_kernel_sizec           	      ó.  — |j                   \  }}}}|j                  }t        |j                  ||z  ||«      |«      j                  ||||«      }|j	                  dd«      j                  «       }t        j                  |dd¬«      }|j                  «       }	|dz   }
|dz   }|
|z  }||z  }| dd…dd…df   j                  «       d	z   | dd…dd…d	f   j                  «       d	z   |z  z   }||t        j                  ||t        j                  ¬
«      ddd…f   z  z   }||t        j                  ||t        j                  ¬
«      dd…df   z  z   }|j                  d«      }dd	d|| |d	z   |d	z    dœ}|j                  «       D ���ci c]  \  \  }}}||f|	||z      “Œ }}}}d|d   |d   z
  z  }d|d   |d   z
  z  }|d   d|d   z  z
  |d   z   }|d   d|d   z  z
  |d   z   }d|d   |d   z
  |d   z
  |d   z   |d   z   |d   z
  |d   z
  |d   z   z  }t        j                  |j                  «      j                   }||z   }||z   }||z  ||z  z
  }||z  ||z  z
  |z  }| |z  ||z  z   |z  }| t        j"                  ||gd¬«      z
  S c c}}}w )aI  Sub-pixel refinement via Hessian on log-heatmaps (UDP Dark Pose).

    Args:
        keypoints: Shape `(num_persons, num_keypoints, 2)` x/y in heatmap pixel coordinates.
        heatmaps: Shape `(num_persons, num_keypoints, height, width)`.

    Returns:
        `(num_persons, num_keypoints, 2)` refined keypoint locations.
    gü©ñÒMbP?g      I@)rK   rK   rK   rK   Ú	replicate)rV   rz   Nr   rK   rR   r&   )©r   r   ©r   rK   ©r   r&   ©rK   r   ©r&   r   ©rK   rK   ©r&   r&   r5   r¤   r¥   r¦   r§   r£   r¨   r©   r'   )rY   rO   r�   r�   ÚclampÚlogra   r‚   ÚflattenÚlongr+   r\   r_   ÚitemsÚfinforN   ÚepsÚcat)rŸ   rw   r    r”   r•   Úheatmap_heightr—   rO   Úheatmaps_paddedÚheatmaps_flattenedÚpadded_heightÚpadded_widthÚkeypoint_strideÚperson_strideÚindexÚposition_to_index_offsetÚdxÚdyÚoffsetÚheatmap_valuesÚ
gradient_xÚ
gradient_yÚ
hessian_xxÚ
hessian_yyÚ
hessian_xyr°   ÚdeterminantÚoffset_xÚoffset_ys                                r#   Ú"post_dark_unbiased_data_processingrÇ   ß   si  € ð AIÇÁÑ=€K� °Ø�_‰_€Fä)Ø×Ñ˜ }Ñ4°nÀmÓTÐVfóç�gˆk˜=¨.¸-ÓHð ð �~‰~˜d DÓ)×-Ñ-Ó/€Hä—e‘e˜H l¸ÔE€OØ(×0Ñ0Ó2Ðà" QÑ&€MØ  1Ñ$€LØ# lÑ2€OØ! OÑ3€Mà’aš˜A�gÑ×#Ñ#Ó%¨Ñ)¨Y²qº!¸Q°wÑ-?×-DÑ-DÓ-FÈÑ-JÈlÑ,ZÑZ€EØ�O¤e§l¡l°=ÈÔW\×WaÑWaÔ&bÐcgÒijÐcjÑ&kÑkÑk€EØ�M¤E§L¡L°ÀVÔSX×S]ÑS]Ô$^Ò_`ÐbfÐ_fÑ$gÑgÑg€EØ�O‰O˜BÓ€Eð ØØØØ�Ø˜qÑ Ø  1Ñ$Ð%ñ Ðð Nf×MkÑMkÓMm÷ð Ù9I¹¸"¸bÀ6ˆˆRˆÐ$ U¨V¡^Ñ4Ñ4ð€Nò ð ˜ tÑ,¨~¸eÑ/DÑDÑE€JØ˜ tÑ,¨~¸eÑ/DÑDÑE€Jà Ñ%¨¨N¸4Ñ,@Ñ(@Ñ@À>ÐRWÑCXÑX€JØ Ñ%¨¨N¸4Ñ,@Ñ(@Ñ@À>ÐRWÑCXÑX€JØØ�tÑØ
˜Ñ
ñ	à
˜Ñ
ñ	ð ˜Ñ
ñ	ð ˜Ñ
ñ		ð
 ˜Ñ
ñ	 ð ˜Ñ
ñ	 ð ˜Ñ
 ñ	!ñ	€Jô �+‰+�j×&Ñ&Ó
'×
+Ñ
+€CØ˜cÑ!€JØ˜cÑ!€JØ˜zÑ)¨J¸Ñ,CÑC€KØ˜ZÑ'¨*°zÑ*AÑAÀ[ÑP€HØ�˜jÑ(¨:¸
Ñ+BÑBÀkÑQ€HØ”u—y‘y (¨HÐ!5¸2Ô>Ñ>Ð>ùô5s   ÆJc            "       óÔ  ‡ — e Zd ZdZeZej                  Ze	Z
eZdddœZdZdddœZdZdZdZdZdZdZdee   fˆ fd	„Ze	 	 d7deded
z  deeee         d
z  dee   def
ˆ fd„«       Z	 d8deded
z  deeee         d
z  dedede e!z  d
z  de"e df   d
z  defd„Z#ded   ded   fd„Z$	 	 	 d9dee%jL                     dede'dddede'ded ed!ed"eee   z  d
z  d#eee   z  d
z  d$ed
z  d%ed&edeeee         d
z  dee%jL                     f d'„Z(	 d:d(ee)   d
z  d)edd*fd+„Z*	 	 	 	 	 d;deeee         d,e+d-ed
z  d.e!ee)e+e+f      z  d
z  d(e!ee)e+e+f      z  d
z  deee,e e%jL                  f         fd/„Z-	 	 	 d<d.e!ee)e+e+f      z  d
z  d(e!ee)e+e+f      z  d
z  d0ed
z  dee,e e%jL                  f      fd1„Z.	 	 	 d<d.e!ee)e+e+f      z  d
z  d(e!ee)e+e+f      z  d
z  d0ed
z  dee,e e%jL                  f      fd2„Z/	 	 d7d(e!ee)e+e+f      z  d
z  d3ed
z  dee,e e%jL                  f      fd4„Z0d5e%jL                  d.e!ee)e+e+f      z  d
z  d(e!ee)e+e+f      z  d
z  d0ed
z  dee%jL                     f
d6„Z1ˆ xZ2S )=ÚSapiens2ImageProcessorz3PIL backend for Sapiens2 with reduce_label support.i   i   )r@   r?   Téà   FÚkwargsc                 ó$   •— t        ‰| �  di |¤Ž y )Nr!   )ÚsuperÚ__init__)ÚselfrË   Ú	__class__s     €r#   rÎ   zSapiens2ImageProcessor.__init__:  s   ø€ Ü‰ÑÑ"˜6Ó"r"   NÚimagesÚsegmentation_mapsr7   r:   c                 ó(   •— t        ‰| �  |||fi |¤ŽS )a  
        segmentation_maps (`ImageInput`, *optional*):
            The segmentation maps to preprocess.
        boxes (`list[list[list[float]]]` or `np.ndarray`, *optional*):
            List or array of bounding boxes for each image. Each box should be a list of 4 floats
            representing the bounding box coordinates in COCO format
            (top_left_x, top_left_y, width, height). When provided, each person crop is
            affine-warped to the model input size instead of resizing the full image.
        )rÍ   Ú
preprocess)rÏ   rÑ   rÒ   r7   rË   rÐ   s        €r#   rÔ   z!Sapiens2ImageProcessor.preprocess=  s   ø€ ô" ‰wÑ! &Ð*;¸UÑMÀfÑMÐMr"   Údo_convert_rgbÚinput_data_formatÚreturn_tensorsrO   ztorch.devicec                 óî  — ||d<   | j                  ||||¬«      }|j                  «       }	d|	d<   i }
 | j                  |fi |	¤Ž|
d<   |�›| j                  |ddt        j                  ¬«      }|j                  «       }|j                  dddœ«        | j                  dd	|i|¤Ž}|D �cg c]0  }|j                  d
«      j                  t        j                  «      ‘Œ2 }}||
d<   t        |
|¬«      S c c}w )z"Handle extra inputs beyond images.r7   )rÑ   rÕ   rÖ   rO   Fr   Úpixel_valuesrz   )rÑ   Úexpected_ndimsrÕ   rÖ   )Údo_normalizeÚ
do_rescalerÑ   r   Úlabels)ÚdataÚtensor_typer!   )Ú_prepare_image_like_inputsÚcopyÚ_preprocessr   ÚFIRSTÚupdateÚsqueezeÚtor+   Úint64r   )rÏ   rÑ   rÒ   r7   rÕ   rÖ   r×   rO   rË   Úimages_kwargsrÞ   Úprocessed_segmentation_mapsÚsegmentation_maps_kwargsÚprocessed_segmentation_maps                 r#   Ú_preprocess_image_like_inputsz4Sapiens2ImageProcessor._preprocess_image_like_inputsP  s4  € ð  ˆˆw‰Ø×0Ñ0Ø¨.ÐL]Ðflð 1ó 
ˆð Ÿ™›ˆØ,1ˆÐ(Ñ)ØˆØ/˜t×/Ñ/°ÑH¸-ÑHˆˆ^Ñð Ð(Ø*.×*IÑ*IØ(Ø Ø$Ü"2×"8Ñ"8ð	 +Jó +Ð'ð (.§{¡{£}Ð$Ø$×+Ñ+¸UÐRWÑ,XÔYØ*:¨$×*:Ñ*:ñ +Ø2ð+Ø6Nñ+Ð'ð 3Nö+à.ð +×2Ñ2°1Ó5×8Ñ8¼¿¹ÕEð+Ð'ð +ð 9ˆD�‰Nä °>ÔBÐBùò+s   Â)5C2rÝ   ztorch.Tensorc           	      óf  — t        t        |«      «      D ]™  }||   }t        j                  |dk(  t        j                  d|j
                  |j                  ¬«      |«      }|dz
  }t        j                  |dk(  t        j                  d|j
                  |j                  ¬«      |«      }|||<   Œ› |S )z/Reduce label values by 1, replacing 0 with 255.r   éÿ   rM   rK   éþ   )ÚrangeÚlenr+   r<   ÚtensorrN   rO   )rÏ   rÝ   ÚidxÚlabels       r#   Úreduce_labelz#Sapiens2ImageProcessor.reduce_label~  sš   € äœ˜V›Ó%ò 	 ˆCØ˜3‘KˆEÜ—K‘K ¨¡
¬E¯L©L¸ÀEÇKÁKÐX]×XdÑXdÔ,eÐglÓmˆEØ˜A‘IˆEÜ—K‘K ¨¡¬e¯l©l¸3ÀeÇkÁkÐZ_×ZfÑZfÔ.gÐinÓoˆEØˆF�3ŠKð	 ð ˆr"   Ú	do_resizeÚsizeÚresamplez7PILImageResampling | tvF.InterpolationMode | int | NoneÚdo_center_cropÚ	crop_sizerÜ   Úrescale_factorrÛ   Ú
image_meanÚ	image_stdÚdisable_groupingr   Údo_padc           	      óL  — |� |d   |d   f}g }t        ||«      D ]�  \  }}t        j                  |t        j                  d¬«      }t        t        j                  |t        j                  |j                  ¬«      «      }|j                  t        |||¬«      «       Œƒ |}d}|r| j                  |«      }t        ||¬«      \  }}i }|j                  «       D ]Z  \  }}|rN|r9t        |d   |d   ¬«      }| j                  |||«      }| j                  ||«      }n| j                  |||«      }|||<   Œ\ t!        ||«      }t        ||¬«      \  }}i }|j                  «       D ]4  \  }}|r| j                  ||«      }| j#                  ||||	|
|«      }|||<   Œ6 t!        ||«      S )	z"Custom preprocessing for Sapiens2.r@   r?   F©rN   rG   rM   )r7   r8   )rþ   )Ú
max_heightÚ	max_width)Úziprƒ   Úto_dtype_imager+   r]   r6   rò   rO   Úextendrv   rõ   r	   r®   r   ÚresizeÚcenter_cropr
   Úrescale_and_normalize)rÏ   rÑ   rö   r÷   rø   rù   rú   rÜ   rû   rÛ   rü   rý   rþ   r   rÿ   r7   rË   r8   ÚcropsrI   Úimage_boxesÚboxes_tensorÚgrouped_imagesÚgrouped_images_indexÚresized_images_groupedrY   Ústacked_imagesÚaspect_ratio_sizeÚresized_imagesÚprocessed_images_groupeds                                 r#   râ   z"Sapiens2ImageProcessor._preprocessˆ  sÞ  € ð( ÐØ ™>¨4°©=Ð9ˆKØˆEÜ&)¨&°%Ó&8ò bÑ"��{Ü×*Ñ*¨5¼¿¹ÈUÔS�Ü1´%·,±,¸{ÔRW×R_ÑR_Ðhm×htÑhtÔ2uÓv�Ø—‘œ_¨U¸,ÐT_Ô`Õaðbð ˆFØˆIáØ×&Ñ& vÓ.ˆFä/DÀVÐ^nÔ/oÑ,ˆÐ,Ø!#ÐØ%3×%9Ñ%9Ó%;ò 		;Ñ!ˆE�>ÙÙä(0¸DÀ¹NÐVZÐ[bÑVcÔ(dÐ%Ø%)§[¡[°ÐARÐT\Ó%]�NØ%)×%5Ñ%5°nÀdÓ%K‘Nà%)§[¡[°ÀÀxÓ%P�NØ,:Ð" 5Ò)ð		;ô (Ð(>Ð@TÓUˆä/DÀ^ÐfvÔ/wÑ,ˆÐ,Ø#%Ð Ø%3×%9Ñ%9Ó%;ò 	=Ñ!ˆE�>ÙØ!%×!1Ñ!1°.À)Ó!L�Ø!×7Ñ7Ø 
¨N¸LÈ*ÐV_óˆNð /=Ð$ UÒ+ð	=ô Ð6Ð8LÓMÐMr"   Útarget_sizesÚreturn_segmentation_scoreszBlist[torch.Tensor] | list[SemanticSegmentationPostProcessorOutput]c                 óÄ  — t        «       st        d«      ‚|j                  }|�Ít        |«      t        |«      k7  rt	        d«      ‚t        |t        j                  «      r|j                  «       }g }t        t        |«      «      D ]g  }t        j                  ||   j                  d¬«      ||   dd¬«      }|d   j                  d¬«      }|j                  t        ||d   dœ¬	«      «       Œi nJ|j                  d
¬«      }	t        |j                   d   «      D �
cg c]  }
t        |	|
   ||
   dœ¬	«      ‘Œ }}
|s|D �cg c]  }|j"                  ‘Œ }}|S c c}
w c c}w )a  
        Converts the output of [`Sapiens2ForSemanticSegmentation`] into semantic segmentation maps.

        Args:
            outputs ([`Sapiens2ForSemanticSegmentation`]):
                Raw outputs of the model.
            target_sizes (`list[Tuple]` of length `batch_size`, *optional*):
                List of tuples corresponding to the requested final size (height, width) of each prediction. If unset,
                predictions will not be resized.
            return_segmentation_scores (`bool`, *optional*, defaults to `False`):
                Whether to return segmentation scores alongside the segmentation map. When `True`, each element of
                the returned list is a [`SemanticSegmentationPostProcessorOutput`] with fields `segmentation`
                (class IDs, shape `(height, width)`) and `segmentation_scores` (shape `(num_classes, height, width)`).

        Returns:
            `list[torch.Tensor]` or `list[SemanticSegmentationPostProcessorOutput]`: When
            `return_segmentation_scores=False` (default), a list of length `batch_size` where each item is a
            segmentation map of shape `(height, width)` with class IDs. When `return_segmentation_scores=True`,
            a list of [`SemanticSegmentationPostProcessorOutput`] with fields `segmentation` (class IDs, shape
            `(height, width)`) and `segmentation_scores` (shape `(num_classes, height, width)`). In both cases,
            `(height, width)` corresponds to the target size (if `target_sizes` is specified).
        z:PyTorch is required for post_process_semantic_segmentationzTMake sure that you pass in as many target sizes as the batch dimension of the logitsr   r'   rS   F)r÷   rV   rX   )ÚsegmentationÚsegmentation_scores)rÞ   rK   )r   ÚImportErrorÚlogitsrñ   r€   Ú
isinstancer+   ÚTensorÚnumpyrð   ra   Úinterpolater_   r‘   Úappendr   rY   r  )rÏ   Úoutputsr  r  r  Úsemantic_segmentationró   Úresized_logitsÚsemantic_mapÚseg_mapsÚiÚitems               r#   Ú"post_process_semantic_segmentationz9Sapiens2ImageProcessor.post_process_semantic_segmentationÃ  sy  € ô2 "Ô#ÜÐZÓ[Ð[à—‘ˆð Ð#Ü�6‹{œc ,Ó/Ò/Ü Øjóð ô ˜,¬¯©Ô5Ø+×1Ñ1Ó3�à$&Ð!äœS ›[Ó)ò 	�Ü!"§¡Ø˜3‘K×)Ñ)¨aÐ)Ó0°|ÀCÑ7HÈzÐinô"�ð  .¨aÑ0×7Ñ7¸AÐ7Ó>�Ø%×,Ñ,Ü;Ø.:ÐSaÐbcÑSdÑeôõñ	ð —}‘}¨�}Ó+ˆHô
 ˜vŸ|™|¨A™Ó/ö	%ð ô 8Ø*2°1©+ÈfÐUVÉiÑXöð%Ð!ð %ñ *ØCXÖ$Y¸4 T×%6Ó%6Ð$YÐ!Ð$Yà$Ð$ùò%ùò %Zs   ÄEÅEr~   Ú	thresholdÚsource_sizesc                 óö  — t        |t        j                  «      r|j                  «       }t        |t        j                  «      r|j                  «       }t	        |«      }|�|€t        d«      ‚|�|t	        |«      k7  rt        d«      ‚|�|t	        |«      k7  rt        d«      ‚|j                  }	|�|	|j                  z   dz  }	|	j                  }
|	j                  \  }}}}|dk(  r|D �cg c]  }g ‘Œ c}S t        j                  |D ��cg c]  }|D ]  }|‘Œ Œ c}}t        j                  |
¬«      }|	j                  «       }	t        |	«      \  }}t        ||	|¬«      }t        t        |«      | j                   d	   | j                   d
   f¬«      \  }}t        j                  |dz
  |dz
  gt        j                  |
¬«      }||z  |dd…ddd…f   z  |dd…ddd…f   z   d|dd…ddd…f   z  z
  }t#        |«      }|�Î|�Ìt        j                  t%        ||«      D ����cg c]  \  \  }}\  }}||z  ||z  g‘Œ c}}}}t        j                  |
¬«      }t        j&                  t)        |«      D �cg c]2  }||   j+                  d«      j-                  t	        ||   «      d«      ‘Œ4 c}«      }||dd…ddd…f   z  }||dd…g d¢f   z  }g } t)        |«      D ]R  }!||!   }"||!   }#t        j.                  ||
¬«      }$|�|#|kD  }%|"|%   }"|#|%   }#|$|%   }$| j1                  |"|#|$||!   dœ«       ŒT g }&d}'|D ])  }t	        |«      }(|&j1                  | |'|'|(z    «       |'|(z  }'Œ+ |&S c c}w c c}}w c c}}}}w c c}w )a  
        Converts the output of [`Sapiens2ForPoseEstimation`] into keypoint predictions in image space.

        Args:
            outputs (`Sapiens2PoseEstimatorOutput`):
                Raw outputs of the model. `outputs.heatmaps` must have shape
                `(N_total, num_keypoints, heatmap_height, heatmap_width)` where
                `N_total = sum(len(b) for b in boxes)`.
            boxes (`list[list[list[float]]]` or `np.ndarray`):
                List or array of bounding boxes for each image in absolute pixel coordinates. Each box
                should be a list of 4 floats representing the bounding box coordinates in COCO format
                (top_left_x, top_left_y, width, height). Must match the `boxes` argument passed to
                `preprocess`.
            outputs_flipped (`Sapiens2PoseEstimatorOutput`, *optional*):
                Outputs from running the model on horizontally flipped inputs. When provided, heatmaps
                are averaged with `outputs` before keypoint extraction to improve accuracy:
                `avg_heatmaps = (outputs.heatmaps + outputs_flipped.heatmaps) / 2`.
            kernel_size (`int`, *optional*, defaults to 11):
                Kernel size for the Gaussian blur used in UDP Dark Pose refinement.
            threshold (`float`, *optional*):
                Score threshold. Keypoints with scores at or below this value are
                filtered out from the result dictionaries.
            source_sizes (`torch.Tensor` or `list[tuple[int, int]]` of length `batch_size`, *optional*):
                Original `(height, width)` of each image in pixels. Required when `target_sizes` is
                provided, as the source coordinate space for scaling keypoints and bounding boxes.
            target_sizes (`torch.Tensor` or `list[tuple[int, int]]` of length `batch_size`, *optional*):
                Desired output `(height, width)` coordinate space for each image. When provided
                alongside `source_sizes`, keypoint coordinates and bounding boxes are scaled from
                source to target space.

        Returns:
            `list[list[dict]]`: Outer list is over images, inner list is over persons.
            Each dict contains:
            - `keypoints` (`torch.FloatTensor` of shape `(num_keypoints, 2)`): absolute x/y coordinates in
              the source image space, or in target space if `target_sizes` is provided.
            - `scores` (`torch.FloatTensor` of shape `(num_keypoints,)`): per-keypoint confidence.
            - `labels` (`torch.LongTensor` of shape `(num_keypoints,)`): keypoint indices.
            - `bbox` (`torch.FloatTensor` of shape `(4,)`): bounding box in absolute (x_min, y_min, x_max, y_max)
               format, in the same coordinate space as `keypoints`.
        NzA`source_sizes` must be provided when `target_sizes` is specified.zHMake sure that you pass in as many source sizes as the number of images.zHMake sure that you pass in as many target sizes as the number of images.rz   r   rM   )rŸ   rw   r    r@   r?   )r8   rK   r5   )r   rK   r   rK   r�   )rŸ   r™   rÝ   Úbbox)r  r+   r  Útolistrñ   r€   rw   rO   rY   rò   r]   r’   rž   rÇ   rH   r6   r÷   r3   r  r±   rð   r_   rc   r\   r  ))rÏ   r   r7   Úoutputs_flippedr~   r(  r)  r  Ú
num_imagesrw   rO   Únum_total_personsr•   r²   r—   r–   r  Úboxr  Úall_keypointsÚ
all_scoresÚcentersÚscalesÚheatmap_sizeÚ	all_boxesÚsource_heightÚsource_widthÚtarget_heightÚtarget_widthÚper_image_scaleÚimage_indexÚper_person_scaleÚperson_resultsÚperson_indexrŸ   r™   rÝ   ÚkeeprŠ   Úperson_offsetÚnum_persons_in_images)                                            r#   Úpost_process_pose_estimationz3Sapiens2ImageProcessor.post_process_pose_estimation  sé  € ôd �l¤E§L¡LÔ1Ø'×.Ñ.Ó0ˆLÜ�l¤E§L¡LÔ1Ø'×.Ñ.Ó0ˆLä˜“Zˆ
àÐ#¨Ð(<ÜÐ`ÓaÐaØÐ#¨
´c¸,Ó6GÒ(GÜÐgÓhÐhØÐ#¨
´c¸,Ó6GÒ(GÜÐgÓhÐhà×#Ñ#ˆØÐ&Ø  ?×#;Ñ#;Ñ;¸qÑ@ˆHà—‘ˆØJRÏ.É.ÑGÐ˜=¨.¸-à Ò!Ø %Ö&˜1’BÒ&Ð&ô —|‘|Ø$)×A�[°[ÒA¨cŠSÐAˆSÓAÌÏÉÐ_eô
ˆð —>‘>Ó#ˆô %=¸XÓ$FÑ!ˆ�zÜ:Ø#¨hÈô
ˆô
 /Ü˜|Ó,¸4¿9¹9ÀXÑ;NÐPT×PYÑPYÐZaÑPbÐ:cô
‰ˆ�ô —|‘| ]°QÑ%6¸ÈÑ8JÐ$KÔSX×S`ÑS`ÐioÔpˆà˜LÑ(¨6²!°Tº1°*Ñ+=Ñ=ÀÊÈ4ÒQRÈ
Ñ@SÑSÐVYÐ\bÒcdÐfjÒlmÐcmÑ\nÑVnÑnð 	ô % \Ó2ˆ	àÐ#¨Ð(@ä#Ÿl™lô Y\Ð\hÐjvÓXw÷ñ áTÑ5˜¨Ñ7T¸À|ð " LÑ0°-À-Ñ2OÒPõô —m‘mØôˆOô  %Ÿy™yô (-¨ZÓ'8öà#ð $ KÑ0×:Ñ:¸1Ó=×DÑDÄSÈÈ{ÑI[ÓE\Ð^_Õ`òó Ðð *Ð,<ºQÀÂa¸ZÑ,HÑHˆMØ!Ð$4²Qº°_Ñ$EÑEˆIàˆÜ!Ð"3Ó4ò 	ˆLØ% lÑ3ˆIØ Ñ-ˆFÜ—\‘\ -¸Ô?ˆFàÐ$Ø 	Ñ)�Ø% d™O�	Ø ™�Ø ™�à×!Ñ!Ø'°6ÀVÐU^Ð_kÑUlÑmõð	ð  ˆØˆØ ò 	2ˆKÜ#& {Ó#3Ð Ø�M‰M˜.¨¸ÐI]Ñ9]Ð^Ô_ØÐ1Ñ1‰Mð	2ð ˆùòI 'ùó Bùõ0ùòs   Ã+	M#ÄM(
È!M.É27M6Údo_remove_paddingc                 ó    — t        j                  |j                  ddd¬«      }| j                  ||||¬«      }|D �cg c]  }d|i‘Œ c}S c c}w )a´  
        Converts the output of [`Sapiens2ForNormalEstimation`] into L2-normalized surface normal maps.

        Args:
            outputs (`Sapiens2NormalEstimatorOutput`):
                Raw outputs of the model.
            source_sizes (`torch.Tensor` or `list[tuple[int, int]]` of length `batch_size`, *optional*):
                Original `(height, width)` of each image before preprocessing. When provided,
                the padding added during preprocessing is removed and predictions are resized back
                to the original image size (unless `target_sizes` overrides the final size).
            target_sizes (`torch.Tensor` or `list[tuple[int, int]]` of length `batch_size`, *optional*):
                Requested final `(height, width)` for each prediction. When provided, used as the
                resize target instead of `source_sizes`. Resized with bilinear interpolation after
                L2 normalization.
            do_remove_padding (`bool`, *optional*):
                Whether to crop away the zero-padding added during preprocessing before resizing.
                Defaults to `True` when `source_sizes` is provided, `False` otherwise.

        Returns:
            `list[dict[str, torch.Tensor]]` of length `batch_size`. Each dict has a `"normals"` key
            mapping to a tensor of shape `(3, height, width)` with L2-normalized unit vectors in
            `[-1, 1]` per channel (XYZ surface normals).
        rz   rK   g:Œ0âŽyE>)Úpr(   r°   ©Úmapsr)  r  rD  Únormals)ra   Ú	normalizerI  Ú_post_process_maps)rÏ   r   r)  r  rD  rI  ÚresultsrŠ   s           r#   Úpost_process_normal_estimationz5Sapiens2ImageProcessor.post_process_normal_estimation“  sV   € ô< —+‘+˜gŸo™o°¸¸tÔDˆØ×)Ñ)Ø |À,Ðbsð *ó 
ˆð 3:Ö:¨�˜FÒ#Ò:Ð:ùÒ:s   ½Ac                 ó¾   — |j                   }|j                  �||j                  dd…dd…ddf   z  }| j                  ||||¬«      }|D �cg c]  }d|i‘Œ c}S c c}w )aŒ  
        Converts the output of [`Sapiens2ForPointmapEstimation`] into pointmap tensors in image space.

        Args:
            outputs (`Sapiens2PointmapEstimatorOutput`):
                Raw outputs of the model.
            source_sizes (`torch.Tensor` or `list[tuple[int, int]]` of length `batch_size`, *optional*):
                Original `(height, width)` of each image before preprocessing. When provided,
                the padding added during preprocessing is removed and predictions are resized back
                to the original image size (unless `target_sizes` overrides the final size).
            target_sizes (`torch.Tensor` or `list[tuple[int, int]]` of length `batch_size`, *optional*):
                Requested final `(height, width)` for each prediction. Overrides `source_sizes`
                as the resize target.
            do_remove_padding (`bool`, *optional*):
                Whether to crop away the zero-padding added during preprocessing before resizing.
                Defaults to `True` when `source_sizes` is provided, `False` otherwise.

        Returns:
            `list[dict[str, torch.Tensor]]` of length `batch_size`. Each dict has a `"pointmap"` key
            mapping to a tensor of shape `(3, height, width)` with per-pixel 3D XYZ coordinates in
            canonical camera space, optionally divided by `outputs.scales` to convert to metric coordinates.
        NrG  Úpointmap)Ú	pointmapsr4  rK  )rÏ   r   r)  r  rD  rP  rL  rŠ   s           r#   Ú post_process_pointmap_estimationz7Sapiens2ImageProcessor.post_process_pointmap_estimation·  so   € ð: ×%Ñ%ˆ	Ø�>‰>Ð%Ø! G§N¡N²1²a¸¸tÐ3CÑ$DÑDˆIØ×)Ñ)Ø¨ÀLÐduð *ó 
ˆð 4;Ö;¨�˜VÒ$Ò;Ð;ùÒ;s   ÁAÚbackgroundsc                 ó¬  ‡‡— t        ‰t        j                  «      r‰j                  «       Š|j                  j
                  d   }|j                  j                  }|j                  j                  }‰�|t        ‰«      k7  rt        d«      ‚‰du xs t        ˆfd„‰D «       «      }g Š|�zt        |«      }t        |«      dk7  rt        |«      |k7  rt        d«      ‚|D �	cg c]<  }	t        j                  t        j                  |	«      |d¬«      j                  |«      ‘Œ> c}	Š‰ xs t        ˆfd	„‰D «       «      }
t        j                   |j                  |j"                  gd¬
«      }‰�<|r:t%        ‰d   «      }t'        j(                  ||ddd¬«      }|j+                  dd«      }g }|ré|
rç|dd…dd…f   }|dd…dd…f   }dg|z  }‰rœt        j,                  ‰«      }|j
                  dd |j
                  dd k7  r't'        j(                  ||j
                  dd ddd¬«      }|d|z
  |z  z   j+                  dd«      }t        j                  |t        j.                  d¬«      }t1        |||«      D ]  \  }}}|j3                  |||dœ«       Œ |S t5        t        |«      «      D �]!  }||   }‰rC|sAt'        j(                  |j7                  d«      ‰|   ddd¬«      d   }|j+                  dd«      }|dd }|dd }d}‰r±t        ‰«      dk(  r‰d   n‰|   }|j
                  dd |j
                  dd k7  r9t'        j(                  |j7                  d«      |j
                  dd ddd¬«      d   }|d|z
  |z  z   j+                  dd«      }t        j                  |t        j.                  d¬«      }|j3                  |||dœ«       �Œ$ |S c c}	w )a   
        Converts the output of [`Sapiens2ForImageMatting`] into alpha mattes and foreground maps.

        Args:
            outputs (`Sapiens2ImageMattingOutput`):
                Raw outputs of the model.
            target_sizes (`torch.Tensor` or `list[tuple[int, int]]` of length `batch_size`, *optional*):
                Requested final `(height, width)` for each prediction. Resized with bilinear
                interpolation. If unset, predictions are returned at the model output resolution.
            backgrounds (`ImageInput`, *optional*):
                Background image(s) to composite over. Can be a single image (applied to every item
                in the batch) or a list of images, one per batch item. Accepts PIL images, numpy
                arrays, or torch tensors of any dtype; integer types (e.g. uint8) are scaled to
                `[0, 1]` automatically. When provided, each result dict gains a `"composite"` key
                with the composited image as a uint8 tensor in `[0, 255]`.

        Returns:
            `list[dict]` of length `batch_size`. Each dict has:
            - `"alpha"` (`torch.Tensor` of shape `(1, height, width)`): alpha values in `[0, 1]`.
            - `"foreground"` (`torch.Tensor` of shape `(3, height, width)`): pre-multiplied RGB in `[0, 1]`.
            - `"composite"` (`torch.Tensor` of shape `(3, height, width)` or `None`): foreground composited
              over `backgrounds` as a uint8 tensor in `[0, 255]`; `None` when `backgrounds` is not provided.
        r   Nz\Make sure that you pass in as many target sizes as the batch dimension of the matting outputc              3   óR   •K  — | ]  }t        |«      t        ‰d    «      k(  –— Œ  y­w©r   N)Útuple)Ú.0r÷   r  s     €r#   ú	<genexpr>zDSapiens2ImageProcessor.post_process_image_matting.<locals>.<genexpr>  s(   øè ø€ ò =
Ø6:ŒE�$‹Kœ5 ¨a¡Ó1Õ1ñ=
ùs   ƒ$'rK   z[Make sure that you pass in as many backgrounds as the batch dimension of the matting outputTr  c              3   ób   •K  — | ]&  }|j                   d d ‰d   j                   d d k(  –— Œ( y­w)éþÿÿÿNr   )rY   )rW  Ú
backgroundÚbackground_tensorss     €r#   rX  zDSapiens2ImageProcessor.post_process_image_matting.<locals>.<genexpr>  s=   øè ø€ ò C
ØJTˆJ×Ñ˜R˜SÐ!Ð%7¸Ñ%:×%@Ñ%@ÀÀÐ%EÕEñC
ùs   ƒ,/r'   rS   F©r÷   rV   rX   Ú	antialiasr|   rL   r   rZ  )Ú
foregroundÚalphaÚ	composite)r  r+   r  r,  ÚforegroundsrY   rO   rN   rñ   r€   Úallr   rƒ   r  Úto_imageræ   r±   ÚalphasrV  ra   r  rª   r,   Úuint8r  r  rð   r_   )rÏ   r   r  rR  Ú
batch_sizerO   rN   Úall_target_sizes_equalÚbackground_listÚbackground_imageÚall_background_sizes_equalÚmattingÚtarget_sizerŠ   rb  re  Ú
compositesr[  r_  r`  ra  r¹   Úmatting_itemr\  s     `                    @r#   Úpost_process_image_mattingz1Sapiens2ImageProcessor.post_process_image_mattingÜ  s8  ù€ ô: �l¤E§L¡LÔ1Ø'×.Ñ.Ó0ˆLà×(Ñ(×.Ñ.¨qÑ1ˆ
Ø×$Ñ$×+Ñ+ˆØ×#Ñ#×)Ñ)ˆàÐ#ØœS Ó.Ò.Ü Øróð ð ".°Ð!5ò "
¼ó =
Ø>Jô=
ó :
Ðð  ÐØÐ"Ü1°+Ó>ˆOÜ�?Ó# qÒ(¬S°Ó-AÀZÒ-OÜ Øqóð ð
 )8ö"à$ô ×"Ñ"¤3§<¡<Ð0@Ó#AÈÐVZÔ[×^Ñ^Ð_eÕfò"Ðð *<Ð%;ò &
¼só C
ØXjôC
ó @
Ð"ô —)‘)˜W×0Ñ0°'·.±.ÐAÀqÔIˆàÐ#Ñ(>Ü ¨Q¡Ó0ˆKÜ—m‘mØØ ØØ#ØôˆGð —m‘m C¨Ó-ˆGàˆÙ!Ñ&@à!¢! R a R %™.ˆKØšQ ¡˜U‘^ˆFØ˜ *Ñ,ˆJÙ!Ü"Ÿ[™[Ð);Ó<�
Ø×#Ñ# B CÐ(¨G¯M©M¸"¸#Ð,>Ò>Ü!"§¡Ø"Ø$Ÿ]™]¨2¨3Ð/Ø'Ø&+Ø"'ô"�Jð *¨Q°©Z¸:Ñ,EÑE×LÑLÈSÐRUÓV�
Ü ×/Ñ/°
Ä%Ç+Á+ÐUYÔZ�
ä03°KÀÈÓ0Tò Ñ,�
˜E 9Ø—‘à&0Ø!&Ø%.ñõððV ˆôA œs 7›|Ó,ó b�Ø& u™~�áÑ(>Ü#$§=¡=Ø$×.Ñ.¨qÓ1Ø)¨%Ñ0Ø'Ø&+Ø"'ô$ð ñ$�Lð $0×#5Ñ#5°c¸3Ó#?�Là)¨"¨1Ð-�
Ø$ Q RÐ(�Ø �	á%Ü:=Ð>PÓ:QÐUVÒ:VÐ!3°AÒ!6Ð\nÐotÑ\u�JØ!×'Ñ'¨¨Ð,°×0BÑ0BÀ2À3Ð0GÒGÜ%&§]¡]Ø&×0Ñ0°Ó3Ø!-×!3Ñ!3°B°CÐ!8Ø!+Ø*/Ø&+ô&ð ñ&˜
ð ",¨q°5©y¸JÑ.FÑ!F× MÑ MÈcÐSVÓ W�IÜ #× 2Ñ 2°9ÄEÇKÁKÐW[Ô \�Ià—‘¨ZÀ%ÐV_Ñ`Öað=bð@ ˆùòg"s   Ã!AOrH  c                 óþ  ‡‡— t        |t        j                  «      r|j                  «       }t        |t        j                  «      r|j                  «       }|€|d u}|r|€t	        d«      ‚|�"t        |«      t        |«      k7  rt	        d«      ‚|�"t        |«      t        |«      k7  rt	        d«      ‚| j                  d   }| j                  d   }g Š|ri|D ]d  \  }}t        ||f||«      \  }	}
|	|k  r||	z
  dz  nd}|
|k  r||
z
  dz  nd}‰j                  |||t        |	|«      z   |t        |
|«      z   f«       Œf ‰ xs t        ˆfd„‰D «       «      }g Š|�|D �cg c]  }t        |«      ‘Œ c}Šn|�|D �cg c]  }t        |«      ‘Œ c}Š‰ xs t        ˆfd	„‰D «       «      }g }|rK|rI|r‰d   \  }}}}|d d …d d …||…||…f   }‰rt        j                  |‰d   d
dd¬«      }t        |«      }|S t        t        |«      «      D ]c  }||   }|r‰|   \  }}}}|d d …||…||…f   }‰r/t        j                  |j!                  d«      ‰|   d
dd¬«      d   }|j                  |«       Œe |S c c}w c c}w )Nz>`source_sizes` must be provided when `do_remove_padding=True`.zUMake sure that you pass in as many source sizes as the batch dimension of the outputszUMake sure that you pass in as many target sizes as the batch dimension of the outputsr@   r?   rz   r   c              3   ó.   •K  — | ]  }|‰d    k(  –— Œ y­wrU  r!   )rW  Úcropr
  s     €r#   rX  z<Sapiens2ImageProcessor._post_process_maps.<locals>.<genexpr>�  s   øè ø€ Ò*NÀ¨4°5¸±8Õ+;Ñ*Nùó   ƒc              3   ó.   •K  — | ]  }|‰d    k(  –— Œ y­wrU  r!   )rW  r÷   Úfinal_sizess     €r#   rX  z<Sapiens2ImageProcessor._post_process_maps.<locals>.<genexpr>”  s   øè ø€ Ò6fÐRV°t¸{È1¹~Õ7MÑ6fùrt  rS   Fr]  )r  r+   r  r,  r€   rñ   r÷   r   r  Úminrc  rV  ra   r  Úlistrð   r_   )rÏ   rH  r)  r  rD  Úmodel_heightÚmodel_widthÚoriginal_heightÚoriginal_widthÚ
new_heightÚ	new_widthÚpad_topÚpad_leftÚall_crops_equalr÷   Úall_final_sizes_equalrŠ   ÚtopÚleftÚbottomÚrightr¹   Úmap_itemr
  rv  s                          @@r#   rK  z)Sapiens2ImageProcessor._post_process_mapse  sé  ù€ ô �l¤E§L¡LÔ1Ø'×.Ñ.Ó0ˆLÜ�l¤E§L¡LÔ1Ø'×.Ñ.Ó0ˆLØÐ$Ø ,°DÐ 8ÐÙ Ð!5ÜÐ]Ó^Ð^àÐ#¬¨D«	´S¸Ó5FÒ(FÜÐtÓuÐuØÐ#¬¨D«	´S¸Ó5FÒ(FÜÐtÓuÐuà—y‘y Ñ*ˆØ—i‘i Ñ(ˆàˆÙØ3?ò Ñ/� Ü(KØ$ nÐ5°|À[ó)Ñ%�
˜Ið ?IÈ<Ò>W˜<¨*Ñ4¸Ò:Ð]^�Ø=FÈÒ=T˜K¨)Ñ3¸Ò9ÐZ[�Ø—‘àØ Ø¤# j°,Ó"?Ñ?Ø ¤3 y°+Ó#>Ñ>ð	õðð $˜)ÒN¤sÓ*NÈÔ*NÓ'NˆàˆØÐ#Ø3?Ö@¨4œ5 �;Ò@‰KØÐ%Ø3?Ö@¨4œ5 �;Ò@ˆKØ$/ Ò f´3Ó6fÐZeÔ6fÓ3fÐàˆÙÑ4á Ø+0°©8Ñ(��T˜6 5ØšAšq # f *¨d°5¨jÐ8Ñ9�áÜ—}‘}ØØ$ Q™Ø#Ø"'Ø#ô�ô ˜$“ZˆFð* ˆô% œs 4›yÓ)ò (�Ø ™;�á$Ø/4°U©|Ñ,�C˜˜v uØ'ª¨3¨v¨:°t¸E°zÐ(AÑB�HáÜ Ÿ}™}Ø ×*Ñ*¨1Ó-Ø(¨Ñ/Ø'Ø&+Ø"'ô ð ñ �Hð —‘˜hÕ'ð!(ð$ ˆùòU Aùâ@s   Å I5Å<I:)NN)N)FFN)NF)Né   NNN)NNN)3r   r   r   r   r   Úvalid_kwargsr   ÚBILINEARrø   r   rü   r   rý   r÷   Údefault_to_squarerú   rö   rù   rÜ   rÛ   r   rÿ   r   rÎ   r   r   rx  r’   r   rÔ   r   r   Ústrr   r   rì   rõ   r+   r  r   râ   rV  r'  ÚintÚdictrC  rM  rQ  rp  rK  Ú__classcell__)rÐ   s   @r#   rÉ   rÉ   '  sQ  ø„ á=à/€Là!×*Ñ*€HØ&€JØ$€IØ SÑ)€DØÐØ¨Ñ-€IØ€IØ€NØ€JØ€LØÐØ€Fð# Ð(DÑ!Eõ #ð ð 04Ø04ñ	NàðNð &¨Ñ,ðNð �D˜˜e™Ñ%Ñ&¨Ñ-ð	Nð
 Ð5Ñ6ðNð 
ôNó ðNð4 59ñ,Càð,Cð &¨Ñ,ð,Cð �D˜˜e™Ñ%Ñ&¨Ñ-ð	,Cð
 ð,Cð ,ð,Cð ˜jÑ(¨4Ñ/ð,Cð �c˜>Ð)Ñ*¨TÑ1ð,Cð 
ó,Cð\ 4¨Ñ#7ð ¸DÀÑ<Pó ð0 "'ØØ04ñ!9Nà�U—\‘\Ñ"ð9Nð ð9Nð ð	9Nð
 Lð9Nð ð9Nð ð9Nð ð9Nð ð9Nð ð9Nð ˜D ™KÑ'¨$Ñ.ð9Nð ˜4 ™;Ñ&¨Ñ-ð9Nð  ™+ð9Nð ð9Nð ð9Nð  �D˜˜e™Ñ%Ñ&¨Ñ-ð!9Nð$ 
ˆe�l‰lÑ	ó%9Nðx diñ@%Ø%)¨%¡[°4Ñ%7ð@%Ø\`ð@%à	Mó@%ðL ØØ"&ØBFØBFñLð �D˜˜e™Ñ%Ñ&ðLð
 ðLð ˜4‘<ðLð ! 4¨¨c°3¨h©Ñ#8Ñ8¸4Ñ?ðLð ! 4¨¨c°3¨h©Ñ#8Ñ8¸4Ñ?ðLð 
ˆd�4˜˜UŸ\™\Ð)Ñ*Ñ+Ñ	,óLðb CGØBFØ)-ñ";ð ! 4¨¨c°3¨h©Ñ#8Ñ8¸4Ñ?ð";ð ! 4¨¨c°3¨h©Ñ#8Ñ8¸4Ñ?ð	";ð
   $™;ð";ð 
ˆd�3˜Ÿ™Ð$Ñ%Ñ	&ó";ðN CGØBFØ)-ñ#<ð ! 4¨¨c°3¨h©Ñ#8Ñ8¸4Ñ?ð#<ð ! 4¨¨c°3¨h©Ñ#8Ñ8¸4Ñ?ð	#<ð
   $™;ð#<ð 
ˆd�3˜Ÿ™Ð$Ñ%Ñ	&ó#<ðP CGØ)-ñ	Gð ! 4¨¨c°3¨h©Ñ#8Ñ8¸4Ñ?ðGð   $Ñ&ð	Gð
 
ˆd�3˜Ÿ™Ð$Ñ%Ñ	&óGðRVà�l‰lðVð ! 4¨¨c°3¨h©Ñ#8Ñ8¸4Ñ?ðVð ! 4¨¨c°3¨h©Ñ#8Ñ8¸4Ñ?ð	Vð
   $™;ðVð 
ˆe�l‰lÑ	÷Vr"   rÉ   )g      ô?)rˆ  )0Útypingr   r+   Útorch.nn.functionalÚnnr   ra   Útorchvision.transforms.v2rƒ   Ú&transformers.image_processing_backendsr   Úimage_processing_outputsr   Úimage_processing_utilsr   Úimage_transformsr	   r
   Úimage_utilsr   r   r   r   r   r   r   r   Úprocessing_utilsr   r   Úutilsr   r   r   r   r3   r6   r  rV  r�  r’   rH   rv   r�   rž   rÇ   rÉ   Ú__all__r!   r"   r#   ú<module>rœ     s¡  ðõ& ã ß Ð Ý 7å Eå OÝ 2ß E÷	÷ 	ó 	÷ 5ß CÑ Cô <°uõ ò"ò"ð ñ#Ø�<‰<ð#à�s˜C�x‘ð#ð ð#ð ˆ5�<‰<˜Ÿ™Ð%Ñ&ó	#ðT ñ	<Ø�<‰<ð<à�<‰<ð<ð �s˜C�x‘ð<ð ð	<ð
 ‡\�\ó<ñ~)¨¯©ð )¸sð )ÈEÏLÉLó )ð< u§|¡|ð ¸¸e¿l¹lÈEÏLÉLÐ>XÑ8Yó ð4 NPñE?Ø�|‰|ðE?Ø',§|¡|ðE?ØGJðE?à
‡\�\óE?ðP ôS
Ð/ó S
ó ðS
ðl $Ð
$�r"   