-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv1_mouseGes.py
227 lines (174 loc) · 6.21 KB
/
v1_mouseGes.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import cv2
import numpy as np
from functions import *
import os
# Defining default ranges for colour values
blue = np.array([[85,69,36],[128,255,255]])# 108,78,20-->105,69,36
yellow = np.array([[11,70,80],[61,255,255]])# 41,70,80--> 31,70,80
red = np.array([[155, 82, 72],[185, 255, 255]])# 174,85,72--> 175,82,58
# Area ranges for contours of different colours to be detected
r_area = [100,1500]
b_area = [100,1500]
y_area = [100,1700]
# Prior initialization of all centers
b_cen, y_pos, r_cen = [240,320],[240,320],[240,320]
cursor = [960,540]
# Status variables
perform = False
showCentroid = False
showContour = False
def changeStatus(key):
global perform
global showCentroid
global showContour
global yellow, red, blue
if key == ord('p'):
perform = not perform
if perform:
print("Mouse simulation ON..")
else:
print("Mouse simulation OFF")
elif key == ord('c'):
showCentroid = not showCentroid
if showCentroid:
print("Showing Cenroid")
else:
print("Not showing Centroid")
elif key == ord('r'):
print(" In recalibration mode.")
print(" Use trackbars to calibrate press space when done")
print(" Press D for default settings")
print("=======================================================")
# Calibration function present in functions.py
yellow = calibration('Yellow', yellow, cap)
blue = calibration('Blue', blue, cap)
red = calibration('Red', red, cap)
elif key == ord('d'):
showContour = not showContour
if showContour:
print("Showing Contours")
else:
print("Not showing Contours")
else:
pass
def performAction(yp, rc, bc, action, drag, perform):
if perform:
cursor[0] = 4*(yp[0]-110)
cursor[1] = 4*(yp[1]-110)
if action == 'move':
# if within boundary of frame go to respective cursor coordinates
if yp[0]>110 and yp[0]<560 and yp[1]>110 and yp[1]<420:
pyautogui.moveTo(cursor[0],cursor[1])
# if on left edge
elif yp[0]<110 and yp[1]>110 and yp[1]<420:
pyautogui.moveTo( 8 , cursor[1])
# if on right edge
elif yp[0]>560 and yp[1]>110 and yp[1]<420:
pyautogui.moveTo(1912, cursor[1])
# if on top edge
elif yp[0]>110 and yp[0]<560 and yp[1]<110:
pyautogui.moveTo(cursor[0] , 8)
# if on bottom edge
elif yp[0]>110 and yp[0]<560 and yp[1]>420:
pyautogui.moveTo(cursor[0] , 1072)
# top left corner
elif yp[0]<110 and yp[1]<110:
pyautogui.moveTo(8, 8)
# bottom left corner
elif yp[0]<110 and yp[1]>420:
pyautogui.moveTo(8, 1072)
# bottom right corner
elif yp[0]>560 and yp[1]>420:
pyautogui.moveTo(1912, 1072)
# top right corner
else:
pyautogui.moveTo(1912, 8)
elif action == 'left':
pyautogui.click(button = 'left')
#time.sleep(0.3)
elif action == 'right':
pyautogui.click(button = 'right')
time.sleep(0.3)
elif action == 'up':
pyautogui.scroll(5)
elif action == 'down':
pyautogui.scroll(-5)
elif action == 'SS':
os.system("gnome-screenshot --file=image.png")
time.sleep(0.3)
elif action == 'drag' and drag == 'true':
global y_pos
drag = 'false'
pyautogui.mouseDown()
while(1):
k = cv2.waitKey(10) & 0xFF
changeStatus(k)
_, frameinv = cap.read()
# flip horizontaly to get mirror image in camera
frame = cv2.flip( frameinv, 1)
hsv = cv2.cvtColor( frame, cv2.COLOR_BGR2HSV)
b_mask = createMask( hsv, blue)
r_mask = createMask( hsv, red)
y_mask = createMask( hsv, yellow)
Yprev = y_pos
b_cen = drawCentroid( frame, b_area, b_mask, showCentroid, showContour)
r_cen = drawCentroid( frame, r_area, r_mask, showCentroid, showContour)
y_cen = drawCentroid( frame, y_area, y_mask, showCentroid, showContour)
if Yprev[0]!=-1 and y_cen[0]!=-1:
y_pos = setCursor(y_cen, Yprev)
performAction(y_pos, r_cen, b_cen, 'move', drag, perform)
cv2.imshow('Frame', frame)
# if fingers go far apart i.e, drag action stops then end
if distance(y_pos,r_cen)>60 or distance(y_pos,b_cen)>60 or distance(r_cen,b_cen)>60:
break
pyautogui.mouseUp()
# Begin
cap = cv2.VideoCapture(0)
print("=======================================================")
print(" In calibration mode.")
print(" Use trackbars to calibrate press space when done")
print(" Press D for default settings")
print("=======================================================")
# Calibration ranges so as to detect only 3 colours
# Calibration function present in functions.py
yellow = calibration('Yellow', yellow, cap)
blue = calibration('Blue', blue, cap)
red = calibration('Red', red, cap)
print("Calibration Successfull!!")
cv2.namedWindow('Frame')
print("=========================================================")
print(" press P to turn ON and OFF mouse control")
print(" press C to display centroid of various colours")
print(" press R to recalibrate")
print(" press D to show contours")
print(" Press ESC to exit")
print("=========================================================")
while (1):
key = cv2.waitKey(10) & 0xFF
# Checking status based on key pressed
changeStatus(key)
'''Now we are going to capture video flip it as what we get
from input is reversed frame then based on color ranges
calibrated we are going to prepare mask for respective colors'''
_, source = cap.read()
frame = cv2.flip(source, 1)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
b_mask = createMask(hsv, blue)
r_mask = createMask(hsv, red)
y_mask = createMask(hsv, yellow)
b_cen = drawCentroid( frame, b_area, b_mask, showCentroid, showContour)
r_cen = drawCentroid( frame, r_area, r_mask, showCentroid, showContour)
y_cen = drawCentroid( frame, y_area, y_mask, showCentroid, showContour)
'''cv2.drawContours(frame, b_cen, -1, (0,255,0), 5)
cv2.drawContours(frame, r_cen, -1, (0,255,0), 5)
cv2.drawContours(frame, y_cen, -1, (0,255,0), 5)'''
Yprev = y_pos
if Yprev[0]!=-1 and y_cen[0]!=-1 and y_pos[0]!=-1:
y_pos = setCursor(y_cen, Yprev)
output = chooseAction(y_pos, r_cen, b_cen)
if output[0]!=-1:
performAction(y_pos, r_cen, b_cen, output[0], output[1], perform)
cv2.imshow('Frame', frame)
if key == 27:
break
cv2.destroyAllWindows()