Spaces:
Runtime error
Runtime error
Commit
·
3acc487
1
Parent(s):
954cccd
Upload folder using huggingface_hub
Browse files- Resources/1.png +0 -0
- Resources/2.png +0 -0
- Resources/3.png +0 -0
- Resources/4.png +0 -0
- Resources/ai_face.jpg +0 -0
- Resources/hand_tracking.py +87 -0
- Resources/rock.py +191 -0
Resources/1.png
ADDED
![]() |
Resources/2.png
ADDED
![]() |
Resources/3.png
ADDED
![]() |
Resources/4.png
ADDED
![]() |
Resources/ai_face.jpg
ADDED
![]() |
Resources/hand_tracking.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# https://medium.com/mlearning-ai/live-webcam-with-streamlit-f32bf68945a4
|
2 |
+
|
3 |
+
from streamlit_webrtc import webrtc_streamer, WebRtcMode, RTCConfiguration
|
4 |
+
import streamlit as st
|
5 |
+
import cv2
|
6 |
+
import numpy as np
|
7 |
+
import av
|
8 |
+
import mediapipe as mp
|
9 |
+
import base64
|
10 |
+
|
11 |
+
|
12 |
+
###################################### Helper functions ##############################
|
13 |
+
# Read the image file and encode it as base64
|
14 |
+
|
15 |
+
with open('/mount/src/rock_paper_scissors/Resources/ai_face.jpg', 'rb') as aiface:
|
16 |
+
image_data = base64.b64encode(aiface.read()).decode('utf-8')
|
17 |
+
|
18 |
+
# Set up MediaPipe Hands
|
19 |
+
mp_drawing = mp.solutions.drawing_utils
|
20 |
+
mp_drawing_styles = mp.solutions.drawing_styles
|
21 |
+
mp_hands = mp.solutions.hands
|
22 |
+
hands = mp_hands.Hands(
|
23 |
+
model_complexity=0,
|
24 |
+
min_detection_confidence=0.5,
|
25 |
+
min_tracking_confidence=0.5
|
26 |
+
)
|
27 |
+
|
28 |
+
# Function to process video frames
|
29 |
+
def process(image):
|
30 |
+
image.flags.writeable = False
|
31 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
32 |
+
results = hands.process(image)
|
33 |
+
|
34 |
+
# Draw hand landmarks on the image
|
35 |
+
image.flags.writeable = True
|
36 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
37 |
+
if results.multi_hand_landmarks:
|
38 |
+
for hand_landmarks in results.multi_hand_landmarks:
|
39 |
+
mp_drawing.draw_landmarks(
|
40 |
+
image,
|
41 |
+
hand_landmarks,
|
42 |
+
mp_hands.HAND_CONNECTIONS,
|
43 |
+
mp_drawing_styles.get_default_hand_landmarks_style(),
|
44 |
+
mp_drawing_styles.get_default_hand_connections_style()
|
45 |
+
)
|
46 |
+
|
47 |
+
return cv2.flip(image, 1)
|
48 |
+
|
49 |
+
# Define RTC Configuration
|
50 |
+
RTC_CONFIGURATION = RTCConfiguration(
|
51 |
+
{"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
|
52 |
+
)
|
53 |
+
|
54 |
+
|
55 |
+
# Create Streamlit web app
|
56 |
+
scores = [0, 0] # [AI, Player]
|
57 |
+
|
58 |
+
st.set_page_config(page_title="RPS", page_icon="🤖", layout="wide",)
|
59 |
+
|
60 |
+
col1, col2 = st.columns(2)
|
61 |
+
|
62 |
+
# Add content to the right column (video stream)
|
63 |
+
with col1:
|
64 |
+
st.info(f"Player{scores[1]}")
|
65 |
+
# Define a video processor class
|
66 |
+
class VideoProcessor:
|
67 |
+
def recv(self, frame):
|
68 |
+
img = frame.to_ndarray(format="bgr24")
|
69 |
+
img = process(img)
|
70 |
+
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
71 |
+
|
72 |
+
# Create the WebRTC streamer
|
73 |
+
webrtc_ctx = webrtc_streamer(
|
74 |
+
key="hand-tracking",
|
75 |
+
mode=WebRtcMode.SENDRECV,
|
76 |
+
rtc_configuration=RTC_CONFIGURATION,
|
77 |
+
media_stream_constraints={"video": True, "audio": False},
|
78 |
+
video_processor_factory=VideoProcessor,
|
79 |
+
async_processing=True,
|
80 |
+
)
|
81 |
+
|
82 |
+
# Add content to the left column (app description)
|
83 |
+
with col2:
|
84 |
+
st.info(f"AI {scores[0]}")
|
85 |
+
img_tag = f'<img src="data:image/png;base64,{image_data}" style="border: 2px solid green; border-radius: 15px;">'
|
86 |
+
# Create a Streamlit component to render the HTML
|
87 |
+
st.components.v1.html(img_tag, height=400)
|
Resources/rock.py
ADDED
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
)
|