-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path정면_얼굴.py
94 lines (73 loc) · 3.33 KB
/
정면_얼굴.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import cv2
import numpy as np
import time
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
mp_holistic = mp.solutions.holistic
def calculate_angle(a, b):
a = np.array(a)
b = np.array(b)
radians = np.arctan2(b[1] - a[1], b[0] - a[0])
angle = np.abs(radians * 180.0 / np.pi)
return angle
cap = cv2.VideoCapture(0)
# Curl counter variables
warning = False
count = 0
start = time.gmtime(time.time()) # 시작 시간 저장
## Setup mediapipe instance
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
while cap.isOpened():
ret, frame = cap.read()
resize_frame = cv2.resize(frame ,None, fx=1.5, fy=1.5, interpolation=cv2.INTER_LINEAR)
# Recolor image to RGB
image = cv2.cvtColor(resize_frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
# Make detection
results = pose.process(image)
# Recolor back to BGR
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Extract landmarks
try:
landmarks = results.pose_landmarks.landmark
# Get coordinates
left_shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x,landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y]
right_shoulder = [landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x,landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y]
# Calculate angle
angle = calculate_angle(left_shoulder, right_shoulder)
print("angle : " + str(angle))
# Curl counter logic
if angle < 170:
count = count + 1
else:
count = 0
except:
pass
cv2.rectangle(image, (0,0), (1500,80), (128,128,128), -1)
#Time
now = time.gmtime(time.time())
cv2.putText(image, 'Time',
(20,25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,0), 1, cv2.LINE_AA)
hour = now.tm_hour - start.tm_hour
minutes = abs(now.tm_min - start.tm_min)
cv2.putText(image, str(hour) +' : '+ str(minutes),
(20,65), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2, cv2.LINE_AA)
if minutes >= 1:
cv2.putText(image, 'Stand up and Stretch ',
(300,50), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0,0,255), 1, cv2.LINE_AA)
#Warning
if minutes < 1 and count > 5:
cv2.putText(image, 'Please Straighten up',
(300,50), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0,0,255), 1, cv2.LINE_AA)
# Render detections
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=2),
mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2)
)
cv2.imshow('Mediapipe Feed', image)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()