
    ^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?5S5S5_J**//1ei 7A6U6U6aJ++002gk-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OO''(EFM!**>::??*}/HM]MiJJ}BJJ?	zz"2"**E??2A*--"5"5bq"99l>P>PUXYcUdfgTh>h,R#,)-JOOL)
 "%77G!))J	.>.F7g M]_fLf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!!DJ!))"2">DW[mDmG%:://%%l3K$&G(7 ?H'8'/:R[_`@
 	;N;UJ
 UA!6J+rzz2[IY7Y _!=>
NN''6MWM`M`MlJ$7$7$H Irv",H\Hh2?CDnr&4uQxuQx+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   