
    h$                     V    S SK r S SKJr  S SKrS SKJrJrJr  S SKJ	r	   " S S\5      r
g)    N)Any)BaseSolutionSolutionAnnotatorSolutionResults)colorsc                   h   ^  \ rS rSrSrS\SS4U 4S jjrS\S\S	\S
\S\SS4S jrS\	4S jr
SrU =r$ )DistanceCalculation   a  
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, list[float]]): Dictionary to store selected bounding boxes and their track IDs.
    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                 P   > [         TU ]  " S0 UD6  SU l        0 U l        / U l        g)zYInitialize the DistanceCalculation class for measuring object distances in video streams.r   N )super__init__left_mouse_countselected_boxes	centroids)selfr   	__class__s     d/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/solutions/distance_calculation.pyr   DistanceCalculation.__init__$   s,    "6" !"68*,    eventxyflagsparamc                    U[         R                  :X  a  U =R                  S-  sl        U R                  S::  a{  [        U R                  U R
                  5       HV  u  pgUS   Us=:  a  US   :  d  M  O  M  US   Us=:  a  US   :  d  M3  O  M7  XpR                  ;  d  MH  X`R                  U'   MX     ggU[         R                  :X  a  0 U l        SU l        gg)aX  
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_idsr   EVENT_RBUTTONDOWN)r   r   r   r   r   r   boxtrack_ids           r   mouse_event_for_distance,DistanceCalculation.mouse_event_for_distance-   s     C)))!!Q&!$$)%(T^^%DMC1v*CF**s1v/BCF/B/BxWjWjGj8;++H5 &E *
 c+++"$D$%D! ,r   c                 "   U R                  U5        [        XR                  S9nSn[        U R                  U R
                  U R                  U R                  5       H  u  pEpgUR                  U[        [        U5      S5      U R                  XgU5      S9  [        U R                  5      S:X  d  MU  U R                  R                  5        H  nX:X  d  M
  X@R                  U'   M     M     [        U R                  5      S:X  a  U R                  R!                  U R                  R#                  5        Vs/ s H1  n[        US   US   -   S-  5      [        US   US   -   S-  5      /PM3     sn5        [$        R&                  " U R                  S   S   U R                  S   S   -
  S-  U R                  S   S   U R                  S   S   -
  S-  -   5      nUR)                  X0R                  5        / U l        UR+                  5       n	U R-                  U	5        U R.                  R1                  S5      (       a2  U R2                  (       a!  [4        R6                  " S	U R8                  5        [;        X[        U R
                  5      S
9$ s  snf )ah  
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    r   r!   showzUltralytics Solutions)plot_impixels_distancetotal_tracks)extract_tracksr   r-   r$   r%   r&   clssconfs	box_labelr   intadjust_box_labellenr   keysr   extendvaluesmathsqrtplot_distance_and_lineresultdisplay_outputCFGget	env_checkr"   setMouseCallbackr*   r   )
r   im0	annotatorr2   r(   r)   clsconftrk_idr1   s
             r   processDistanceCalculation.processG   s5   . 	C %cooF	(+DJJ		SWS]S](^$C36#c(D+AI^I^_bjrIst 4&&'1,"11668F)8;++H5 9 )_ t""#q(NN!!W[WjWjWqWqWstWsPS#s1vA1,-sCFSVO3I/JKWst #ii"1%q(9!(<<BdnnUVFWXYFZ]a]k]klm]nop]qFqvwEwwO ,,_nnM""$G$88<<DNN  !8$:W:WX w^abfbpbp^qrr us   68J)r   r   r   )__name__
__module____qualname____firstlineno____doc__r   r   r8   r*   r   rL   __static_attributes____classcell__)r   s   @r   r	   r	      s`    .- - -&c &c &c &# &VY &^b &47so 7s 7sr   r	   )r>   typingr   r"   ultralytics.solutions.solutionsr   r   r   ultralytics.utils.plottingr   r	   r   r   r   <module>rX      s)      
 \ \ -rs, rsr   