
    |h)                     F    d dl mZ d dlmZmZmZ d dlmZ  G d de      Zy)    )Any)BaseSolutionSolutionAnnotatorSolutionResults)colorsc                   8     e Zd ZdZdeddf fdZdefdZ xZS )QueueManagera;  
    Manages queue counting in real-time video streams based on object tracks.

    This class extends BaseSolution to provide functionality for tracking and counting objects within a specified
    region in video frames.

    Attributes:
        counts (int): The current count of objects in the queue.
        rect_color (Tuple[int, int, int]): RGB color tuple for drawing the queue region rectangle.
        region_length (int): The number of points defining the queue region.
        track_line (List[Tuple[int, int]]): List of track line coordinates.
        track_history (Dict[int, List[Tuple[int, int]]]): Dictionary storing tracking history for each object.

    Methods:
        initialize_region: Initialize the queue region.
        process: Process a single frame for queue management.
        extract_tracks: Extract object tracks from the current frame.
        store_tracking_history: Store the tracking history for an object.
        display_output: Display the processed output.

    Examples:
        >>> cap = cv2.VideoCapture("path/to/video.mp4")
        >>> queue_manager = QueueManager(region=[100, 100, 200, 200, 300, 300])
        >>> while cap.isOpened():
        >>>     success, im0 = cap.read()
        >>>     if not success:
        >>>         break
        >>>     results = queue_manager.process(im0)
    kwargsreturnNc                     t        |   di | | j                          d| _        d| _        t        | j                        | _        y)z`Initialize the QueueManager with parameters for tracking and counting objects in a video stream.r   )   r   r   N )super__init__initialize_regioncounts
rect_colorlenregionregion_length)selfr
   	__class__s     e/var/www/html/test/engine/venv/lib/python3.12/site-packages/ultralytics/solutions/queue_management.pyr   zQueueManager.__init__(   s>    "6" ) -    c           	         d| _         | j                  |       t        || j                        }|j	                  | j
                  | j                  | j                  dz         t        | j                  | j                  | j                  | j                        D ]  \  }}}}|j                  || j                  |||      t        |d             | j                  ||       | j                   j#                  |g       }d}t%        |      dkD  r|d	   }| j&                  d
k\  s|s| j(                  j+                  | j-                  | j.                  d               s| xj                   dz  c_          |j1                  dt3        | j                          | j
                  | j                  d       |j5                         }	| j7                  |	       t9        |	| j                   t%        | j                              S )a3  
        Process queue management for a single frame of video.

        Args:
            im0 (numpy.ndarray): Input image for processing, typically a frame from a video stream.

        Returns:
            (SolutionResults): Contains processed image `im0`, 'queue_count' (int, number of objects in the queue) and
                'total_tracks' (int, total number of tracked objects).

        Examples:
            >>> queue_manager = QueueManager()
            >>> frame = cv2.imread("frame.jpg")
            >>> results = queue_manager.process(frame)
        r   )
line_width   )reg_ptscolor	thicknessT)labelr   N      zQueue Counts : )h         )pointsregion_color	txt_color)plot_imqueue_counttotal_tracks)r   extract_tracksr   r   draw_regionr   r   zipboxes	track_idsclssconfs	box_labeladjust_box_labelr   store_tracking_historytrack_historygetr   r   r_scontainsPoint
track_linequeue_counts_displaystrresultdisplay_outputr   )
r   im0	annotatorboxtrack_idclsconfr9   prev_positionr,   s
             r   processzQueueManager.process0   s     C %cdooF	dkkTXTcTcfgTgh(+DJJ		SWS]S](^ 	!$C34+@+@dH+U]cdlnr]st''#6 !..228R@M !M=!A% -b 1!!Q&=TXX=N=NtzzZ^ZiZijlZmOn=oq 	!  	&&c$++./0;;#	 	' 	
 ""$G$ wDKKVYZ^ZhZhVijjr   )	__name__
__module____qualname____doc__r   r   r   rJ   __classcell__)r   s   @r   r	   r	   	   s)    <. . ./ko /kr   r	   N)	typingr   ultralytics.solutions.solutionsr   r   r   ultralytics.utils.plottingr   r	   r   r   r   <module>rS      s#     \ \ -Vk< Vkr   