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

Finger Counter Using Hand Tracking Added #999

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions BasicPythonScripts/Fingers Counter using Hand Tracking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Finger-Counter-using-Hand-Tracking-And-Open-cv
finger counting based on open-cv and hand tracking in real time

the important library :
- opencv-python
- mediapipe

the project detects the counting fingers for the right hand and count from (0-7)

# Screenshot
![](fingerCounting.png)

to run the project execute the fingerCountingProject.py script

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 3 23:09:19 2021

@author: amine gasa
"""
import cv2
import os
import time
import handTrackingModule as htm
def getNumber(ar):
s=""
for i in ar:
s+=str(ar[i]);

if(s=="00000"):
return (0)
elif(s=="01000"):
return(1)
elif(s=="01100"):
return(2)
elif(s=="01110"):
return(3)
elif(s=="01111"):
return(4)
elif(s=="11111"):
return(5)
elif(s=="01001"):
return(6)
elif(s=="01011"):
return(7)

wcam,hcam=640,480
cap=cv2.VideoCapture(0)
cap.set(3,wcam)
cap.set(4,hcam)
pTime=0
detector = htm.handDetector(detectionCon=0.75)
while True:
success,img=cap.read()
img = detector.findHands(img, draw=True )
lmList=detector.findPosition(img,draw=False)
#print(lmList)
tipId=[4,8,12,16,20]
if(len(lmList)!=0):
fingers=[]
#thumb
if(lmList[tipId[0]][1]>lmList[tipId[0]-1][1]):
fingers.append(1)
else :
fingers.append(0)
#4 fingers
for id in range(1,len(tipId)):

if(lmList[tipId[id]][2]<lmList[tipId[id]-2][2]):
fingers.append(1)

else :
fingers.append(0)


cv2.rectangle(img,(20,255),(170,425),(0,255,0),cv2.FILLED)
cv2.putText(img,str(getNumber(fingers)),(45,375),cv2.FONT_HERSHEY_PLAIN,
10,(255,0,0),20)



cTime=time.time()
fps=1/(cTime-pTime)
pTime=cTime
cv2.putText(img, f'FPS: {int(fps)}',(400,70),cv2.FONT_HERSHEY_COMPLEX,1,(255,255,0),3)
cv2.imshow("image",img)
if(cv2.waitKey(1) & 0xFF== ord('q')):
break


Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import cv2
import mediapipe as mp
import time


class handDetector():
def __init__(self, mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5):
self.mode = mode
self.maxHands = maxHands
self.detectionCon = detectionCon
self.trackCon = trackCon

self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxHands,
self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils

def findHands(self, img, draw=True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imgRGB)
#print(results.multi_hand_landmarks)

if self.results.multi_hand_landmarks:
for handLms in self.results.multi_hand_landmarks:
if draw:
self.mpDraw.draw_landmarks(img, handLms,
self.mpHands.HAND_CONNECTIONS)
return img

def findPosition(self, img, handNo=0, draw=True):

lmList = []
if self.results.multi_hand_landmarks:
myHand = self.results.multi_hand_landmarks[handNo]
for id, lm in enumerate(myHand.landmark):
# print(id, lm)
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
# print(id, cx, cy)
lmList.append([id, cx, cy])
if draw:
cv2.circle(img, (cx, cy), 15, (255, 0, 255), cv2.FILLED)

return lmList


def main():
pTime = 0
cTime = 0
cap = cv2.VideoCapture(1)
detector = handDetector()
while True:
success, img = cap.read()
img = detector.findHands(img)
lmList = detector.findPosition(img)
if len(lmList) != 0:
print(lmList[4])

cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime

cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,
(255, 0, 255), 3)

cv2.imshow("Image", img)
cv2.waitKey(1)


if __name__ == "__main__":
main()