diff --git a/BasicPythonScripts/Fingers Counter using Hand Tracking/README.md b/BasicPythonScripts/Fingers Counter using Hand Tracking/README.md new file mode 100644 index 000000000..ee2b29ea1 --- /dev/null +++ b/BasicPythonScripts/Fingers Counter using Hand Tracking/README.md @@ -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 + + +to run the project execute the fingerCountingProject.py script + diff --git a/BasicPythonScripts/Fingers Counter using Hand Tracking/fingerCounting.png b/BasicPythonScripts/Fingers Counter using Hand Tracking/fingerCounting.png new file mode 100644 index 000000000..a460a514f Binary files /dev/null and b/BasicPythonScripts/Fingers Counter using Hand Tracking/fingerCounting.png differ diff --git a/BasicPythonScripts/Fingers Counter using Hand Tracking/fingerCountingProject.py b/BasicPythonScripts/Fingers Counter using Hand Tracking/fingerCountingProject.py new file mode 100644 index 000000000..35ed01610 --- /dev/null +++ b/BasicPythonScripts/Fingers Counter using Hand Tracking/fingerCountingProject.py @@ -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 + + diff --git a/BasicPythonScripts/Fingers Counter using Hand Tracking/handTrackingModule.py b/BasicPythonScripts/Fingers Counter using Hand Tracking/handTrackingModule.py new file mode 100644 index 000000000..92c9988bc --- /dev/null +++ b/BasicPythonScripts/Fingers Counter using Hand Tracking/handTrackingModule.py @@ -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() \ No newline at end of file