Skip to content

Commit e6090a1

Browse files
authored
Add files via upload
0 parents  commit e6090a1

3 files changed

Lines changed: 201 additions & 0 deletions

File tree

AIVirtualMouse.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import numpy as np
2+
import cv2
3+
import autopy
4+
import HandTrackingModule as htm
5+
import time
6+
7+
##########################
8+
wCam, hCam = 640, 480
9+
frameR = 100 # Frame Reduction
10+
smoothening = 7
11+
##########################
12+
13+
pTime = 0
14+
plocX, plocY = 0, 0
15+
clocX, clocY = 0, 0
16+
17+
try:
18+
cap = cv2.VideoCapture(0)
19+
except:
20+
cap = cv2.VideoCapture(1)
21+
cap.set(3, wCam)
22+
cap.set(4, hCam)
23+
detector = htm.handDetector(maxHands=1)
24+
wScr, hScr = autopy.screen.size()
25+
print(wScr, hScr)
26+
27+
while True:
28+
# 1. Find hand Landmarks
29+
success, img = cap.read()
30+
img = detector.findHands(img)
31+
lmList, bbox = detector.findPosition(img)
32+
33+
# 2. Get the tip of the index and middle fingers
34+
if len(lmList)!=0:
35+
x1, y1 = lmList[8][1:]
36+
x2, y2 = lmList[12][1:]
37+
# print(x1, y1, x2, y2)
38+
39+
# 3. Check which fingers are up
40+
fingers = detector.fingersUp()
41+
# print(fingers)
42+
cv2.rectangle(img, (frameR, frameR), (wCam - frameR, hCam - frameR), (255, 0, 255), 2)
43+
# 4. Only Index Finger : Moving Mode
44+
if fingers[1] == 1 and fingers[2] == 0:
45+
# 5. Convert Coordinates
46+
x3 = np.interp(x1, (frameR, wCam - frameR), (0,wScr))
47+
y3 = np.interp(y1, (frameR, hCam - frameR), (0,hScr))
48+
49+
# 6. Smoothen Values
50+
clocX = plocX + (x3 - plocX) / smoothening
51+
clocY = plocY + (y3 - plocY) / smoothening
52+
# 7. Move Mouse
53+
autopy.mouse.move(wScr - clocX, clocY)
54+
cv2.circle(img, (x1, y1), 10, (255, 0, 255), cv2.FILLED)
55+
plocX, plocY = clocX, clocY
56+
57+
# 8. Both Index and middle fingers are up : Clicking Mode
58+
if fingers[1] == 1 and fingers[2] == 1:
59+
60+
# 9. Find distance between fingers
61+
length, img, lineInfo = detector.findDistance(8, 12, img)
62+
63+
# 10. Click mouse if distance short
64+
if length < 40:
65+
cv2.circle(img, (lineInfo[4], lineInfo[5]), 10, (0, 255, 255), cv2.FILLED)
66+
autopy.mouse.click()
67+
68+
69+
70+
# 11. Frame Rate
71+
cTime = time.time()
72+
fps = 1/(cTime-pTime)
73+
pTime = cTime
74+
cv2.putText(img, str(int(fps)), (20, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
75+
76+
# 12. Display
77+
78+
cv2.imshow("Image", img)
79+
cv2.waitKey(1)
80+

HandTrackingModule.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import cv2
2+
import mediapipe as mp
3+
import time
4+
import math
5+
import numpy as np
6+
7+
class handDetector():
8+
def __init__(self, mode=False, maxHands=2, detectionCon=0.5,modelComplexity=1, trackCon=0.5):
9+
self.mode = mode
10+
self.maxHands = maxHands
11+
self.detectionCon = detectionCon
12+
self.modelComplex = modelComplexity
13+
self.trackCon = trackCon
14+
15+
self.mpHands = mp.solutions.hands
16+
self.hands = self.mpHands.Hands(self.mode, self.maxHands,self.modelComplex, self.detectionCon, self.trackCon)
17+
self.mpDraw = mp.solutions.drawing_utils
18+
self.tipIds = [4, 8, 12, 16, 20]
19+
20+
def findHands(self, img, draw=True):
21+
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
22+
self.results = self.hands.process(imgRGB)
23+
# print(results.multi_hand_landmarks)
24+
25+
if self.results.multi_hand_landmarks:
26+
for handLms in self.results.multi_hand_landmarks:
27+
if draw:
28+
self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
29+
return img
30+
31+
def findPosition(self, img, handNo=0, draw=True):
32+
xList = []
33+
yList = []
34+
bbox = []
35+
self.lmList = []
36+
if self.results.multi_hand_landmarks:
37+
myHand = self.results.multi_hand_landmarks[handNo]
38+
for id, lm in enumerate(myHand.landmark):
39+
# print(id, lm)
40+
h, w, c = img.shape
41+
cx, cy = int(lm.x * w), int(lm.y * h)
42+
xList.append(cx)
43+
yList.append(cy)
44+
# print(id, cx, cy)
45+
self.lmList.append([id, cx, cy])
46+
if draw:
47+
cv2.circle(img, (cx, cy), 5, (0, 0, 255), cv2.FILLED)
48+
49+
xmin, xmax = min(xList), max(xList)
50+
ymin, ymax = min(yList), max(yList)
51+
bbox = xmin, ymin, xmax, ymax
52+
53+
if draw:
54+
cv2.rectangle(img, (xmin - 20, ymin - 20), (xmax + 20, ymax + 20), (0, 255, 0), 2)
55+
56+
return self.lmList, bbox
57+
58+
def fingersUp(self):
59+
fingers = []
60+
# Thumb
61+
if self.lmList[self.tipIds[0]][1] > self.lmList[self.tipIds[0] - 1][1]:
62+
fingers.append(1)
63+
else:
64+
fingers.append(0)
65+
66+
# Fingers
67+
for id in range(1, 5):
68+
69+
if self.lmList[self.tipIds[id]][2] < self.lmList[self.tipIds[id] - 2][2]:
70+
fingers.append(1)
71+
else:
72+
fingers.append(0)
73+
74+
# totalFingers = fingers.count(1)
75+
return fingers
76+
77+
def findDistance(self, p1, p2, img, draw=True, r=15, t=3):
78+
x1, y1 = self.lmList[p1][1:]
79+
x2, y2 = self.lmList[p2][1:]
80+
cx, cy = (x1 + x2) // 2, (y1 + y2) //2
81+
82+
if draw:
83+
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), t)
84+
cv2.circle(img, (x1, y1), r, (255, 0, 255), cv2.FILLED)
85+
cv2.circle(img, (x2, y2), r, (255, 0, 255), cv2.FILLED)
86+
cv2.circle(img, (cx, cy), r, (0, 0, 255), cv2.FILLED)
87+
length = math.hypot(x2 - x1, y2 - y1)
88+
89+
return length, img, [x1, y1, x2, y2, cx, cy]
90+
91+
def main():
92+
pTime = 0
93+
cTime = 0
94+
try:
95+
cap = cv2.VideoCapture(0)
96+
except:
97+
cap = cv2.VideoCapture(1)
98+
detector = handDetector()
99+
while True:
100+
success, img = cap.read()
101+
img = detector.findHands(img)
102+
lmList, bbox = detector.findPosition(img)
103+
if len(lmList) != 0:
104+
print(lmList[4])
105+
106+
cTime = time.time()
107+
fps = 1 / (cTime - pTime)
108+
pTime = cTime
109+
110+
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
111+
112+
cv2.imshow("Image", img)
113+
cv2.waitKey(1)
114+
115+
if __name__ == "__main__":
116+
main()

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
numpy
2+
opencv-python
3+
opencv-contrib-python
4+
autopy
5+
mediapipe

0 commit comments

Comments
 (0)