Ë
    Fêñi=  ã                   óF   — d dl mZ d dlmZ d dlmZmZmZ  G d„ de«      Zy)é    )Údefaultdict)ÚAny)ÚBaseSolutionÚSolutionAnnotatorÚSolutionResultsc                   ó8   ‡ — e Zd ZdZdeddfˆ 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                 ó  •— |j                  dd«      |d<   t        ‰| �  di |¤Ž t        d„ «      | _        t        | j                  d   «      | _        t        | j                  d   «      | _        | j                  d   | _	        y)	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   ó    ú^/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/solutions/ai_gym.pyú<lambda>z AIGym.__init__.<locals>.<lambda>*   s   € °AÀÈCÑ*P€ r   Úup_angleÚ
down_angleÚkptsNr   )
ÚgetÚsuperÚ__init__r   ÚstatesÚfloatÚCFGr   r   r   )Úselfr
   Ú	__class__s     €r   r   zAIGym.__init__!   ss   ø€ ð !Ÿ*™* WÐ.?Ó@ˆˆw‰Ü‰ÑÑ"˜6Ò"Ü!Ñ"PÓQˆŒô ˜dŸh™h zÑ2Ó3ˆŒÜ §¡¨Ñ 6Ó7ˆŒØ—H‘H˜VÑ$ˆ�	r   c                 ó˜  — t        || j                  ¬«      }| j                  |«       t        | j                  «      �r:| j
                  j                  j                  }t        |«      D �]  \  }}| j                  | j                  |      } |j                  | j                  D �cg c]  }|t        |«         ‘Œ c}Ž |d<   |j                  || j                  | j                  dz  ¬«       |d   | j                  k  r|d   dk(  r|dxx   dz  cc<   d	|d<   n|d   | j                   kD  rd|d<   | j"                  sŒÕ|j%                  |d   |d   |d   |t        | j                  d   «         ¬
«       �Œ |j'                  «       }| j)                  |«       t+        || j                  j-                  «       D �	cg c]  }	|	d   ‘Œ	 c}	| j                  j-                  «       D �	cg c]  }	|	d   ‘Œ	 c}	| j                  j-                  «       D �	cg c]  }	|	d   ‘Œ	 c}	t        | j                  «      ¬«      S c c}w c c}	w c c}	w c c}	w )a[  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 of completed reps),
                'workout_stage' (list of current stages), 'workout_angle' (list of angles), and 'total_tracks' (total
                number of tracked individuals).

        Examples:
            >>> gym = AIGym()
            >>> image = cv2.imread("workout.jpg")
            >>> results = gym.process(image)
            >>> processed_image = results.plot_im
        )Ú
line_widthr   é   )Úradiusr   Úupr   é   Údown)Ú
angle_textÚ
count_textÚ
stage_textÚ
center_kpt)Úplot_imÚworkout_countÚworkout_stageÚworkout_angleÚtotal_tracks)r   r#   Úextract_tracksÚlenÚboxesÚtracksÚ	keypointsÚdataÚ	enumerater   Ú	track_idsÚestimate_pose_angler   ÚintÚdraw_specific_kptsr   r   Úshow_labelsÚplot_angle_and_count_and_stageÚresultÚdisplay_outputr   Úvalues)
r    Úim0Ú	annotatorÚkpt_dataÚiÚkÚstateÚidxr-   Úvs
             r   ÚprocesszAIGym.process1   s  € ô( & c°d·o±oÔFˆ	à×Ñ˜CÔ äˆt�z‰z�?Ø—{‘{×,Ñ,×1Ñ1ˆHä! (Ó+ó ‘��1ØŸ™ D§N¡N°1Ñ$5Ñ6�à!> ×!>Ñ!>ÐX\×XaÑXaÖ@bÐQTÀÄ3ÀsÃ8ÃÒ@bÐ!c��g‘Ø×,Ñ,¨Q°·	±	À$Ç/Á/ÐTUÑBUÐ,ÔVð ˜‘> D§O¡OÒ3Ø˜W‘~¨Ò-Ø˜g›¨!Ñ+›Ø%+�E˜'’NØ˜7‘^ d§m¡mÒ3Ø%)�E˜'‘Nð ×#Ó#Ø×<Ñ<Ø#(¨¡>Ø#(¨¡>Ø#(¨¡>Ø#$¤S¨¯©°1©Ó%6Ñ#7ð	 =ö ð!ð, ×"Ñ"Ó$ˆØ×Ñ˜GÔ$ô ØØ/3¯{©{×/AÑ/AÓ/CÖD¨!˜1˜W›:ÒDØ/3¯{©{×/AÑ/AÓ/CÖD¨!˜1˜W›:ÒDØ/3¯{©{×/AÑ/AÓ/CÖD¨!˜1˜W›:ÒDÜ˜TŸ^™^Ó,ô
ð 	
ùò/ Acùò2 EùÚDùÚDs   Â'H8
Æ<H=
Ç&IÈI)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   rJ   Ú__classcell__)r!   s   @r   r	   r	   	   s'   ø„ ñð.% ð %¨õ %ð ;
˜o÷ ;
r   r	   N)	Úcollectionsr   Útypingr   Úultralytics.solutions.solutionsr   r   r   r	   r   r   r   ú<module>rS      s!   ðõ $Ý ç \Ñ \ôc
ˆLõ c
r   