Skip to content

Commit

Permalink
Create main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Sayad-Uddin-Tahsin authored Jul 9, 2022
0 parents commit 62db0a2
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import cv2
import mediapipe as mp
import pyautogui

cap = cv2.VideoCapture(0)
opened = cap.isOpened()
if opened:
print("OK!")
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(landmark.y*frame_height)
# print(x, y)
if id == 8:
cv2.circle(img=frame, center=(x, y), radius=10, color=(0, 255, 255))
index_x = screen_width/frame_width*x
index_y = screen_height/frame_width*y
pyautogui.moveTo(index_x, index_y)
if id == 4:
cv2.circle(img=frame, center=(x, y), radius=10, color=(0, 255, 255))
thumb_x = screen_width/frame_width*x
thumb_y = screen_height/frame_width*y
if abs(index_y - thumb_y) <= 20:
pyautogui.click()
# pyautogui.sleep(0.5)
cv2.imshow("Virtual Mouse", frame)
cv2.waitKey(1)
else:
raise ConnectionAbortedError("cv2: No Camera Found!")

0 comments on commit 62db0a2

Please sign in to comment.