
    he                     V    S SK Jr  S SKrS SKrS SKJrJrJr  S SK	J
r
   " S S\5      rg)    )AnyN)BaseSolutionSolutionAnnotatorSolutionResults)colorsc                   ^   ^  \ rS rSrSrS\SS4U 4S jjrS\R                  S\	4S jr
S	rU =r$ )
	TrackZone   ab  
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U ]  " S0 UD6  / SQn[        R                  " [        R
                  " U R                  =(       d    U[        R                  S95      U l        SU l        g)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      Y/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/solutions/trackzone.pyr   TrackZone.__init__'   sL     	"6"EnnRXXdkk.K^SUS[S[%\]	    im0c           
         [        XR                  S9nU R                  cR  [        R                  " USS2SS2S4   5      U l        [
        R                  " U R                  U R                  /S5        [
        R                  " XU R                  S9nU R                  U5        [
        R                  " XR                  /SSU R                  S-  S	9  [        U R                  U R                  U R                  U R                  5       H.  u  pEpgUR!                  X@R#                  XgUS
9[%        US5      S9  M0     UR'                  5       nU R)                  U5        [+        U[-        U R                  5      S9$ )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   processTrackZone.process3   s1   & &cooF	99c!Q'l3DILLT[[M37sdii@L) 	cKK=4Z^ZiZilmZmn ),DJJ		SWS]S](^$C300X0NV\]egkVl    )_
 ""$G$ wS=PQQr!   )r   r   )__name__
__module____qualname____firstlineno____doc__r   r   r   ndarrayr   rB   __static_attributes____classcell__)r   s   @r   r	   r	      s;    4
 
 
(R2:: (R/ (R (Rr!   r	   )typingr   r   numpyr   ultralytics.solutions.solutionsr   r   r   ultralytics.utils.plottingr   r	   r   r!   r   <module>rP      s)     
  \ \ -OR ORr!   