§
    ­¸j^"  ã                  óf   — d Z ddlmZ ddlmZ ddlZddlmZ ddgZ	dd„Z
dd„Zdd„Zdd„Zdd„ZdS )zAKeypoint utility functions shared by inference and visualization.é    )Úannotations)ÚAnyN)ÚNDArrayÚschemas_semantically_equalÚ&precision_cholesky_to_pixel_covarianceÚschemaú	list[int]ÚreturnÚboolc                ó8   — t          | ¦  «        o| d         dk    S )a‹  Return True if *schema* uses a background-first layout.

    A background-first schema has a leading slot with zero keypoints that
    acts as a background/no-keypoint sentinel, e.g. ``[0, 17]`` where slot 0
    is background and slot 1 is person with 17 keypoints.  Active-first
    schemas omit that slot entirely, e.g. ``[17]``.

    Args:
        schema: Keypoints-per-class list.

    Returns:
        ``True`` when ``schema`` is non-empty and its first element is zero.

    Examples:
        >>> _is_bg_first_schema([0, 17])
        True
        >>> _is_bg_first_schema([17])
        False
        >>> _is_bg_first_schema([])
        False
    r   )r   ©r   s    új/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/utilities/keypoints.pyÚ_is_bg_first_schemar      s   € õ, �‰<Œ<Ð*˜F 1œI¨šNÐ*ó    c                óR   — t          | ¦  «        r
| dd…         S t          | ¦  «        S )a¸  Strip the leading background slot from a bg-first schema.

    Always returns a new list. A no-op (copy) when *schema* is already
    active-first or empty. Only the first leading zero slot is removed;
    schemas with multiple leading zeros (e.g. ``[0, 0, 17]``) retain all
    but the first.

    Args:
        schema: Keypoints-per-class list.

    Returns:
        Schema with the leading zero-keypoint slot removed when bg-first,
        or a copy of *schema* otherwise.

    Examples:
        >>> _to_active_first([0, 17])
        [17]
        >>> _to_active_first([17])
        [17]
        >>> _to_active_first([0, 17, 4])
        [17, 4]
        >>> _to_active_first([0])
        []
    é   N©r   Úlistr   s    r   Ú_to_active_firstr   .   s.   € õ2 ˜6Ñ"Ô"ð Ø�a�b�bŒzÐÝ�‰<Œ<Ðr   c                óh   — t          | ¦  «        s| st          | ¦  «        S dgt          | ¦  «        z   S )a»  Prepend a background slot to an active-first schema.

    A no-op when *schema* already starts with a zero-keypoint slot or is empty.

    Args:
        schema: Keypoints-per-class list.

    Returns:
        Schema with a leading ``0`` prepended when not already bg-first.

    Examples:
        >>> _to_bg_first([17])
        [0, 17]
        >>> _to_bg_first([0, 17])
        [0, 17]
        >>> _to_bg_first([17, 4])
        [0, 17, 4]
    r   r   r   s    r   Ú_to_bg_firstr   L   s:   € õ& ˜6Ñ"Ô"ð ¨&ð Ý�F‰|Œ|ÐØˆ3•�f‘”ÑÐr   ÚaÚbc                óB   — t          | ¦  «        t          |¦  «        k    S )as  Return True if *a* and *b* represent the same keypoint structure.

    Two schemas are semantically equal when they encode identical active
    keypoint counts per class after stripping any leading background slot.
    This allows ``[0, 17]`` (bg-first) and ``[17]`` (active-first) to
    compare equal.

    Args:
        a: First keypoints-per-class list.
        b: Second keypoints-per-class list.

    Returns:
        ``True`` when both schemas reduce to the same active-first form.

    Note:
        A schema of ``[0]`` (one detection-only background slot) is semantically
        equal to ``[]`` (no schema) because both reduce to an empty active-first
        form.  Callers that need to distinguish "no schema" from "bg-first with no
        active slots" should compare ``to_active_first(a)`` directly or check
        ``bool(schema)`` before calling this function.

        Use this function for user-facing validation where representational form
        does not matter.  Use exact ``!=`` comparison when preserving
        representational form is required (e.g. auto-align in checkpoint loading).

    Examples:
        >>> schemas_semantically_equal([0, 17], [17])
        True
        >>> schemas_semantically_equal([17], [17])
        True
        >>> schemas_semantically_equal([0, 17], [0, 33])
        False
        >>> schemas_semantically_equal([0], [])
        True
    )r   )r   r   s     r   r   r   d   s!   € õH ˜AÑÔÕ"2°1Ñ"5Ô"5Ò5Ð5r   Úprecision_choleskyúNDArray[np.floating[Any]]Úsource_shapeúNDArray[np.float32]c                ó  — | j         dk    s| j        d         dk    rt          d| j        › d�¦  «        ‚|j        | j        d         dfk    r&t          d| j        d         › d|j        › d�¦  «        ‚|                      t          j        d¬	¦  «        }|d
         }|d         }|d         }t	          j        |¦  «                             d¬¦  «        }t	          j        |¦  «        }t	          j        |¦  «        }t	          j	        ddd¬¦  «        5  d||z  |z  |z  z  }	|	||z  ||z  z   z  }
|	| |z  z  dz   }|	||z  z  }|dd…dd…f                              t          j        ¦  «        }|dd…dd…f                              t          j        ¦  «        }||z  |
z  }||z  |z  }||z  |z  }ddd¦  «         n# 1 swxY w Y   |t	          j        |¦  «        z  t	          j        |¦  «        z  t	          j        |¦  «        z  }t	          j
        t          j        ¦  «        }t	          j        g | j        dd…         ¢d‘d‘R |t          j
        ¬¦  «        }t	          j        ||                     t          j
        ¦  «        |¦  «        |d<   t	          j        ||                     t          j
        ¦  «        |¦  «        |d<   |d         |d<   t	          j        ||                     t          j
        ¦  «        |¦  «        |d<   |S )a.  Convert RF-DETR keypoint precision parameters into pixel covariances.

    The keypoint head predicts lower-triangular precision-Cholesky parameters in
    normalized image coordinates. This helper inverts those precision matrices
    and scales them to pixel coordinates for Supervision covariance annotators.

    Args:
        precision_cholesky: Lower-triangular precision parameters with shape
            ``(N, K, 3)``. Each triplet is ``(log_l11, l21, log_l22)``.
        source_shape: Per-detection ``(height, width)`` rows with shape
            ``(N, 2)``.

    Returns:
        Pixel-space covariance matrices with shape ``(N, K, 2, 2)``.

    Raises:
        ValueError: If ``precision_cholesky`` or ``source_shape`` has an
            incompatible shape.

    Example:
        >>> precision = np.array([[[0.0, 0.0, 0.0]]], dtype=np.float32)
        >>> shape = np.array([[10.0, 20.0]], dtype=np.float32)
        >>> precision_cholesky_to_pixel_covariance(precision, shape)[0, 0]
        array([[400.,   0.],
               [  0., 100.]], dtype=float32)
    é   é   z2precision_cholesky must have shape (N, K, 3), got ú.r   zsource_shape must have shape (z
, 2), got F)Úcopy).r   ).r   ).r!   éÿÿÿÿ)ÚaxisÚignore)ÚdivideÚinvalidÚoverg      ð?g        Nr   )Údtype).r   r   ).r   r   ).r   r   ).r   r   )ÚndimÚshapeÚ
ValueErrorÚastypeÚnpÚfloat64ÚisfiniteÚallÚexpÚerrstateÚfloat32ÚnanÚfullÚwhere)r   r   Úprecision_cholesky_f64Úlog_l11Úl21Úlog_l22Úfinite_inputÚl11Úl22Úinv_detÚcov00Úcov01Úcov11ÚheightÚwidthÚpx00Úpx01Úpx11Ú
finite_allr6   Úcovariancess                        r   r   r   ‹   sC  € ð< Ô !Ò#Ð#Ð'9Ô'?ÀÔ'BÀaÒ'GÐ'GÝÐiÐN`ÔNfÐiÐiÐiÑjÔjÐjØÔÐ0Ô6°qÔ9¸1Ð=Ò=Ð=ÝÐvÐ:LÔ:RÐSTÔ:UÐvÐvÐamÔasÐvÐvÐvÑwÔwÐwð( 0×6Ò6µr´zÈÐ6ÑNÔNÐØ$ VÔ,€GØ
  Ô
(€CØ$ VÔ,€Gå”;Ð5Ñ6Ô6×:Ò:ÀÐ:ÑCÔC€Lå
Œ&�‰/Œ/€CÝ
Œ&�‰/Œ/€Cõ
 
Œ˜H¨h¸XÐ	FÑ	FÔ	Fð 'ð 'Ø˜˜s™ S™¨3Ñ.Ñ/ˆØ˜3 ™9 s¨S¡yÑ0Ñ1ˆð ˜C˜4 #™:Ñ&¨Ñ,ˆØ˜3 ™9Ñ%ˆð ˜a˜a˜a  1 ˜fÔ%×,Ò,­R¬ZÑ8Ô8ˆØ˜Q˜Q˜Q  ! ˜VÔ$×+Ò+­B¬JÑ7Ô7ˆà�u‰}˜uÑ$ˆØ�v‰~ Ñ%ˆØ˜‰ Ñ&ˆð'ð 'ð 'ñ 'ô 'ð 'ð 'ð 'ð 'ð 'ð 'øøøð 'ð 'ð 'ð 'ð" ¥¤¨DÑ 1Ô 1Ñ1µB´KÀÑ4EÔ4EÑEÍÌÐTXÑHYÔHYÑY€JÝ
Œ*•R”VÑ
Ô
€Cå”'Ð?Ð.Ô4°R°a°RÔ8Ð?¸!Ð?¸QÐ?Ð?ÀÍBÌJÐWÑWÔW€KÝœX j°$·+²+½b¼jÑ2IÔ2IÈ3ÑOÔO€K�	ÑÝœX j°$·+²+½b¼jÑ2IÔ2IÈ3ÑOÔO€K�	ÑØ(¨Ô3€K�	ÑÝœX j°$·+²+½b¼jÑ2IÔ2IÈ3ÑOÔO€K�	ÑØÐs   ÄBF>Æ>GÇG)r   r	   r
   r   )r   r	   r
   r	   )r   r	   r   r	   r
   r   )r   r   r   r   r
   r   )Ú__doc__Ú
__future__r   Útypingr   Únumpyr/   Únumpy.typingr   Ú__all__r   r   r   r   r   © r   r   ú<module>rR      sÔ   ðð HÐ Gà "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð à Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  ð !Ø,ð€ð+ð +ð +ð +ð2ð ð ð ð<ð ð ð ð0$6ð $6ð $6ð $6ðN[ð [ð [ð [ð [ð [r   