Spaces:
Runtime error
Runtime error
Commit ·
2dea67c
1
Parent(s): de71611
Delete rock.py
Browse files
rock.py
DELETED
|
@@ -1,191 +0,0 @@
|
|
| 1 |
-
from streamlit_webrtc import VideoTransformerBase, webrtc_streamer
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import streamlit as st
|
| 4 |
-
import cv2
|
| 5 |
-
import mediapipe as mp
|
| 6 |
-
import time
|
| 7 |
-
import numpy as np
|
| 8 |
-
import requests
|
| 9 |
-
import cvzone
|
| 10 |
-
import random
|
| 11 |
-
|
| 12 |
-
# Rock paper scissors configuration
|
| 13 |
-
st.set_page_config(page_title="RPS", page_icon="🤖")
|
| 14 |
-
st.title("Rock Paper Scissors Game")
|
| 15 |
-
col1, col2 = st.columns(2)
|
| 16 |
-
######################################Helper functions ######################################################
|
| 17 |
-
timer = 0
|
| 18 |
-
stateResult = False
|
| 19 |
-
startGame = False
|
| 20 |
-
scores = [0, 0] # [AI, Player]
|
| 21 |
-
font = 1
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def fingerStats(hand_landmarks, finger_name):
|
| 25 |
-
finger_map = {'INDEX': 6, 'MIDDLE': 10, 'RING': 14, 'PINKY': 18}
|
| 26 |
-
|
| 27 |
-
fingerPip = hand_landmarks.landmark[finger_map[finger_name]].y
|
| 28 |
-
fingerTip = hand_landmarks.landmark[finger_map[finger_name] + 2].y
|
| 29 |
-
|
| 30 |
-
return fingerPip > fingerTip
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def userMove(curr_state):
|
| 34 |
-
if curr_state == "0000":
|
| 35 |
-
user_move = 1
|
| 36 |
-
elif curr_state == "1111":
|
| 37 |
-
user_move = 2
|
| 38 |
-
elif curr_state == "1100":
|
| 39 |
-
user_move = 3
|
| 40 |
-
else:
|
| 41 |
-
user_move = 0
|
| 42 |
-
return user_move
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def compMove():
|
| 46 |
-
options = [1, 2, 3]
|
| 47 |
-
computer_move = random.choice(options)
|
| 48 |
-
return computer_move
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
def getWinner(user, comp):
|
| 52 |
-
if user != 0:
|
| 53 |
-
# Determine the winner
|
| 54 |
-
champ = (user - comp) % 3
|
| 55 |
-
# print(winner)
|
| 56 |
-
if champ == 1:
|
| 57 |
-
scores[1] += 1
|
| 58 |
-
result = "You win"
|
| 59 |
-
elif champ == 2:
|
| 60 |
-
scores[0] += 1
|
| 61 |
-
result = "AI wins"
|
| 62 |
-
else:
|
| 63 |
-
result = "Draw"
|
| 64 |
-
else:
|
| 65 |
-
result = "User move Unknown"
|
| 66 |
-
return result
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
mpHands = mp.solutions.hands
|
| 70 |
-
hands = mpHands.Hands()
|
| 71 |
-
mpDraw = mp.solutions.drawing_utils
|
| 72 |
-
|
| 73 |
-
trig = 0
|
| 74 |
-
|
| 75 |
-
######################################Play the game ######################################################
|
| 76 |
-
def main(image):
|
| 77 |
-
img = image
|
| 78 |
-
|
| 79 |
-
imgScaled = cv2.resize(img, (0, 0), None, 0.875, 0.875)
|
| 80 |
-
imgScaled = imgScaled[:, 80:480]
|
| 81 |
-
|
| 82 |
-
results = hands.process(imgScaled)
|
| 83 |
-
# print(results.multi_hand_landmarks)
|
| 84 |
-
|
| 85 |
-
if startGame:
|
| 86 |
-
if stateResult is False:
|
| 87 |
-
timer = time.time() - initialTime
|
| 88 |
-
cv2.putText(imgBG, str(int(timer)), (605, 435), font, 6, (34, 139, 34), 4)
|
| 89 |
-
|
| 90 |
-
if timer > 3:
|
| 91 |
-
stateResult = True
|
| 92 |
-
timer = 1
|
| 93 |
-
|
| 94 |
-
if results.multi_hand_landmarks:
|
| 95 |
-
current_state = ""
|
| 96 |
-
for handLms in results.multi_hand_landmarks:
|
| 97 |
-
# mpDraw.draw_landmarks(imgScaled, handLms, mpHands.HAND_CONNECTIONS)
|
| 98 |
-
|
| 99 |
-
index_status = fingerStats(handLms, 'INDEX')
|
| 100 |
-
current_state += "1" if index_status else "0"
|
| 101 |
-
|
| 102 |
-
middle_status = fingerStats(handLms, 'MIDDLE')
|
| 103 |
-
current_state += "1" if middle_status else "0"
|
| 104 |
-
|
| 105 |
-
ring_status = fingerStats(handLms, 'RING')
|
| 106 |
-
current_state += "1" if ring_status else "0"
|
| 107 |
-
|
| 108 |
-
pinky_status = fingerStats(handLms, 'PINKY')
|
| 109 |
-
current_state += "1" if pinky_status else "0"
|
| 110 |
-
|
| 111 |
-
# Get user choice
|
| 112 |
-
user_choice = userMove(current_state)
|
| 113 |
-
if user_choice == 0:
|
| 114 |
-
x = 542
|
| 115 |
-
else:
|
| 116 |
-
x = 600
|
| 117 |
-
|
| 118 |
-
# computer choice
|
| 119 |
-
computer_choice = compMove()
|
| 120 |
-
|
| 121 |
-
# Get the winner
|
| 122 |
-
winner = getWinner(user_choice, computer_choice)
|
| 123 |
-
|
| 124 |
-
# Print choices
|
| 125 |
-
choices = ["Unknown", "Rock", "Paper", "Scissors"]
|
| 126 |
-
print(f'\nYou choose {choices[user_choice]}')
|
| 127 |
-
print(f'Computer choose {choices[computer_choice]}')
|
| 128 |
-
# print(current_state)
|
| 129 |
-
print(winner)
|
| 130 |
-
|
| 131 |
-
# Get Computer choice images
|
| 132 |
-
imgAI = cv2.imread(f'Resources/{computer_choice}.png', cv2.IMREAD_UNCHANGED)
|
| 133 |
-
|
| 134 |
-
else:
|
| 135 |
-
imgAI = cv2.imread(f'Resources/4.png', cv2.IMREAD_UNCHANGED)
|
| 136 |
-
winner = "No Hand Detected, Try Again"
|
| 137 |
-
x = 525
|
| 138 |
-
startGame = False
|
| 139 |
-
initialTime = time.time()
|
| 140 |
-
stateResult = True
|
| 141 |
-
|
| 142 |
-
if stateResult:
|
| 143 |
-
with col2:
|
| 144 |
-
st.info(str(winner))
|
| 145 |
-
st.image(imgAI, caption="Comp choice", use_column_width=True)
|
| 146 |
-
|
| 147 |
-
else:
|
| 148 |
-
if trig == 0:
|
| 149 |
-
st.info("Ready to play?")
|
| 150 |
-
|
| 151 |
-
st.info(str(scores[0]))
|
| 152 |
-
st.info(str(scores[1]))
|
| 153 |
-
|
| 154 |
-
key = cv2.waitKey(1)
|
| 155 |
-
# Start Game
|
| 156 |
-
if key == 32:
|
| 157 |
-
startGame = True
|
| 158 |
-
initialTime = time.time()
|
| 159 |
-
stateResult = False
|
| 160 |
-
trig += 1
|
| 161 |
-
# Refresh Game
|
| 162 |
-
if key == 8:
|
| 163 |
-
startGame = False
|
| 164 |
-
initialTime = time.time()
|
| 165 |
-
stateResult = False
|
| 166 |
-
scores = [0, 0]
|
| 167 |
-
trig = 0
|
| 168 |
-
# End game
|
| 169 |
-
if key == 27:
|
| 170 |
-
trig = 0
|
| 171 |
-
|
| 172 |
-
######################################Get video feed######################################################
|
| 173 |
-
|
| 174 |
-
class VideoProcessor:
|
| 175 |
-
def recv(self, frame):
|
| 176 |
-
img = frame.to_ndarray(format="bgr24")
|
| 177 |
-
|
| 178 |
-
img = process(img)
|
| 179 |
-
|
| 180 |
-
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
| 181 |
-
|
| 182 |
-
with col1:
|
| 183 |
-
st.header('Column 1')
|
| 184 |
-
webrtc_ctx = webrtc_streamer(
|
| 185 |
-
key="WYH",
|
| 186 |
-
mode=WebRtcMode.SENDRECV,
|
| 187 |
-
rtc_configuration=RTC_CONFIGURATION,
|
| 188 |
-
media_stream_constraints={"video": True, "audio": False},
|
| 189 |
-
video_processor_factory=VideoProcessor,
|
| 190 |
-
async_processing=True,
|
| 191 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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