Ë
    Fêñi?  ã                   ó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      €úa/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/solutions/trackzone.pyr   zTrackZone.__init__&   sL   ø€ ô 	‰ÑÑ"˜6Ò"ÚEˆÜ—n‘n¤R§X¡X¨d¯k©kÒ.K¸^ÔSU×S[Ñ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.process1   sC  € ô$ & c°d·o±oÔFˆ	à�9‰9ÐÜŸ™ cª!ªQ°¨'¡lÓ3ˆDŒIÜ�L‰L˜Ÿ™ T§[¡[ M°3Ô7Ü—‘ s¨C°d·i±iÔ@ˆØ×Ñ˜LÔ)ô 	�‰�c˜DŸK™K˜=°4¸ÐZ^×ZiÑZiÐlmÑZmÕnô ),¨D¯J©J¸¿¹ÈÏ	É	ÐSW×S]ÑS]Ó(^ò 	Ñ$ˆC�˜3 Ø×ÑØ˜4×0Ñ0°°dÀXÐ0ÓNÔV\Ð]eÐgkÓVlð  õ ð	ð
 ×"Ñ"Ó$ˆØ×Ñ˜GÔ$ô  w¼SÀÇÁÓ=PÔQÐQr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Úndarrayr   r@   Ú__classcell__)r   s   @r   r	   r	      s5   ø„ ñð2	 ð 	¨õ 	ð'R˜2Ÿ:™:ð '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r   