Skip to content

Commit

Permalink
1v1 game
Browse files Browse the repository at this point in the history
  • Loading branch information
Nabil-Lahssini committed Sep 29, 2021
1 parent 19255fd commit b968101
Showing 1 changed file with 52 additions and 27 deletions.
79 changes: 52 additions & 27 deletions HandTrackingGame.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import cv2
import time
import sys
import keyboard
from HandTrackingModule import HandDetector

cap = cv2.VideoCapture(0)
pTime = 0
cTime = 0
instance = HandDetector()
tipIds = [4, 8, 12, 16, 20]



def most_frequent(List: list) -> int:
return max(set(List), key=List.count)


def analyze_fingers(sample: int) -> list:
def analyze_fingers(sample: int, player) -> list:
cap = cv2.VideoCapture(0)
pTime = 0
cTime = 0
instance = HandDetector(maxHands=1)
tipIds = [4, 8, 12, 16, 20]
frequency = []
while len(frequency) < sample:
item = ""
while True:
success, img = cap.read()
img = instance.find_hands(img, draw=True)
lmList = instance.find_position(img)
Expand All @@ -34,38 +38,59 @@ def analyze_fingers(sample: int) -> list:
fingers.append(0)
totalFingers = fingers.count(1)
frequency.append(totalFingers)
# 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)
if len(frequency) > sample:
frequency = []
if len(frequency) == sample:
item = translate(most_frequent(frequency))
cv2.putText(img, item, (10, 100), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
cv2.putText(img, f"player {str(player)}:", (10, 65), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
cv2.imshow("Image", img)
cv2.waitKey(1)
return frequency

if keyboard.is_pressed("space"):
return item

def translate(number):
if number == 2 or number == 3:
return "ciseaux"
return "schaar"
elif number == 5 or number == 4:
return "feuille"
return "papier"
elif number is 0 or number is 1:
return "pierre"
return "steen"

def logic(player1, player2):
if player1 is player2:
return "Equal"

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"

def do():
frequency = analyze_fingers(10)
number = most_frequent(frequency)
result = translate(number)
return result
if player1 == "steen" and player2 == "schaar":
return "player 1"
if player1 == "schaar" and player2 == "steen":
return "player 2"


def main():
print("player 1: ")
print(do())
print("player 2 in 3 seconds...")
time.sleep(3)
print("player 2: ")
print(do())
player1 = analyze_fingers(10, player=1)
player2 = analyze_fingers(10, player=2)
result = (logic(player1, player2) + " WINS !!")
cap = cv2.VideoCapture(0)
instance = HandDetector(maxHands=1)
while True:
success, img = cap.read()
img = instance.find_hands(img, draw=True)
cv2.putText(img, result, (10, 100), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)
cv2.imshow("Image", img)
cv2.waitKey(1)
if keyboard.is_pressed("space"):
break


if __name__ == "__main__":
Expand Down

0 comments on commit b968101

Please sign in to comment.