
    ^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>    "JAq!Q
qAEQU$A;;qb!!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    "JAq!Q
cAg+S1WQ0A;;qb!!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%Hhv[[(H-26F7?LW$M"-M<-/LKK	4	4i@\<,#>?RH]\1=Ar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<.3kk+L,(KQXYMFEr*Hh %R 0Ka;.Gq L0G--1C7K^^]%--M\u||LNFF
 GAtTM**Xatm-DDs[YZ\`bfYfMgGggDGAtTM**Xatm-DDs\Z[]acgZgMhGhhDKKt{Q7#=sTz\\]M]?^ad?dekmnEAI[[L-V[VbVbjojujuvF q!H#Z0K<2KL 
d88:==
BB7d$"F4L 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#    zQ&A+MNNFQJ#%)*S0EqjQF==V=,L UU8ffff=JVYZFVV4DUTYN[GQww67F;;6;*L\A-|U__\=Z[JKKq(,*CU__UaEbcEE!T4-(((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<>>0K=__F##KCL2&F$$$,J-446K.557Ks"[+.B7

K2D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.
    gMbP?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)}4nmTVfgk=.-H  ~~dD)--/HeeHlEO(002"Q&M 1$L#l2O!O3MaAg##%)Yq!Qw-?-D-D-F-Jl,ZZEOell=W\WaWa&bcgijcj&kkkEMELLVSXS]S]$^_`bf_f$gggEOOBE q  1$%  NfMkMkMm 9I"b6R$UV^44N  t,~e/DDEJt,~e/DDEJ%N4,@(@@>RWCXXJ%N4,@(@@>RWCXXJt

	

	 
	 
		
 
	  
	  
 	!	J ++j&&
'
+
+Cc!Jc!Jz)J,CCKZ'*z*AA[PHj(:
+BBkQHuyy(H!52>>>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Mf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 +' (.{{}$$++URW,XY*:$*:*: +2+6N+' 3N+. +221588E+' + 9D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3KEKK
ELLEKKX]XdXd,eglmEAIEKKell3ekkZ_ZfZf.ginoEF3K	  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=9KE&)&%&8 b"{**5US1%,,{RWR_R_hmhtht2uv_U,T_`ab FI&&v.F/DV^n/o,,!#%3%9%9%; 		;!E>(0DNVZ[bVc(d%%)[[ART\%]N%)%5%5nd%KN%)[[x%PN,:"5)		; ((>@TU/D^fv/w,,#% %3%9%9%; 	=!E>!%!1!1.)!L!77
NL*V_N /=$U+	= 68L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[) 	!"3K))a)0|C7Hzin"  .a077A7>%,,;.:SabcSde	 }}}+H
 v||A/	%  8*21+fUViX%! % *CX$Y4T%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 lELL1'..0LlELL1'..0LZ
#(<`aa#
c,6G(Gghh#
c,6G(Gghh##& ?#;#;;q@HJR..G=.-! %&1B&& ||$)A[[AcSASA_e
 >># %=X$F!z:#h

 /|,499X;NPTPYPYZaPb:c
 ||]Q%68J$KSXS`S`iopL(6!T1*+==4QR
@SSVY\bcdfjlmcm\nVnn 	 %\2	#(@#ll Y\\hjvXw T57T| "L0--2OP mmO  %yy (-Z'8# $K0::1=DDS{I[E\^_`  *,<QaZ,HHM!$4Q_$EEI!"34 	L%l3I-F\\-?F$	)%dO	!!'6VU^_kUlm	    	2K#&{#3 MM.I]9]^_11M	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:0yE>)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    < ++goo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    : %%	>>%!GNN1at3C$DDI))Ldu * 
 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  }rt        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}rt              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$K5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
JTJRS!%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   : lELL1'..0L((..q1
$$++##))#S.. r  ".!5 "
 =
>J=
 :
  "1+>O?#q(S-AZ-O q 
 )8"$ ""3<<0@#AVZ[^^_ef" *<%; &
s C
XjC
 @
" ))W00'..AqI#(>Q0Kmm #G mmC-G!&@!!RaR%.KQU^F*,J!"[[);<
##BC(GMM"#,>>!""$]]23/'&+"'"J *QZ:,EELLSRUV
 //
%++UYZ
03K0T ,
E9&0!&%.V A s7|, b&u~(>#$==$..q1)%0'&+"'$ $L $0#5#5c3#?L)"1-
$QR( 	%:=>P:QUV:V!3A!6\not\uJ!'',0B0B230GG%&]]&003!-!3!3BC!8!+*/&+& &
 ",q5yJ.F!F M McSV WI # 2 29EKKW[ \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45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     6fRVt{1~7M6frt  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    lELL1'..0LlELL1'..0L$ ,D 8!5]^^#D	S5F(Ftuu#D	S5F(Ftuuyy*ii(3? /(K$n5|[)%
I ?I<>W<*4:]^=F=TK)39Z[ #j,"?? 3y+#>>	 $)Ns*N*N'N#3?@45;@K%3?@45;@K$/ f36fZe6f3f4 +08(T65Aq#f*d5j89}}$Q#"'# $ZF* % s4y) (;$/4U|,Cvu'3v:tEz(ABH }} **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 #  0404	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$ 
ell	%9Nx di@%%)%[4%7@%\`@%	M@%L "&BFBFL De%&L
 L 4<L !4c3h#884?L !4c3h#884?L 
d4U\\)*+	,Lb CGBF)-"; !4c3h#884?"; !4c3h#884?	";
  $;"; 
d3$%	&";N CGBF)-#< !4c3h#884?#< !4c3h#884?	#<
  $;#< 
d3$%	&#<P CG)-	G !4c3h#884?G  $&	G
 
d3$%	&GRVllV !4c3h#884?V !4c3h#884?	V
  $;V 
ell	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 "" #<<#sCx# # 5<<%&	#T 	<<<<<<< sCx< 	<
 \\<~) )s )ELL )<u|| ellELL>X8Y 4 NPE?||E?',||E?GJE?
\\E?P S
/ S
 S
l $
$r"   