ronka commited on
Commit
a429cec
·
verified ·
1 Parent(s): 41553bb

Create posture_with_vid.py

Browse files
Files changed (1) hide show
  1. posture_with_vid.py +79 -0
posture_with_vid.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import math as m
3
+ import mediapipe as mp
4
+
5
+ def findDistance(x1, y1, x2, y2):
6
+ dist = m.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
7
+ return dist
8
+
9
+ def findAngle(x1, y1, x2, y2):
10
+ theta = m.acos((y2 - y1) * (-y1) / (m.sqrt(
11
+ (x2 - x1) ** 2 + (y2 - y1) ** 2) * y1))
12
+ degree = int(180 / m.pi) * theta
13
+ return degree
14
+
15
+ mp_pose = mp.solutions.pose
16
+ pose = mp_pose.Pose()
17
+
18
+ cap = cv2.VideoCapture(0)
19
+
20
+ while cap.isOpened():
21
+ ret, frame = cap.read()
22
+ if not ret:
23
+ break
24
+
25
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
26
+
27
+ keypoints = pose.process(frame)
28
+
29
+ frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
30
+
31
+ if keypoints.pose_landmarks:
32
+ lm = keypoints.pose_landmarks
33
+ lmPose = mp_pose.PoseLandmark
34
+
35
+ # Acquire the landmark coordinates.
36
+ l_shldr_x = int(lm.landmark[lmPose.LEFT_SHOULDER].x * frame.shape[1])
37
+ l_shldr_y = int(lm.landmark[lmPose.LEFT_SHOULDER].y * frame.shape[0])
38
+ r_shldr_x = int(lm.landmark[lmPose.RIGHT_SHOULDER].x * frame.shape[1])
39
+ r_shldr_y = int(lm.landmark[lmPose.RIGHT_SHOULDER].y * frame.shape[0])
40
+ l_ear_x = int(lm.landmark[lmPose.LEFT_EAR].x * frame.shape[1])
41
+ l_ear_y = int(lm.landmark[lmPose.LEFT_EAR].y * frame.shape[0])
42
+ l_hip_x = int(lm.landmark[lmPose.LEFT_HIP].x * frame.shape[1])
43
+ l_hip_y = int(lm.landmark[lmPose.LEFT_HIP].y * frame.shape[0])
44
+
45
+ # Calculate distance between left shoulder and right shoulder points.
46
+ offset = findDistance(l_shldr_x, l_shldr_y, r_shldr_x, r_shldr_y)
47
+
48
+ # Calculate angles.
49
+ neck_inclination = findAngle(l_shldr_x, l_shldr_y, l_ear_x, l_ear_y)
50
+ torso_inclination = findAngle(l_hip_x, l_hip_y, l_shldr_x, l_shldr_y)
51
+
52
+ # Draw landmarks and lines.
53
+ cv2.circle(frame, (l_shldr_x, l_shldr_y), 7, (255, 255, 0), -1)
54
+ cv2.circle(frame, (l_ear_x, l_ear_y), 7, (0, 255, 255), -1)
55
+ cv2.circle(frame, (r_shldr_x, r_shldr_y), 7, (255, 0, 255), -1)
56
+ cv2.circle(frame, (l_hip_x, l_hip_y), 7, (0, 255, 0), -1)
57
+
58
+ cv2.line(frame, (l_shldr_x, l_shldr_y), (r_shldr_x, r_shldr_y), (255, 255, 0), 2)
59
+ cv2.line(frame, (l_shldr_x, l_shldr_y), (l_ear_x, l_ear_y), (0, 255, 255), 2)
60
+ cv2.line(frame, (l_ear_x, l_ear_y), (r_shldr_x, r_shldr_y), (255, 0, 255), 2)
61
+ cv2.line(frame, (l_shldr_x, l_shldr_y), (l_hip_x, l_hip_y), (0, 255, 0), 2)
62
+ cv2.line(frame, (l_hip_x, l_hip_y), (r_shldr_x, r_shldr_y), (255, 255, 0), 2)
63
+
64
+ angle_text_string = 'Neck : ' + str(int(neck_inclination)) + 'degrees. Torso : ' + str(int(torso_inclination)) + 'degrees.'
65
+ cv2.putText(frame, angle_text_string, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
66
+
67
+ # Determine whether good posture or bad posture.
68
+ if neck_inclination < 40 and torso_inclination < 10:
69
+ cv2.putText(frame, 'Good Posture', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
70
+ else:
71
+ cv2.putText(frame, 'Bad Posture', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
72
+
73
+ cv2.imshow('Posture Analysis', frame)
74
+
75
+ if cv2.waitKey(1) & 0xFF == ord('q'):
76
+ break
77
+
78
+ cap.release()
79
+ cv2.destroyAllWindows()