§
    l¬¸jÏ  ã                   óN   — d dl mZ d dlmZ d dlmZmZmZ  G d„ de¦  «        ZdS )é    )Údefaultdict)ÚAny)ÚBaseSolutionÚSolutionAnnotatorÚSolutionResultsc                   óB   ‡ — e Zd ZdZdeddfˆ fd„Zˆ fd„Zdefd„Zˆ xZ	S )ÚAIGyma@  A class to manage gym steps of people in a real-time video stream based on their poses.

    This class extends BaseSolution to monitor workouts using YOLO pose estimation models. It tracks and counts
    repetitions of exercises based on predefined angle thresholds for up and down positions.

    Attributes:
        states (dict[int, dict[str, float | int | str]]): Per-track angle, rep count, and stage for workout monitoring.
        up_angle (float): Angle threshold for considering the 'up' position of an exercise.
        down_angle (float): Angle threshold for considering the 'down' position of an exercise.
        kpts (list[int]): Indices of keypoints used for angle calculation.

    Methods:
        process: Process a frame to detect poses, calculate angles, and count repetitions.

    Examples:
        >>> gym = AIGym(model="yolo26n-pose.pt")
        >>> image = cv2.imread("gym_scene.jpg")
        >>> results = gym.process(image)
        >>> processed_image = results.plot_im
        >>> cv2.imshow("Processed Image", processed_image)
        >>> cv2.waitKey(0)
    ÚkwargsÚreturnNc                 ó6  •— |                      dd¦  «        |d<    t          ¦   «         j        di |¤Ž t          d„ ¦  «        | _        t          | j        d         ¦  «        | _        t          | j        d         ¦  «        | _        | j        d         | _	        dS )	a  Initialize AIGym for workout monitoring using pose estimation and predefined angles.

        Args:
            **kwargs (Any): Keyword arguments passed to the parent class constructor including:
                - model (str): Model name or path, defaults to "yolo26n-pose.pt".
        Úmodelzyolo26n-pose.ptc                  ó   — ddddœS )Nr   ú-)ÚangleÚcountÚstage© r   ó    úl/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/solutions/ai_gym.pyú<lambda>z AIGym.__init__.<locals>.<lambda>*   s   € °AÀÈCÐ*PÐ*P€ r   Úup_angleÚ
down_angleÚkptsNr   )
ÚgetÚsuperÚ__init__r   ÚstatesÚfloatÚCFGr   r   r   )Úselfr
   Ú	__class__s     €r   r   zAIGym.__init__!   s�   ø€ ð !Ÿ*š* WÐ.?Ñ@Ô@ˆˆw‰Ø�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ý!Ð"PÐ"PÑQÔQˆŒõ ˜dœh zÔ2Ñ3Ô3ˆŒÝ ¤¨Ô 6Ñ7Ô7ˆŒØ”H˜VÔ$ˆŒ	ˆ	ˆ	r   c                 óŠ   •— t          ¦   «                              |¦  «         |D ]}| j                             |d¦  «         ŒdS )z_Drop retired IDs from workout state so it doesn't grow across a 24/7 stream (see BaseSolution).N)r   Úforget_tracksr   Úpop)r    Ú	track_idsÚtrack_idr!   s      €r   r#   zAIGym.forget_tracks1   sM   ø€ å‰Œ×Ò˜iÑ(Ô(Ð(Ø!ð 	,ð 	,ˆHØŒK�OŠO˜H dÑ+Ô+Ð+Ð+ð	,ð 	,r   c                 ó  ‡ ‡— t          |‰ j        ¬¦  «        }‰                      |¦  «         t          ‰ j        ¦  «        �r>‰ j        j        j                             ¦   «          	                    ¦   «         }t          |¦  «        D ]ù\  }Š‰ j        ‰ j        |                  } |j        ˆfd„‰ j        D ¦   «         Ž |d<   |                     ‰‰ j        ‰ j        dz  ¬¦  «         |d         ‰ j        k     r"|d         dk    r|dxx         d	z  cc<   d
|d<   n|d         ‰ j        k    rd|d<   ‰ j        rI|                     |d         |d         |d         ‰t)          ‰ j        d	         ¦  «                 ¬¦  «         Œú|                     ¦   «         }‰                      |¦  «         t/          |ˆ fd„‰ j        D ¦   «         ˆ fd„‰ j        D ¦   «         ˆ fd„‰ j        D ¦   «         t          ‰ j        ¦  «        ¬¦  «        S )aG  Monitor workouts using Ultralytics YOLO Pose Model.

        This function processes an input image to track and analyze human poses for workout monitoring. It uses the YOLO
        Pose model to detect keypoints, estimate angles, and count repetitions based on predefined angle thresholds.

        Args:
            im0 (np.ndarray): Input image for processing.

        Returns:
            (SolutionResults): Contains processed image `plot_im`, 'workout_count' (list[int] of completed reps, one
                entry per currently tracked individual), 'workout_stage' (list[str] of current stages), 'workout_angle'
                (list[float] of current angles), and 'total_tracks' (int, total number of tracked individuals). All
                per-track lists are aligned with the currently visible tracks so that ``len(results.workout_count)
                == results.total_tracks``.

        Examples:
            >>> gym = AIGym()
            >>> image = cv2.imread("workout.jpg")
            >>> results = gym.process(image)
            >>> processed_image = results.plot_im
        )Ú
line_widthc                 ó:   •— g | ]}‰t          |¦  «                 ‘ŒS r   )Úint)Ú.0ÚidxÚks     €r   ú
<listcomp>z!AIGym.process.<locals>.<listcomp>W   s$   ø€ Ð@bÐ@bÐ@bÐQTÀÅ3ÀsÁ8Ä8ÄÐ@bÐ@bÐ@br   r   é   )Úradiusr   Úupr   é   Údown)Ú
angle_textÚ
count_textÚ
stage_textÚ
center_kptc                 ó6   •— g | ]}‰j         |         d          ‘ŒS )r   ©r   ©r+   Útidr    s     €r   r.   z!AIGym.process.<locals>.<listcomp>s   ó%   ø€ ÐOÐOÐO¸˜4œ; sÔ+¨GÔ4ÐOÐOÐOr   c                 ó6   •— g | ]}‰j         |         d          ‘ŒS )r   r9   r:   s     €r   r.   z!AIGym.process.<locals>.<listcomp>t   r<   r   c                 ó6   •— g | ]}‰j         |         d          ‘ŒS )r   r9   r:   s     €r   r.   z!AIGym.process.<locals>.<listcomp>u   r<   r   )Úplot_imÚworkout_countÚworkout_stageÚworkout_angleÚtotal_tracks)r   r(   Úextract_tracksÚlenÚboxesÚtracksÚ	keypointsÚdataÚcpuÚnumpyÚ	enumerater   r%   Úestimate_pose_angler   Údraw_specific_kptsr   r   Úshow_labelsÚplot_angle_and_count_and_stager*   ÚresultÚdisplay_outputr   )r    Úim0Ú	annotatorÚkpt_dataÚiÚstater?   r-   s   `      @r   ÚprocesszAIGym.process7   s0  øø€ õ, & c°d´oÐFÑFÔFˆ	à×Ò˜CÑ Ô Ð åˆtŒz‰?Œ?ñ 	Ø”{Ô,Ô1×5Ò5Ñ7Ô7×=Ò=Ñ?Ô?ˆHå! (Ñ+Ô+ð ð ‘��1Øœ D¤N°1Ô$5Ô6�à!> Ô!>Ð@bÐ@bÐ@bÐ@bÐX\ÔXaÐ@bÑ@bÔ@bÐ!c��g‘Ø×,Ò,¨Q°´	À$Ä/ÐTUÑBUÐ,ÑVÔVÐVð ˜”> D¤OÒ3Ð3Ø˜W”~¨Ò-Ð-Ø˜g˜˜œ¨!Ñ+˜˜™Ø%+�E˜'‘N�NØ˜7”^ d¤mÒ3Ð3Ø%)�E˜'‘Nð Ô#ð Ø×<Ò<Ø#(¨¤>Ø#(¨¤>Ø#(¨¤>Ø#$¥S¨¬°1¬Ñ%6Ô%6Ô#7ð	 =ñ ô ð øð ×"Ò"Ñ$Ô$ˆØ×Ò˜GÑ$Ô$Ð$õ ØØOÐOÐOÐOÀÄÐOÑOÔOØOÐOÐOÐOÀÄÐOÑOÔOØOÐOÐOÐOÀÄÐOÑOÔOÝ˜Tœ^Ñ,Ô,ð
ñ 
ô 
ð 	
r   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r#   r   rX   Ú__classcell__)r!   s   @r   r	   r	   	   s’   ø€ € € € € ðð ð.% ð %¨ð %ð %ð %ð %ð %ð %ð ,ð ,ð ,ð ,ð ,ð@
˜oð @
ð @
ð @
ð @
ð @
ð @
ð @
ð @
r   r	   N)	Úcollectionsr   Útypingr   Úultralytics.solutions.solutionsr   r   r   r	   r   r   r   ú<module>ra      s‡   ðð $Ð #Ð #Ð #Ð #Ð #Ø Ð Ð Ð Ð Ð à \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \ðn
ð n
ð n
ð n
ð n
ˆLñ n
ô n
ð n
ð n
ð n
r   