o
    Wh                     @   sD   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 deZ	dS )    N)BaseSolutionSolutionAnnotatorSolutionResults)colorsc                       s(   e Zd ZdZ fddZdd Z  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: Processes each frame of the video, applying region-based tracking.
        extract_tracks: Extracts tracking information from the input frame.
        display_output: Displays the processed output.

    Examples:
        >>> tracker = TrackZone()
        >>> frame = cv2.imread("frame.jpg")
        >>> results = tracker.process(frame)
        >>> cv2.imshow("Tracked Frame", results.plot_im)
    c                    s<   t  jdi | g d}ttj| jp|tjd| _dS )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.
        ))   r   )j  r   )r   :  )r   r	   )dtypeN )super__init__cv2
convexHullnparrayregionint32)selfkwargsdefault_region	__class__r   S/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/solutions/trackzone.pyr   %   s   "zTrackZone.__init__c           
      C   s   t || jd}t|dddddf }t|| jgd}tj|||d}| | tj	|| jgdd| jd d	 t
| j| j| j| jD ]\}}}}|j|| j|||d
t|dd qE| }	| |	 t|	t| jd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      )maskT)r   r   r      )isClosedcolor	thickness)track_id)labelr   )plot_imtotal_tracks)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   im0	annotatorr   masked_frameboxr!   clsconfr#   r   r   r   process0   s   
"
zTrackZone.process)__name__
__module____qualname____doc__r   r:   __classcell__r   r   r   r   r   
   s    r   )
r   numpyr   ultralytics.solutions.solutionsr   r   r   ultralytics.utils.plottingr   r   r   r   r   r   <module>   s
   