
    3j%                         S SK Jr  S SKJr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  / S	Qr " S
 S\5      r " S S5      r " S S\R(                  5      rg)    )Enum)DictListOptionalN)nn)nms)YuNet)PriorBox)decode)FaceDetectorFaceDetectorResultFaceKeypointc                   ,    \ rS rSrSrSrSrSrSrSr	Sr
g	)
r       zcDefine the keypoints detected in a face.

The left/right convention is based on the screen viewer.
r                N)__name__
__module____qualname____firstlineno____doc__EYE_LEFT	EYE_RIGHTNOSE
MOUTH_LEFTMOUTH_RIGHT__static_attributes__r       W/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/contrib/face_detection.pyr   r       s"    
 HIDJKr!   r   c                      \ rS rSrSrS\R                  SS4S jrSS\\R                     S\\R                     SS 4S	 jjr\S\R                  4S
 j5       r\S\R                  4S j5       r\S\R                  4S j5       r\S\R                  4S j5       rS\S\R                  4S jr\S\R                  4S j5       r\S\R                  4S j5       r\S\R                  4S j5       r\S\R                  4S j5       r\S\R                  4S j5       r\S\R                  4S j5       r\S\R                  4S j5       rSrg)r   -   zEncapsulate the results obtained by the :py:class:`kornia.contrib.FaceDetector`.

Args:
    data: the encoded results coming from the feature detector with shape :math:`(14,)`.

datareturnNc                 `    [        U5      S:  a  [        SUR                   S35      eXl        g )N   z.Result must comes as vector of size(14). Got: .)len
ValueErrorshape_data)selfr%   s     r"   __init__FaceDetectorResult.__init__5   s,    t9r>Mdjj\YZ[\\
r!   devicedtypec                 B    U R                   R                  XS9U l         U $ )z)Like :func:`torch.nn.Module.to()` method.r1   r2   )r-   to)r.   r1   r2   s      r"   r5   FaceDetectorResult.to:   s    ZZ]]&]>
r!   c                      U R                   S   $ )z'The bounding box top-left x-coordinate..r   r-   r.   s    r"   xminFaceDetectorResult.xmin?        zz&!!r!   c                      U R                   S   $ )z'The bounding box top-left y-coordinate..r   r9   r:   s    r"   yminFaceDetectorResult.yminD   r=   r!   c                      U R                   S   $ )z+The bounding box bottom-right x-coordinate.).r   r9   r:   s    r"   xmaxFaceDetectorResult.xmaxI   r=   r!   c                      U R                   S   $ )z+The bounding box bottom-right y-coordinate.).r   r9   r:   s    r"   ymaxFaceDetectorResult.ymaxN   r=   r!   keypointc                    U[         R                  :X  a  U R                  S   nU$ U[         R                  :X  a  U R                  S   nU$ U[         R                  :X  a  U R                  S   nU$ U[         R
                  :X  a  U R                  S   nU$ U[         R                  :X  a  U R                  S   nU$ [        SU S35      e)zsGet the [x y] position of a given facial keypoint.

Args:
    keypoint: the keypoint type to return the position.

).)r      ).)      ).)   	   ).)
      ).)      zNot valid keypoint type. Got: r)   )r   r   r-   r   r   r   r   r+   )r.   rH   outs      r"   get_keypointFaceDetectorResult.get_keypointS   s     |,,,**[)C 
 ///**[)C 
 *****[)C 
 000**]+C
 
	 111**]+C 
 =hZqIJJr!   c                      U R                   S   $ )zThe detection score.).   r9   r:   s    r"   scoreFaceDetectorResult.scoreh   s     zz'""r!   c                 4    U R                   U R                  -
  $ )zThe bounding box width.)rC   r;   r:   s    r"   widthFaceDetectorResult.widthm        yy499$$r!   c                 4    U R                   U R                  -
  $ )zThe bounding box height.)rF   r@   r:   s    r"   heightFaceDetectorResult.heightr   r]   r!   c                      U R                   S   $ )BThe [x y] position of the top-left coordinate of the bounding box.).)r   r   r9   r:   s    r"   top_leftFaceDetectorResult.top_leftw        zz+&&r!   c                 L    U R                   nUS==   U R                  -  ss'   U$ )rb   r8   )rc   r[   r.   rS   s     r"   	top_rightFaceDetectorResult.top_right|   s$     mmFtzz!
r!   c                      U R                   S   $ )zFThe [x y] position of the bottom-right coordinate of the bounding box.).)r   r   r9   r:   s    r"   bottom_rightFaceDetectorResult.bottom_right   re   r!   c                 L    U R                   nUS==   U R                  -  ss'   U$ )rb   r?   )rc   r_   rg   s     r"   bottom_leftFaceDetectorResult.bottom_left   s$     mmFt{{"
r!   r9   )NN)r   r   r   r   r   torchTensorr/   r   r1   r2   r5   propertyr;   r@   rC   rF   r   rT   rX   r[   r_   rc   rh   rk   rn   r    r   r!   r"   r   r   -   s   U\\ d 
%,,/ x?T `t 
 "ell " " "ell " " "ell " " "ell " "\ ell * #u|| # # %u|| % % % % % '%,, ' ' 5<<   'ell ' ' U\\  r!   r   c                     ^  \ rS rSrSr SS\S\S\S\SS4
U 4S	 jjjrS
\R                  S\R                  4S jr
S\\\R                  4   S\S\S\\R                     4S jrS
\R                  S\\R                     4S jrSrU =r$ )r      a  Detect faces in a given image using YuNet model.

This is a high-level API that wraps the :py:class:`kornia.models.YuNet` model for face detection.
By default, it uses the method described in :cite:`facedetect-yu`.

Args:
    top_k: the maximum number of detections to return before the nms.
    confidence_threshold: the threshold used to discard detections.
    nms_threshold: the threshold used by the nms for iou.
    keep_top_k: the maximum number of detections to return after the nms.

Return:
    A list of B tensors with shape :math:`(N,15)` to be used with :py:class:`kornia.contrib.FaceDetectorResult`.

Example:
    >>> img = torch.rand(1, 3, 320, 320)
    >>> detect = FaceDetector()
    >>> res = detect(img)

top_kconfidence_thresholdnms_threshold
keep_top_kr&   Nc                   > [         TU ]  5         Xl        X l        X0l        X@l        S/ SQSS/SS// SQ// SQS	S
/SS.U l        / SQSS/SS// SQ/U l        / SQU l        S	S
/U l	        SU l
        [        SSS9U l        [        U l        g )Nr	   )rO         r   0   @   `   )         )rM   rz   r   r}   g?g?F)name	min_sizesstepsvariancecliptestT)
pretrained)superr/   ru   rv   rw   rx   configr   r   r   r   r	   model
nms_korniar   )r.   ru   rv   rw   rx   	__class__s        r"   r/   FaceDetector.__init__   s     	
$8!*$&R2r(OL$c

 'R2r(OL$
c
	6d3
r!   imagec                     U$ )a  Preprocess input images before feeding them to YuNet.

Args:
    image: Batch of RGB images with shape :math:`(B, 3, H, W)`, where:
        - ``B`` is batch size,
        - ``3`` is the RGB channel dimension,
        - ``H`` is image height,
        - ``W`` is image width.

Returns:
    The preprocessed image tensor. The current implementation returns the
    input unchanged and acts as an extension point for custom pipelines.
r   )r.   r   s     r"   
preprocessFaceDetector.preprocess   s	     r!   r%   r_   r[   c                    US   US   US   pen[         R                  " X2X2X2X2X2X2X2/UR                  UR                  S9n[	        U R
                  U R                  U R                  X#4S9nUR                  UR                  UR                  5      n/ n	[        UR                  S   5       GH  n
[        XJ   U" 5       U R                  5      nX-  nXZSS2S4   XjSS2S4   pXR                  S	S
5      -  R                  5       nXR                  :  nX   X   pUR!                  SS9S   SU R"                   nUU   UU   p[         R$                  " XSS2S4   4SS9nU R'                  USS2SS24   XR(                  5      n[+        U5      S:  a	  UUSS24   nU	R-                  USU R.                   5        GM     U	$ )aw  Decode model outputs into filtered face detections.

Args:
    data: Raw output dictionary from YuNet containing ``loc``, ``conf``,
        and ``iou`` tensors.
    height: Input image height used to scale decoded box coordinates.
    width: Input image width used to scale decoded box coordinates.

Returns:
    A list with one tensor per batch element. Each tensor is shaped
    :math:`(N, 15)`, where ``N`` is the number of detections kept after
    confidence filtering and non-maximum suppression (NMS).

    The 15 values per detection are:
    - 14 geometry values (bounding box + five keypoints),
    - 1 confidence score.
locconfiour4   )
image_sizer   Nr   g        g      ?T)
descending)dimr   )rp   tensorr1   r2   r
   r   r   r   r5   ranger,   _decoder   clampsqrtrv   sortru   catr   rw   r*   appendrx   )r.   r%   r_   r[   r   r   r   scalepriorsbatched_dets
batch_elemboxes
cls_scores
iou_scoresscoresindsorderdetskeeps                      r"   postprocessFaceDetector.postprocess   s   $ ed6lDK3E5%X]glu::))
 $..$**diiVO\3::syy1+-		!-JCOVXt}}EEME &*a*:%;SQPQAQ=R
 #3#3C#==CCEF 555D!K6 KK4K03LdjjAE!%L&-6 99eAtG_52>D88E!RaR%L&2D2DED4y1}D!G} %6t 781 .2 r!   c                     U R                  U5      nU R                  U5      nU R                  X2R                  S   UR                  S   5      $ )zDetect faces in a given batch of images.

Args:
    image: batch of images :math:`(B,3,H,W)`

Return:
    List[torch.Tensor]: list with the boxes found on each image. :math:`Bx(N,15)`.

r   )r   r   r   r,   )r.   r   imgrS   s       r"   forwardFaceDetector.forward  sB     ooe$jjoYYr]CIIbMBBr!   )r   rv   r   rx   r   r   r   rw   r   ru   r   )i  333333?r   i  )r   r   r   r   r   intfloatr/   rp   rq   r   r   strr   r   r   r    __classcell__)r   s   @r"   r   r      s    , sv7<SXlo	 ,   7S%,,%6 7 7 7S 7UYZ_ZfZfUg 7rCU\\ Cd5<<.@ C Cr!   r   )enumr   typingr   r   r   rp   r   kornia.geometry.bboxr   r   kornia.models.yunetr	   kornia.models.yunet.processorsr
   r   r   __all__r   r   Moduler   r   r!   r"   <module>r      sT   $  ' '   2 % 3 <
@
4 
` `FAC299 ACr!   