
    j?                     ^    d dl mZ d dlZd dlZd dlmZmZmZ d dl	m
Z
  G d de          ZdS )    )AnyN)BaseSolutionSolutionAnnotatorSolutionResults)colorsc                   F     e Zd ZdZdeddf fdZdej        defdZ	 xZ
S )	TrackZonea  A class to manage region-based object tracking in a video stream.

    This class extends the BaseSolution class and provides functionality for tracking objects within a specific region
    defined by a polygonal area. Objects outside the region are excluded from tracking.

    Attributes:
        region (np.ndarray): The polygonal region for tracking, represented as a convex hull of points.
        line_width (int): Width of the lines used for drawing bounding boxes and region boundaries.
        names (list[str]): List of class names that the model can detect.
        boxes (list[np.ndarray]): Bounding boxes of tracked objects.
        track_ids (list[int]): Unique identifiers for each tracked object.
        clss (list[int]): Class indices of tracked objects.

    Methods:
        process: Process each frame of the video, applying region-based tracking.
        extract_tracks: Extract tracking information from the input frame.
        display_output: Display the processed output.

    Examples:
        >>> tracker = TrackZone()
        >>> frame = cv2.imread("frame.jpg")
        >>> results = tracker.process(frame)
        >>> cv2.imshow("Tracked Frame", results.plot_im)
    kwargsreturnNc                      t                      j        di | g d}t          j        t	          j        | j        p|t          j                            | _        d| _        dS )zInitialize the TrackZone class for tracking objects within a defined region in video streams.

        Args:
            **kwargs (Any): Additional keyword arguments passed to the parent class.
        ))K   r   )5  r   )r     )r   r   )dtypeN )	super__init__cv2
convexHullnparrayregionint32mask)selfr
   default_region	__class__s      d/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/solutions/trackzone.pyr   zTrackZone.__init__&   sc     	""6"""EEEnRXdk.K^SUS[%\%\%\]]			    im0c           	         t          || j                  }| j        Gt          j        |dddddf                   | _        t          j        | j        | j        gd           t          j        ||| j                  }| 	                    |           t          j
        || j        gdd| j        dz  	           t          | j        | j        | j        | j                  D ]C\  }}}}|                    ||                     |||
          t%          |d                     D|                                }|                     |           t+          |t-          | j                            S )a  Process the input frame to track objects within a defined region.

        This method initializes the annotator, creates a mask for the specified region, extracts tracks only from the
        masked area, and updates tracking information. Objects outside the region are ignored.

        Args:
            im0 (np.ndarray): The input image or frame to be processed.

        Returns:
            (SolutionResults): Contains processed image `plot_im` and `total_tracks` (int) representing the total number
                of tracked objects within the defined region.

        Examples:
            >>> tracker = TrackZone()
            >>> frame = cv2.imread("path/to/image.jpg")
            >>> results = tracker.process(frame)
        )
line_widthNr      )r   T)r#   r#   r#      )isClosedcolor	thickness)track_id)labelr&   )plot_imtotal_tracks)r   r"   r   r   
zeros_liker   fillPolyr   bitwise_andextract_tracks	polylineszipboxes	track_idsclssconfs	box_labeladjust_box_labelr   resultdisplay_outputr   len)	r   r    	annotatormasked_frameboxr(   clsconfr*   s	            r   processzTrackZone.process1   sw   $ &cdoFFF	9c!!!QQQ'l33DILT[M3777sCdi@@@L))) 	cDK=4Z^ZilmZmnnnn ),DJ	SWS](^(^ 	 	$C3400dX0NNV\]egkVlVl       ""$$G$$$ wS=P=PQQQQr   )__name__
__module____qualname____doc__r   r   r   ndarrayr   r@   __classcell__)r   s   @r   r	   r	      s         2	 	 	 	 	 	 	 	'R2: 'R/ 'R 'R 'R 'R 'R 'R 'R 'Rr   r	   )typingr   r   numpyr   ultralytics.solutions.solutionsr   r   r   ultralytics.utils.plottingr   r	   r   r   r   <module>rK      s          



     \ \ \ \ \ \ \ \ \ \ - - - - - -LR LR LR LR LR LR LR LR LR LRr   