o
    Wh                     @   s4   d dl mZmZmZ d dlmZ G dd deZdS )    )BaseSolutionSolutionAnnotatorSolutionResults)colorsc                       s(   e Zd ZdZ fddZdd Z  ZS )QueueManageraA  
    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: Initializes the queue region.
        process: Processes a single frame for queue management.
        extract_tracks: Extracts object tracks from the current frame.
        store_tracking_history: Stores the tracking history for an object.
        display_output: Displays 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)
    c                    s6   t  jdi | |   d| _d| _t| j| _dS )zaInitializes 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)selfkwargs	__class__r   Z/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/solutions/queue_management.pyr
   &   s
   zQueueManager.__init__c           
      C   s,  d| _ | | t|| jd}|j| j| j| jd d t| j| j	| j
| jD ]I\}}}}|j|| |||t|dd | || | j|g }d}t|dkrV|d	 }| jd
krp|rp| j| | jd rp|  j d7  _ q'|jdt| j  | j| jdd | }	| |	 t|	| j t| j	d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confr3   prev_positionr&   r   r   r   process.   s.   
" &
zQueueManager.process)__name__
__module____qualname____doc__r
   rD   __classcell__r   r   r   r   r      s    r   N)ultralytics.solutions.solutionsr   r   r   ultralytics.utils.plottingr   r   r   r   r   r   <module>   s   