ronka commited on
Commit
9777f9d
·
verified ·
1 Parent(s): 912f9ea

Create try_1.py

Browse files
Files changed (1) hide show
  1. try_1.py +86 -0
try_1.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import torch
3
+ import torch.nn as nn
4
+ from transformers import PreTrainedModel, PretrainedConfig
5
+
6
+ # Load the ronka/postureDetection-Mediapipe model
7
+ import mediapipe as mp
8
+ mp_pose = mp.solutions.pose
9
+ pose = mp_pose.Pose()
10
+ lmPose = mp_pose.PoseLandmark
11
+
12
+ class PostureDetectionConfig(PretrainedConfig):
13
+ model_type = "posture_detection"
14
+
15
+ def __init__(self, **kwargs):
16
+ super().__init__(**kwargs)
17
+
18
+ class PostureDetectionModel(PreTrainedModel):
19
+ config_class = PostureDetectionConfig
20
+
21
+ def __init__(self, config):
22
+ super().__init__(config)
23
+ self.pose = pose
24
+
25
+ def forward(self, pixel_values):
26
+ images = [cv2.cvtColor(pixel_value.numpy(), cv2.COLOR_RGB2BGR) for pixel_value in pixel_values]
27
+ keypoints = [self.pose.process(image) for image in images]
28
+
29
+ results = []
30
+ for image, keypoint in zip(images, keypoints):
31
+ lm = keypoint.pose_landmarks
32
+ if lm is None:
33
+ results.append(None)
34
+ continue
35
+
36
+ # Acquire the landmark coordinates.
37
+ # Left shoulder.
38
+ l_shldr_x = int(lm.landmark[lmPose.LEFT_SHOULDER].x * image.shape[1])
39
+ l_shldr_y = int(lm.landmark[lmPose.LEFT_SHOULDER].y * image.shape[0])
40
+
41
+ # Right shoulder
42
+ r_shldr_x = int(lm.landmark[lmPose.RIGHT_SHOULDER].x * image.shape[1])
43
+ r_shldr_y = int(lm.landmark[lmPose.RIGHT_SHOULDER].y * image.shape[0])
44
+
45
+ # Left ear.
46
+ l_ear_x = int(lm.landmark[lmPose.LEFT_EAR].x * image.shape[1])
47
+ l_ear_y = int(lm.landmark[lmPose.LEFT_EAR].y * image.shape[0])
48
+
49
+ # Left hip.
50
+ l_hip_x = int(lm.landmark[lmPose.LEFT_HIP].x * image.shape[1])
51
+ l_hip_y = int(lm.landmark[lmPose.LEFT_HIP].y * image.shape[0])
52
+
53
+ # Calculate distance between left shoulder and right shoulder points.
54
+ offset = self.findDistance(l_shldr_x, l_shldr_y, r_shldr_x, r_shldr_y)
55
+
56
+ # Calculate angles.
57
+ neck_inclination = self.findAngle(l_shldr_x, l_shldr_y, l_ear_x, l_ear_y)
58
+ torso_inclination = self.findAngle(l_hip_x, l_hip_y, l_shldr_x, l_shldr_y)
59
+
60
+ output = {
61
+ "offset": offset,
62
+ "neck_inclination": neck_inclination,
63
+ "torso_inclination": torso_inclination
64
+ }
65
+
66
+ results.append(output)
67
+
68
+ return results
69
+
70
+ def findDistance(self, x1, y1, x2, y2):
71
+ dist = torch.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
72
+ return dist
73
+
74
+ def findAngle(self, x1, y1, x2, y2):
75
+ theta = torch.acos((y2 - y1) * (-y1) / (torch.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) * y1))
76
+ degree = (180 / torch.pi) * theta
77
+ return degree
78
+
79
+ # Register the model and configuration
80
+ MODEL_MAPPING = {
81
+ PostureDetectionConfig.model_type: PostureDetectionModel
82
+ }
83
+
84
+ CONFIG_MAPPING = {
85
+ PostureDetectionConfig.model_type: PostureDetectionConfig
86
+ }