-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEyeToMouse.py
More file actions
147 lines (126 loc) · 3.82 KB
/
Copy pathEyeToMouse.py
File metadata and controls
147 lines (126 loc) · 3.82 KB
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
# With reference to this tutorial
# https://picoledelimao.github.io/blog/2017/01/28/eyeball-tracking-for-mouse-control-in-opencv/
# And various openCV tutorials
import cv2
import numpy as np
import win32api
from win32api import GetSystemMetrics
import win32con
def main():
webCam = cv2.VideoCapture(0)
while (True):
ret, img = webCam.read()
#cv2.imshow('frame', frame)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
face_cascade = cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('data/haarcascade_eye.xml')
detectEyes(img, face_cascade, eye_cascade)
click(mousePoint[0], mousePoint[1])
cv2.imshow('img',img)
webCam.release()
cv2.destroyAllWindows()
def detectEyes(img, faceCascade, eyeCascade):
global lastPoint
global mousePoint
try:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
except Exception as e:
print('img not loaded')
cv2.imshow('img',img)
else:
pass
try:
faces = faceCascade.detectMultiScale(gray, 1.1, 3)
except Exception as e:
print('No faces found')
cv2.imshow('img',img)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eyeCascade.detectMultiScale(roi_gray, 1.1, 3)
if(isinstance(eyes, tuple)):
continue
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
eyeRect = getLeftMostEye(eyes)
(ex, ey, ew, eh) = eyeRect
eye = roi_gray[ey:ey+eh, ex:ex+ew] #face rect
eyeColor =roi_color[ey:ey+eh, ex:ex+ew] #face rect with color
eye = cv2.equalizeHist(eye)
circles = cv2.HoughCircles(eye, cv2.HOUGH_GRADIENT, 1, 6, 1, 300, 7, 8, 13)
if(len(circles) > 0):
eyeball = getEyeBall(eye, circles)
if(type(eyeball) == np.ndarray):
# print(ex)
# print(eyeball[0])
# print(ey)
# print(eyeball[1])
centers.append((int(eyeball[0]), int(eyeball[1])))
stableCenter = stabilize(centers, 5)
cv2.circle(eyeColor, stableCenter , int(eyeball[2]), (0, 0, 255), 2 )
if(len(centers) > 0):
xVal = int((stableCenter[0] / ew) * GetSystemMetrics(0))
yVal = int((stableCenter[1] / eh) * GetSystemMetrics(1))
mousePoint = (xVal, yVal)
print(mousePoint)
lastPoint = stableCenter
def getLeftMostEye(eyes):
leftmost = 99999999
leftmostIndex = -1
i = 0
for (x, y, w, h) in eyes:
if(x < leftmost):
leftmost = x
leftmostIndex = i
i += 1
return eyes[leftmostIndex]
def getEyeBall(eye, circles):
sums = np.zeros(len(circles), np.int)
for y in range(0, eye.shape[0]):
for x in range(0, eye.shape[1]):
value = eye[x][y]
for i in range(0, circles.shape[0]):
try:
center = (circles[0][i][0], circles[0][i][1])
radius = circles[0][i][2]
if((((x-center[0]) * (x-center[0])) +( (y - center[1]) * (y - center[1]))) < (radius * radius)):
sums[i] += value
except IndexError:
return None
smallestSum = 9999999
smallestSumIndex = -1
for i in range(0, circles.shape[0]):
if(sums[i] < smallestSum):
smallestSum = sums[i]
smallestSumIndex = i
return circles[0][smallestSumIndex]
def stabilize(points, period):
sumX = 0
sumY = 0
count = 0
while(len(points) > period):
points.pop(0)
for i in range(max(0, len(points) - period), len(points)):
sumX += points[i][0]
sumY += points[i][1]
count += 1
if(count > 0):
sumX = sumX / count
sumY = sumY / count
return (int(sumX), int(sumY))
def click(x, y):
if(x > GetSystemMetrics(0)): x = GetSystemMetrics(0)
if(x < 0): x = 0
if(y > GetSystemMetrics(1)): y = GetSystemMetrics(1)
if(y < 0): y = 0
print("{:d} {:d}".format(x, y))
win32api.SetCursorPos((x, y))
# win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
# win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
centers = []
lastPoint = (500, 500)
mousePoint = (500, 500)
main()