a
    yfl                     @   sX   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rTddd	Z	ee	Z
dS )
    N)check_imshow)	Annotatorcolorsc                   @   s2   e Zd ZdZdddZdd	 Zd
d Zdd ZdS )DistanceCalculationzdA class to calculate distance between two objects in a real-time video stream based on their tracks.F      r   r   h         c                 C   s^   d| _ d| _|| _|| _|| _|| _d| _|| _d| _g | _	d| _
i | _tdd| _d| _dS )ac  
        Initializes the DistanceCalculation class with the given parameters.

        Args:
            names (dict): Dictionary of classes names.
            view_img (bool, optional): Flag to indicate if the video stream should be displayed. Defaults to False.
            line_thickness (int, optional): Thickness of the lines drawn on the image. Defaults to 2.
            line_color (tuple, optional): Color of the lines drawn on the image (BGR format). Defaults to (255, 255, 0).
            centroid_color (tuple, optional): Color of the centroids drawn (BGR format). Defaults to (255, 0, 255).
        Nr   T)warnzUltralytics Solutions)im0	annotatorview_img
line_colorcentroid_colornamesboxesline_thicknesstrk_ids	centroidsleft_mouse_countselected_boxesr   	env_checkwindow_name)selfr   r   r   r   r    r   f/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/solutions/distance_calculation.py__init__   s    zDistanceCalculation.__init__c                 C   s   |t jkr|  jd7  _| jdkrt| j| jD ]\\}}|d |  k rT|d k r0n q0|d |  k rt|d k r0n q0|| jvr0|| j|< q0n|t jkri | _d| _dS )a  
        Handles mouse events to select regions in a real-time video stream.

        Args:
            event (int): Type of mouse event (e.g., cv2.EVENT_MOUSEMOVE, cv2.EVENT_LBUTTONDOWN, etc.).
            x (int): X-coordinate of the mouse pointer.
            y (int): Y-coordinate of the mouse pointer.
            flags (int): Flags associated with the event (e.g., cv2.EVENT_FLAG_CTRLKEY, cv2.EVENT_FLAG_SHIFTKEY, etc.).
            param (dict): Additional parameters passed to the function.
           r   r      N)cv2ZEVENT_LBUTTONDOWNr   zipr   r   r   ZEVENT_RBUTTONDOWN)r   eventxyflagsparamboxtrack_idr   r   r   mouse_event_for_distance8   s    

J
z,DistanceCalculation.mouse_event_for_distancec           	      C   s  || _ |d jjdu r(| jr$|   |S |d jj | _|d jj  }|d jj	   | _
t| j | jd| _t| j|| j
D ]`\}}}| jj|tt	|d| jt	| d t| jdkr| j D ]}||kr|| j|< qqt| jdkrv| jdd | j D  t| jd d | jd	 d  d | jd d	 | jd	 d	  d  }| j|| j| j| j g | _| jr| jr|   |S )
a7  
        Processes the video frame and calculates the distance between two bounding boxes.

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

        Returns:
            (ndarray): The processed image frame.
        r   N)
line_widthT)colorlabelr   c                 S   s<   g | ]4}t |d  |d  d t |d |d  d gqS )r   r   r    r!   )int).0r)   r   r   r   
<listcomp>p       z5DistanceCalculation.start_process.<locals>.<listcomp>r    )r   r   idr   display_framesZxyxycpuclstolistr/   r   r   r   r   r#   Z	box_labelr   r   lenr   keysr   extendvaluesmathsqrtZplot_distance_and_liner   r   r   )	r   r   ZtracksZclssr)   r6   r*   Ztrk_idZpixels_distancer   r   r   start_processN   s6    &>z!DistanceCalculation.start_processc                 C   sJ   t | j t | j| j t | j| j t dd@ tdkrFdS dS )z,Displays the current frame with annotations.r    r   qN)	r"   ZnamedWindowr   ZsetMouseCallbackr+   Zimshowr   ZwaitKeyord)r   r   r   r   r4      s
    z"DistanceCalculation.display_framesN)Fr   r   r	   )__name__
__module____qualname____doc__r   r+   r>   r4   r   r   r   r   r      s       
*1r   __main__Zpersoncar)r   r    )r<   r"   Zultralytics.utils.checksr   Zultralytics.utils.plottingr   r   r   rA   r   Zdistance_calculationr   r   r   r   <module>   s   ~
