232 lines
10 KiB
Python
232 lines
10 KiB
Python
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 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.time_1 = 0
|
|
# 摆动状态
|
|
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)
|
|
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()
|
|
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)
|
|
shoulder_1 = self.detector.findAngle(img, 13, 11, 23)
|
|
elbow_2 = self.detector.findAngle(img, 12, 14, 16)
|
|
shoulder_2 = self.detector.findAngle(img, 14, 12, 24)
|
|
shoulder_1_y = self.detector.findPosition(img, False)[11][2]
|
|
shoulder_2_y = self.detector.findPosition(img, False)[12][2]
|
|
shoulder_1_x = self.detector.findPosition(img, False)[11][1]
|
|
shoulder_2_x = self.detector.findPosition(img, False)[12][1]
|
|
eye_1_y = self.detector.findPosition(img, False)[3][2]
|
|
eye_2_y = self.detector.findPosition(img, False)[6][2]
|
|
mouth_1_y = self.detector.findPosition(img, False)[9][2]
|
|
mouth_2_y = self.detector.findPosition(img, False)[10][2]
|
|
wrist_1_y = self.detector.findPosition(img, False)[15][2]
|
|
wrist_2_y = self.detector.findPosition(img, False)[16][2]
|
|
index_1_y = self.detector.findPosition(img, False)[19][2]
|
|
index_2_y = self.detector.findPosition(img, False)[20][2]
|
|
hip_1_x = self.detector.findPosition(img, False)[23][1]
|
|
hip_1_y = self.detector.findPosition(img, False)[23][2]
|
|
hip_2_x = self.detector.findPosition(img, False)[24][1]
|
|
hip_2_y = self.detector.findPosition(img, False)[24][2]
|
|
|
|
hand_1_y = (wrist_1_y + index_1_y) / 2
|
|
hand_2_y = (wrist_2_y + index_2_y) / 2
|
|
head_1_y = (mouth_1_y + eye_1_y) / 2
|
|
head_2_y = (mouth_2_y + eye_2_y) / 2
|
|
h_range = mouth_1_y - hand_1_y
|
|
|
|
# 成功概率
|
|
self.per = np.interp(h_range, (90, 0), (0, 100))
|
|
# 显示进度栏
|
|
self.bar = np.interp(h_range, (90, 0), (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.time_1 += 1
|
|
if self.time_1 > 30:
|
|
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 / 5)
|
|
# 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 / 5)
|
|
|
|
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))
|
|
|
|
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 > 150 and elbow_2 > 150 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 mouth_1_y < hand_1_y or mouth_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.t = 0
|
|
self.count += 0.5
|
|
self.direction = 0
|
|
self._time = 0
|
|
if self.count % 1 == 0:
|
|
self.speak_driver.speed_control(320)
|
|
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
|