LISHUZUOXUN_yangjiang/Exercise_mn/pull_up_1.py

226 lines
10 KiB
Python

import math
import time
import numpy as np
from MCamera.mn_camera import MN_RESULT
from PureBackend.general import *
from MCamera.mp_camera import *
from MCamera.camera import *
from Database.manager_database import *
from score_doc import get_fin_score
from Exercise_mn.base_exercise import BaseExercise
from MCamera.mn_algorithm import MoveNetAlgorithmPlugin
class PullUp_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.body_state = 0
self.direction = 0
self.is_storard = 1
# 动作持续帧数
self._time = 0
self.time = 0
self.t = 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.state = 0
# 初始化
self.initial_height_left = 0
self.initial_height_right = 0
self.initial_wrist_1_y = 0
self.initial_wrist_2_y = 0
# 引体向上参数
self.corner = (0, 480, 260, 380)
MoveNetAlgorithmPlugin.set_corner(corner=self.corner)
MoveNetAlgorithmPlugin.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()
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, [NOSE, LEFT_SHOULDER, LEFT_ELBOW, LEFT_WRIST, LEFT_KNEE], bias_x=self.corner[2], bias_y=self.corner[0])
self.detector.drawPoint_more(img, [NOSE, RIGHT_SHOULDER, RIGHT_ELBOW, RIGHT_WRIST, RIGHT_KNEE], 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[MN_RESULT]
self.detector.set_result(lm_list)
if len(lm_list) != 0:
elbow_1 = self.detector.findAngle(img, LEFT_SHOULDER, LEFT_ELBOW, LEFT_WRIST)
shoulder_1 = self.detector.findAngle(img, LEFT_ELBOW, LEFT_SHOULDER, LEFT_HIP)
elbow_2 = self.detector.findAngle(img, RIGHT_SHOULDER, RIGHT_ELBOW, RIGHT_WRIST)
shoulder_2 = self.detector.findAngle(img, RIGHT_ELBOW, RIGHT_SHOULDER, RIGHT_HIP)
shoulder_1_y = self.detector.findPosition(img, False)[LEFT_SHOULDER]['key_points'][1]
shoulder_2_y = self.detector.findPosition(img, False)[RIGHT_SHOULDER]['key_points'][1]
shoulder_1_x = self.detector.findPosition(img, False)[LEFT_SHOULDER]['key_points'][0]
shoulder_2_x = self.detector.findPosition(img, False)[RIGHT_SHOULDER]['key_points'][0]
eye_1_y = self.detector.findPosition(img, False)[LEFT_EYE]['key_points'][1]
eye_2_y = self.detector.findPosition(img, False)[RIGHT_EYE]['key_points'][1]
nose_y = self.detector.findPosition(img, False)[NOSE]['key_points'][1]
wrist_1_y = self.detector.findPosition(img, False)[LEFT_WRIST]['key_points'][1]
wrist_2_y = self.detector.findPosition(img, False)[RIGHT_WRIST]['key_points'][1]
hip_1_x = self.detector.findPosition(img, False)[LEFT_HIP]['key_points'][0]
hip_1_y = self.detector.findPosition(img, False)[LEFT_HIP]['key_points'][1]
hip_2_x = self.detector.findPosition(img, False)[RIGHT_HIP]['key_points'][0]
hip_2_y = self.detector.findPosition(img, False)[RIGHT_HIP]['key_points'][1]
# 成功概率
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.pre_pos = 1
self.speak_driver.add_speak("请进入准备状态")
if self.form == 0:
if time.time() - self.sta_time > 1.5:
if elbow_1 > 150 and elbow_2 > 150 and shoulder_1 > 100 and shoulder_2 > 100 and eye_1_y > wrist_1_y and eye_2_y > wrist_2_y:
self.form = 1
self.speak_driver.add_speak("请开始考试")
self.starttime = time.time()
self.initial_wrist_1_y = wrist_1_y
self.initial_wrist_2_y = wrist_2_y
self.initial_height_left = math.sqrt(
math.pow(shoulder_2_x - hip_2_x, 2) + math.pow(shoulder_2_y - hip_2_y, 2)) * math.cos(
math.pi / 6)
self.initial_height_right = math.sqrt(
math.pow(shoulder_1_x - hip_1_x, 2) + math.pow(shoulder_1_y - hip_1_y, 2)) * math.cos(
math.pi / 6)
elif self.pre_pos == 1 and self.form == 1:
height_left = math.sqrt(math.pow(shoulder_2_x - hip_2_x, 2) + math.pow(shoulder_2_y - hip_2_y, 2))
height_right = math.sqrt(math.pow(shoulder_1_x - hip_1_x, 2) + math.pow(shoulder_1_y - hip_1_y, 2))
hand_1_y = wrist_1_y - (shoulder_1_y - wrist_1_y) / 3
hand_2_y = wrist_2_y - (shoulder_2_y - wrist_2_y) / 3
head_1_y = nose_y + (nose_y - eye_1_y) * 1.3
head_2_y = nose_y + (nose_y - eye_2_y) * 1.3
h_range = head_1_y - hand_1_y
if wrist_1_y - self.initial_wrist_1_y > 50 and wrist_2_y - self.initial_wrist_2_y > 50 and self.pre_pos == 1:
self.end_test += 1
if self.end_test > 2:
self.speak_driver.add_speak("双手已离开单杠,考试结束")
self.pre_pos = 2
self.had_done = True
if height_left < self.initial_height_left and height_right < self.initial_height_right:
self.t += 1
if self.t > 4:
self.state = 1
self.t = 0
if self.body_state == 0:
self.speak_driver.add_speak('摆幅过大')
self.body_state = 1
if elbow_1 > 157 and elbow_2 > 157 and head_1_y > hand_1_y and head_2_y > hand_2_y:
self.feedback = "上升"
self.state = 0
if self.direction == 0:
self._time += 1
if self._time > 3:
self.count += 0.5
self.direction = 1
self._time = 0
self.body_state = 0
if head_1_y < hand_1_y and head_2_y < hand_2_y and not self.had_done:
self.feedback = "下落"
if self.state == 0:
if self.direction == 1:
self._time += 1
if self._time > 2:
self.count += 0.5
self.direction = 0
self._time = 0
if self.count % 1 == 0:
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
if h_range < 20 and self.is_storard == 1:
self.time += 1
if self.time > 3:
self.is_storard = 2
self.time = 0
elif head_1_y < hand_1_y and head_2_y < hand_2_y and self.is_storard == 2:
self.time += 1
if self.time > 3:
self.is_storard = 3
self.time = 0
elif h_range > 30 and self.is_storard == 2:
self.time += 1
if self.time > 3:
self.is_storard = 1
self.speak_driver.add_speak('未过杠')
self.time = 0
elif h_range > 30 and self.is_storard == 3:
self.time += 1
if self.time > 3:
self.is_storard = 1
self.time = 0