§
    l¬¸j„  ã                   ó^   — d dl Z d dlmZ d dlZd dlmZmZmZ d dlm	Z	  G d„ de¦  «        Z
dS )é    N)ÚAny)ÚBaseSolutionÚSolutionAnnotatorÚSolutionResults)Úcolorsc                   óX   ‡ — e Zd ZdZdeddfˆ fd„Zdededed	ed
eddfd„Zdefd„Z	ˆ xZ
S )ÚDistanceCalculationa  A class to calculate distance between two objects in a real-time video stream based on their tracks.

    This class extends BaseSolution to provide functionality for selecting objects and calculating the distance between
    them in a video stream using YOLO object detection and tracking.

    Attributes:
        left_mouse_count (int): Counter for left mouse button clicks.
        selected_boxes (dict[int, Any]): Dictionary to store selected bounding boxes keyed by track ID.
        centroids (list[list[int]]): List to store centroids of selected bounding boxes.

    Methods:
        mouse_event_for_distance: Handle mouse events for selecting objects in the video stream.
        process: Process video frames and calculate the distance between selected objects.

    Examples:
        >>> distance_calc = DistanceCalculation()
        >>> frame = cv2.imread("frame.jpg")
        >>> results = distance_calc.process(frame)
        >>> cv2.imshow("Distance Calculation", results.plot_im)
        >>> cv2.waitKey(0)
    ÚkwargsÚreturnNc                 ód   •—  t          ¦   «         j        di |¤Ž d| _        i | _        g | _        dS )zYInitialize the DistanceCalculation class for measuring object distances in video streams.r   N© )ÚsuperÚ__init__Úleft_mouse_countÚselected_boxesÚ	centroids)Úselfr
   Ú	__class__s     €úz/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/solutions/distance_calculation.pyr   zDistanceCalculation.__init__#   s=   ø€ à�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"ð !"ˆÔØ68ˆÔØ*,ˆŒˆˆó    ÚeventÚxÚyÚflagsÚparamc                 ó~  — |t           j        k    rŒ| xj        dz  c_        | j        dk    rmt          | j        | j        ¦  «        D ]T\  }}|                      |¦  «        \  }}	}
}||cxk     r|
k     r&n Œ/|	|cxk     r|k     rn Œ?|| j        vr
|| j        |<   ŒSdS dS |t           j        k    ri | _        d| _        dS dS )a§  Handle mouse events to select regions in a real-time video stream for distance calculation.

        Args:
            event (int): Type of mouse event (e.g., cv2.EVENT_MOUSEMOVE, cv2.EVENT_LBUTTONDOWN).
            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).
            param (Any): Additional parameters passed to the function.

        Examples:
            >>> # Assuming 'dc' is an instance of DistanceCalculation
            >>> cv2.setMouseCallback("window_name", dc.mouse_event_for_distance)
        é   é   r   N)	Úcv2ÚEVENT_LBUTTONDOWNr   ÚzipÚboxesÚ	track_idsÚget_enclosing_boxr   ÚEVENT_RBUTTONDOWN)r   r   r   r   r   r   ÚboxÚtrack_idÚx0Úy0Úx1Úy1s               r   Úmouse_event_for_distancez,DistanceCalculation.mouse_event_for_distance,   sü   € ð •CÔ)Ò)Ð)ØÐ!Ô! QÑ&Ð!Ô!ØÔ$¨Ò)Ð)Ý%(¨¬°T´^Ñ%DÔ%Dð <ð <‘M�C˜Ø%)×%;Ò%;¸CÑ%@Ô%@‘N�B˜˜B Ø˜A�{�{’{�{ ’{�{�{�{�{ r¨A { {¢{ {°¢{ { { { {°xÀtÔGZÐ7ZÐ7ZØ8;˜Ô+¨HÑ5øð	 *Ð)ð<ð <ð
 •cÔ+Ò+Ð+Ø"$ˆDÔØ$%ˆDÔ!Ð!Ð!ð ,Ð+r   c           
      óš  — |                       |¦  «         t          || j        ¬¦  «        }d}t          | j        | j        | j        | j        ¦  «        D ]�\  }}}}|                     |t          t          |¦  «        d¦  «        |                      |||¦  «        ¬¦  «         t          | j        ¦  «        dk    r| j        D ]}||k    r
|| j        |<   ŒŒ‚t          | j        ¦  «        dk    rÐ| j                             d„ t!          | j        | j                             ¦   «         ¦  «        D ¦   «         ¦  «         t'          j        | j        d         d         | j        d         d         z
  dz  | j        d         d         | j        d         d         z
  dz  z   ¦  «        }|                     || j        ¦  «         g | _        |                     ¦   «         }	|                      |	¦  «         | j                             d¦  «        r!| j        rt7          j        d	| j        ¦  «         t=          |	|t          | j        ¦  «        ¬
¦  «        S )aç  Process a video frame and calculate the distance between two selected bounding boxes.

        This method extracts tracks from the input frame, annotates bounding boxes, and calculates the distance between
        two user-selected objects if they have been chosen.

        Args:
            im0 (np.ndarray): The input image frame to process.

        Returns:
            (SolutionResults): Contains processed image `plot_im`, `total_tracks` (int) representing the total number of
                tracked objects, and `pixels_distance` (float) representing the distance between selected objects
                in pixels.

        Examples:
            >>> import numpy as np
            >>> from ultralytics.solutions import DistanceCalculation
            >>> dc = DistanceCalculation()
            >>> frame = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
            >>> results = dc.process(frame)
            >>> print(f"Distance: {results.pixels_distance:.2f} pixels")
        )Ú
line_widthr   T)ÚcolorÚlabelr   c                 ó’   — g | ]D}t          |d          |d         z   dz  ¦  «        t          |d         |d         z   dz  ¦  «        g‘ŒES )r   r   r   é   )Úint)Ú.0r&   s     r   ú
<listcomp>z/DistanceCalculation.process.<locals>.<listcomp>m   s_   € ð ð ð àõ ˜#˜aœ& 3 q¤6™/¨aÑ/Ñ0Ô0µ#°s¸1´vÀÀAÄ±È1Ñ6LÑ2MÔ2MÐNðð ð r   r   ÚshowzUltralytics Solutions)Úplot_imÚpixels_distanceÚtotal_tracks)Úextract_tracksr   r.   r!   r"   r#   ÚclssÚconfsÚ	box_labelr   r3   Úadjust_box_labelÚlenr   r   ÚextendÚmapr$   ÚvaluesÚmathÚsqrtÚplot_distance_and_lineÚresultÚdisplay_outputÚCFGÚgetÚ	env_checkr   ÚsetMouseCallbackr,   r   )
r   Úim0Ú	annotatorr8   r&   r'   ÚclsÚconfÚtrk_idr7   s
             r   ÚprocesszDistanceCalculation.processF   sU  € ð, 	×Ò˜CÑ Ô Ð Ý% c°d´oÐFÑFÔFˆ	àˆå(+¨D¬J¸¼ÈÌ	ÐSWÔS]Ñ(^Ô(^ð 	<ð 	<Ñ$ˆC�˜3 Ø×Ò ­6µ#°c±(´(¸DÑ+AÔ+AÈ×I^ÒI^Ð_bÐdhÐjrÑIsÔIsÐÑtÔtÐtõ �4Ô&Ñ'Ô'¨1Ò,Ð,Ø"Ô1ð <ð <�FØ Ò)Ð)Ø8;˜Ô+¨HÑ5øøåˆtÔ"Ñ#Ô# qÒ(Ð(àŒN×!Ò!ðð å" 4Ô#9¸4Ô;N×;UÒ;UÑ;WÔ;WÑXÔXðñ ô ñô ð õ #œiØ” Ô" 1Ô%¨¬°qÔ(9¸!Ô(<Ñ<ÀÑBÀdÄnÐUVÔFWÐXYÔFZÐ]aÔ]kÐlmÔ]nÐopÔ]qÑFqÐvwÑEwÑwñô ˆOð ×,Ò,¨_¸d¼nÑMÔMÐMàˆŒØ×"Ò"Ñ$Ô$ˆØ×Ò˜GÑ$Ô$Ð$ØŒ8�<Š<˜ÑÔð 	Y D¤Nð 	YÝÔ Ð!8¸$Ô:WÑXÔXÐXõ  wÀÕ^aÐbfÔbpÑ^qÔ^qÐrÑrÔrÐrr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r3   r,   r   rQ   Ú__classcell__)r   s   @r   r	   r	      s¹   ø€ € € € € ðð ð,- ð -¨ð -ð -ð -ð -ð -ð -ð&¨cð &°cð &¸cð &È#ð &ÐVYð &Ð^bð &ð &ð &ð &ð49s˜oð 9sð 9sð 9sð 9sð 9sð 9sð 9sð 9sr   r	   )rC   Útypingr   r   Úultralytics.solutions.solutionsr   r   r   Úultralytics.utils.plottingr   r	   r   r   r   ú<module>rZ      s£   ðð €€€Ø Ð Ð Ð Ð Ð à 
€
€
€
à \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ø -Ð -Ð -Ð -Ð -Ð -ðssð ssð ssð ssð ss˜,ñ ssô ssð ssð ssð ssr   