o
    Wh5                     @   s@   d dl m Z  d dlmZmZmZ d dlmZ G dd deZdS )    )time)BaseSolutionSolutionAnnotatorSolutionResults)colorsc                       s(   e Zd ZdZ fddZdd Z  ZS )SpeedEstimatora  
    A class to estimate the speed of objects in a real-time video stream based on their tracks.

    This class extends the BaseSolution class and provides functionality for estimating object speeds using
    tracking data in video streams.

    Attributes:
        spd (Dict[int, float]): Dictionary storing speed data for tracked objects.
        trkd_ids (List[int]): List of tracked object IDs that have already been speed-estimated.
        trk_pt (Dict[int, float]): Dictionary storing previous timestamps for tracked objects.
        trk_pp (Dict[int, Tuple[float, float]]): Dictionary storing previous positions for tracked objects.
        region (List[Tuple[int, int]]): List of points defining the speed estimation region.
        track_line (List[Tuple[float, float]]): List of points representing the object's track.
        r_s (LineString): LineString object representing the speed estimation region.

    Methods:
        initialize_region: Initializes the speed estimation region.
        process: Processes input frames to estimate object speeds.
        store_tracking_history: Stores the tracking history for an object.
        extract_tracks: Extracts tracks from the current frame.
        display_output: Displays the output with annotations.

    Examples:
        >>> estimator = SpeedEstimator()
        >>> frame = cv2.imread("frame.jpg")
        >>> results = estimator.process(frame)
        >>> cv2.imshow("Speed Estimation", results.plot_im)
    c                    s6   t  jdi | |   i | _g | _i | _i | _dS )z
        Initialize the SpeedEstimator object with speed estimation parameters and data structures.

        Args:
            **kwargs (Any): Additional keyword arguments passed to the parent class.
        N )super__init__initialize_regionspdtrkd_idstrk_pttrk_pp)selfkwargs	__class__r   Z/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/solutions/speed_estimation.pyr
   '   s   
zSpeedEstimator.__init__c                 C   s  |  | t|| jd}|j| jd| jd d t| j| j| j| j	D ]\}}}}| 
|| || jvr9d| j|< || jvrF| jd | j|< || jv rX| jrXt| j|  dn| |||}|j||t|dd	 | | j| | jd g| jr~d
}nd}|d
kr|| jvr| j| t | j|  }	|	dkrt| jd d | j| d  |	 | j|< t | j|< | jd | j|< q#| }
| |
 t|
t| jdS )a  
        Process an input frame to estimate object speeds based on tracking data.

        Args:
            im0 (np.ndarray): Input image for processing with shape (H, W, C) for RGB images.

        Returns:
            (SolutionResults): Contains processed image `plot_im` and `total_tracks` (number of tracked objects).

        Examples:
            >>> estimator = SpeedEstimator()
            >>> image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
            >>> results = estimator.process(image)
        )
line_width)h   r   {      )reg_ptscolor	thicknessr   z km/hT)labelr   knownunknown   )plot_imtotal_tracks)extract_tracksr   r   draw_regionregionzipboxes	track_idsclssconfsstore_tracking_historyr   r   
track_liner   show_labelsintadjust_box_label	box_labelr   
LineString
intersectsr_sr   appendr   absresultdisplay_outputr   len)r   im0	annotatorboxtrack_idclsconfspeed_label	directiontime_differencer!   r   r   r   process7   s6   
"


"*
zSpeedEstimator.process)__name__
__module____qualname____doc__r
   rB   __classcell__r   r   r   r   r   	   s    r   N)r   ultralytics.solutions.solutionsr   r   r   ultralytics.utils.plottingr   r   r   r   r   r   <module>   s   