Create posture_with_image.py
Browse files- posture_with_image.py +82 -0
posture_with_image.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
print('distance: ', dist)
|
| 8 |
+
return dist
|
| 9 |
+
|
| 10 |
+
def findAngle(x1, y1, x2, y2):
|
| 11 |
+
theta = m.acos((y2 - y1) * (-y1) / (m.sqrt(
|
| 12 |
+
(x2 - x1) ** 2 + (y2 - y1) ** 2) * y1))
|
| 13 |
+
degree = int(180 / m.pi) * theta
|
| 14 |
+
print('degree: ', degree)
|
| 15 |
+
return degree
|
| 16 |
+
|
| 17 |
+
mp_pose = mp.solutions.pose
|
| 18 |
+
pose = mp_pose.Pose()
|
| 19 |
+
|
| 20 |
+
bad_image_path = 'images/bad_posture.jpg'
|
| 21 |
+
good_image_path = 'images\good_posture.jpeg'
|
| 22 |
+
image = cv2.imread(bad_image_path)
|
| 23 |
+
|
| 24 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 25 |
+
|
| 26 |
+
keypoints = pose.process(image)
|
| 27 |
+
|
| 28 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 29 |
+
|
| 30 |
+
# Use lm and lmPose as representative of the following methods.
|
| 31 |
+
lm = keypoints.pose_landmarks
|
| 32 |
+
lmPose = mp_pose.PoseLandmark
|
| 33 |
+
|
| 34 |
+
# Acquire the landmark coordinates.
|
| 35 |
+
# Left shoulder.
|
| 36 |
+
l_shldr_x = int(lm.landmark[lmPose.LEFT_SHOULDER].x * image.shape[1])
|
| 37 |
+
l_shldr_y = int(lm.landmark[lmPose.LEFT_SHOULDER].y * image.shape[0])
|
| 38 |
+
# Right shoulder
|
| 39 |
+
r_shldr_x = int(lm.landmark[lmPose.RIGHT_SHOULDER].x * image.shape[1])
|
| 40 |
+
r_shldr_y = int(lm.landmark[lmPose.RIGHT_SHOULDER].y * image.shape[0])
|
| 41 |
+
# Left ear.
|
| 42 |
+
l_ear_x = int(lm.landmark[lmPose.LEFT_EAR].x * image.shape[1])
|
| 43 |
+
l_ear_y = int(lm.landmark[lmPose.LEFT_EAR].y * image.shape[0])
|
| 44 |
+
# Left hip.
|
| 45 |
+
l_hip_x = int(lm.landmark[lmPose.LEFT_HIP].x * image.shape[1])
|
| 46 |
+
l_hip_y = int(lm.landmark[lmPose.LEFT_HIP].y * image.shape[0])
|
| 47 |
+
|
| 48 |
+
# Calculate distance between left shoulder and right shoulder points.
|
| 49 |
+
offset = findDistance(l_shldr_x, l_shldr_y, r_shldr_x, r_shldr_y)
|
| 50 |
+
|
| 51 |
+
# Calculate angles.
|
| 52 |
+
neck_inclination = findAngle(l_shldr_x, l_shldr_y, l_ear_x, l_ear_y)
|
| 53 |
+
torso_inclination = findAngle(l_hip_x, l_hip_y, l_shldr_x, l_shldr_y)
|
| 54 |
+
|
| 55 |
+
cv2.circle(image, (l_shldr_x, l_shldr_y), 7, (255, 255, 0), -1)
|
| 56 |
+
cv2.circle(image, (l_ear_x, l_ear_y), 7, (0, 255, 255), -1)
|
| 57 |
+
cv2.circle(image, (r_shldr_x, r_shldr_y), 7, (255, 0, 255), -1)
|
| 58 |
+
cv2.circle(image, (l_hip_x, l_hip_y), 7, (0, 255, 0), -1)
|
| 59 |
+
|
| 60 |
+
# Draw lines between the landmarks
|
| 61 |
+
cv2.line(image, (l_shldr_x, l_shldr_y), (r_shldr_x, r_shldr_y), (255, 255, 0), 2)
|
| 62 |
+
cv2.line(image, (l_shldr_x, l_shldr_y), (l_ear_x, l_ear_y), (0, 255, 255), 2)
|
| 63 |
+
cv2.line(image, (l_ear_x, l_ear_y), (r_shldr_x, r_shldr_y), (255, 0, 255), 2)
|
| 64 |
+
cv2.line(image, (l_shldr_x, l_shldr_y), (l_hip_x, l_hip_y), (0, 255, 0), 2)
|
| 65 |
+
cv2.line(image, (l_hip_x, l_hip_y), (r_shldr_x, r_shldr_y), (255, 255, 0), 2)
|
| 66 |
+
|
| 67 |
+
# Put text, Posture and angle inclination.
|
| 68 |
+
angle_text_string = 'Neck : ' + str(int(neck_inclination)) + 'degrees. Torso : ' + str(int(torso_inclination)) + 'degrees.'
|
| 69 |
+
cv2.putText(image, angle_text_string, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 70 |
+
|
| 71 |
+
# Determine whether good posture or bad posture.
|
| 72 |
+
# The threshold angles have been set based on intuition.
|
| 73 |
+
if neck_inclination < 40 and torso_inclination < 10:
|
| 74 |
+
cv2.putText(image, 'Good Posture', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 75 |
+
else:
|
| 76 |
+
cv2.putText(image, 'Bad Posture', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
cv2.resize(image, (600, 600))
|
| 80 |
+
cv2.imshow('Posture Analysis', image)
|
| 81 |
+
cv2.waitKey(0)
|
| 82 |
+
cv2.destroyAllWindows()
|