Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -2,10 +2,55 @@ import cv2
|
|
| 2 |
import mediapipe as mp
|
| 3 |
import numpy as np
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def draw_text(
|
| 6 |
img,
|
| 7 |
-
# count_text,
|
| 8 |
msg,
|
|
|
|
| 9 |
font=cv2.FONT_HERSHEY_SIMPLEX,
|
| 10 |
pos=(0, 0),
|
| 11 |
font_scale=1,
|
|
@@ -13,21 +58,40 @@ def draw_text(
|
|
| 13 |
text_color=(0, 255, 0),
|
| 14 |
text_color_bg=(0, 0, 0),
|
| 15 |
box_offset=(20, 10),
|
|
|
|
|
|
|
| 16 |
):
|
| 17 |
|
| 18 |
offset = box_offset
|
| 19 |
x, y = pos
|
| 20 |
text_size, _ = cv2.getTextSize(msg, font, font_scale, font_thickness)
|
| 21 |
text_w, text_h = text_size
|
|
|
|
| 22 |
rec_start = tuple(p - o for p, o in zip(pos, offset))
|
| 23 |
rec_end = tuple(m + n - o for m, n, o in zip((x + text_w, y + text_h), offset, (25, 0)))
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
cv2.putText(
|
| 28 |
img,
|
| 29 |
msg,
|
| 30 |
-
(int(rec_start[0] +
|
| 31 |
font,
|
| 32 |
font_scale,
|
| 33 |
text_color,
|
|
@@ -41,7 +105,6 @@ def draw_text(
|
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
-
|
| 45 |
def find_angle(p1, p2, ref_pt = np.array([0,0])):
|
| 46 |
p1_ref = p1 - ref_pt
|
| 47 |
p2_ref = p2 - ref_pt
|
|
|
|
| 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
|
| 13 |
+
x2, y2 = rect_end
|
| 14 |
+
w = corner_width
|
| 15 |
+
|
| 16 |
+
# draw filled rectangles
|
| 17 |
+
cv2.rectangle(img, (x1 + w, y1), (x2 - w, y1 + w), box_color, -1)
|
| 18 |
+
cv2.rectangle(img, (x1 + w, y2 - w), (x2 - w, y2), box_color, -1)
|
| 19 |
+
cv2.rectangle(img, (x1, y1 + w), (x1 + w, y2 - w), box_color, -1)
|
| 20 |
+
cv2.rectangle(img, (x2 - w, y1 + w), (x2, y2 - w), box_color, -1)
|
| 21 |
+
cv2.rectangle(img, (x1 + w, y1 + w), (x2 - w, y2 - w), box_color, -1)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# draw filled ellipses
|
| 25 |
+
cv2.ellipse(img, (x1 + w, y1 + w), (w, w),
|
| 26 |
+
angle = 0, startAngle = -90, endAngle = -180, color = box_color, thickness = -1)
|
| 27 |
+
|
| 28 |
+
cv2.ellipse(img, (x2 - w, y1 + w), (w, w),
|
| 29 |
+
angle = 0, startAngle = 0, endAngle = -90, color = box_color, thickness = -1)
|
| 30 |
+
|
| 31 |
+
cv2.ellipse(img, (x1 + w, y2 - w), (w, w),
|
| 32 |
+
angle = 0, startAngle = 90, endAngle = 180, color = box_color, thickness = -1)
|
| 33 |
+
|
| 34 |
+
cv2.ellipse(img, (x2 - w, y2 - w), (w, w),
|
| 35 |
+
angle = 0, startAngle = 0, endAngle = 90, color = box_color, thickness = -1)
|
| 36 |
+
|
| 37 |
+
return img
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def draw_dotted_line(frame, lm_coord, start, end, line_color):
|
| 43 |
+
pix_step = 0
|
| 44 |
+
|
| 45 |
+
for i in range(start, end+1, 8):
|
| 46 |
+
cv2.circle(frame, (lm_coord[0], i+pix_step), 2, line_color, -1, lineType=cv2.LINE_AA)
|
| 47 |
+
|
| 48 |
+
return frame
|
| 49 |
+
|
| 50 |
def draw_text(
|
| 51 |
img,
|
|
|
|
| 52 |
msg,
|
| 53 |
+
width = 7,
|
| 54 |
font=cv2.FONT_HERSHEY_SIMPLEX,
|
| 55 |
pos=(0, 0),
|
| 56 |
font_scale=1,
|
|
|
|
| 58 |
text_color=(0, 255, 0),
|
| 59 |
text_color_bg=(0, 0, 0),
|
| 60 |
box_offset=(20, 10),
|
| 61 |
+
overlay_image = False,
|
| 62 |
+
overlay_type = None
|
| 63 |
):
|
| 64 |
|
| 65 |
offset = box_offset
|
| 66 |
x, y = pos
|
| 67 |
text_size, _ = cv2.getTextSize(msg, font, font_scale, font_thickness)
|
| 68 |
text_w, text_h = text_size
|
| 69 |
+
|
| 70 |
rec_start = tuple(p - o for p, o in zip(pos, offset))
|
| 71 |
rec_end = tuple(m + n - o for m, n, o in zip((x + text_w, y + text_h), offset, (25, 0)))
|
| 72 |
+
|
| 73 |
+
resize_height = 0
|
| 74 |
+
|
| 75 |
+
if overlay_image:
|
| 76 |
+
resize_height = rec_end[1] - rec_start[1]
|
| 77 |
+
# print("Height: ", resize_height)
|
| 78 |
+
# print("Width: ", rec_end[0] - rec_start[0])
|
| 79 |
+
img = draw_rounded_rect(img, rec_start, (rec_end[0]+resize_height, rec_end[1]), width, text_color_bg)
|
| 80 |
+
if overlay_type == "correct":
|
| 81 |
+
overlay_res = cv2.resize(correct, (resize_height, resize_height), interpolation = cv2.INTER_AREA)
|
| 82 |
+
elif overlay_type == "incorrect":
|
| 83 |
+
overlay_res = cv2.resize(incorrect, (resize_height, resize_height), interpolation = cv2.INTER_AREA)
|
| 84 |
+
|
| 85 |
+
img[rec_start[1]:rec_start[1]+resize_height, rec_start[0]+width:rec_start[0]+width+resize_height] = overlay_res
|
| 86 |
+
|
| 87 |
+
else:
|
| 88 |
+
img = draw_rounded_rect(img, rec_start, rec_end, width, text_color_bg)
|
| 89 |
|
| 90 |
|
| 91 |
cv2.putText(
|
| 92 |
img,
|
| 93 |
msg,
|
| 94 |
+
(int(rec_start[0]+resize_height + 8), int(y + text_h + font_scale - 1)),
|
| 95 |
font,
|
| 96 |
font_scale,
|
| 97 |
text_color,
|
|
|
|
| 105 |
|
| 106 |
|
| 107 |
|
|
|
|
| 108 |
def find_angle(p1, p2, ref_pt = np.array([0,0])):
|
| 109 |
p1_ref = p1 - ref_pt
|
| 110 |
p2_ref = p2 - ref_pt
|