/Eyes-Controlled-Mouse

An AI which helps you to control your mouse without touch it.

Primary LanguagePythonGNU Affero General Public License v3.0AGPL-3.0

An AI which helps you to control your mouse using your eyes.

Requirements

  • Python 3
  • OpenCV
  • Mediapipe
  • PyAutoGUI

Code

Zooming and cropping the frame.

scale = 10
centerX, centerY = int(height/2), int(width/2)
radiusX, radiusY = int(scale*height/60), int(scale*width/60)
minX, maxX = centerX - radiusX, centerX + radiusX
minY, maxY = centerY - radiusY, centerY + radiusY
cropped = frame[minX:maxX, minY:maxY]
resized_cropped = cv2.resize(cropped, (width, height))

Finding out landamrks of the eyes.
rgb_frame = cv2.cvtColor(resized_cropped, cv2.COLOR_BGR2RGB)
output = face_mesh.process(rgb_frame)
landmark_points = output.multi_face_landmarks

Moving the mouse the same space the eyes move.
screen_x = screen_w / frame_w * x
screen_y = screen_h / frame_h * y
try:
  pyautogui.moveTo(screen_x, screen_y) 
except pyautogui.FailSafeException:
  print('Running code before exiting.')
  break
pyautogui.moveTo(screen_x, screen_y)

Left click: if the y coordinates of upper and lower eyelid are close enough.
for landmark in left:
  x = int(landmark.x * frame_w)
  y = int(landmark.y * frame_h)
  cv2.circle(resized_cropped, (x, y), 3, (0, 255, 255))
if (left[0].y - left[1].y) < 0.04:
  pyautogui.click()
  pyautogui.sleep(1)