
    |he                     V    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y)    )AnyN)BaseSolutionSolutionAnnotatorSolutionResults)colorsc                   P     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        |   di | g d}t        j                  t	        j
                  | j                  xs |t        j                              | _        d| _        y)z
        Initialize 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      ^/var/www/html/test/engine/venv/lib/python3.12/site-packages/ultralytics/solutions/trackzone.pyr   zTrackZone.__init__'   sL     	"6"EnnRXXdkk.K^SUS[S[%\]	    im0c           	         t        || j                        }| j                  Pt        j                  |dddddf         | _        t        j                  | j                  | j                  gd       t        j                  ||| j                        }| j                  |       t        j                  || j                  gdd| j                  dz  	       t        | j                  | j                  | j                  | j                        D ]7  \  }}}}|j!                  || j#                  |||
      t%        |d             9 |j'                         }| j)                  |       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.process3   sC   & &cdooF	99c!Q'l3DILLT[[M37sCdii@L) 	cDKK=4Z^ZiZilmZmn ),DJJ		SWS]S](^ 	$C3400dX0NV\]egkVl   	
 ""$G$ wS=PQQr   )__name__
__module____qualname____doc__r   r   r   ndarrayr   r@   __classcell__)r   s   @r   r	   r	      s5    4
 
 
(R2:: (R/ (Rr   r	   )typingr   r   numpyr   ultralytics.solutions.solutionsr   r   r   ultralytics.utils.plottingr   r	   r   r   r   <module>rK      s)     
  \ \ -OR ORr   