Ë
    FêñiÆ  ã            	       óT  — d dl Zd dlZd dlmZ d dlmZmZ 	 d dlZej                  sJ ‚	 ddej                  dedefd	„Zd
ededej                  fd„Zddedededej                  fd„Zdej                  dedej                  fd„Zy# e
eef$ r d dlmZ  ed«       d dlZY Œ{w xY w)é    N)Úcdist)Úbatch_probiouÚbbox_ioa)Úcheck_requirementszlap>=0.5.12Úcost_matrixÚthreshÚuse_lapc           	      ó´  — | j                   dk(  r\t        j                  dt        ¬«      t	        t        | j                  d   «      «      t	        t        | j                  d   «      «      fS |ryt        j                  | d|¬«      \  }}}t        |«      D ��cg c]  \  }}|dk\  sŒ||g‘Œ }}}t        j                  |dk  «      d   }	t        j                  |dk  «      d   }
�nct        j                  j                  | «      \  }}t        j                  t        t        |«      «      D �cg c]  }| ||   ||   f   |k  sŒ||   ||   g‘Œ c}«      }t        |«      dk(  rWt!        t        j"                  | j                  d   «      «      }	t!        t        j"                  | j                  d   «      «      }
nŽt!        t%        t        j"                  | j                  d   «      «      t%        |dd…df   «      z
  «      }	t!        t%        t        j"                  | j                  d   «      «      t%        |dd…df   «      z
  «      }
||	|
fS c c}}w c c}w )a•  Perform linear assignment using either the scipy or lap.lapjv method.

    Args:
        cost_matrix (np.ndarray): The matrix containing cost values for assignments, with shape (N, M).
        thresh (float): Threshold for considering an assignment valid.
        use_lap (bool): Use lap.lapjv for the assignment. If False, scipy.optimize.linear_sum_assignment is used.

    Returns:
        matched_indices (list[list[int]] | np.ndarray): Matched indices of shape (K, 2), where K is the number of
            matches.
        unmatched_a (tuple | list | np.ndarray): Unmatched indices from the first set.
        unmatched_b (tuple | list | np.ndarray): Unmatched indices from the second set.

    Examples:
        >>> cost_matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        >>> thresh = 5.0
        >>> matched_indices, unmatched_a, unmatched_b = linear_assignment(cost_matrix, thresh, use_lap=True)
    r   )r   é   ©Údtypeé   T)Úextend_costÚ
cost_limitN)ÚsizeÚnpÚemptyÚintÚtupleÚrangeÚshapeÚlapÚlapjvÚ	enumerateÚwhereÚscipyÚoptimizeÚlinear_sum_assignmentÚasarrayÚlenÚlistÚarangeÚ	frozenset)r   r   r	   Ú_ÚxÚyÚixÚmxÚmatchesÚunmatched_aÚunmatched_bÚis               úe/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/trackers/utils/matching.pyÚlinear_assignmentr.      s  € ð& ×Ñ˜1ÒÜ�x‰x˜¤cÔ*¬E´%¸×8IÑ8IÈ!Ñ8LÓ2MÓ,NÔPUÔV[Ð\g×\mÑ\mÐnoÑ\pÓVqÓPrÐrÐráô —)‘)˜K°TÀfÔM‰ˆˆ1ˆaÜ*3°A«,×B¡  B¸"À»'�B˜’8ÐBˆÑBÜ—h‘h˜q 1™u“o aÑ(ˆÜ—h‘h˜q 1™u“o aÑ(Šô �~‰~×3Ñ3°KÓ@‰ˆˆ1Ü—*‘*´E¼#¸a»&³MÖg¨qÀ[ÐQRÐSTÑQUÐWXÐYZÑW[ÐQ[ÑE\Ð`fÓEf˜q ™t Q q¡TšlÒgÓhˆÜˆw‹<˜1ÒÜœrŸy™y¨×):Ñ):¸1Ñ)=Ó>Ó?ˆKÜœrŸy™y¨×):Ñ):¸1Ñ)=Ó>Ó?‰Käœy¬¯©°;×3DÑ3DÀQÑ3GÓ)HÓIÌIÐV]Ò^_ÐabÐ^bÑVcÓLdÑdÓeˆKÜœy¬¯©°;×3DÑ3DÀQÑ3GÓ)HÓIÌIÐV]Ò^_ÐabÐ^bÑVcÓLdÑdÓeˆKà�K Ð,Ð,ùó Cùò hs   ÂIÂ&IÄ.IÅIÚatracksÚbtracksÚreturnc                 ó¾  — | rt        | d   t        j                  «      s|r"t        |d   t        j                  «      r| }|}nb| D �cg c]&  }|j                  �|j                  n|j
                  ‘Œ( }}|D �cg c]&  }|j                  �|j                  n|j
                  ‘Œ( }}t        j                  t        |«      t        |«      ft        j                  ¬«      }t        |«      ræt        |«      rÛt        |d   «      dk(  rvt        |d   «      dk(  ret        t        j                  |t        j                  ¬«      t        j                  |t        j                  ¬«      «      j                  «       }d|z
  S t        t        j                  |t        j                  ¬«      t        j                  |t        j                  ¬«      d¬«      }d|z
  S c c}w c c}w )aŽ  Compute cost based on Intersection over Union (IoU) between tracks.

    Args:
        atracks (list[STrack] | list[np.ndarray]): List of tracks 'a' or bounding boxes.
        btracks (list[STrack] | list[np.ndarray]): List of tracks 'b' or bounding boxes.

    Returns:
        (np.ndarray): Cost matrix computed based on IoU with shape (len(atracks), len(btracks)).

    Examples:
        Compute IoU distance between two sets of tracks
        >>> atracks = [np.array([0, 0, 10, 10]), np.array([20, 20, 30, 30])]
        >>> btracks = [np.array([5, 5, 15, 15]), np.array([25, 25, 35, 35])]
        >>> cost_matrix = iou_distance(atracks, btracks)
    r   r   é   T)Úiour   )Ú
isinstancer   ÚndarrayÚangleÚxywhaÚxyxyÚzerosr    Úfloat32r   ÚascontiguousarrayÚnumpyr   )r/   r0   ÚatlbrsÚbtlbrsÚtrackÚiouss         r-   Úiou_distancerB   @   sn  € ñ  	”J˜w q™z¬2¯:©:Ô6¹GÌ
ÐSZÐ[\ÑS]Ô_a×_iÑ_iÔHjØˆØ‰àV]Ö^ÈU §¡Ð!8�%—+’+¸e¿j¹jÑHÐ^ˆÐ^ØV]Ö^ÈU §¡Ð!8�%—+’+¸e¿j¹jÑHÐ^ˆÐ^ä�8‰8”S˜“[¤# f£+Ð.´b·j±jÔA€DÜ
ˆ6„{”s˜6”{Üˆv�a‰y‹>˜QÒ¤3 v¨a¡y£>°QÒ#6Ü Ü×$Ñ$ V´2·:±:Ô>Ü×$Ñ$ V´2·:±:Ô>ó÷ ‰e‹gð ð ˆt‰8€Oô Ü×$Ñ$ V´2·:±:Ô>Ü×$Ñ$ V´2·:±:Ô>ØôˆDð
 ˆt‰8€Oùò! _ùÚ^s   Á+GÁ9+GÚtracksÚ
detectionsÚmetricc                 óä  — t        j                  t        | «      t        |«      ft         j                  ¬«      }|j                  dk(  r|S t        j
                  |D �cg c]  }|j                  ‘Œ c}t         j                  ¬«      }t        j
                  | D �cg c]  }|j                  ‘Œ c}t         j                  ¬«      }t        j                  dt        |||«      «      }|S c c}w c c}w )a¿  Compute distance between tracks and detections based on embeddings.

    Args:
        tracks (list[BOTrack]): List of tracks, where each track contains embedding features.
        detections (list[BOTrack]): List of detections, where each detection contains embedding features.
        metric (str): Metric for distance computation. Supported metrics include 'cosine', 'euclidean', etc.

    Returns:
        (np.ndarray): Cost matrix computed based on embeddings with shape (N, M), where N is the number of tracks and M
            is the number of detections.

    Examples:
        Compute the embedding distance between tracks and detections using cosine metric
        >>> tracks = [BOTrack(...), BOTrack(...)]  # List of track objects with embedding features
        >>> detections = [BOTrack(...), BOTrack(...)]  # List of detection objects with embedding features
        >>> cost_matrix = embedding_distance(tracks, detections, metric="cosine")
    r   r   g        )
r   r:   r    r;   r   r   Ú	curr_featÚsmooth_featÚmaximumr   )rC   rD   rE   r   r@   Údet_featuresÚtrack_featuress          r-   Úembedding_distancerL   g   s®   € ô$ —(‘(œC ›K¬¨Z«Ð9ÄÇÁÔL€KØ×Ñ˜1ÒØÐÜ—:‘:¸JÖG°5˜uŸ›ÒGÌrÏzÉzÔZ€Lô —Z‘ZÀÖ G°u ×!2Ó!2Ò GÌrÏzÉzÔZ€NÜ—*‘*˜S¤%¨¸ÀfÓ"MÓN€KØÐùò Hùò !Hs   ÁC(ÂC-c                 óì   — | j                   dk(  r| S d| z
  }t        j                  |D �cg c]  }|j                  ‘Œ c}«      }|d   j	                  | j
                  d   d¬«      }||z  }d|z
  S c c}w )a‚  Fuse cost matrix with detection scores to produce a single cost matrix.

    Args:
        cost_matrix (np.ndarray): The matrix containing cost values for assignments, with shape (N, M).
        detections (list[BaseTrack]): List of detections, each containing a score attribute.

    Returns:
        (np.ndarray): Fused cost matrix with shape (N, M).

    Examples:
        Fuse a cost matrix with detection scores
        >>> cost_matrix = np.random.rand(5, 10)  # 5 tracks and 10 detections
        >>> detections = [BaseTrack(score=np.random.rand()) for _ in range(10)]
        >>> fused_matrix = fuse_score(cost_matrix, detections)
    r   r   N)Úaxis)r   r   ÚarrayÚscoreÚrepeatr   )r   rD   Úiou_simÚdetÚ
det_scoresÚfuse_sims         r-   Ú
fuse_scorerV   „   s|   € ð  ×Ñ˜1ÒØÐØ�+‰o€GÜ—‘°
Ö;¨˜3Ÿ9›9Ò;Ó<€JØ˜DÑ!×(Ñ(¨×):Ñ):¸1Ñ)=ÀAÐ(ÓF€JØ˜Ñ#€HØˆx‰<Ðùò <s   ªA1)T)Úcosine)r=   r   r   Úscipy.spatial.distancer   Úultralytics.utils.metricsr   r   r   Ú__version__ÚImportErrorÚAssertionErrorÚAttributeErrorÚultralytics.utils.checksr   r6   ÚfloatÚboolr.   r!   rB   ÚstrrL   rV   © ó    r-   ú<module>rd      sÖ   ðó Û Ý (ç =ðÛà�?Š?Ð‰?ñ)- 2§:¡:ð )-°uð )-Àtó )-ðX$˜$ð $¨ð $°"·*±*ó $ñN˜tð °ð ¸sð ÐRT×R\ÑR\ó ð:˜BŸJ™Jð °Dð ¸R¿Z¹Zô øðo 	�^ ^Ð4ò Ý;á�}Ô%Ýð	ús   ˜B
 Â
B'Â&B'