o
    Wh                     @   s(   d dl mZmZmZ G dd deZdS )    )BaseSolutionSolutionAnnotatorSolutionResultsc                       s(   e Zd ZdZ fddZdd Z  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:
        count (List[int]): Repetition counts for each detected person.
        angle (List[float]): Current angle of the tracked body part for each person.
        stage (List[str]): Current exercise stage ('up', 'down', or '-') for each person.
        initial_stage (str | None): Initial stage of the exercise.
        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: Processes a frame to detect poses, calculate angles, and count repetitions.

    Examples:
        >>> gym = AIGym(model="yolo11n-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)
    c                    sj   | dd|d< t jdi | g | _g | _g | _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.
                model (str): Model name or path, defaults to "yolo11n-pose.pt".
        modelzyolo11n-pose.ptNup_angle
down_anglekpts )getsuper__init__countanglestageinitial_stagefloatCFGr   r   r	   )selfkwargs	__class__r
   P/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/solutions/ai_gym.pyr   "   s   zAIGym.__init__c              	      s  t |jd}| jd }|jjdurt|tjkrGt|tj } jdg| 7  _ jdg| 7  _ j	dg| 7  _	t
t|jjD ]k\}  fddtdD }|j| j|< |j jjd d j| jk rj	| d	krj|  d
7  < dj	|< nj| jkrd	j	|< jr|jj| j| j	|  tjd
  d qO| }| t|jj	jtjdS )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   N-c                    s"   g | ]} t j|   qS r
   )intr	   cpu).0ikr   r
   r   
<listcomp>]   s   " z!AIGym.process.<locals>.<listcomp>   )radiusup   down)
angle_text
count_text
stage_text
center_kpt)plot_imworkout_countworkout_stageworkout_angletotal_tracks)r   r   extract_trackstracksboxesidlenr   r   r   	enumeratereversed	keypointsdatarangeestimate_pose_angledraw_specific_kptsr	   r   r   show_labelsplot_angle_and_count_and_stager   resultdisplay_outputr   	track_ids)r   im0	annotatorr1   	new_humanindr	   r+   r
   r   r   process6   sH   



zAIGym.process)__name__
__module____qualname____doc__r   rE   __classcell__r
   r
   r   r   r      s    r   N)ultralytics.solutions.solutionsr   r   r   r   r
   r
   r
   r   <module>   s   