Ë
    ëÓ^jB  ã                  óŠ   — d Z ddlmZ ddlmZ ddlmZ ddlZddl	m
Z
 ddlmZ dd„Zd	d
œ	 	 	 	 	 dd„Zdd	dœ	 	 	 	 	 	 	 dd„Zy)zIPrivate keypoint visualization helpers for RF-DETR demos and diagnostics.é    )Úannotations)ÚPath)ÚAnyN)Ú	KeyPoints)Ú&precision_cholesky_to_pixel_covariancec           
     óª  — t        | j                  j                  «       | j                  �| j                  j                  «       nd| j                  �| j                  j                  «       nd| j
                  �| j
                  j                  «       nd| j                  �| j                  j                  «       ndt        | j                  «      ¬«      S )z<Return a mutable copy of a Supervision ``KeyPoints`` object.N)ÚxyÚkeypoint_confidenceÚdetection_confidenceÚvisibleÚclass_idÚdata)	r   r	   Úcopyr
   r   r   r   Údictr   )Ú
key_pointss    úe/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/rfdetr/visualize/keypoints.pyÚ_copy_key_pointsr      s®   € äØ�=‰=×ÑÓà5?×5SÑ5SÐ5_ˆJ×*Ñ*×/Ñ/Ô1Ðeið 7A×6UÑ6UÐ6aˆJ×+Ñ+×0Ñ0Ô2Ðgkà-7×-?Ñ-?Ð-K�
×"Ñ"×'Ñ'Ô)ÐQUØ/9×/BÑ/BÐ/N�×$Ñ$×)Ñ)Ô+ÐTXÜ�*—/‘/Ó"ôð ó    g        )Úkeypoint_thresholdc               ó¢  — t        | «      } t        | «      dk(  r| S | j                  }|€t        d«      ‚| j                  j                  d«      }| j                  j                  d«      }d| j                  vr«|�©|�§t        j                  |t        j                  ¬«      }t        j                  |t        j                  ¬«      }|j                  dd | j                  j                  dd k(  r4|j                  t        | «      dfk(  rt        ||¬	«      | j                  d<   ||k\  }| j                  }t        | d
|€|«       | S ||z  «       | S )aÍ  Build ``KeyPoints`` for visualization from RF-DETR keypoint predictions.

    Args:
        key_points: RF-DETR keypoint prediction output.
        keypoint_threshold: Per-keypoint confidence threshold used to hide
            low-confidence points through ``key_points.visible``.

    Returns:
        A Supervision ``KeyPoints`` object ready for keypoint annotators.

    Raises:
        ValueError: If keypoints are present without per-point confidence.
    r   Nz>Expected RF-DETR keypoints to include per-keypoint confidence.Úkeypoint_precision_choleskyÚsource_shapeÚ
covariance)Údtypeé   )Úprecision_choleskyr   r   )r   Úlenr
   Ú
ValueErrorr   ÚgetÚnpÚasarrayÚfloat32Úshaper	   r   r   Úsetattr)	r   r   r
   Úraw_precisionÚraw_source_shapeÚ	precisionr   r   Úexisting_visibles	            r   Ú_key_points_for_displayr)   #   sF  € ô$ " *Ó-€Jä
ˆ:ƒ˜!ÒØÐØ$×8Ñ8ÐØÐ"ÜÐYÓZÐZà—O‘O×'Ñ'Ð(EÓF€MØ!—‘×*Ñ*¨>Ó:ÐØ˜:Ÿ?™?Ñ*¨}Ð/HÐM]ÐMiÜ—J‘J˜}´B·J±JÔ?ˆ	Ü—z‘zÐ"2¼"¿*¹*ÔEˆØ�?‰?˜2˜AÐ *§-¡-×"5Ñ"5°b°qÐ"9Ò9¸l×>PÑ>PÔUXÐYcÓUdÐfgÐThÒ>hÜ,RØ#,Ø)ô-ˆJ�O‰O˜LÑ)ð
 "Ð%7Ñ7€GØ!×)Ñ)ÐÜˆJ˜	Ð.>Ð.F 7ÔgØÐð M]Ð_fÑLfÔgØÐr   )Úimager   c               óä  — | j                   }|€g S |�t        |«      j                  nd}| j                  }|�|n||k\  }| j                  }| j
                  j                  d«      }g }	t        | j                  «      D ]ð  \  }
}t        t        |||
   ||
   d¬«      «      D ]Ê  \  }\  }}}|rt        j                  |d«      rŒ#d}t        |t        j                  «      r|
t        |«      k  rt        ||
   «      }|	j!                  ||
| j"                  �t%        | j"                  |
   «      nd||�t'        ||
   «      nd|t'        |d   «      t'        |d   «      t'        |«      dœ	«       ŒÌ Œò |	S )aà  Build flat keypoint prediction rows for notebook or terminal display.

    Args:
        key_points: RF-DETR keypoint prediction output.
        image: Optional image identifier added to each output row. Paths are
            represented by their file name.
        keypoint_threshold: Per-keypoint confidence threshold used when
            ``key_points.visible`` is not already populated.

    Returns:
        A list of row dictionaries, one per visible/non-zero keypoint.
    NÚ
class_nameT)Ústrictr   é   )	r*   Údetection_indexr   r,   r   Úkeypoint_indexÚxÚyr
   )r
   r   Únamer   r   r   r   Ú	enumerater	   Úzipr    ÚallcloseÚ
isinstanceÚndarrayr   ÚstrÚappendr   ÚintÚfloat)r   r*   r   r
   Ú
image_namer(   r   r   Úclass_namesÚrecordsr/   r	   r0   ÚpointÚ
confidenceÚ
is_visibler,   s                    r   Ú_keypoint_prediction_recordsrC   N   sŠ  € ð$ %×8Ñ8ÐØÐ"Øˆ	à%*Ð%6”�e“×!Ò!¸D€JØ!×)Ñ)ÐØ"2Ð">ÑÐDWÐ[mÑDm€GØ%×:Ñ:ÐØ—/‘/×%Ñ% lÓ3€KØ$&€GÜ(¨¯©Ó7ò Ñˆ˜Ü?HÜ�Ð'¨Ñ8¸'À/Ñ:RÐ[_Ô`ó@
ò 	Ñ;ˆNÑ;˜U J°
ñ ¤§¡¨U°AÔ!6ØØˆJÜ˜+¤r§z¡zÔ2°ÌÈ[ÓIYÒ7YÜ  ¨_Ñ!=Ó>�
Ø�N‰Nà'Ø'6ØMW×M`ÑM`ÐMl¤ J×$7Ñ$7¸Ñ$HÔ IÐrvØ",àH\ÐHhœÐ2°?ÑCÔDÐnrà&4Ü˜u Q™x›Ü˜u Q™x›Ü+0°Ó+<ñõñ	ðð0 €Nr   )r   r   Úreturnr   )r   r   r   r<   rD   r   )r   r   r*   zstr | Path | Noner   r<   rD   zlist[dict[str, Any]])Ú__doc__Ú
__future__r   Úpathlibr   Útypingr   Únumpyr    Úsupervisionr   Úrfdetr.utilities.keypointsr   r   r)   rC   © r   r   ú<module>rM      s|   ðñ På "å Ý ã Ý !å Móð& !$ñ(Øð(ð ð(ð ó	(ð\  $Ø #ñ	4Øð4ð ð4ð ð	4ð
 ô4r   