Skip to content

Commit

Permalink
Bug fix + documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nabil-Lahssini committed Oct 31, 2021
1 parent e371734 commit 707a80a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
23 changes: 10 additions & 13 deletions HandTrackingGame.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import cv2
import time
import sys
import keyboard
from HandTrackingModule import HandDetector
from collections import deque



def most_frequent(List: list) -> int:
"""Returns the most frequent value from the last X frames"""
return max(set(List), key=List.count)


Expand Down Expand Up @@ -39,9 +37,9 @@ def analyze_fingers(sample: int, player) -> list:
fingers.append(1)
else:
fingers.append(0)
if len(lmList) >40:
if len(lmList) > 40:
# Thumb
if lmList[tipIds[5]][1] < lmList[tipIds[5] - 1][1]:
if lmList[tipIds[6]][1] < lmList[tipIds[6] - 1][1]:
fingers.append(1)
else:
fingers.append(0)
Expand All @@ -65,28 +63,29 @@ def analyze_fingers(sample: int, player) -> list:
if keyboard.is_pressed("space"):
return item, item2

def translate(number):

def translate(number: int) -> str:
"""Detects if its 'schaar' 'steen' or 'papier'"""
if number == 2 or number == 3:
return "schaar"
elif number == 5 or number == 4:
return "papier"
elif number is 0 or number is 1:
return "steen"

def logic(player1, player2):

def logic(player1: str, player2: str) -> str:
"""By the lack of 'switch case' in older versions of Python I used an ugly if if if case..."""
if player1 is player2:
return "Nobody"

if player1 == "schaar" and player2 == "papier":
return "player 1"
if player1 == "papier" and player2 == "schaar":
return "player 2"

if player1 == "papier" and player2 == "steen":
return "player 2"
if player1 == "steen" and player2 == "papier":
return "player 1"

if player1 == "steen" and player2 == "schaar":
return "player 1"
if player1 == "schaar" and player2 == "steen":
Expand All @@ -97,7 +96,7 @@ def main():
player1, player2 = analyze_fingers(10, player=1)
result = (logic(player1, player2) + " WINS !!")
cap = cv2.VideoCapture(0)
instance = HandDetector(maxHands=1)
instance = HandDetector(maxHands=3)
while True:
success, img = cap.read()
img = instance.find_hands(img, draw=True)
Expand All @@ -107,7 +106,5 @@ def main():
if keyboard.is_pressed("space"):
break



if __name__ == "__main__":
main()
21 changes: 10 additions & 11 deletions HandTrackingModule.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import cv2
import mediapipe as mp
import time


class HandDetector:
Expand All @@ -14,6 +13,7 @@ def __init__(self, mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5):
self.mpDraw = mp.solutions.drawing_utils

def find_hands(self, img, draw=True):
"""finds the hands on the image and draw a rectangle around it """
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imgRGB)
if self.results.multi_hand_landmarks:
Expand All @@ -22,16 +22,15 @@ def find_hands(self, img, draw=True):
self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
return img

def find_position(self, img, handNo=0, draw= True):
def find_position(self, img, draw: bool = True) -> list:
"""returns a list of all the handmarks"""
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)
lmList.append([id, cx, cy])
if draw:
cv2.circle(img, (cx, cy), 5, (255,0,255), cv2.FILLED)
for myHand in self.results.multi_hand_landmarks:
for id, lm in enumerate(myHand.landmark):
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
lmList.append([id, cx, cy])
if draw:
cv2.circle(img, (cx, cy), 5, (255,0,255), cv2.FILLED)
return lmList

0 comments on commit 707a80a

Please sign in to comment.