ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ ΠΊ содСрТимому

Бсылка для ultralytics/solutions/ai_gym.py

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅

Π­Ρ‚ΠΎΡ‚ Ρ„Π°ΠΉΠ» доступСн ΠΏΠΎ адрСсу https://github.com/ultralytics/ ultralytics/blob/main/ ultralytics/solutions/ai_gym .py. Если Ρ‚Ρ‹ Π·Π°ΠΌΠ΅Ρ‚ΠΈΠ» ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΡƒ, поТалуйста, ΠΏΠΎΠΌΠΎΠ³ΠΈ ΠΈΡΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ Π΅Π΅, ΠΎΡ‚ΠΏΡ€Π°Π²ΠΈΠ² Pull Request πŸ› οΈ. Бпасибо πŸ™!



ultralytics.solutions.ai_gym.AIGym

Класс для управлСния Ρ‚Ρ€Π΅Π½Π°ΠΆΠ΅Ρ€Π½Ρ‹ΠΌΠΈ шагами людСй Π² Π²ΠΈΠ΄Π΅ΠΎΠΏΠΎΡ‚ΠΎΠΊΠ΅ Π² Ρ€Π΅Π°Π»ΡŒΠ½ΠΎΠΌ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ Π½Π° основС ΠΈΡ… ΠΏΠΎΠ·.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/solutions/ai_gym.py
class AIGym:
    """A class to manage the gym steps of people in a real-time video stream based on their poses."""

    def __init__(self):
        """Initializes the AIGym with default values for Visual and Image parameters."""

        # Image and line thickness
        self.im0 = None
        self.tf = None

        # Keypoints and count information
        self.keypoints = None
        self.poseup_angle = None
        self.posedown_angle = None
        self.threshold = 0.001

        # Store stage, count and angle information
        self.angle = None
        self.count = None
        self.stage = None
        self.pose_type = "pushup"
        self.kpts_to_check = None

        # Visual Information
        self.view_img = False
        self.annotator = None

        # Check if environment support imshow
        self.env_check = check_imshow(warn=True)

    def set_args(
        self,
        kpts_to_check,
        line_thickness=2,
        view_img=False,
        pose_up_angle=145.0,
        pose_down_angle=90.0,
        pose_type="pullup",
    ):
        """
        Configures the AIGym line_thickness, save image and view image parameters.

        Args:
            kpts_to_check (list): 3 keypoints for counting
            line_thickness (int): Line thickness for bounding boxes.
            view_img (bool): display the im0
            pose_up_angle (float): Angle to set pose position up
            pose_down_angle (float): Angle to set pose position down
            pose_type (str): "pushup", "pullup" or "abworkout"
        """
        self.kpts_to_check = kpts_to_check
        self.tf = line_thickness
        self.view_img = view_img
        self.poseup_angle = pose_up_angle
        self.posedown_angle = pose_down_angle
        self.pose_type = pose_type

    def start_counting(self, im0, results, frame_count):
        """
        Function used to count the gym steps.

        Args:
            im0 (ndarray): Current frame from the video stream.
            results (list): Pose estimation data
            frame_count (int): store current frame count
        """
        self.im0 = im0
        if frame_count == 1:
            self.count = [0] * len(results[0])
            self.angle = [0] * len(results[0])
            self.stage = ["-" for _ in results[0]]
        self.keypoints = results[0].keypoints.data
        self.annotator = Annotator(im0, line_width=2)

        for ind, k in enumerate(reversed(self.keypoints)):
            if self.pose_type in {"pushup", "pullup"}:
                self.angle[ind] = self.annotator.estimate_pose_angle(
                    k[int(self.kpts_to_check[0])].cpu(),
                    k[int(self.kpts_to_check[1])].cpu(),
                    k[int(self.kpts_to_check[2])].cpu(),
                )
                self.im0 = self.annotator.draw_specific_points(k, self.kpts_to_check, shape=(640, 640), radius=10)

            if self.pose_type == "abworkout":
                self.angle[ind] = self.annotator.estimate_pose_angle(
                    k[int(self.kpts_to_check[0])].cpu(),
                    k[int(self.kpts_to_check[1])].cpu(),
                    k[int(self.kpts_to_check[2])].cpu(),
                )
                self.im0 = self.annotator.draw_specific_points(k, self.kpts_to_check, shape=(640, 640), radius=10)
                if self.angle[ind] > self.poseup_angle:
                    self.stage[ind] = "down"
                if self.angle[ind] < self.posedown_angle and self.stage[ind] == "down":
                    self.stage[ind] = "up"
                    self.count[ind] += 1
                self.annotator.plot_angle_and_count_and_stage(
                    angle_text=self.angle[ind],
                    count_text=self.count[ind],
                    stage_text=self.stage[ind],
                    center_kpt=k[int(self.kpts_to_check[1])],
                    line_thickness=self.tf,
                )

            if self.pose_type == "pushup":
                if self.angle[ind] > self.poseup_angle:
                    self.stage[ind] = "up"
                if self.angle[ind] < self.posedown_angle and self.stage[ind] == "up":
                    self.stage[ind] = "down"
                    self.count[ind] += 1
                self.annotator.plot_angle_and_count_and_stage(
                    angle_text=self.angle[ind],
                    count_text=self.count[ind],
                    stage_text=self.stage[ind],
                    center_kpt=k[int(self.kpts_to_check[1])],
                    line_thickness=self.tf,
                )
            if self.pose_type == "pullup":
                if self.angle[ind] > self.poseup_angle:
                    self.stage[ind] = "down"
                if self.angle[ind] < self.posedown_angle and self.stage[ind] == "down":
                    self.stage[ind] = "up"
                    self.count[ind] += 1
                self.annotator.plot_angle_and_count_and_stage(
                    angle_text=self.angle[ind],
                    count_text=self.count[ind],
                    stage_text=self.stage[ind],
                    center_kpt=k[int(self.kpts_to_check[1])],
                    line_thickness=self.tf,
                )

            self.annotator.kpts(k, shape=(640, 640), radius=1, kpt_line=True)

        if self.env_check and self.view_img:
            cv2.imshow("Ultralytics YOLOv8 AI GYM", self.im0)
            if cv2.waitKey(1) & 0xFF == ord("q"):
                return

        return self.im0

__init__()

Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅Ρ‚ AIGym со значСниями ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ для ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ² Visual ΠΈ Image.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/solutions/ai_gym.py
def __init__(self):
    """Initializes the AIGym with default values for Visual and Image parameters."""

    # Image and line thickness
    self.im0 = None
    self.tf = None

    # Keypoints and count information
    self.keypoints = None
    self.poseup_angle = None
    self.posedown_angle = None
    self.threshold = 0.001

    # Store stage, count and angle information
    self.angle = None
    self.count = None
    self.stage = None
    self.pose_type = "pushup"
    self.kpts_to_check = None

    # Visual Information
    self.view_img = False
    self.annotator = None

    # Check if environment support imshow
    self.env_check = check_imshow(warn=True)

set_args(kpts_to_check, line_thickness=2, view_img=False, pose_up_angle=145.0, pose_down_angle=90.0, pose_type='pullup')

НастраиваСт ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ Ρ‚ΠΎΠ»Ρ‰ΠΈΠ½Ρ‹ Π»ΠΈΠ½ΠΈΠΈ AIGym, сохранСния изобраТСния ΠΈ просмотра изобраТСния.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
kpts_to_check list

3 ΠΊΠ»ΡŽΡ‡Π΅Π²Ρ‹Ρ… ΠΌΠΎΠΌΠ΅Π½Ρ‚Π° для подсчСта

трСбуСтся
line_thickness int

Π’ΠΎΠ»Ρ‰ΠΈΠ½Π° Π»ΠΈΠ½ΠΈΠΉ для ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… Ρ€Π°ΠΌΠΎΠΊ.

2
view_img bool

Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ Π½Π° экран ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ im0

False
pose_up_angle float

Π£Π³ΠΎΠ» для установки ΠΏΠΎΠ·Ρ‹

145.0
pose_down_angle float

Π£Π³ΠΎΠ» для установки ΠΏΠΎΠ·Ρ‹ Π²Π½ΠΈΠ·

90.0
pose_type str

"ΠžΡ‚ΠΆΠΈΠΌΠ°Π½ΠΈΠ΅", "подтягиваниС" ΠΈΠ»ΠΈ "Ρ‚Ρ€Π΅Π½ΠΈΡ€ΠΎΠ²ΠΊΠ° ΠΌΡ‹ΡˆΡ† ΠΆΠΈΠ²ΠΎΡ‚Π°".

'pullup'
Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/solutions/ai_gym.py
def set_args(
    self,
    kpts_to_check,
    line_thickness=2,
    view_img=False,
    pose_up_angle=145.0,
    pose_down_angle=90.0,
    pose_type="pullup",
):
    """
    Configures the AIGym line_thickness, save image and view image parameters.

    Args:
        kpts_to_check (list): 3 keypoints for counting
        line_thickness (int): Line thickness for bounding boxes.
        view_img (bool): display the im0
        pose_up_angle (float): Angle to set pose position up
        pose_down_angle (float): Angle to set pose position down
        pose_type (str): "pushup", "pullup" or "abworkout"
    """
    self.kpts_to_check = kpts_to_check
    self.tf = line_thickness
    self.view_img = view_img
    self.poseup_angle = pose_up_angle
    self.posedown_angle = pose_down_angle
    self.pose_type = pose_type

start_counting(im0, results, frame_count)

Ѐункция, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌΠ°Ρ для подсчСта шагов Π² Ρ‚Ρ€Π΅Π½Π°ΠΆΠ΅Ρ€Π½ΠΎΠΌ Π·Π°Π»Π΅.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
im0 ndarray

Π’Π΅ΠΊΡƒΡ‰ΠΈΠΉ ΠΊΠ°Π΄Ρ€ ΠΈΠ· Π²ΠΈΠ΄Π΅ΠΎΠΏΠΎΡ‚ΠΎΠΊΠ°.

трСбуСтся
results list

Π”Π°Π½Π½Ρ‹Π΅ для ΠΎΡ†Π΅Π½ΠΊΠΈ ΠΏΠΎΠ·Ρ‹

трСбуСтся
frame_count int

ΡΠΎΡ…Ρ€Π°Π½ΠΈΡ‚ΡŒ Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ счСтчик ΠΊΠ°Π΄Ρ€ΠΎΠ²

трСбуСтся
Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/solutions/ai_gym.py
def start_counting(self, im0, results, frame_count):
    """
    Function used to count the gym steps.

    Args:
        im0 (ndarray): Current frame from the video stream.
        results (list): Pose estimation data
        frame_count (int): store current frame count
    """
    self.im0 = im0
    if frame_count == 1:
        self.count = [0] * len(results[0])
        self.angle = [0] * len(results[0])
        self.stage = ["-" for _ in results[0]]
    self.keypoints = results[0].keypoints.data
    self.annotator = Annotator(im0, line_width=2)

    for ind, k in enumerate(reversed(self.keypoints)):
        if self.pose_type in {"pushup", "pullup"}:
            self.angle[ind] = self.annotator.estimate_pose_angle(
                k[int(self.kpts_to_check[0])].cpu(),
                k[int(self.kpts_to_check[1])].cpu(),
                k[int(self.kpts_to_check[2])].cpu(),
            )
            self.im0 = self.annotator.draw_specific_points(k, self.kpts_to_check, shape=(640, 640), radius=10)

        if self.pose_type == "abworkout":
            self.angle[ind] = self.annotator.estimate_pose_angle(
                k[int(self.kpts_to_check[0])].cpu(),
                k[int(self.kpts_to_check[1])].cpu(),
                k[int(self.kpts_to_check[2])].cpu(),
            )
            self.im0 = self.annotator.draw_specific_points(k, self.kpts_to_check, shape=(640, 640), radius=10)
            if self.angle[ind] > self.poseup_angle:
                self.stage[ind] = "down"
            if self.angle[ind] < self.posedown_angle and self.stage[ind] == "down":
                self.stage[ind] = "up"
                self.count[ind] += 1
            self.annotator.plot_angle_and_count_and_stage(
                angle_text=self.angle[ind],
                count_text=self.count[ind],
                stage_text=self.stage[ind],
                center_kpt=k[int(self.kpts_to_check[1])],
                line_thickness=self.tf,
            )

        if self.pose_type == "pushup":
            if self.angle[ind] > self.poseup_angle:
                self.stage[ind] = "up"
            if self.angle[ind] < self.posedown_angle and self.stage[ind] == "up":
                self.stage[ind] = "down"
                self.count[ind] += 1
            self.annotator.plot_angle_and_count_and_stage(
                angle_text=self.angle[ind],
                count_text=self.count[ind],
                stage_text=self.stage[ind],
                center_kpt=k[int(self.kpts_to_check[1])],
                line_thickness=self.tf,
            )
        if self.pose_type == "pullup":
            if self.angle[ind] > self.poseup_angle:
                self.stage[ind] = "down"
            if self.angle[ind] < self.posedown_angle and self.stage[ind] == "down":
                self.stage[ind] = "up"
                self.count[ind] += 1
            self.annotator.plot_angle_and_count_and_stage(
                angle_text=self.angle[ind],
                count_text=self.count[ind],
                stage_text=self.stage[ind],
                center_kpt=k[int(self.kpts_to_check[1])],
                line_thickness=self.tf,
            )

        self.annotator.kpts(k, shape=(640, 640), radius=1, kpt_line=True)

    if self.env_check and self.view_img:
        cv2.imshow("Ultralytics YOLOv8 AI GYM", self.im0)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            return

    return self.im0





Боздано 2023-12-02, ОбновлСно 2024-05-08
Авторы: Burhan-Q (1), RizwanMunawar (1)