-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand_tracking_game.py
130 lines (109 loc) · 4.48 KB
/
hand_tracking_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import cv2
import mediapipe as mp
import random
def show_text(frame, text, position, font_size=1, color=(255, 0, 0)):
cv2.putText(frame, text, position, cv2.FONT_HERSHEY_SIMPLEX, font_size, color, 2, cv2.LINE_AA)
def check_winner(player_gesture, computer_gesture):
if player_gesture == computer_gesture:
return "Tie"
if (
(player_gesture == "Rock" and computer_gesture == "Scissors") or
(player_gesture == "Paper" and computer_gesture == "Rock") or
(player_gesture == "Scissors" and computer_gesture == "Paper")
):
return "Winner"
return "Loser"
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0)
player_score = 0
computer_score = 0
game_started = False
player_gesture_last = None
player_gesture = None
gestures = ["Rock", "Paper", "Scissors"]
computer_gesture = ""
result = "Tie"
with mp_hands.Hands(
min_detection_confidence=0.5,
min_tracking_confidence=0.5
) as hands:
while True:
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
thumb_tip = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP].y
thumb_ip = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_IP].y
index_finger_tip = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y
middle_finger_tip = hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].y
middle_finger_dip = hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_DIP].y
ring_finger_tip = hand_landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_TIP].y
pinky_tip = hand_landmarks.landmark[mp_hands.HandLandmark.PINKY_TIP].y
if (
thumb_tip > index_finger_tip
and thumb_tip > middle_finger_tip
and thumb_tip > ring_finger_tip
and thumb_tip > pinky_tip
):
player_gesture_last = player_gesture
player_gesture = "Paper"
elif (
thumb_tip < index_finger_tip
and thumb_tip < middle_finger_tip
and thumb_tip < ring_finger_tip
and thumb_tip < pinky_tip
):
player_gesture_last = player_gesture
player_gesture = "Rock"
elif (
thumb_tip < thumb_ip and middle_finger_tip < middle_finger_dip
):
player_gesture_last = player_gesture
player_gesture = "Scissors"
else:
player_gesture = None
if (player_gesture and game_started):
if (player_gesture_last != player_gesture):
result_color = (0, 255, 255)
computer_gesture = random.choice(gestures)
result = check_winner(player_gesture, computer_gesture)
if result == "Winner":
player_score += 1
result_color = (0, 255, 0)
elif result == "Loser":
computer_score += 1
result_color = (0, 0, 255)
show_text(frame, "Computer: {}".format(computer_gesture), (frame.shape[1] - 250, 80), font_size=0.7)
show_text(frame, "Player: {}".format(player_gesture), (frame.shape[1] - 250, 40), font_size=0.7)
show_text(frame, result, (int(frame.shape[1] / 2) - 50, int(frame.shape[0] / 2)), 1, result_color)
if game_started:
show_text(frame, "Player = {}".format(player_score), (20, 40))
show_text(frame, "Computer = {}".format(computer_score), (20, 80))
show_text(frame, "Press Q to finish game", (20, frame.shape[0] - 20))
else:
show_text(frame, "Press S to start game", (20, frame.shape[0] - 20))
key = cv2.waitKey(1)
cv2.imshow("Rock Paper Scissor Game", frame)
if key == ord("s") and not game_started:
game_started = True
player_score = 0
computer_score = 0
if key == ord("q"):
final_result = "Tie"
final_result_color = (0, 255, 255)
if player_score > computer_score:
final_result = "Victory"
final_result_color = (0, 255, 0)
elif player_score < computer_score:
final_result = "Defeat"
final_result_color = (0, 0, 255)
show_text(frame, final_result, (int(frame.shape[1] / 2) - 50, int(frame.shape[0] / 2)), 1, final_result_color)
cv2.imshow("Rock Paper Scissor Game", frame)
cv2.waitKey(2000)
break
cap.release()
cv2.destroyAllWindows()