Skip to content

Commit

Permalink
Files
Browse files Browse the repository at this point in the history
  • Loading branch information
Utpal Tiwari committed Mar 16, 2023
0 parents commit 1449e18
Show file tree
Hide file tree
Showing 9 changed files with 656 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/
53 changes: 53 additions & 0 deletions Gestures/GamingGestures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pyautogui
from HandDetection import HandDetection


class GamingGestures:
def __init__(self, detector:HandDetection, trigger:int=32):
self.detector = detector
self.trigger = trigger
self.CLICKED = False
# self.HOLD = False


def detectGestures(self, frame, hands):
# TWO HANDED MODE
if len(hands) == 2:
handL, handR = None, None
handL = hands[0] if hands[0].get("type") == "Left" else hands[1]
handR = hands[0] if hands[0].get("type") == "Right" else hands[1]

fingersL, handInfoL = self.detector.fingersUpAndHandSide(handL)
fingersR, handInfoR = self.detector.fingersUpAndHandSide(handR)

if handInfoL[0][:-1] == "Front" == handInfoR[0][:-1] and handInfoL[1][:-1] == "Up" == handInfoR[1][:-1]:

if 1 not in fingersL and 1 not in fingersR and self.CLICKED:
print("RESET")
self.CLICKED = False

elif 1 not in fingersR and 0 not in fingersL and not self.CLICKED:
pyautogui.press("left")
self.CLICKED = True

elif 1 not in fingersL and 0 not in fingersR and not self.CLICKED:
pyautogui.press("right")
self.CLICKED = True

elif ((1 not in fingersL[1:] and 1 not in fingersR and fingersL[0] == 1) or (1 not in fingersR[1:] and 1 not in fingersL and fingersR[0] == 1)) and not self.CLICKED:
pyautogui.press("down")
self.CLICKED = True

elif ((1 not in fingersL[2:] and 1 not in fingersR and fingersL[0] == 0 and fingersL[1] == 1) or (1 not in fingersR[2:] and 1 not in fingersL and fingersR[0] == 0 and fingersR[1] == 1)) and not self.CLICKED:
pyautogui.press("up")
self.CLICKED = True

elif 1 not in fingersL[3:] and 1 not in fingersR and fingersL[0] == 0 and 0 not in fingersL[1:3] and not self.CLICKED:
pyautogui.press("space")
self.CLICKED = True

elif 1 not in fingersR[3:] and 1 not in fingersL and fingersR[0] == 0 and 0 not in fingersR[1:3] and not self.CLICKED:
pyautogui.press("e")
self.CLICKED = True

return frame
124 changes: 124 additions & 0 deletions Gestures/KeyboardGestures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import pyautogui
from HandDetection import HandDetection


class KeyboardGestures:
def __init__(self, detector:HandDetection, trigger:int=32):
self.detector = detector
self.trigger = trigger
self.CLICKED = False
self.HOLD = False


def detectGestures(self, frame, hands, distTrigger):
if len(hands) == 0:
self.CLICKED = False
self.HOLD = False

elif len(hands) == 1:
myHandType = hands[0].get("type")
fingers, handInfo = self.detector.fingersUpAndHandSide(hands[0])

if myHandType == "Right":

if handInfo[0][:-1] == "Back" and handInfo[1][:-1] == "Up":

# ALT + F4 GESRURE (TO CLOSE)
if 0 not in fingers[1:] and fingers[0] == 0 and not self.CLICKED:
pyautogui.keyDown("alt")
pyautogui.keyDown("f4")
pyautogui.keyUp("f4")
pyautogui.keyUp("alt")
print("QUIT")
self.CLICKED = True

# WIN + D GESRURE (TO SHOW/HIDE DESKTOP)
elif 1 not in fingers[1:] and fingers[0] == 1 and not self.CLICKED:
pyautogui.keyDown("win")
pyautogui.keyDown("d")
pyautogui.keyUp("d")
pyautogui.keyUp("win")
self.CLICKED = True


elif handInfo[0][:-1] == "Front" and handInfo[1][:-1] == "Up":
# TAB GESTURE (TO ADD A TAB BUTTON PRESS)
if 1 not in fingers[1:] and fingers[0] == 1 and not self.CLICKED:
pyautogui.keyDown("tab")
pyautogui.keyUp("tab")
self.CLICKED = True

# SPACEBAR GESTURE (TO ADD A SPACE BUTTON PRESS)
elif 1 not in fingers[2:] and 0 not in fingers[:2] and not self.CLICKED:
pyautogui.keyDown("space")
pyautogui.keyUp("space")
self.CLICKED = True

# ENTER KEY GESTURE (TO ADD A ENTER KEY PRESS)
elif 0 not in fingers[1:] and fingers[0] == 0 and not self.CLICKED:
pyautogui.keyDown("enter")
pyautogui.keyUp("enter")
self.CLICKED = True

elif myHandType == "Left":

if handInfo[0][:-1] == "Back" and handInfo[1][:-1] == "Up":
# ESC BTN GESRURE (TO CLICK ESC KEY)
if 0 not in fingers[1:] and fingers[0] == 0 and not self.CLICKED:
pyautogui.keyDown("esc")
pyautogui.keyDown("esc")
self.CLICKED = True

# WIN GESRURE (TO OPEN UP APPLICATIONS)
elif 1 not in fingers[1:] and fingers[0] == 1 and not self.CLICKED:
pyautogui.keyDown("win")
pyautogui.keyUp("win")
self.CLICKED = True

# PRINT SCREEN GESTURE (FOR SCREEN SHOT)
elif 0 not in fingers[1:3] and fingers[0] == 0 == fingers[4] and not self.CLICKED:
pyautogui.keyDown("alt")
pyautogui.keyDown("printscr")
pyautogui.keyUp("printscr")
pyautogui.keyUp("alt")
self.CLICKED = True



if 1 not in fingers:
self.CLICKED = False
self.HOLD = False

if len(hands) == 2:
handL, handR = None, None
handL = hands[0] if hands[0].get("type") == "Left" else hands[1]
handR = hands[0] if hands[0].get("type") == "Right" else hands[1]

fingersL, handInfoL = self.detector.fingersUpAndHandSide(handL)
fingersR, handInfoR = self.detector.fingersUpAndHandSide(handR)

if handInfoL[0][:-1] == "Front" == handInfoR[0][:-1] and handInfoL[1][:-1] == "Up" == handInfoR[1][:-1]:

if 1 not in fingersL and 1 not in fingersR and self.CLICKED:
print("RESET")
self.CLICKED = False

elif 1 not in fingersR and 0 not in fingersL and not self.CLICKED:
pyautogui.press("left")
self.CLICKED = True

elif 1 not in fingersL and 0 not in fingersR and not self.CLICKED:
pyautogui.press("right")
self.CLICKED = True

elif ((1 not in fingersL[1:] and 1 not in fingersR and fingersL[0] == 1) or (1 not in fingersR[1:] and 1 not in fingersL and fingersR[0] == 1)) and not self.CLICKED:
pyautogui.press("down")
self.CLICKED = True

elif ((1 not in fingersL[2:] and 1 not in fingersR and fingersL[0] == 0 and fingersL[1] == 1) or (1 not in fingersR[2:] and 1 not in fingersL and fingersR[0] == 0 and fingersR[1] == 1)) and not self.CLICKED:
pyautogui.press("up")
self.CLICKED = True



return frame
136 changes: 136 additions & 0 deletions Gestures/MouseGestures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import pyautogui, mouse
from HandDetection import HandDetection
import numpy as np
import cv2


class MouseGestures:
def __init__(self, detector:HandDetection, trigger:int=32, scrollSpeed:int=10):
self.detector = detector
self.status = ""
self.CLICKED = False
self.trigger = trigger
self.scrollSpeed = scrollSpeed
self.DRAG = False


def moveMouse(self, x, y, t):
pyautogui.moveTo(x, y, t)


def convertPoints(self, p1:tuple, screenSize:tuple, frameSize:tuple, frameMargin:tuple):
newX = np.interp(p1[0], (frameMargin[1], frameSize[0] - frameMargin[3]), (0, screenSize[0]))
newY = np.interp(p1[1], (frameMargin[0], frameSize[1] - frameMargin[2]), (0, screenSize[1]))
return (newX, newY)


def detectMovements(self, frameSize, frameMargin, frame, hands):
# ONE HANDED CONTROLS
if len(hands) >= 1:
lmList = hands[0].get("lmList")
handType = hands[0].get("type")
fingers, handInfo = self.detector.fingersUpAndHandSide(hands[0])
rightClickEnable = False

if handInfo[0][:-1] == "Front" and handInfo[1][:-1] == "Up":
cv2.rectangle(frame, (frameMargin[3], frameMargin[0]), (frameSize[0] - frameMargin[1], frameSize[1] - frameMargin[2]), (220, 255, 0), 2, 1)

# FOR MOVEMENT AND MOUSE & LEFT SINGLE CLICK CONTROL & RIGHT & LEFT DOUBLE CLICK CONTROL
if 1 not in fingers[3:4] and 0 not in fingers[1:3]:
dist, info, frame = self.detector.findDistance(lmList[8][:2], lmList[12][:2], frame)
x, y = self.convertPoints(info[-2:], pyautogui.size(), frameSize, frameMargin)

if dist >= self.trigger:

# PERFECTING THUMB DETECTION
if handType == "Right":

if handInfo[0] == "FrontR" and handInfo[1] == "UpR":
if lmList[4][0] >= lmList[9][0]:
rightClickEnable = False
elif lmList[4][0] < lmList[9][0]:
rightClickEnable = True

elif handInfo[0] == "FrontR" and handInfo[1] == "DownR":
if lmList[4][0] <= lmList[9][0]:
rightClickEnable = False
elif lmList[4][0] > lmList[9][0]:
rightClickEnable = True

elif handInfo[0] == "BackR" and handInfo[1] == "UpR":
if lmList[4][0] <= lmList[9][0]:
rightClickEnable = False
elif lmList[4][0] > lmList[9][0]:
rightClickEnable = True

elif handInfo[0] == "BackR" and handInfo[1] == "DownR":
if lmList[4][0] >= lmList[9][0]:
rightClickEnable = False
elif lmList[4][0] < lmList[9][0]:
rightClickEnable = True

elif handType == "Left":

if handInfo[0] == "FrontL" and handInfo[1] == "UpL":
if lmList[4][0] <= lmList[9][0]:
rightClickEnable = False
elif lmList[4][0] > lmList[9][0]:
rightClickEnable = True

elif handInfo[0] == "FrontL" and handInfo[1] == "DownL":
if lmList[4][0] >= lmList[9][0]:
rightClickEnable = False
elif lmList[4][0] < lmList[9][0]:
rightClickEnable = True

elif handInfo[0] == "BackL" and handInfo[1] == "UpL":
if lmList[4][0] >= lmList[9][0]:
rightClickEnable = False
elif lmList[4][0] < lmList[9][0]:
rightClickEnable = True

elif handInfo[0] == "BackL" and handInfo[1] == "DownL":
if lmList[4][0] <= lmList[9][0]:
rightClickEnable = False
elif lmList[4][0] > lmList[9][0]:
rightClickEnable = True


if fingers[0] == 0 == fingers[4] and len(hands) == 2:
print("DRAGGING")
self.DRAG = True
if not self.CLICKED:
pyautogui.mouseDown()
self.CLICKED = True
pyautogui.moveTo(x, y)

elif fingers[0] == 0 == fingers[4]:
self.moveMouse(x, y, 0.05)
self.CLICKED = False

elif fingers[0] == 0 and fingers[4] == 1 and not self.CLICKED:
pyautogui.doubleClick(x, y)
self.CLICKED = True
self.DRAG = False

elif fingers[0] == 1 and fingers[4] == 0 and rightClickEnable and not self.CLICKED:
pyautogui.rightClick(x, y)
self.CLICKED = True
self.DRAG = False

else:
cv2.circle(frame, info[-2:], 7, (200, 220, 0), cv2.FILLED)
if not self.CLICKED:
pyautogui.leftClick(x, y)
self.CLICKED = True
self.DRAG = False
print("CLICKED")

elif 0 not in fingers[1:] and fingers[0] == 0:
pyautogui.scroll(self.trigger)

elif handInfo[0][:-1] == "Back" and handInfo[1][:-1] == "Up":
if 0 not in fingers[1:] and fingers[0] == 0:
pyautogui.scroll(-self.trigger)

return frame
Loading

0 comments on commit 1449e18

Please sign in to comment.