pip install mediapipe
from mediapipe.tasks.python import vision
names = ["left shoulder", "right shoulder", "left elbow", "right elbow", "left wrist", "right wrist"]
for i, name in enumerate(names):
print(name)
print(results.pose_landmarks.landmark[i+11].y)
print('-----------------------------')
# const for joints
RIGHT_SHOULDER = results.pose_landmarks.landmark[12].y
RIGHT_ELBOW = results.pose_landmarks.landmark[14].y
RIGHT_WRIST = results.pose_landmarks.landmark[16].y
LEFT_SHOULDER = results.pose_landmarks.landmark[11].y
LEFT_ELBOW = results.pose_landmarks.landmark[13].y
LEFT_WRIST = results.pose_landmarks.landmark[15].y
# left up
if (RIGHT_WRIST < RIGHT_ELBOW and RIGHT_ELBOW < RIGHT_SHOULDER) and LEFT_WRIST > LEFT_SHOULDER:
print("RIGHT UP")
elif (LEFT_WRIST < LEFT_ELBOW and LEFT_ELBOW < LEFT_SHOULDER) and RIGHT_WRIST > RIGHT_SHOULDER:
print("LEFT UP")
elif (RIGHT_WRIST < RIGHT_ELBOW and RIGHT_ELBOW < RIGHT_SHOULDER) and LEFT_WRIST < LEFT_SHOULDER:
print("BOTH UP")
elif (RIGHT_WRIST > RIGHT_ELBOW and RIGHT_ELBOW > RIGHT_SHOULDER) and LEFT_WRIST > LEFT_SHOULDER:
print("BOTH DOWN")
# while문 시작 전에 json과 list 담을 변수 선언
pose_point_json = {}
right_up_list = []
left_up_list = []
both_up_list = []
both_down_list = []
...
# left up
if (RIGHT_WRIST < RIGHT_ELBOW and RIGHT_ELBOW < RIGHT_SHOULDER) and LEFT_WRIST > LEFT_SHOULDER:
print("RIGHT UP")
right_up_json = {}
right_up_json['RIGHT_WRIST'] = RIGHT_WRIST
right_up_json['RIGHT_SHOULDER'] = RIGHT_SHOULDER
right_up_json['LEFT_WRIST'] = LEFT_WRIST
right_up_json['LEFT_SHOULDER'] = LEFT_SHOULDER
right_up_list.append(right_up_json)
elif (LEFT_WRIST < LEFT_ELBOW and LEFT_ELBOW < LEFT_SHOULDER) and RIGHT_WRIST > RIGHT_SHOULDER:
print("LEFT UP")
left_up_json = {}
left_up_json['RIGHT_WRIST'] = RIGHT_WRIST
left_up_json['RIGHT_SHOULDER'] = RIGHT_SHOULDER
left_up_json['LEFT_WRIST'] = LEFT_WRIST
left_up_json['LEFT_SHOULDER'] = LEFT_SHOULDER
left_up_list.append(left_up_json)
elif (RIGHT_WRIST < RIGHT_ELBOW and RIGHT_ELBOW < RIGHT_SHOULDER) and LEFT_WRIST < LEFT_SHOULDER:
print("BOTH UP")
both_up_json = {}
both_up_json['RIGHT_WRIST'] = RIGHT_WRIST
both_up_json['RIGHT_SHOULDER'] = RIGHT_SHOULDER
both_up_json['LEFT_WRIST'] = LEFT_WRIST
both_up_json['LEFT_SHOULDER'] = LEFT_SHOULDER
both_up_list.append(both_up_json)
elif (RIGHT_WRIST > RIGHT_ELBOW and RIGHT_ELBOW > RIGHT_SHOULDER) and LEFT_WRIST > LEFT_SHOULDER:
print("BOTH DOWN")
both_down_json = {}
both_down_json['RIGHT_WRIST'] = RIGHT_WRIST
both_down_json['RIGHT_SHOULDER'] = RIGHT_SHOULDER
both_down_json['LEFT_WRIST'] = LEFT_WRIST
both_down_json['LEFT_SHOULDER'] = LEFT_SHOULDER
both_down_list.append(both_down_json)
# ESC 키 입력 시 json 파일 저장
if cv2.waitKey(5) & 0xFF == 27:
pose_point_json['RIGHT_UP'] = right_up_list
pose_point_json['LEFT_UP'] = left_up_list
pose_point_json['BOTH_UP'] = both_up_list
pose_point_json['BOTH_DOWN'] = both_down_list
with open('../data/pose_point.json', 'w', encoding='utf-8') as f:
json.dump(pose_point_json, f, ensure_ascii=False, indent=4)
...
이제 Json 데이터를 얻었으니 학습시켜서 모델을 만들면 된다.
iris 데이터 실습 때 썼던 decision tree를 활용해 보자.