Spaces:
Runtime error
Runtime error
Update utils.py
Browse files
utils.py
CHANGED
@@ -2,6 +2,11 @@ import cv2
|
|
2 |
import mediapipe as mp
|
3 |
import numpy as np
|
4 |
|
|
|
|
|
|
|
|
|
|
|
5 |
def draw_rounded_rect(img, rect_start, rect_end, corner_width, box_color):
|
6 |
|
7 |
x1, y1 = rect_start
|
@@ -32,8 +37,6 @@ def draw_rounded_rect(img, rect_start, rect_end, corner_width, box_color):
|
|
32 |
return img
|
33 |
|
34 |
|
35 |
-
|
36 |
-
|
37 |
def draw_dotted_line(frame, lm_coord, start, end, line_color):
|
38 |
pix_step = 0
|
39 |
|
@@ -46,7 +49,7 @@ def draw_dotted_line(frame, lm_coord, start, end, line_color):
|
|
46 |
def draw_text(
|
47 |
img,
|
48 |
msg,
|
49 |
-
width =
|
50 |
font=cv2.FONT_HERSHEY_SIMPLEX,
|
51 |
pos=(0, 0),
|
52 |
font_scale=1,
|
@@ -54,35 +57,49 @@ def draw_text(
|
|
54 |
text_color=(0, 255, 0),
|
55 |
text_color_bg=(0, 0, 0),
|
56 |
box_offset=(20, 10),
|
|
|
|
|
57 |
):
|
58 |
|
59 |
offset = box_offset
|
60 |
x, y = pos
|
61 |
text_size, _ = cv2.getTextSize(msg, font, font_scale, font_thickness)
|
62 |
text_w, text_h = text_size
|
|
|
63 |
rec_start = tuple(p - o for p, o in zip(pos, offset))
|
64 |
rec_end = tuple(m + n - o for m, n, o in zip((x + text_w, y + text_h), offset, (25, 0)))
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
|
69 |
cv2.putText(
|
70 |
img,
|
71 |
msg,
|
72 |
-
(int(rec_start[0] +
|
73 |
font,
|
74 |
font_scale,
|
75 |
text_color,
|
76 |
font_thickness,
|
77 |
cv2.LINE_AA,
|
78 |
)
|
79 |
-
|
80 |
|
81 |
return text_size
|
82 |
|
83 |
|
84 |
-
|
85 |
-
|
86 |
def find_angle(p1, p2, ref_pt = np.array([0,0])):
|
87 |
p1_ref = p1 - ref_pt
|
88 |
p2_ref = p2 - ref_pt
|
@@ -96,8 +113,6 @@ def find_angle(p1, p2, ref_pt = np.array([0,0])):
|
|
96 |
|
97 |
|
98 |
|
99 |
-
|
100 |
-
|
101 |
def get_landmark_array(pose_landmark, key, frame_width, frame_height):
|
102 |
|
103 |
denorm_x = int(pose_landmark[key].x * frame_width)
|
@@ -107,7 +122,6 @@ def get_landmark_array(pose_landmark, key, frame_width, frame_height):
|
|
107 |
|
108 |
|
109 |
|
110 |
-
|
111 |
def get_landmark_features(kp_results, dict_features, feature, frame_width, frame_height):
|
112 |
|
113 |
if feature == 'nose':
|
@@ -115,12 +129,12 @@ def get_landmark_features(kp_results, dict_features, feature, frame_width, frame
|
|
115 |
|
116 |
elif feature == 'left' or 'right':
|
117 |
shldr_coord = get_landmark_array(kp_results, dict_features[feature]['shoulder'], frame_width, frame_height)
|
118 |
-
elbow_coord
|
119 |
-
wrist_coord
|
120 |
hip_coord = get_landmark_array(kp_results, dict_features[feature]['hip'], frame_width, frame_height)
|
121 |
-
knee_coord
|
122 |
-
ankle_coord
|
123 |
-
foot_coord
|
124 |
|
125 |
return shldr_coord, elbow_coord, wrist_coord, hip_coord, knee_coord, ankle_coord, foot_coord
|
126 |
|
|
|
2 |
import mediapipe as mp
|
3 |
import numpy as np
|
4 |
|
5 |
+
correct = cv2.imread('right.png')
|
6 |
+
correct = cv2.cvtColor(correct, cv2.COLOR_BGR2RGB)
|
7 |
+
incorrect = cv2.imread('wrong.png')
|
8 |
+
incorrect = cv2.cvtColor(incorrect, cv2.COLOR_BGR2RGB)
|
9 |
+
|
10 |
def draw_rounded_rect(img, rect_start, rect_end, corner_width, box_color):
|
11 |
|
12 |
x1, y1 = rect_start
|
|
|
37 |
return img
|
38 |
|
39 |
|
|
|
|
|
40 |
def draw_dotted_line(frame, lm_coord, start, end, line_color):
|
41 |
pix_step = 0
|
42 |
|
|
|
49 |
def draw_text(
|
50 |
img,
|
51 |
msg,
|
52 |
+
width = 7,
|
53 |
font=cv2.FONT_HERSHEY_SIMPLEX,
|
54 |
pos=(0, 0),
|
55 |
font_scale=1,
|
|
|
57 |
text_color=(0, 255, 0),
|
58 |
text_color_bg=(0, 0, 0),
|
59 |
box_offset=(20, 10),
|
60 |
+
overlay_image = False,
|
61 |
+
overlay_type = None
|
62 |
):
|
63 |
|
64 |
offset = box_offset
|
65 |
x, y = pos
|
66 |
text_size, _ = cv2.getTextSize(msg, font, font_scale, font_thickness)
|
67 |
text_w, text_h = text_size
|
68 |
+
|
69 |
rec_start = tuple(p - o for p, o in zip(pos, offset))
|
70 |
rec_end = tuple(m + n - o for m, n, o in zip((x + text_w, y + text_h), offset, (25, 0)))
|
71 |
+
|
72 |
+
resize_height = 0
|
73 |
+
|
74 |
+
if overlay_image:
|
75 |
+
resize_height = rec_end[1] - rec_start[1]
|
76 |
+
|
77 |
+
img = draw_rounded_rect(img, rec_start, (rec_end[0]+resize_height, rec_end[1]), width, text_color_bg)
|
78 |
+
if overlay_type == "correct":
|
79 |
+
overlay_res = cv2.resize(correct, (resize_height, resize_height), interpolation = cv2.INTER_AREA)
|
80 |
+
elif overlay_type == "incorrect":
|
81 |
+
overlay_res = cv2.resize(incorrect, (resize_height, resize_height), interpolation = cv2.INTER_AREA)
|
82 |
+
|
83 |
+
img[rec_start[1]:rec_start[1]+resize_height, rec_start[0]+width:rec_start[0]+width+resize_height] = overlay_res
|
84 |
+
|
85 |
+
else:
|
86 |
+
img = draw_rounded_rect(img, rec_start, rec_end, width, text_color_bg)
|
87 |
|
88 |
|
89 |
cv2.putText(
|
90 |
img,
|
91 |
msg,
|
92 |
+
(int(rec_start[0]+resize_height + 8), int(y + text_h + font_scale - 1)),
|
93 |
font,
|
94 |
font_scale,
|
95 |
text_color,
|
96 |
font_thickness,
|
97 |
cv2.LINE_AA,
|
98 |
)
|
|
|
99 |
|
100 |
return text_size
|
101 |
|
102 |
|
|
|
|
|
103 |
def find_angle(p1, p2, ref_pt = np.array([0,0])):
|
104 |
p1_ref = p1 - ref_pt
|
105 |
p2_ref = p2 - ref_pt
|
|
|
113 |
|
114 |
|
115 |
|
|
|
|
|
116 |
def get_landmark_array(pose_landmark, key, frame_width, frame_height):
|
117 |
|
118 |
denorm_x = int(pose_landmark[key].x * frame_width)
|
|
|
122 |
|
123 |
|
124 |
|
|
|
125 |
def get_landmark_features(kp_results, dict_features, feature, frame_width, frame_height):
|
126 |
|
127 |
if feature == 'nose':
|
|
|
129 |
|
130 |
elif feature == 'left' or 'right':
|
131 |
shldr_coord = get_landmark_array(kp_results, dict_features[feature]['shoulder'], frame_width, frame_height)
|
132 |
+
elbow_coord = get_landmark_array(kp_results, dict_features[feature]['elbow'], frame_width, frame_height)
|
133 |
+
wrist_coord = get_landmark_array(kp_results, dict_features[feature]['wrist'], frame_width, frame_height)
|
134 |
hip_coord = get_landmark_array(kp_results, dict_features[feature]['hip'], frame_width, frame_height)
|
135 |
+
knee_coord = get_landmark_array(kp_results, dict_features[feature]['knee'], frame_width, frame_height)
|
136 |
+
ankle_coord = get_landmark_array(kp_results, dict_features[feature]['ankle'], frame_width, frame_height)
|
137 |
+
foot_coord = get_landmark_array(kp_results, dict_features[feature]['foot'], frame_width, frame_height)
|
138 |
|
139 |
return shldr_coord, elbow_coord, wrist_coord, hip_coord, knee_coord, ankle_coord, foot_coord
|
140 |
|