a
    yfo                     @   sJ   d dl Z d dlmZ d dlmZ G dd dZedkrFg dZeeZdS )    N)check_imshow)	Annotatorc                   @   s"   e Zd ZdZdddZd	d
 ZdS )AIGymz[A class to manage the gym steps of people in a real-time video stream based on their poses.   F      b@     V@pullupc                 C   sp   d| _ || _d| _|| _|| _d| _d| _d| _d| _|| _	|| _
|| _d| _tdd| _g | _g | _g | _dS )a  
        Initializes the AIGym class with the specified parameters.

        Args:
            kpts_to_check (list): Indices of keypoints to check.
            line_thickness (int, optional): Thickness of the lines drawn. Defaults to 2.
            view_img (bool, optional): Flag to display the image. Defaults to False.
            pose_up_angle (float, optional): Angle threshold for the 'up' pose. Defaults to 145.0.
            pose_down_angle (float, optional): Angle threshold for the 'down' pose. Defaults to 90.0.
            pose_type (str, optional): Type of pose to detect ('pullup', 'pushup', 'abworkout'). Defaults to "pullup".
        NgMbP?T)warn)im0tf	keypointsposeup_angleposedown_angle	thresholdanglecountstage	pose_typekpts_to_checkview_img	annotatorr   	env_check)selfr   Zline_thicknessr   Zpose_up_angleZpose_down_angler    r   X/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/solutions/ai_gym.py__init__   s"    zAIGym.__init__c              	   C   s~  || _ t|d s| j S t|d t| jkrt|d t| j }|  jdg| 7  _|  jdg| 7  _|  jdg| 7  _|d jj| _t|| jd| _	t
t| jD ]\}}| jdv r(| j	|t| jd   |t| jd   |t| jd   | j|< | j	j|| jddd	| _ | jd
v r| j| | jkrNd| j|< | j| | jk r| j| dkrd| j|< | j|  d7  < nf| jdv r| j| | jkrd| j|< | j| | jk r| j| dkrd| j|< | j|  d7  < | j	j| j| | j| | j| |t| jd  d | j	j|dddd q| jrx| jrxtd| j  tdd@ tdkrxdS | j S )z
        Function used to count the gym steps.

        Args:
            im0 (ndarray): Current frame from the video stream.
            results (list): Pose estimation data.
        r   -)
line_width>   squatr   pushup	abworkout   r   )  r"   
   )shaperadius>   r   r    Zdownup>   r   r   )Z
angle_textZ
count_textZ
stage_textZ
center_kptT)r$   r%   Zkpt_linezUltralytics YOLOv8 AI GYM   qN)r
   lenr   r   r   r   datar   r   r   	enumeratereversedr   Zestimate_pose_angleintr   cpuZdraw_specific_pointsr   r   Zplot_angle_and_count_and_stageZkptsr   r   cv2ZimshowZwaitKeyord)r   r
   resultsZ	new_humanindkr   r   r   start_counting;   sT    

"

"
zAIGym.start_countingN)r   Fr   r   r   )__name__
__module____qualname____doc__r   r4   r   r   r   r   r   	   s        
/r   __main__)r   r!   r   )	r/   Zultralytics.utils.checksr   Zultralytics.utils.plottingr   r   r5   r   Zaigymr   r   r   r   <module>   s   t