-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
73 lines (55 loc) · 1.98 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import cv2
import numpy as np
from pygame import mixer
# Computes if the given joints are horizontal according to the y axis position, within a given error.
def horizontal_joints(joints_y, y_axis, error):
for y in joints_y:
if not(y_axis - error <= y <= y_axis + error):
return False
return True
# Computes if the given joints are vertical according to the x axis position, within a given error.
def vertical_joints(joints_x, x_axis, error):
for x in joints_x:
if not(x_axis - error <= x <= x_axis + error):
return False
return True
def joint_in_region(x, y, x_axis, y_axis, x_error, y_error):
return x_axis - x_error <= x <= x_axis + x_error and y_axis - y_error <= y <= y_axis + y_error
def angle_in_region(angle, expected_radius, radius_error):
return expected_radius - radius_error <= angle <= expected_radius + radius_error
# Computes the angle of 3 given joints. Points must be given in order.
def calculate_angle(p1, p2, p3):
radians = np.arctan2(p3[1] - p2[1], p3[0] - p2[0]) - np.arctan2(p1[1] - p2[1], p1[0] - p2[0])
joint_angle = np.abs(radians * 180.0 / np.pi)
if joint_angle > 180.0:
joint_angle = 360 - joint_angle
return joint_angle
# Draws the angle on the reference joint.
def visualize_angle(frame, pos, angle):
cv2.putText(
img=frame,
text=str(angle),
org=pos,
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=2.0,
color=(0, 255, 0),
thickness=2,
lineType=cv2.LINE_AA
)
# Displays any message on the frame.
def display_message(frame, text, pos, color=(0, 255, 0)):
cv2.putText(
img=frame,
text=text,
org=pos,
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=2.0,
color=color,
thickness=2,
lineType=cv2.LINE_AA
)
# Plays the reward sound if a repetition was successful.
def play_reward(sound_path):
mixer.init()
mixer.music.load(sound_path)
mixer.music.play()