a
    yf                     @   sp   d dl mZ d dlmZ d dlZd dlZd dlmZ d dlm	Z	m
Z
 G dd dZedkrld	d
dZeeZdS )    )defaultdict)timeN)check_imshow)	Annotatorcolorsc                   @   s"   e Zd ZdZd
ddZdd	 ZdS )SpeedEstimatorz[A class to estimate the speed of objects in a real-time video stream based on their tracks.NF   
   c                 C   s`   |dur|nddg| _ || _tt| _|| _|| _i | _g | _|| _	i | _
i | _tdd| _dS )aH  
        Initializes the SpeedEstimator with the given parameters.

        Args:
            names (dict): Dictionary of class names.
            reg_pts (list, optional): List of region points for speed estimation. Defaults to [(20, 400), (1260, 400)].
            view_img (bool, optional): Whether to display the image with annotations. Defaults to False.
            line_thickness (int, optional): Thickness of the lines for drawing boxes and tracks. Defaults to 2.
            spdl_dist_thresh (int, optional): Distance threshold for speed calculation. Defaults to 10.
        N)     )i  r   T)warn)reg_ptsnamesr   listtrk_historyview_imgtfspdtrkd_idsspdltrk_pttrk_ppr   	env_check)selfr   r   r   Zline_thicknessZspdl_dist_thresh r   b/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/solutions/speed_estimation.py__init__   s    
zSpeedEstimator.__init__c                 C   sP  |d j jdu r|S |d j j }|d j j  }|d j j   }t|| jd}|j	| j
d| jd d t|||D ]\}}}	| j| }
t|d |d  d t|d |d  d f}|
| t|
d	kr|
d t|
tjd
}|| jvrd| j|< || jv r6t| j|  dn| jt|	 }tt|d}|||| tj||gd|| jd t|t|
d d t|
d d f| jd |d | j
d d |
d d   k r| j
d d k sn  dS | j
d d | j |
d d   k r(| j
d d | j k r2n nd}nN| j
d d | j |
d d   k rr| j
d d | j k r|n nd}nd}| j|dkr|dkr|| j vr| j | t! | j|  }|dkrt"|
d d | j#| d  | | j|< t! | j|< |
d | j#|< q| j$rL| j%rLt&d| t'dd@ t(dkrLdS |S )a"  
        Estimates the speed of objects based on tracking data.

        Args:
            im0 (ndarray): Image.
            tracks (list): List of tracks obtained from the object tracking process.

        Returns:
            (ndarray): The image with annotated boxes and tracks.
        r   N)
line_width)   r   r   r   )r   color	thickness         )r!   r   z km/hTF)ZisClosedr   r    r$   ZknownunknownzUltralytics Speed Estimationr   q))boxesidZxyxycpuclstolistintr   r   Zdraw_regionr   zipr   floatappendlenpopnpZhstackZastypeZint32Zreshaper   r   r   r   Z	box_labelcv2Z	polylinescircler   getr   r   absr   r   r   ZimshowZwaitKeyord)r   Zim0Ztracksr'   ZclssZt_idsZ	annotatorboxZt_idr*   trackZbbox_centerZtrk_ptsZspeed_labelZ
bbox_color	directionZtime_differencer   r   r   estimate_speed.   sP    
0


.46DD(
*zSpeedEstimator.estimate_speed)NFr   r	   )__name__
__module____qualname____doc__r   r;   r   r   r   r   r      s   
r   __main__Zpersoncar)r   r!   )collectionsr   r   r3   numpyr2   Zultralytics.utils.checksr   Zultralytics.utils.plottingr   r   r   r<   r   Zspeed_estimatorr   r   r   r   <module>   s   e
