
    3j                        S SK Jr  S SKrS SKrS SKr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Jr  S SKJr  S SKJr  S S	KJr  S S
KJr  S/r\R4                  " \5      r " S S5      rg)    )annotationsN)Path)AnyOptionalUnion)kornia_config)ObjectDetectorRTDETRDetectorBuilder)boxmot)numpy)tensor_to_image)write_imageBoxMotTrackerc                     ^  \ rS rSr% SrSrS\S'        S             SU 4S jjjrSS jrSSS jjr	SSS	 jjr
S
rU =r$ )r   (   a-  BoxMotTracker is a module that wraps a detector and a tracker model.

This module uses BoxMot library for tracking.

Args:
    detector: The detector model.
    tracker_model_name: The name of the tracker model. Valid options are:
        - "BoTSORT"
        - "DeepOCSORT"
        - "OCSORT"
        - "HybridSORT"
        - "ByteTrack"
        - "StrongSORT"
        - "ImprAssoc"
    tracker_model_weights: Path to the model weights for ReID (Re-Identification).
    device: Union[str, torch.device, None] on which to run the model (e.g., 'cpu' or 'cuda').
    fp16: Whether to use half-precision (fp16) for faster inference on compatible devices.
    per_class: Whether to perform per-class tracking.
    track_high_thresh: High threshold for detection confidence.
        Detections above this threshold are used in the first association round.
    track_low_thresh: Low threshold for detection confidence.
        Detections below this threshold are ignored.
    new_track_thresh: Threshold for creating a new track.
        Detections above this threshold will be considered as potential new tracks.
    track_buffer: Number of frames to keep a track alive after it was last detected.
    match_thresh: Threshold for the matching step in data association.
    proximity_thresh: Threshold for IoU (Intersection over Union) distance in first-round association.
    appearance_thresh: Threshold for appearance embedding distance in the ReID module.
    cmc_method: Method for correcting camera motion. Options include "sof" (simple optical flow).
    frame_rate: Frame rate of the video being processed. Used to scale the track buffer size.
    fuse_first_associate: Whether to fuse appearance and motion information during the first association step.
    with_reid: Whether to use ReID (Re-Identification) features for association.

Note:
    To use this tracker, load image tensors (shape: ``(1, C, H, W)``) and call ``model.update(image)``
    for at least 4 frames before calling ``model.save(image)``.

.. note::
    At least 4 frames are needed to initialize the tracking position.

boxmot_trackerstrnamec           	       > [         TU ]  5         [        U[        5      (       a:  UR	                  S5      (       a  [
        R                  " US9nO[        SU S35      eXl        [        R                  " [        R                   S3SS9  [        [        U5      " S	[        [        R                   R#                  [        R                   S3U5      5      UUS.UD6U l        g )
Nrtdetr)
model_namez
Detector `zA` not available. You may pass an ObjectDetector instance instead.z/boxmotTexist_ok)model_weightsdevicefp16 )super__init__
isinstancer   
startswithr
   build
ValueErrordetectorosmakedirsr   hub_models_dirgetattrr   r   pathjointracker)selfr$   tracker_model_nametracker_model_weightsr   r   kwargs	__class__s          W/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/contrib/boxmot_tracker.pyr   BoxMotTracker.__init__U   s     	h$$""8,,066(K  
*kl  !
}334G<tLv'9: 
rww||}/K/K.LG,TVklm
 	
    c           
        UR                   S:X  a  UR                  S   S:X  d(  UR                   S:X  d  [        SUR                   35      eUR                   S:X  a  UR                  S5      nU R	                  U5      nUS   R                  5       R                  5       n[        R                  " USS2S4   USS2S4   USS2S4   USS2S4   -   USS2S4   USS2S4   -   USS2S4   USS2S4   /5      R                  nUR                  S   S:X  a  [        R                  " S	5      n[        U5      S
-  R                  [        R                  5      nU R                  R                  X45      $ )zIUpdate the tracker with a new image.

Args:
    image: The input image.

   r         zCInput torch.Tensor must be of shape (1, 3, H, W) or (3, H, W). Got N      )r         )ndimshaper#   	unsqueezer$   cpur   nparrayTemptyr   astypeuint8r+   update)r,   imagedetections_raw
detections	frame_raws        r1   rF   BoxMotTracker.updateo   s^    

aEKKNa$7qbchcncnbopqq::?OOA&EBF--PUBV#A&**,224
XX1a4 1a4 1a4 :ad#331a4 :ad#331a4 1a4 	
 ! 	 A!#&)J$U+c199"((C	||"":99r3   c                    [        U5      S-  R                  [        R                  5      nU R                  R                  X2S9  [        R                  " U5      R                  SSS5      $ )zVisualize the results of the tracker.

Args:
    image: The input image.
    show_trajectories: Whether to show the trajectories.

Returns:
    The image with the results of the tracker.

r;   show_trajectoriesr8   r   r6   )	r   rD   r@   rE   r+   plot_resultstorchtensorpermute)r,   rG   rN   rJ   s       r1   	visualizeBoxMotTracker.visualize   sU     %U+c199"((C	!!)!Q||I&..q!Q77r3   c                   Ucj  U R                    S[        R                  R                  [        R                  S9R	                  S5      < 3n[
        R                  R                  SU5      nU R                  XS9n[
        R                  " USS9  [        [
        R                  R                  U[        S	5      R                  S
5       S35      UR                  5       5        [        R                  SU 35        g)zSave the model to ONNX format.

Args:
    image: The input image.
    show_trajectories: Whether to visualize trajectories.
    directory: Where to save the file(s).

N_)tzz%Y%m%d%H%M%Skornia_outputsrM   Tr   r   r:   z.jpgzOutputs are saved in )r   datetimenowUTCstrftimer%   r)   r*   rS   r&   r   r   zfillbyteloggerinfo)r,   rG   rN   	directoryr   outputs         r1   saveBoxMotTracker.save   s     ii[("3"3"7"78<<"7"H"Q"QR`"a!deD%5t<IK
I-GGLLs1v||A&7t$<=KKM	
 	+I;78r3   )r$   r+   )rtdetr_r18vd
DeepOCSORTzosnet_x0_25_msmt17.ptr?   F)r$   zUnion[ObjectDetector, str]r-   r   r.   r   r   r   r   boolr/   r   returnNone)rG   torch.Tensorrh   ri   )T)rG   rj   rN   rg   rh   rj   )TN)rG   rj   rN   rg   ra   zOptional[str]rh   ri   )__name__
__module____qualname____firstlineno____doc__r   __annotations__r   rF   rS   rc   __static_attributes____classcell__)r0   s   @r1   r   r   (   s    (T !D#  0>".%<
,
  
  #	

 
 
 
 

 
4":H8 9 9r3   )
__future__r   rY   loggingr%   pathlibr   typingr   r   r   rP   kornia.configr   kornia.contrib.object_detectionr	   r
   kornia.core.externalr   r   r@   kornia.image.utilsr   	kornia.ior   __all__	getLoggerrk   r_   r   r   r3   r1   <module>r~      sU   $ #   	  ' '  ' Q ' , . !
			8	$N9 N9r3   