§
    l¬¸j,  ã                  óp   — d Z ddlmZ dZddlZddlmZ ddlm	Z	 	 d"d#d„Z
d$d„Zd%d„Zd%d„Zd&d'd„Zd(d!„ZdS ))zþShared helpers for operating on lists of track objects across trackers.

These functions are intentionally generic: they only touch attributes that every track
implementation exposes (`track_id`, `frame_id`, `start_frame`, `xyxy`, `mean`, `covariance`).
é    )Úannotations)Újoint_stracksÚmerge_track_poolsÚ	multi_gmcÚparse_bboxesÚremove_duplicate_stracksÚsub_stracksNé   )Ú
TrackStateé   )Úmatchingéè  Ú	activatedÚlistÚrefindÚlostÚremovedÚremoved_bufferÚintÚreturnÚNonec                ó8  — d„ | j         D ¦   «         | _         t          | j         |¦  «        | _         t          | j         |¦  «        | _         t          | j        | j         ¦  «        | _        | j                             |¦  «         t          | j        | j        ¦  «        | _        t          | j         | j        ¦  «        \  | _         | _        || _        | j                             |¦  «         t          | j        ¦  «        |k    r| j        | d…         | _        dS dS )aü  Apply the standard end-of-frame bookkeeping to a tracker's persistent pools in place.

    Merges newly activated and re-found tracks into `tracker.tracked_stracks`, moves the transitioned tracks into
    `tracker.lost_stracks`, dedups by IoU, appends removals to `tracker.removed_stracks`, and trims the removed buffer
    to `removed_buffer` entries.

    Args:
        tracker (Any): Object exposing `tracked_stracks`, `lost_stracks`, `removed_stracks` lists.
        activated (list): Tracks updated from the Tracked state this frame.
        refind (list): Tracks re-activated from the Lost state this frame.
        lost (list): Tracks transitioned to Lost this frame.
        removed (list): Tracks transitioned to Removed this frame.
        removed_buffer (int): Maximum number of historical removed tracks to retain.

    Examples:
        Run end-of-frame bookkeeping inside a tracker's `update` method
        >>> merge_track_pools(self, activated_stracks, refind_stracks, lost_stracks, removed_stracks)
    c                ó<   — g | ]}|j         t          j        k    ¯|‘ŒS © )Ústater   ÚTracked©Ú.0Úts     úr/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/trackers/utils/stracks.pyú
<listcomp>z%merge_track_pools.<locals>.<listcomp>,   s(   € ÐcÐcÐc QÀQÄWÕPZÔPbÒEbÐEb˜qÐEbÐEbÐEbó    N)	Útracked_stracksr   r	   Úlost_stracksÚextendÚremoved_stracksr   Úremoved_stracks_frameÚlen)Útrackerr   r   r   r   r   s         r    r   r      s  € ð4 dÐc¨'Ô*AÐcÑcÔc€GÔÝ+¨GÔ,CÀYÑOÔO€GÔÝ+¨GÔ,CÀVÑLÔL€GÔÝ& wÔ';¸WÔ=TÑUÔU€GÔØÔ×Ò Ñ%Ô%Ð%Ý& wÔ';¸WÔ=TÑUÔU€GÔÝ4LØÔ Ô!5ñ5ô 5Ñ1€GÔ˜WÔ1ð %,€GÔ!ØÔ×"Ò" 7Ñ+Ô+Ð+Ý
ˆ7Ô"Ñ#Ô# nÒ4Ð4Ø")Ô"9¸>¸/Ð:JÐ:JÔ"KˆÔÐÐð 5Ð4r"   ú
np.ndarrayc                óÔ   — t          | d¦  «        r| j        n| j        }t          j        |t          j        t          |¦  «        ¦  «                             dd¦  «        gd¬¦  «        S )aj  Return detection bounding boxes with appended indices from a Results-like object.

    Args:
        results (Any): Object exposing ``xywh`` (or ``xywhr``), ``conf``, and ``cls``.

    Returns:
        (np.ndarray): Array of shape ``(N, 5)`` for ``xywh`` or ``(N, 6)`` for ``xywhr``, with the last column
            containing the original detection index.
    Úxywhréÿÿÿÿr   )Úaxis)Úhasattrr,   ÚxywhÚnpÚconcatenateÚaranger(   Úreshape)ÚresultsÚbboxess     r    r   r   ;   s[   € õ & g¨wÑ7Ô7ÐIˆWŒ]ˆ]¸W¼\€FÝŒ>˜6¥2¤9­S°©[¬[Ñ#9Ô#9×#AÒ#AÀ"ÀaÑ#HÔ#HÐIÐPRÐSÑSÔSÐSr"   ÚatracksÚbtracksc                ó>   ‡— d„ | D ¦   «         Š| ˆfd„|D ¦   «         z   S )aè  Combine two track lists into one, de-duplicating by `track_id`.

    Args:
        atracks (list[STrack]): First list of tracks; entries win on `track_id` collisions.
        btracks (list[STrack]): Second list of tracks.

    Returns:
        (list[STrack]): Union of `atracks` and `btracks` with duplicate `track_id`s removed.

    Examples:
        Merge the currently tracked pool with newly activated tracks
        >>> merged = joint_stracks(tracked_stracks, activated_stracks)
    c                ó   — h | ]	}|j         ’Œ
S r   ©Útrack_idr   s     r    ú	<setcomp>z joint_stracks.<locals>.<setcomp>W   s   € Ð)Ð)Ð)˜AˆQŒZÐ)Ð)Ð)r"   c                ó&   •— g | ]}|j         ‰v¯|‘ŒS r   r;   )r   r   Úa_idss     €r    r!   z!joint_stracks.<locals>.<listcomp>X   s%   ø€ ÐDÐDÐD˜A¨A¬J¸eÐ,CÐ,C�aÐ,CÐ,CÐ,Cr"   r   )r7   r8   r?   s     @r    r   r   I   s8   ø€ ð *Ð) Ð)Ñ)Ô)€EØÐDÐDÐDÐD ÐDÑDÔDÑDÐDr"   c                ó8   ‡— d„ |D ¦   «         Šˆfd„| D ¦   «         S )aí  Filter out tracks from `atracks` whose `track_id` appears in `btracks`.

    Args:
        atracks (list[STrack]): Source list of tracks to filter.
        btracks (list[STrack]): Tracks whose `track_id`s should be excluded from the output.

    Returns:
        (list[STrack]): Elements of `atracks` whose `track_id` is not present in `btracks`.

    Examples:
        Remove any re-tracked objects from the lost pool
        >>> lost_stracks = sub_stracks(lost_stracks, tracked_stracks)
    c                ó   — h | ]	}|j         ’Œ
S r   r;   r   s     r    r=   zsub_stracks.<locals>.<setcomp>i   s   € Ð.Ð.Ð. �!”*Ð.Ð.Ð.r"   c                ó&   •— g | ]}|j         ‰v¯|‘ŒS r   r;   )r   r   Ú
btrack_idss     €r    r!   zsub_stracks.<locals>.<listcomp>j   s%   ø€ Ð?Ð?Ð?�! !¤*°JÐ">Ð">ˆAÐ">Ð">Ð">r"   r   )r7   r8   rC   s     @r    r	   r	   [   s2   ø€ ð /Ð. gÐ.Ñ.Ô.€JØ?Ð?Ð?Ð?�wÐ?Ñ?Ô?Ð?r"   ç333333Ã?Ú
dup_threshÚfloatútuple[list, list]c                ó  ‡‡— t          j        | |¦  «        }t          j        ||k     ¦  «        }g g }}t	          |Ž D ]l\  }}| |         j        | |         j        z
  }	||         j        ||         j        z
  }
|	|
k    r|                     |¦  «         ŒW|                     |¦  «         Œmt          |¦  «        t          |¦  «        cŠŠˆfd„t          | ¦  «        D ¦   «         }ˆfd„t          |¦  «        D ¦   «         }||fS )aÎ  Remove duplicate tracks across two lists based on Intersection over Union (IoU) distance.

    Track pairs with IoU distance < `dup_thresh` (IoU > `1 - dup_thresh`) are treated as duplicates of the same
    object. The shorter-lived track (smaller `frame_id - start_frame`) is dropped; ties drop
    from `atracks`.

    Args:
        atracks (list[STrack]): First list of tracks; entries must expose `xyxy`, `frame_id`, and `start_frame`.
        btracks (list[STrack]): Second list of tracks with the same attribute requirements.
        dup_thresh (float): IoU-distance ceiling for treating two tracks as duplicates. Default 0.15 (IoU > 0.85).

    Returns:
        resa (list[STrack]): `atracks` with duplicate tracks removed.
        resb (list[STrack]): `btracks` with duplicate tracks removed.

    Examples:
        De-duplicate the tracked and lost pools at the end of a frame
        >>> tracked, lost = remove_duplicate_stracks(tracked_stracks, lost_stracks)
    c                ó"   •— g | ]\  }}|‰v¯	|‘ŒS r   r   )r   Úir   Údupa_sets      €r    r!   z,remove_duplicate_stracks.<locals>.<listcomp>Œ   ó'   ø€ ÐBÐBÐB‘$�!�Q°¸Ð0AÐ0AˆAÐ0AÐ0AÐ0Ar"   c                ó"   •— g | ]\  }}|‰v¯	|‘ŒS r   r   )r   rJ   r   Údupb_sets      €r    r!   z,remove_duplicate_stracks.<locals>.<listcomp>�   rL   r"   )
r   Úiou_distancer1   ÚwhereÚzipÚframe_idÚstart_frameÚappendÚsetÚ	enumerate)r7   r8   rE   ÚpdistÚpairsÚdupaÚdupbÚpÚqÚtimepÚtimeqÚresaÚresbrK   rN   s                @@r    r   r   m   s  øø€ õ( Ô! '¨7Ñ3Ô3€EÝŒH�U˜ZÒ'Ñ(Ô(€EØ�Rˆ$€DÝ�U�ð ð ‰ˆˆ1Ø˜”
Ô# g¨a¤jÔ&<Ñ<ˆØ˜”
Ô# g¨a¤jÔ&<Ñ<ˆØ�5Š=ˆ=Ø�KŠK˜‰NŒNˆNˆNà�KŠK˜‰NŒNˆNˆNÝ˜T™œ¥C¨¡I¤IÐ€HˆhØBÐBÐBÐB�) GÑ,Ô,ÐBÑBÔB€DØBÐBÐBÐB�) GÑ,Ô,ÐBÑBÔB€DØ�ˆ:Ðr"   ÚstracksÚHc                óŒ  — | sdS t          j        d„ | D ¦   «         ¦  «        }t          j        d„ | D ¦   «         ¦  «        }|dd…dd…f         }t          j        t          j        dt           j        ¬¦  «        |¦  «        }|dd…df         }t          j        ||d         ¦  «        d         }|dd…dd…fxx         |z  cc<   t          j        t          j        ||¦  «        t          j        |j        ¦  «        ¦  «        }t          t          ||¦  «        ¦  «        D ]"\  }\  }}	|| |         _
        |	| |         _        Œ#dS )	aU  Update multiple track positions and covariances using a 2x3 affine homography.

    The Kalman state is assumed to be laid out as `(*box, *box_velocity)` with the box center `(x, y)` in the first two
    dims. `R8x8` rotates all four 2-d pairs block-diagonally; the translation `t` is applied only to the position.
    This assumes the state layout is four spatial/velocity pairs (e.g. XYWH); XYAH trackers must override this.

    Args:
        stracks (list[STrack]): Tracks to warp in place; each must expose `mean` (shape (8,)) and `covariance` (shape
            (8, 8)).
        H (np.ndarray): 2x3 affine homography mapping the previous frame to the current one.

    Examples:
        Apply camera-motion compensation to the active track pool
        >>> warp = gmc.apply(frame, detection_boxes)
        >>> multi_gmc(tracked_stracks, warp)
    Nc                ó   — g | ]	}|j         ‘Œ
S r   )Úmean©r   Ústs     r    r!   zmulti_gmc.<locals>.<listcomp>¤   s   € Ð7Ð7Ð7¨˜RœWÐ7Ð7Ð7r"   c                ó   — g | ]	}|j         ‘Œ
S r   )Ú
covariancerf   s     r    r!   zmulti_gmc.<locals>.<listcomp>¥   s   € Ð"CÐ"CÐ"C°R 2¤=Ð"CÐ"CÐ"Cr"   r
   é   )Údtype).N).r   )r1   ÚasarrayÚkronÚeyeÚfloat32ÚmatmulÚascontiguousarrayÚTrV   rQ   re   ri   )
ra   rb   Ú
multi_meanÚmulti_covarianceÚRÚR8x8r   rJ   re   Úcovs
             r    r   r   ‘   sW  € ð" ð ØˆÝ”Ð7Ð7¨wÐ7Ñ7Ô7Ñ8Ô8€JÝ”zÐ"CÐ"C¸7Ð"CÑ"CÔ"CÑDÔDÐà	ˆ"ˆ1ˆ"ˆbˆqˆbˆ&Œ	€AÝŒ7•2”6˜!¥2¤:Ð.Ñ.Ô.°Ñ2Ô2€DØ	ˆ"ˆ1ˆ"ˆaˆ%Œ€Aå”˜4 ¨IÔ!6Ñ7Ô7¸Ô?€JØˆqˆqˆq�"�1�"ˆuÐÐÔ˜ÑÐÐÑõ ”y¥¤¨4Ð1AÑ!BÔ!BÅBÔDXÐY]ÔY_ÑD`ÔD`ÑaÔaÐÝ#¥C¨
Ð4DÑ$EÔ$EÑFÔFð $ð $‰ˆ‰;ˆD�#Øˆ�Œ
ŒØ #ˆ�Œ
ÔÐð$ð $r"   )r   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r*   )r7   r   r8   r   r   r   )rD   )r7   r   r8   r   rE   rF   r   rG   )ra   r   rb   r*   r   r   )Ú__doc__Ú
__future__r   Ú__all__Únumpyr1   Ú	basetrackr   Ú r   r   r   r   r	   r   r   r   r"   r    ú<module>r~      sü   ððð ð #Ð "Ð "Ð "Ð "Ð "à
x€à Ð Ð Ð à "Ð "Ð "Ð "Ð "Ð "Ø Ð Ð Ð Ð Ð ð ð&Lð &Lð &Lð &Lð &LðRTð Tð Tð TðEð Eð Eð Eð$@ð @ð @ð @ð$!ð !ð !ð !ð !ðH"$ð "$ð "$ð "$ð "$ð "$r"   