amarkc commited on
Commit
2a4205b
1 Parent(s): e2dff2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -60
app.py CHANGED
@@ -9,9 +9,10 @@ from recommendations import check_pose_angle
9
  from landmarks import extract_landmarks
10
  from calc_angles import rangles
11
 
12
-
13
  def init_cam():
14
- cam = cv2.VideoCapture(0)
 
 
15
  cam.set(cv2.CAP_PROP_AUTOFOCUS, 0)
16
  cam.set(cv2.CAP_PROP_FOCUS, 360)
17
  cam.set(cv2.CAP_PROP_BRIGHTNESS, 130)
@@ -20,7 +21,6 @@ def init_cam():
20
  cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
21
  return cam
22
 
23
-
24
  def get_pose_name(index):
25
  names = {
26
  0: "Adho Mukha Svanasana",
@@ -28,8 +28,7 @@ def get_pose_name(index):
28
  2: "Utkata Konasana",
29
  3: "Vrikshasana",
30
  }
31
- return str(names[index])
32
-
33
 
34
  def init_dicts():
35
  landmarks_points = {
@@ -43,26 +42,9 @@ def init_dicts():
43
  "left_heel": 29, "right_heel": 30,
44
  "left_foot_index": 31, "right_foot_index": 32,
45
  }
46
- landmarks_points_array = {
47
- "left_shoulder": [], "right_shoulder": [],
48
- "left_elbow": [], "right_elbow": [],
49
- "left_wrist": [], "right_wrist": [],
50
- "left_hip": [], "right_hip": [],
51
- "left_knee": [], "right_knee": [],
52
- "left_ankle": [], "right_ankle": [],
53
- "left_heel": [], "right_heel": [],
54
- "left_foot_index": [], "right_foot_index": [],
55
- }
56
- col_names = []
57
- for i in range(len(landmarks_points.keys())):
58
- name = list(landmarks_points.keys())[i]
59
- col_names.append(name + "_x")
60
- col_names.append(name + "_y")
61
- col_names.append(name + "_z")
62
- col_names.append(name + "_v")
63
- cols = col_names.copy()
64
- return cols, landmarks_points_array
65
-
66
 
67
  def cv2_put_text(image, message):
68
  cv2.putText(
@@ -76,44 +58,39 @@ def cv2_put_text(image, message):
76
  cv2.LINE_AA
77
  )
78
 
79
-
80
  def destroy(cam):
81
- cv2.destroyAllWindows()
82
  cam.release()
83
-
84
 
85
  if __name__ == "__main__":
86
- cam = init_cam()
87
- model = pk.load(open("./models/4_poses.model", "rb"))
88
- cols, landmarks_points_array = init_dicts()
89
- angles_df = pd.read_csv("./csv_files/4_angles_poses_angles.csv")
90
- mp_drawing = mp.solutions.drawing_utils
91
- mp_pose = mp.solutions.pose
92
-
93
- tts_last_exec = time() + 5
94
-
95
- while True:
96
- result, image = cam.read()
97
- flipped = cv2.flip(image, 1)
98
- resized_image = cv2.resize(
99
- flipped,
100
- (640, 360),
101
- interpolation=cv2.INTER_AREA
102
- )
103
-
104
- key = cv2.waitKey(1)
105
- if key == ord("q"):
106
- destroy(cam)
107
- break
108
-
109
- if result:
110
  err, df, landmarks = extract_landmarks(
111
  resized_image,
112
  mp_pose,
113
  cols
114
  )
115
 
116
- if err == False:
117
  prediction = model.predict(df)
118
  probabilities = model.predict_proba(df)
119
 
@@ -133,13 +110,10 @@ if __name__ == "__main__":
133
  suggestions = check_pose_angle(
134
  prediction[0], angles, angles_df)
135
 
136
- if time() > tts_last_exec:
137
- # Display suggestions on screen
138
- cv2_put_text(
139
- flipped,
140
- suggestions[0]
141
- )
142
- tts_last_exec = time() + 5
143
 
144
  else:
145
  cv2_put_text(
@@ -147,3 +121,7 @@ if __name__ == "__main__":
147
  "No Pose Detected"
148
  )
149
  cv2.imshow("Frame", flipped)
 
 
 
 
 
9
  from landmarks import extract_landmarks
10
  from calc_angles import rangles
11
 
 
12
  def init_cam():
13
+ cam = cv2.VideoCapture(0) # Use 0 for default camera
14
+ if not cam.isOpened():
15
+ raise ValueError("Could not open camera.")
16
  cam.set(cv2.CAP_PROP_AUTOFOCUS, 0)
17
  cam.set(cv2.CAP_PROP_FOCUS, 360)
18
  cam.set(cv2.CAP_PROP_BRIGHTNESS, 130)
 
21
  cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
22
  return cam
23
 
 
24
  def get_pose_name(index):
25
  names = {
26
  0: "Adho Mukha Svanasana",
 
28
  2: "Utkata Konasana",
29
  3: "Vrikshasana",
30
  }
31
+ return str(names.get(index, "Unknown Pose"))
 
32
 
33
  def init_dicts():
34
  landmarks_points = {
 
42
  "left_heel": 29, "right_heel": 30,
43
  "left_foot_index": 31, "right_foot_index": 32,
44
  }
45
+ landmarks_points_array = {key: [] for key in landmarks_points.keys()}
46
+ col_names = [f"{name}_{axis}" for name in landmarks_points.keys() for axis in ["x", "y", "z", "v"]]
47
+ return col_names, landmarks_points_array
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  def cv2_put_text(image, message):
50
  cv2.putText(
 
58
  cv2.LINE_AA
59
  )
60
 
 
61
  def destroy(cam):
 
62
  cam.release()
63
+ cv2.destroyAllWindows()
64
 
65
  if __name__ == "__main__":
66
+ try:
67
+ cam = init_cam()
68
+ model = pk.load(open("./models/4_poses.model", "rb"))
69
+ cols, landmarks_points_array = init_dicts()
70
+ angles_df = pd.read_csv("./csv_files/4_angles_poses_angles.csv")
71
+ mp_drawing = mp.solutions.drawing_utils
72
+ mp_pose = mp.solutions.pose
73
+
74
+ while True:
75
+ result, image = cam.read()
76
+ if not result:
77
+ print("Failed to grab frame.")
78
+ break
79
+
80
+ flipped = cv2.flip(image, 1)
81
+ resized_image = cv2.resize(
82
+ flipped,
83
+ (640, 360),
84
+ interpolation=cv2.INTER_AREA
85
+ )
86
+
 
 
 
87
  err, df, landmarks = extract_landmarks(
88
  resized_image,
89
  mp_pose,
90
  cols
91
  )
92
 
93
+ if not err:
94
  prediction = model.predict(df)
95
  probabilities = model.predict_proba(df)
96
 
 
110
  suggestions = check_pose_angle(
111
  prediction[0], angles, angles_df)
112
 
113
+ cv2_put_text(
114
+ flipped,
115
+ suggestions[0]
116
+ )
 
 
 
117
 
118
  else:
119
  cv2_put_text(
 
121
  "No Pose Detected"
122
  )
123
  cv2.imshow("Frame", flipped)
124
+ if cv2.waitKey(1) & 0xFF == ord('q'):
125
+ break
126
+ finally:
127
+ destroy(cam)