-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPS.py
More file actions
136 lines (108 loc) · 4.08 KB
/
RPS.py
File metadata and controls
136 lines (108 loc) · 4.08 KB
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
131
132
133
134
135
136
import cv2
import handTrackingModule as htm
import os
from random import choice
def calculate_winner(move1, move2):
if move1 == move2:
return "Tie"
elif move1 == "rock":
if move2 == "scissors":
return "User"
if move2 == "paper":
return "Computer"
if move1 == "paper":
if move2 == "rock":
return "User"
if move2 == "scissors":
return "Computer"
if move1 == "scissors":
if move2 == "paper":
return "User"
if move2 == "rock":
return "Computer"
wCam, hCam = 640, 720
cap = cv2.VideoCapture(0)
cap.set(3, wCam)
cap.set(4, hCam)
folderPath = "images"
imgList = os.listdir(folderPath)
# print(imgList)
overlayList = []
for imgPath in imgList:
image = cv2.imread(f'{folderPath}/{imgPath}')
# print(f'{folderPath}/{imgPath}')
overlayList.append(image)
overlayList[0] = cv2.resize(overlayList[0], (200, 200))
overlayList[1] = cv2.resize(overlayList[1], (200, 200))
overlayList[2] = cv2.resize(overlayList[2], (200, 200))
detector = htm.handDetector()
tipIds = [4, 8, 12, 16, 20]
prev_move = None
user_move = ""
while True:
success, img = cap.read()
cv2.rectangle(img, (105, 0), (565, 50), (102, 255, 255), cv2.FILLED)
cv2.putText(img, "ROCK PAPER SCISSORS", (115, 40),
cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 69, 255), 4)
cv2.line(img, (305, 55), (305, 380), (0, 0, 0), 2)
img = detector.findHands(img)
lmList = detector.findPosition(img, draw=False)
if len(lmList) != 0:
fingers = []
# For Thumb
if lmList[tipIds[0]][1] > lmList[tipIds[0]-1][1]:
fingers.append(1)
else:
fingers.append(0)
# For Fingers
for id in range(1, 5):
if lmList[tipIds[id]][2] < lmList[tipIds[id]-2][2]:
fingers.append(1)
else:
fingers.append(0)
# print(fingers)
totalFingers = fingers.count(1)
# print(totalFingers)
font = cv2.FONT_HERSHEY_SIMPLEX
if totalFingers == 5:
user_move = "paper"
cv2.rectangle(img, (15, 75), (280, 110),
(102, 255, 255), cv2.FILLED)
cv2.putText(img, f'User Move: {user_move}',
(19, 100), font, 0.8, (255, 0, 0), 2, cv2.LINE_AA)
elif totalFingers == 2:
if lmList[tipIds[1]][2] < lmList[tipIds[1]-2][2] and lmList[tipIds[2]][2] < lmList[tipIds[2]-2][2]:
user_move = "scissors"
cv2.rectangle(img, (15, 75), (280, 110),
(102, 255, 255), cv2.FILLED)
cv2.putText(img, f'User Move: {user_move}',
(19, 100), font, 0.8, (255, 0, 0), 2, cv2.LINE_AA)
else:
pass
elif totalFingers == 0:
user_move = "rock"
cv2.rectangle(img, (15, 75), (280, 110),
(102, 255, 255), cv2.FILLED)
cv2.putText(img, f'User Move: {user_move}',
(19, 100), font, 0.8, (255, 0, 0), 2, cv2.LINE_AA)
if prev_move != user_move:
computer_move = choice(['rock', 'paper', 'scissors'])
winner = calculate_winner(user_move, computer_move)
prev_move = user_move
if computer_move == "paper":
img[160:360, 365:565] = overlayList[0]
elif computer_move == "scissors":
img[160:360, 365:565] = overlayList[2]
elif computer_move == "rock":
img[160:360, 365:565] = overlayList[1]
cv2.rectangle(img, (325, 75), (615, 110), (102, 255, 255), cv2.FILLED)
cv2.putText(
img, f'Computer Move: {computer_move}', (330, 100), font, 0.7, (255, 0, 0), 2, cv2.LINE_AA)
cv2.rectangle(img, (180, 405), (540, 450), (102, 255, 255), cv2.FILLED)
cv2.putText(img, f'Winner: {winner}',
(190, 440), font, 1.2, (0, 0, 255), 3, cv2.LINE_AA)
cv2.imshow("Rock Paper Scissor", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()