-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmode_controller.py
79 lines (59 loc) · 2.98 KB
/
mode_controller.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
74
75
76
77
78
79
import math
from constants import *
class ModeController:
cx: int = 100
cy: int = 100
w: int = 75
h: int = 75
def __init__(self):
pass
@staticmethod
def display_selected_mode(img, mode):
cv2.putText(img, 'Analytic Mode', (10, 30), FONT_FAMILY, FONT_SCALE, GREEN_COLOR, FONT_SIZE, FONT_LINE)
counter_text = 'Counter: 1'
volume_text = 'Volume Controller: 2'
object_text = 'Object Moving: 3'
if mode == 1:
counter_text = counter_text.replace('1', 'Selected')
if mode == 2:
volume_text = volume_text.replace('2', 'Selected')
if mode == 3:
object_text = object_text.replace('3', 'Selected')
cv2.putText(img, counter_text, (10, 385), FONT_FAMILY, FONT_SCALE, BLUE_COLOR, FONT_SIZE, FONT_LINE)
cv2.putText(img, volume_text, (10, 425), FONT_FAMILY, FONT_SCALE, BLUE_COLOR, FONT_SIZE, FONT_LINE)
cv2.putText(img, object_text, (10, 465), FONT_FAMILY, FONT_SCALE, BLUE_COLOR, FONT_SIZE, FONT_LINE)
@staticmethod
def use_counter_mode(img, landmark_list):
founded_fingers = []
if landmark_list[FINGERS[0]][1] < landmark_list[FINGERS[0] - 1][1]:
founded_fingers.append(1)
else:
founded_fingers.append(0)
for index in range(1, 5):
if landmark_list[FINGERS[index]][2] < landmark_list[FINGERS[index] - 2][2]:
founded_fingers.append(1)
else:
founded_fingers.append(0)
fingers_counted = str(founded_fingers.count(1))
cv2.putText(img, fingers_counted, (530, 465), FONT_FAMILY, 5, WHITE_COLOR, 10, FONT_LINE)
@staticmethod
def use_volume_mode(img, volume_ctrl, landmark_list):
finger_1 = (landmark_list[4][1], landmark_list[4][2])
finger_2 = (landmark_list[8][1], landmark_list[8][2])
between_fingers = ((finger_1[0] + finger_2[0]) // 2, (finger_1[1] + finger_2[1]) // 2)
cv2.circle(img, between_fingers, 8, RED_COLOR, cv2.FILLED)
cv2.circle(img, finger_1, 8, ORANGE_COLOR, cv2.FILLED)
cv2.circle(img, finger_2, 8, ORANGE_COLOR, cv2.FILLED)
cv2.line(img, finger_1, finger_2, ORANGE_COLOR, 4)
length = math.hypot(finger_2[0] - finger_1[0], finger_2[1] - finger_1[1])
volume_bar = volume_ctrl.set_volume(length)
volume_ctrl.display_volume_bar(img, volume_bar)
def use_object_moving_mode(self, img, landmark_list):
rectangle_color = WHITE_COLOR
index_finger = (landmark_list[8][1], landmark_list[8][2])
if self.cx - self.w // 2 < index_finger[0] < self.cx + self.w // 2 and self.cy - self.h // 2 < index_finger[1] < self.cy + self.h // 2:
self.cx, self.cy = index_finger
rectangle_color = YELLOW_COLOR
start_position = (self.cx - self.w // 2, self.cy - self.h // 2)
end_position = (self.cx + self.w // 2, self.cy + self.h // 2)
cv2.rectangle(img, start_position, end_position, rectangle_color, cv2.FILLED)