Skip to content

Commit 0acda5c

Browse files
committed
Finger Counter Using Hand Tracking Added
1 parent d878f67 commit 0acda5c

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Finger-Counter-using-Hand-Tracking-And-Open-cv
2+
finger counting based on open-cv and hand tracking in real time
3+
4+
the important library :
5+
- opencv-python
6+
- mediapipe
7+
8+
the project detects the counting fingers for the right hand and count from (0-7)
9+
10+
# Screenshot
11+
![](fingerCounting.png)
12+
13+
to run the project execute the fingerCountingProject.py script
14+
Loading
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Sat Apr 3 23:09:19 2021
4+
5+
@author: amine gasa
6+
"""
7+
import cv2
8+
import os
9+
import time
10+
import handTrackingModule as htm
11+
def getNumber(ar):
12+
s=""
13+
for i in ar:
14+
s+=str(ar[i]);
15+
16+
if(s=="00000"):
17+
return (0)
18+
elif(s=="01000"):
19+
return(1)
20+
elif(s=="01100"):
21+
return(2)
22+
elif(s=="01110"):
23+
return(3)
24+
elif(s=="01111"):
25+
return(4)
26+
elif(s=="11111"):
27+
return(5)
28+
elif(s=="01001"):
29+
return(6)
30+
elif(s=="01011"):
31+
return(7)
32+
33+
wcam,hcam=640,480
34+
cap=cv2.VideoCapture(0)
35+
cap.set(3,wcam)
36+
cap.set(4,hcam)
37+
pTime=0
38+
detector = htm.handDetector(detectionCon=0.75)
39+
while True:
40+
success,img=cap.read()
41+
img = detector.findHands(img, draw=True )
42+
lmList=detector.findPosition(img,draw=False)
43+
#print(lmList)
44+
tipId=[4,8,12,16,20]
45+
if(len(lmList)!=0):
46+
fingers=[]
47+
#thumb
48+
if(lmList[tipId[0]][1]>lmList[tipId[0]-1][1]):
49+
fingers.append(1)
50+
else :
51+
fingers.append(0)
52+
#4 fingers
53+
for id in range(1,len(tipId)):
54+
55+
if(lmList[tipId[id]][2]<lmList[tipId[id]-2][2]):
56+
fingers.append(1)
57+
58+
else :
59+
fingers.append(0)
60+
61+
62+
cv2.rectangle(img,(20,255),(170,425),(0,255,0),cv2.FILLED)
63+
cv2.putText(img,str(getNumber(fingers)),(45,375),cv2.FONT_HERSHEY_PLAIN,
64+
10,(255,0,0),20)
65+
66+
67+
68+
cTime=time.time()
69+
fps=1/(cTime-pTime)
70+
pTime=cTime
71+
cv2.putText(img, f'FPS: {int(fps)}',(400,70),cv2.FONT_HERSHEY_COMPLEX,1,(255,255,0),3)
72+
cv2.imshow("image",img)
73+
if(cv2.waitKey(1) & 0xFF== ord('q')):
74+
break
75+
76+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import cv2
2+
import mediapipe as mp
3+
import time
4+
5+
6+
class handDetector():
7+
def __init__(self, mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5):
8+
self.mode = mode
9+
self.maxHands = maxHands
10+
self.detectionCon = detectionCon
11+
self.trackCon = trackCon
12+
13+
self.mpHands = mp.solutions.hands
14+
self.hands = self.mpHands.Hands(self.mode, self.maxHands,
15+
self.detectionCon, self.trackCon)
16+
self.mpDraw = mp.solutions.drawing_utils
17+
18+
def findHands(self, img, draw=True):
19+
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
20+
self.results = self.hands.process(imgRGB)
21+
#print(results.multi_hand_landmarks)
22+
23+
if self.results.multi_hand_landmarks:
24+
for handLms in self.results.multi_hand_landmarks:
25+
if draw:
26+
self.mpDraw.draw_landmarks(img, handLms,
27+
self.mpHands.HAND_CONNECTIONS)
28+
return img
29+
30+
def findPosition(self, img, handNo=0, draw=True):
31+
32+
lmList = []
33+
if self.results.multi_hand_landmarks:
34+
myHand = self.results.multi_hand_landmarks[handNo]
35+
for id, lm in enumerate(myHand.landmark):
36+
# print(id, lm)
37+
h, w, c = img.shape
38+
cx, cy = int(lm.x * w), int(lm.y * h)
39+
# print(id, cx, cy)
40+
lmList.append([id, cx, cy])
41+
if draw:
42+
cv2.circle(img, (cx, cy), 15, (255, 0, 255), cv2.FILLED)
43+
44+
return lmList
45+
46+
47+
def main():
48+
pTime = 0
49+
cTime = 0
50+
cap = cv2.VideoCapture(1)
51+
detector = handDetector()
52+
while True:
53+
success, img = cap.read()
54+
img = detector.findHands(img)
55+
lmList = detector.findPosition(img)
56+
if len(lmList) != 0:
57+
print(lmList[4])
58+
59+
cTime = time.time()
60+
fps = 1 / (cTime - pTime)
61+
pTime = cTime
62+
63+
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,
64+
(255, 0, 255), 3)
65+
66+
cv2.imshow("Image", img)
67+
cv2.waitKey(1)
68+
69+
70+
if __name__ == "__main__":
71+
main()

0 commit comments

Comments
 (0)