From 8daea93470d7a9ecb88cbbfeab9d2439e227eda2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 24 Sep 2024 15:30:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=8C=E6=9D=A0=E8=87=82=E5=B1=88=E4=BC=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Exercise3/tricep_dip.py | 223 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 Exercise3/tricep_dip.py diff --git a/Exercise3/tricep_dip.py b/Exercise3/tricep_dip.py new file mode 100644 index 0000000..f8850ec --- /dev/null +++ b/Exercise3/tricep_dip.py @@ -0,0 +1,223 @@ +import math +import time + +import numpy as np + +from MCamera.mp_camera import * +from MCamera.camera import * +from Database.manager_database import * +from score_doc import get_fin_score +from .base_exercise import BaseExercise + + +class Tricep_dip_1(BaseExercise): + def __init__(self, info, statistic_time=120, camera=None): + super().__init__(info, statistic_time, camera=camera) + # 个数统计 + self.bar = None + self.per = None + self.count = 0 + # 当前状态 + self.heel = 0 + # 动作持续帧数 + self.time_2 = 0 + self.time_1 = 0 + self.time = 0 + self.starttime = 0 + # 开始标志 + self.form = 0 + # 状态反馈 + self.pre_pos = 0 + self.sta_time = time.time() + self.feedback = "开始" + self.exercise_type = "双杠臂屈伸" + self.end_test = 0 + self.had_done = False + self.sign = 0 + self.readiness_state = 0 + # 摆动状态 + self.state = 1 + # 初始化 + self.initial_wrist_1_y = 0 + self.initial_wrist_2_y = 0 + + # 双杠臂屈伸参数 + self.corner = (0, 480, 260, 380) + MediapipeAlgorithmPlugin.set_corner(corner=self.corner) + MediapipeAlgorithmPlugin.set_config(config=self.config) + + self.a = time.time() + self.b = 0 + + def get_result(self): + # 人员年龄 + age = self.info.get(AGE) + # 人员性别 + gender = "woman" if self.info.get(GENDER) == "女" else "man" + # score = get_fin_score.Military(gender, int(age), + # integratedProjectResult=int(self.count)).IntegratedProjectScoreEvaluation() + score = int(self.count) + if self.starttime != 0: + this_time = time.time() + count_time = this_time - self.starttime + else: + count_time = 0 + result = { + "count": int(self.count), + "score": score, + "had_done": self.had_done, + 'countdown': count_time + } + return result + + def speak_counting(self, counting_times, name): + self.speak_driver.start() + self.speak_driver.speed_control(200) + self.speak_driver.volume_control(1) + self.speak_driver.add_speak("考试人员{}".format(name)) + self.speak_driver.add_speak(f"考试项目{self.exercise_type}") + + def display(self, img): + cv2.rectangle(img, (self.corner[2], self.corner[0]), (self.corner[3], self.corner[1]), (0, 255, 0), 2) + # 绘制状态条 + cv2.rectangle(img, (650 - 100, 60), (650 - 80, 282), (255, 255, 255), 2) + self.detector.drawPoint_more(img, [9, 11, 13, 19, 25], bias_x=self.corner[2], bias_y=self.corner[0]) + self.detector.drawPoint_more(img, [10, 12, 14, 20, 26], bias_x=self.corner[2], bias_y=self.corner[0]) + if self.bar and self.per: + cv2.rectangle(img, (650 - 98, int(self.bar)), (650 - 82, 280), (102, 106, 233), + cv2.FILLED) + cv2.putText(img, f'{int(self.per)}%', (650 - 125, 320), cv2.FONT_HERSHEY_PLAIN, 2, + (255, 255, 255), 2) + # 绘制计数器 + cv2.rectangle(img, (0, 480 - 120), (120, 480), (102, 106, 233), cv2.FILLED) + cv2.putText(img, str(int(self.count)), (10, 480 - 35), cv2.FONT_HERSHEY_PLAIN, 5, + (255, 255, 255), 5) + # 展示状态反馈 + cv2.rectangle(img, (640 - 160, 0), (640, 50), (102, 106, 233), cv2.FILLED) + img_output = self.cv2_img_add_text(img, self.feedback, 640 - 120, 5, (255, 255, 255), 38) + return img_output + + def analysis(self, frame): + catch_time = frame[CATCH_TIME] + if catch_time < self.start_cal_time: + return + img = frame[FRAME_DAT] + lm_list = frame[MP_RESULT] + self.detector.set_result(lm_list) + + if len(lm_list) != 0: + elbow_1 = self.detector.findAngle(img, 11, 13, 15) + elbow_2 = self.detector.findAngle(img, 12, 14, 16) + eye_1_y = self.detector.findPosition(img, False)[3][2] + eye_2_y = self.detector.findPosition(img, False)[6][2] + elbow_1_x = self.detector.findPosition(img, False)[13][1] + elbow_1_y = self.detector.findPosition(img, False)[13][2] + elbow_2_x = self.detector.findPosition(img, False)[14][1] + elbow_2_y = self.detector.findPosition(img, False)[14][2] + wrist_1_y = self.detector.findPosition(img, False)[15][2] + wrist_2_y = self.detector.findPosition(img, False)[16][2] + heel_y = self.detector.findPosition(img, False)[27][2] + shoulder_1_x = self.detector.findPosition(img, False)[11][1] + shoulder_2_x = self.detector.findPosition(img, False)[11][1] + shoulder_1_y = self.detector.findPosition(img, False)[11][2] + shoulder_2_y = self.detector.findPosition(img, False)[11][2] + vis_shoulder_1 = self.detector.findPosition(img, False)[11][4] + vis_shoulder_2 = self.detector.findPosition(img, False)[12][4] + + # 成功概率 + self.per = np.interp(elbow_1, (157, 100), (0, 100)) + # 显示进度栏 + self.bar = np.interp(elbow_1, (157, 100), (280, 62)) + if self.pre_pos == 0: + self.speak_driver.add_speak("双手握杠后,进入准备状态") + self.pre_pos = 1 + + + if self.form == 0 and self.pre_pos == 1: + if self.readiness_state == 0: + if elbow_1 < 90 and elbow_2 < 90: + self.readiness_state = 1 + # self.heel = heel_y + self.speak_driver.add_speak("上杠,听到提示声后开始考试") + if self.readiness_state == 1: + # if elbow_1 > 157 and eye_1_y < wrist_1_y and eye_2_y < wrist_2_y and heel_y < self.heel - 50: + if elbow_1 > 157 and elbow_2 > 157 and eye_1_y < wrist_1_y and eye_2_y < wrist_2_y: + if vis_shoulder_1 > 0.8 and vis_shoulder_1 > vis_shoulder_2: + self.time_1 += 1 + if self.time_1 > 3: + self.dir = 1 + self.sign = 1 + self.time_1 = 0 + self.time_2 = 0 + elif vis_shoulder_2 > 0.8 and vis_shoulder_1 < vis_shoulder_2: + self.time_2 += 1 + if self.time_2 > 3: + self.dir = 2 + self.sign = 1 + self.time_2 = 0 + self.time_1 = 0 + if self.sign == 1: + self.form = 1 + beep() + self.starttime = time.time() + self.initial_wrist_1_y = wrist_1_y + self.initial_wrist_2_y = wrist_2_y + + if self.dir == 1 and self.pre_pos == 1 and self.form == 1: + # if wrist_1_y - self.initial_wrist_1_y > 50 or heel_y < self.heel - 10: + if wrist_1_y - self.initial_wrist_1_y > 50: + self.end_test += 1 + if self.end_test > 2: + self.speak_driver.add_speak("双手已离开双杠,考试结束") + self.pre_pos = 2 + self.had_done = True + + if elbow_1 <= 100 or shoulder_1_y > elbow_1_y: + self.feedback = "上升" + if self.state == 1: + self.time += 1 + if self.time > 1: + self.count += 0.5 + self.state = 0 + self.time = 0 + + if elbow_1 >= 157 and shoulder_1_y < elbow_1_y and not self.had_done and self.count > 0: + self.feedback = "下落" + if self.state == 0: + self.time += 1 + if self.time > 1: + self.count += 0.5 + self.state = 1 + self.speak_driver.add_speak("{}".format(int(self.count))) + if self.count == 1: + self.initial_wrist_1_y = wrist_1_y + self.initial_wrist_2_y = wrist_2_y + elif self.dir == 2 and self.pre_pos == 1 and self.form == 1: + if wrist_2_y - self.initial_wrist_2_y > 50: + self.end_test += 1 + if self.end_test > 2: + self.speak_driver.add_speak("双手已离开双杠,考试结束") + self.pre_pos = 2 + self.had_done = True + + if elbow_2 <= 100 or shoulder_2_y > elbow_2_y: + self.feedback = "上升" + if self.state == 1: + self.time += 1 + if self.time > 1: + self.count += 0.5 + self.state = 0 + self.time = 0 + + if elbow_2 >= 157 and shoulder_2_y < elbow_2_y and not self.had_done and self.count > 0: + self.feedback = "下落" + if self.state == 0: + self.time += 1 + if self.time > 1: + self.count += 0.5 + self.state = 1 + self.speak_driver.add_speak("{}".format(int(self.count))) + if self.count == 1: + self.initial_wrist_1_y = wrist_1_y + self.initial_wrist_2_y = wrist_2_y +