Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtualmouse #696

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Computer Vision/Virtual Mouse/Virtualmouse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import cv2
import mediapipe as mp
import pyautogui
cap = cv2.VideoCapture(0)
hand_detector = mp.solutions.hands.Hands()
drawing_utils = mp.solutions.drawing_utils
screen_width, screen_height = pyautogui.size()
index_y = 0
while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1)
frame_height,frame_width, _ = frame.shape
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
output = hand_detector.process(rgb_frame)
hands = output.multi_hand_landmarks
if hands:
for hand in hands:
drawing_utils.draw_landmarks(frame, hand)
landmarks = hand.landmark
for id, landmark in enumerate(landmarks):
x = int(landmark.x * frame_width)
y: int = int(landmark.y * frame_height)

if id == 8:
cv2.circle(img=frame, center=(x, y), radius=10, color=(128, 0, 128))
index_x = screen_width / frame_width * x
index_y = screen_height / frame_height * y

if id == 4:
cv2.circle(img=frame,center=(x, y), radius=10, color=(128, 0, 128))
thumb_x = screen_width / frame_width * x
thumb_y = screen_height / frame_height * y
print('outside', abs(index_y - thumb_y))
if abs(index_y - thumb_y) < 20:
pyautogui.click()
pyautogui.sleep(1)
elif abs(index_y - thumb_y) < 100:
pyautogui.moveTo(index_x, index_y)
cv2.imshow('Virtual Mouse',frame)
cv2.waitKey(1)
Loading